rwsem.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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-x86/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 asmregparm struct rw_semaphore *
  42. rwsem_down_read_failed(struct rw_semaphore *sem);
  43. extern asmregparm struct rw_semaphore *
  44. rwsem_down_write_failed(struct rw_semaphore *sem);
  45. extern asmregparm struct rw_semaphore *
  46. rwsem_wake(struct rw_semaphore *);
  47. extern asmregparm struct rw_semaphore *
  48. rwsem_downgrade_wake(struct rw_semaphore *sem);
  49. /*
  50. * the semaphore definition
  51. */
  52. struct rw_semaphore {
  53. signed long count;
  54. #define RWSEM_UNLOCKED_VALUE 0x00000000
  55. #define RWSEM_ACTIVE_BIAS 0x00000001
  56. #define RWSEM_ACTIVE_MASK 0x0000ffff
  57. #define RWSEM_WAITING_BIAS (-0x00010000)
  58. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  59. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  60. spinlock_t wait_lock;
  61. struct list_head wait_list;
  62. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  63. struct lockdep_map dep_map;
  64. #endif
  65. };
  66. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  67. # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
  68. #else
  69. # define __RWSEM_DEP_MAP_INIT(lockname)
  70. #endif
  71. #define __RWSEM_INITIALIZER(name) \
  72. { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED((name).wait_lock), \
  73. LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
  74. #define DECLARE_RWSEM(name) \
  75. struct rw_semaphore name = __RWSEM_INITIALIZER(name)
  76. extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  77. struct lock_class_key *key);
  78. #define init_rwsem(sem) \
  79. do { \
  80. static struct lock_class_key __key; \
  81. \
  82. __init_rwsem((sem), #sem, &__key); \
  83. } while (0)
  84. /*
  85. * lock for reading
  86. */
  87. static inline void __down_read(struct rw_semaphore *sem)
  88. {
  89. __asm__ __volatile__(
  90. "# beginning down_read\n\t"
  91. LOCK_PREFIX " incl (%%eax)\n\t" /* adds 0x00000001, returns the old value */
  92. " jns 1f\n"
  93. " call call_rwsem_down_read_failed\n"
  94. "1:\n\t"
  95. "# ending down_read\n\t"
  96. : "+m" (sem->count)
  97. : "a" (sem)
  98. : "memory", "cc");
  99. }
  100. /*
  101. * trylock for reading -- returns 1 if successful, 0 if contention
  102. */
  103. static inline int __down_read_trylock(struct rw_semaphore *sem)
  104. {
  105. __s32 result, tmp;
  106. __asm__ __volatile__(
  107. "# beginning __down_read_trylock\n\t"
  108. " movl %0,%1\n\t"
  109. "1:\n\t"
  110. " movl %1,%2\n\t"
  111. " addl %3,%2\n\t"
  112. " jle 2f\n\t"
  113. LOCK_PREFIX " cmpxchgl %2,%0\n\t"
  114. " jnz 1b\n\t"
  115. "2:\n\t"
  116. "# ending __down_read_trylock\n\t"
  117. : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
  118. : "i" (RWSEM_ACTIVE_READ_BIAS)
  119. : "memory", "cc");
  120. return result>=0 ? 1 : 0;
  121. }
  122. /*
  123. * lock for writing
  124. */
  125. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  126. {
  127. int tmp;
  128. tmp = RWSEM_ACTIVE_WRITE_BIAS;
  129. __asm__ __volatile__(
  130. "# beginning down_write\n\t"
  131. LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" /* subtract 0x0000ffff, returns the old value */
  132. " testl %%edx,%%edx\n\t" /* was the count 0 before? */
  133. " jz 1f\n"
  134. " call call_rwsem_down_write_failed\n"
  135. "1:\n"
  136. "# ending down_write"
  137. : "+m" (sem->count), "=d" (tmp)
  138. : "a" (sem), "1" (tmp)
  139. : "memory", "cc");
  140. }
  141. static inline void __down_write(struct rw_semaphore *sem)
  142. {
  143. __down_write_nested(sem, 0);
  144. }
  145. /*
  146. * trylock for writing -- returns 1 if successful, 0 if contention
  147. */
  148. static inline int __down_write_trylock(struct rw_semaphore *sem)
  149. {
  150. signed long ret = cmpxchg(&sem->count,
  151. RWSEM_UNLOCKED_VALUE,
  152. RWSEM_ACTIVE_WRITE_BIAS);
  153. if (ret == RWSEM_UNLOCKED_VALUE)
  154. return 1;
  155. return 0;
  156. }
  157. /*
  158. * unlock after reading
  159. */
  160. static inline void __up_read(struct rw_semaphore *sem)
  161. {
  162. __s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
  163. __asm__ __volatile__(
  164. "# beginning __up_read\n\t"
  165. LOCK_PREFIX " xadd %%edx,(%%eax)\n\t" /* subtracts 1, returns the old value */
  166. " jns 1f\n\t"
  167. " call call_rwsem_wake\n"
  168. "1:\n"
  169. "# ending __up_read\n"
  170. : "+m" (sem->count), "=d" (tmp)
  171. : "a" (sem), "1" (tmp)
  172. : "memory", "cc");
  173. }
  174. /*
  175. * unlock after writing
  176. */
  177. static inline void __up_write(struct rw_semaphore *sem)
  178. {
  179. __asm__ __volatile__(
  180. "# beginning __up_write\n\t"
  181. " movl %2,%%edx\n\t"
  182. LOCK_PREFIX " xaddl %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
  183. " jz 1f\n"
  184. " call call_rwsem_wake\n"
  185. "1:\n\t"
  186. "# ending __up_write\n"
  187. : "+m" (sem->count)
  188. : "a" (sem), "i" (-RWSEM_ACTIVE_WRITE_BIAS)
  189. : "memory", "cc", "edx");
  190. }
  191. /*
  192. * downgrade write lock to read lock
  193. */
  194. static inline void __downgrade_write(struct rw_semaphore *sem)
  195. {
  196. __asm__ __volatile__(
  197. "# beginning __downgrade_write\n\t"
  198. LOCK_PREFIX " addl %2,(%%eax)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */
  199. " jns 1f\n\t"
  200. " call call_rwsem_downgrade_wake\n"
  201. "1:\n\t"
  202. "# ending __downgrade_write\n"
  203. : "+m" (sem->count)
  204. : "a" (sem), "i" (-RWSEM_WAITING_BIAS)
  205. : "memory", "cc");
  206. }
  207. /*
  208. * implement atomic add functionality
  209. */
  210. static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
  211. {
  212. __asm__ __volatile__(
  213. LOCK_PREFIX "addl %1,%0"
  214. : "+m" (sem->count)
  215. : "ir" (delta));
  216. }
  217. /*
  218. * implement exchange and add functionality
  219. */
  220. static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
  221. {
  222. int tmp = delta;
  223. __asm__ __volatile__(
  224. LOCK_PREFIX "xadd %0,%1"
  225. : "+r" (tmp), "+m" (sem->count)
  226. : : "memory");
  227. return tmp+delta;
  228. }
  229. static inline int rwsem_is_locked(struct rw_semaphore *sem)
  230. {
  231. return (sem->count != 0);
  232. }
  233. #endif /* __KERNEL__ */
  234. #endif /* _I386_RWSEM_H */