rwsem.h 6.5 KB

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