rwsem.h 7.8 KB

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