rwsem.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for i486+
  2. *
  3. * Written by David Howells (dhowells@redhat.com).
  4. *
  5. * Derived from asm-i386/semaphore.h
  6. *
  7. *
  8. * The MSW of the count is the negated number of active writers and waiting
  9. * lockers, and the LSW is the total number of active locks
  10. *
  11. * The lock count is initialized to 0 (no active and no waiting lockers).
  12. *
  13. * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
  14. * uncontended lock. This can be determined because XADD returns the old value.
  15. * Readers increment by 1 and see a positive value when uncontended, negative
  16. * if there are writers (and maybe) readers waiting (in which case it goes to
  17. * sleep).
  18. *
  19. * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
  20. * be extended to 65534 by manually checking the whole MSW rather than relying
  21. * on the S flag.
  22. *
  23. * The value of ACTIVE_BIAS supports up to 65535 active processes.
  24. *
  25. * This should be totally fair - if anything is waiting, a process that wants a
  26. * lock will go to the back of the queue. When the currently active lock is
  27. * released, if there's a writer at the front of the queue, then that and only
  28. * that will be woken up; if there's a bunch of consequtive readers at the
  29. * front, then they'll all be woken up, but no other readers will be.
  30. */
  31. #ifndef _I386_RWSEM_H
  32. #define _I386_RWSEM_H
  33. #ifndef _LINUX_RWSEM_H
  34. #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
  35. #endif
  36. #ifdef __KERNEL__
  37. #include <linux/list.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/lockdep.h>
  40. struct rwsem_waiter;
  41. extern struct rw_semaphore *FASTCALL(rwsem_down_read_failed(struct rw_semaphore *sem));
  42. extern struct rw_semaphore *FASTCALL(rwsem_down_write_failed(struct rw_semaphore *sem));
  43. extern struct rw_semaphore *FASTCALL(rwsem_wake(struct rw_semaphore *));
  44. extern struct rw_semaphore *FASTCALL(rwsem_downgrade_wake(struct rw_semaphore *sem));
  45. /*
  46. * the semaphore definition
  47. */
  48. struct rw_semaphore {
  49. signed long count;
  50. #define RWSEM_UNLOCKED_VALUE 0x00000000
  51. #define RWSEM_ACTIVE_BIAS 0x00000001
  52. #define RWSEM_ACTIVE_MASK 0x0000ffff
  53. #define RWSEM_WAITING_BIAS (-0x00010000)
  54. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  55. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  56. spinlock_t wait_lock;
  57. struct list_head wait_list;
  58. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  59. struct lockdep_map dep_map;
  60. #endif
  61. };
  62. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  63. # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  64. #else
  65. # define __RWSEM_DEP_MAP_INIT(lockname)
  66. #endif
  67. #define __RWSEM_INITIALIZER(name) \
  68. { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED((name).wait_lock), \
  69. LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
  70. #define DECLARE_RWSEM(name) \
  71. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  72. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  73. struct lock_class_key *key);
  74. #define init_rwsem(sem) \
  75. do { \
  76. static struct lock_class_key __key; \
  77. \
  78. __init_rwsem((sem), #sem, &__key); \
  79. } while (0)
  80. /*
  81. * lock for reading
  82. */
  83. static inline void __down_read(struct rw_semaphore *sem)
  84. {
  85. __asm__ __volatile__(
  86. "# beginning down_read\n\t"
  87. LOCK_PREFIX " incl (%%eax)\n\t" /* adds 0x00000001, returns the old value */
  88. " jns 1f\n"
  89. " call call_rwsem_down_read_failed\n"
  90. "1:\n\t"
  91. "# ending down_read\n\t"
  92. : "+m" (sem->count)
  93. : "a" (sem)
  94. : "memory", "cc");
  95. }
  96. /*
  97. * trylock for reading -- returns 1 if successful, 0 if contention
  98. */
  99. static inline int __down_read_trylock(struct rw_semaphore *sem)
  100. {
  101. __s32 result, tmp;
  102. __asm__ __volatile__(
  103. "# beginning __down_read_trylock\n\t"
  104. " movl %0,%1\n\t"
  105. "1:\n\t"
  106. " movl %1,%2\n\t"
  107. " addl %3,%2\n\t"
  108. " jle 2f\n\t"
  109. LOCK_PREFIX " cmpxchgl %2,%0\n\t"
  110. " jnz 1b\n\t"
  111. "2:\n\t"
  112. "# ending __down_read_trylock\n\t"
  113. : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
  114. : "i" (RWSEM_ACTIVE_READ_BIAS)
  115. : "memory", "cc");
  116. return result>=0 ? 1 : 0;
  117. }
  118. /*
  119. * lock for writing
  120. */
  121. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  122. {
  123. int tmp;
  124. tmp = RWSEM_ACTIVE_WRITE_BIAS;
  125. __asm__ __volatile__(
  126. "# beginning down_write\n\t"
  127. LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" /* subtract 0x0000ffff, returns the old value */
  128. " testl %%edx,%%edx\n\t" /* was the count 0 before? */
  129. " jz 1f\n"
  130. " call call_rwsem_down_write_failed\n"
  131. "1:\n"
  132. "# ending down_write"
  133. : "+m" (sem->count), "=d" (tmp)
  134. : "a" (sem), "1" (tmp)
  135. : "memory", "cc");
  136. }
  137. static inline void __down_write(struct rw_semaphore *sem)
  138. {
  139. __down_write_nested(sem, 0);
  140. }
  141. /*
  142. * trylock for writing -- returns 1 if successful, 0 if contention
  143. */
  144. static inline int __down_write_trylock(struct rw_semaphore *sem)
  145. {
  146. signed long ret = cmpxchg(&sem->count,
  147. RWSEM_UNLOCKED_VALUE,
  148. RWSEM_ACTIVE_WRITE_BIAS);
  149. if (ret == RWSEM_UNLOCKED_VALUE)
  150. return 1;
  151. return 0;
  152. }
  153. /*
  154. * unlock after reading
  155. */
  156. static inline void __up_read(struct rw_semaphore *sem)
  157. {
  158. __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
  159. __asm__ __volatile__(
  160. "# beginning __up_read\n\t"
  161. LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" /* subtracts 1, returns the old value */
  162. " jns 1f\n\t"
  163. " call call_rwsem_wake\n"
  164. "1:\n"
  165. "# ending __up_read\n"
  166. : "+m" (sem->count), "=d" (tmp)
  167. : "a" (sem), "1" (tmp)
  168. : "memory", "cc");
  169. }
  170. /*
  171. * unlock after writing
  172. */
  173. static inline void __up_write(struct rw_semaphore *sem)
  174. {
  175. __asm__ __volatile__(
  176. "# beginning __up_write\n\t"
  177. " movl %2,%%edx\n\t"
  178. LOCK_PREFIX " xaddl %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
  179. " jz 1f\n"
  180. " call call_rwsem_wake\n"
  181. "1:\n\t"
  182. "# ending __up_write\n"
  183. : "+m" (sem->count)
  184. : "a" (sem), "i" (-RWSEM_ACTIVE_WRITE_BIAS)
  185. : "memory", "cc", "edx");
  186. }
  187. /*
  188. * downgrade write lock to read lock
  189. */
  190. static inline void __downgrade_write(struct rw_semaphore *sem)
  191. {
  192. __asm__ __volatile__(
  193. "# beginning __downgrade_write\n\t"
  194. LOCK_PREFIX " addl %2,(%%eax)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
  195. " jns 1f\n\t"
  196. " call call_rwsem_downgrade_wake\n"
  197. "1:\n\t"
  198. "# ending __downgrade_write\n"
  199. : "+m" (sem->count)
  200. : "a" (sem), "i" (-RWSEM_WAITING_BIAS)
  201. : "memory", "cc");
  202. }
  203. /*
  204. * implement atomic add functionality
  205. */
  206. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  207. {
  208. __asm__ __volatile__(
  209. LOCK_PREFIX "addl %1,%0"
  210. : "+m" (sem->count)
  211. : "ir" (delta));
  212. }
  213. /*
  214. * implement exchange and add functionality
  215. */
  216. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  217. {
  218. int tmp = delta;
  219. __asm__ __volatile__(
  220. LOCK_PREFIX "xadd %0,%1"
  221. : "+r" (tmp), "+m" (sem->count)
  222. : : "memory");
  223. return tmp+delta;
  224. }
  225. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  226. {
  227. return (sem->count != 0);
  228. }
  229. #endif /* __KERNEL__ */
  230. #endif /* _I386_RWSEM_H */