rwsem.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 %1,%1\n\t"
  100. /* was the count 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 ret = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
  119. RWSEM_ACTIVE_WRITE_BIAS);
  120. if (ret == RWSEM_UNLOCKED_VALUE)
  121. return 1;
  122. return 0;
  123. }
  124. /*
  125. * unlock after reading
  126. */
  127. static inline void __up_read(struct rw_semaphore *sem)
  128. {
  129. long tmp;
  130. asm volatile("# beginning __up_read\n\t"
  131. LOCK_PREFIX " xadd %1,(%2)\n\t"
  132. /* subtracts 1, returns the old value */
  133. " jns 1f\n\t"
  134. " call call_rwsem_wake\n" /* expects old value in %edx */
  135. "1:\n"
  136. "# ending __up_read\n"
  137. : "+m" (sem->count), "=d" (tmp)
  138. : "a" (sem), "1" (-RWSEM_ACTIVE_READ_BIAS)
  139. : "memory", "cc");
  140. }
  141. /*
  142. * unlock after writing
  143. */
  144. static inline void __up_write(struct rw_semaphore *sem)
  145. {
  146. long tmp;
  147. asm volatile("# beginning __up_write\n\t"
  148. LOCK_PREFIX " xadd %1,(%2)\n\t"
  149. /* subtracts 0xffff0001, returns the old value */
  150. " jns 1f\n\t"
  151. " call call_rwsem_wake\n" /* expects old value in %edx */
  152. "1:\n\t"
  153. "# ending __up_write\n"
  154. : "+m" (sem->count), "=d" (tmp)
  155. : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
  156. : "memory", "cc");
  157. }
  158. /*
  159. * downgrade write lock to read lock
  160. */
  161. static inline void __downgrade_write(struct rw_semaphore *sem)
  162. {
  163. asm volatile("# beginning __downgrade_write\n\t"
  164. LOCK_PREFIX _ASM_ADD "%2,(%1)\n\t"
  165. /*
  166. * transitions 0xZZZZ0001 -> 0xYYYY0001 (i386)
  167. * 0xZZZZZZZZ00000001 -> 0xYYYYYYYY00000001 (x86_64)
  168. */
  169. " jns 1f\n\t"
  170. " call call_rwsem_downgrade_wake\n"
  171. "1:\n\t"
  172. "# ending __downgrade_write\n"
  173. : "+m" (sem->count)
  174. : "a" (sem), "er" (-RWSEM_WAITING_BIAS)
  175. : "memory", "cc");
  176. }
  177. /*
  178. * implement atomic add functionality
  179. */
  180. static inline void rwsem_atomic_add(long delta, struct rw_semaphore *sem)
  181. {
  182. asm volatile(LOCK_PREFIX _ASM_ADD "%1,%0"
  183. : "+m" (sem->count)
  184. : "er" (delta));
  185. }
  186. /*
  187. * implement exchange and add functionality
  188. */
  189. static inline long rwsem_atomic_update(long delta, struct rw_semaphore *sem)
  190. {
  191. return delta + xadd(&sem->count, delta);
  192. }
  193. #endif /* __KERNEL__ */
  194. #endif /* _ASM_X86_RWSEM_H */