5 * Written by Ivan Kokshaysky <ink@jurassic.park.msu.ru>, 2001.
6 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
10 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
15 #include <linux/compiler.h>
17 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
18 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
19 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
20 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
21 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
22 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
24 static inline void __down_read(struct rw_semaphore
*sem
)
28 oldcount
= sem
->count
;
29 sem
->count
+= RWSEM_ACTIVE_READ_BIAS
;
41 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
42 :"Ir" (RWSEM_ACTIVE_READ_BIAS
), "m" (sem
->count
) : "memory");
44 if (unlikely(oldcount
< 0))
45 rwsem_down_read_failed(sem
);
49 * trylock for reading -- returns 1 if successful, 0 if contention
51 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
57 new = res
+ RWSEM_ACTIVE_READ_BIAS
;
61 res
= cmpxchg(&sem
->count
, old
, new);
63 return res
>= 0 ? 1 : 0;
66 static inline void __down_write(struct rw_semaphore
*sem
)
70 oldcount
= sem
->count
;
71 sem
->count
+= RWSEM_ACTIVE_WRITE_BIAS
;
83 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
84 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS
), "m" (sem
->count
) : "memory");
86 if (unlikely(oldcount
))
87 rwsem_down_write_failed(sem
);
91 * trylock for writing -- returns 1 if successful, 0 if contention
93 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
95 long ret
= cmpxchg(&sem
->count
, RWSEM_UNLOCKED_VALUE
,
96 RWSEM_ACTIVE_WRITE_BIAS
);
97 if (ret
== RWSEM_UNLOCKED_VALUE
)
102 static inline void __up_read(struct rw_semaphore
*sem
)
106 oldcount
= sem
->count
;
107 sem
->count
-= RWSEM_ACTIVE_READ_BIAS
;
110 __asm__
__volatile__(
119 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
120 :"Ir" (RWSEM_ACTIVE_READ_BIAS
), "m" (sem
->count
) : "memory");
122 if (unlikely(oldcount
< 0))
123 if ((int)oldcount
- RWSEM_ACTIVE_READ_BIAS
== 0)
127 static inline void __up_write(struct rw_semaphore
*sem
)
131 sem
->count
-= RWSEM_ACTIVE_WRITE_BIAS
;
135 __asm__
__volatile__(
145 :"=&r" (count
), "=m" (sem
->count
), "=&r" (temp
)
146 :"Ir" (RWSEM_ACTIVE_WRITE_BIAS
), "m" (sem
->count
) : "memory");
154 * downgrade write lock to read lock
156 static inline void __downgrade_write(struct rw_semaphore
*sem
)
160 oldcount
= sem
->count
;
161 sem
->count
-= RWSEM_WAITING_BIAS
;
164 __asm__
__volatile__(
173 :"=&r" (oldcount
), "=m" (sem
->count
), "=&r" (temp
)
174 :"Ir" (-RWSEM_WAITING_BIAS
), "m" (sem
->count
) : "memory");
176 if (unlikely(oldcount
< 0))
177 rwsem_downgrade_wake(sem
);
180 static inline void rwsem_atomic_add(long val
, struct rw_semaphore
*sem
)
186 __asm__
__volatile__(
194 :"=&r" (temp
), "=m" (sem
->count
)
195 :"Ir" (val
), "m" (sem
->count
));
199 static inline long rwsem_atomic_update(long val
, struct rw_semaphore
*sem
)
206 __asm__
__volatile__(
215 :"=&r" (ret
), "=m" (sem
->count
), "=&r" (temp
)
216 :"Ir" (val
), "m" (sem
->count
));
222 #endif /* __KERNEL__ */
223 #endif /* _ALPHA_RWSEM_H */