@@ -97,19 +97,22 @@ static inline void arch_spin_lock(arch_spinlock_t *lock)
9797
9898static inline int arch_spin_trylock (arch_spinlock_t * lock )
9999{
100- unsigned long tmp ;
100+ unsigned long contended , res ;
101101 u32 slock ;
102102
103- __asm__ __volatile__(
104- " ldrex %0, [%2]\n"
105- " subs %1, %0, %0, ror #16\n"
106- " addeq %0, %0, %3\n"
107- " strexeq %1, %0, [%2]"
108- : "=&r" (slock ), "=&r" (tmp )
109- : "r" (& lock -> slock ), "I" (1 << TICKET_SHIFT )
110- : "cc" );
111-
112- if (tmp == 0 ) {
103+ do {
104+ __asm__ __volatile__(
105+ " ldrex %0, [%3]\n"
106+ " mov %2, #0\n"
107+ " subs %1, %0, %0, ror #16\n"
108+ " addeq %0, %0, %4\n"
109+ " strexeq %2, %0, [%3]"
110+ : "=&r" (slock ), "=&r" (contended ), "=r" (res )
111+ : "r" (& lock -> slock ), "I" (1 << TICKET_SHIFT )
112+ : "cc" );
113+ } while (res );
114+
115+ if (!contended ) {
113116 smp_mb ();
114117 return 1 ;
115118 } else {
@@ -165,17 +168,20 @@ static inline void arch_write_lock(arch_rwlock_t *rw)
165168
166169static inline int arch_write_trylock (arch_rwlock_t * rw )
167170{
168- unsigned long tmp ;
169-
170- __asm__ __volatile__(
171- " ldrex %0, [%1]\n"
172- " teq %0, #0\n"
173- " strexeq %0, %2, [%1]"
174- : "=&r" (tmp )
175- : "r" (& rw -> lock ), "r" (0x80000000 )
176- : "cc" );
177-
178- if (tmp == 0 ) {
171+ unsigned long contended , res ;
172+
173+ do {
174+ __asm__ __volatile__(
175+ " ldrex %0, [%2]\n"
176+ " mov %1, #0\n"
177+ " teq %0, #0\n"
178+ " strexeq %1, %3, [%2]"
179+ : "=&r" (contended ), "=&r" (res )
180+ : "r" (& rw -> lock ), "r" (0x80000000 )
181+ : "cc" );
182+ } while (res );
183+
184+ if (!contended ) {
179185 smp_mb ();
180186 return 1 ;
181187 } else {
@@ -251,18 +257,26 @@ static inline void arch_read_unlock(arch_rwlock_t *rw)
251257
252258static inline int arch_read_trylock (arch_rwlock_t * rw )
253259{
254- unsigned long tmp , tmp2 = 1 ;
255-
256- __asm__ __volatile__(
257- " ldrex %0, [%2]\n"
258- " adds %0, %0, #1\n"
259- " strexpl %1, %0, [%2]\n"
260- : "=&r" (tmp ), "+r" (tmp2 )
261- : "r" (& rw -> lock )
262- : "cc" );
263-
264- smp_mb ();
265- return tmp2 == 0 ;
260+ unsigned long contended , res ;
261+
262+ do {
263+ __asm__ __volatile__(
264+ " ldrex %0, [%2]\n"
265+ " mov %1, #0\n"
266+ " adds %0, %0, #1\n"
267+ " strexpl %1, %0, [%2]"
268+ : "=&r" (contended ), "=&r" (res )
269+ : "r" (& rw -> lock )
270+ : "cc" );
271+ } while (res );
272+
273+ /* If the lock is negative, then it is already held for write. */
274+ if (contended < 0x80000000 ) {
275+ smp_mb ();
276+ return 1 ;
277+ } else {
278+ return 0 ;
279+ }
266280}
267281
268282/* read_can_lock - would read_trylock() succeed? */
0 commit comments