rwsem.h 7.6 KB

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