rwsem.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 _ASM_X86_RWSEM_H
  32. #define _ASM_X86_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 <asm/asm.h>
  38. extern asmregparm struct rw_semaphore *
  39. rwsem_down_read_failed(struct rw_semaphore *sem);
  40. extern asmregparm struct rw_semaphore *
  41. rwsem_down_write_failed(struct rw_semaphore *sem);
  42. extern asmregparm struct rw_semaphore *
  43. rwsem_wake(struct rw_semaphore *);
  44. extern asmregparm struct rw_semaphore *
  45. rwsem_downgrade_wake(struct rw_semaphore *sem);
  46. /*
  47. * The bias values and the counter type limits the number of
  48. * potential readers/writers to 32767 for 32 bits and 2147483647
  49. * for 64 bits.
  50. */
  51. #ifdef CONFIG_X86_64
  52. # define RWSEM_ACTIVE_MASK 0xffffffffL
  53. #else
  54. # define RWSEM_ACTIVE_MASK 0x0000ffffL
  55. #endif
  56. #define RWSEM_UNLOCKED_VALUE 0x00000000L
  57. #define RWSEM_ACTIVE_BIAS 0x00000001L
  58. #define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1)
  59. #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
  60. #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
  61. /*
  62. * lock for reading
  63. */
  64. static inline void __down_read(struct rw_semaphore *sem)
  65. {
  66. asm volatile("# beginning down_read\n\t"
  67. LOCK_PREFIX _ASM_INC "(%1)\n\t"
  68. /* adds 0x00000001 */
  69. " jns 1f\n"
  70. " call call_rwsem_down_read_failed\n"
  71. "1:\n\t"
  72. "# ending down_read\n\t"
  73. : "+m" (sem->count)
  74. : "a" (sem)
  75. : "memory", "cc");
  76. }
  77. /*
  78. * trylock for reading -- returns 1 if successful, 0 if contention
  79. */
  80. static inline int __down_read_trylock(struct rw_semaphore *sem)
  81. {
  82. long result, tmp;
  83. asm volatile("# beginning __down_read_trylock\n\t"
  84. " mov %0,%1\n\t"
  85. "1:\n\t"
  86. " mov %1,%2\n\t"
  87. " add %3,%2\n\t"
  88. " jle 2f\n\t"
  89. LOCK_PREFIX " cmpxchg %2,%0\n\t"
  90. " jnz 1b\n\t"
  91. "2:\n\t"
  92. "# ending __down_read_trylock\n\t"
  93. : "+m" (sem->count), "=&a" (result), "=&r" (tmp)
  94. : "i" (RWSEM_ACTIVE_READ_BIAS)
  95. : "memory", "cc");
  96. return result >= 0 ? 1 : 0;
  97. }
  98. /*
  99. * lock for writing
  100. */
  101. static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
  102. {
  103. long tmp;
  104. asm volatile("# beginning down_write\n\t"
  105. LOCK_PREFIX " xadd %1,(%2)\n\t"
  106. /* adds 0xffff0001, returns the old value */
  107. " test %1,%1\n\t"
  108. /* was the count 0 before? */
  109. " jz 1f\n"
  110. " call call_rwsem_down_write_failed\n"
  111. "1:\n"
  112. "# ending down_write"
  113. : "+m" (sem->count), "=d" (tmp)
  114. : "a" (sem), "1" (RWSEM_ACTIVE_WRITE_BIAS)
  115. : "memory", "cc");
  116. }
  117. static inline void __down_write(struct rw_semaphore *sem)
  118. {
  119. __down_write_nested(sem, 0);
  120. }
  121. /*
  122. * trylock for writing -- returns 1 if successful, 0 if contention
  123. */
  124. static inline int __down_write_trylock(struct rw_semaphore *sem)
  125. {
  126. long ret = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  127. RWSEM_ACTIVE_WRITE_BIAS);
  128. if (ret == RWSEM_UNLOCKED_VALUE)
  129. return 1;
  130. return 0;
  131. }
  132. /*
  133. * unlock after reading
  134. */
  135. static inline void __up_read(struct rw_semaphore *sem)
  136. {
  137. long tmp;
  138. asm volatile("# beginning __up_read\n\t"
  139. LOCK_PREFIX " xadd %1,(%2)\n\t"
  140. /* subtracts 1, returns the old value */
  141. " jns 1f\n\t"
  142. " call call_rwsem_wake\n" /* expects old value in %edx */
  143. "1:\n"
  144. "# ending __up_read\n"
  145. : "+m" (sem->count), "=d" (tmp)
  146. : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS)
  147. : "memory", "cc");
  148. }
  149. /*
  150. * unlock after writing
  151. */
  152. static inline void __up_write(struct rw_semaphore *sem)
  153. {
  154. long tmp;
  155. asm volatile("# beginning __up_write\n\t"
  156. LOCK_PREFIX " xadd %1,(%2)\n\t"
  157. /* subtracts 0xffff0001, returns the old value */
  158. " jns 1f\n\t"
  159. " call call_rwsem_wake\n" /* expects old value in %edx */
  160. "1:\n\t"
  161. "# ending __up_write\n"
  162. : "+m" (sem->count), "=d" (tmp)
  163. : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
  164. : "memory", "cc");
  165. }
  166. /*
  167. * downgrade write lock to read lock
  168. */
  169. static inline void __downgrade_write(struct rw_semaphore *sem)
  170. {
  171. asm volatile("# beginning __downgrade_write\n\t"
  172. LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
  173. /*
  174. * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
  175. * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
  176. */
  177. " jns 1f\n\t"
  178. " call call_rwsem_downgrade_wake\n"
  179. "1:\n\t"
  180. "# ending __downgrade_write\n"
  181. : "+m" (sem->count)
  182. : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
  183. : "memory", "cc");
  184. }
  185. /*
  186. * implement atomic add functionality
  187. */
  188. static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
  189. {
  190. asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0"
  191. : "+m" (sem->count)
  192. : "er" (delta));
  193. }
  194. /*
  195. * implement exchange and add functionality
  196. */
  197. static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
  198. {
  199. long tmp = delta;
  200. asm volatile(LOCK_PREFIX "xadd %0,%1"
  201. : "+r" (tmp), "+m" (sem->count)
  202. : : "memory");
  203. return tmp + delta;
  204. }
  205. #endif /* __KERNEL__ */
  206. #endif /* _ASM_X86_RWSEM_H */