rwsem.h 7.9 KB

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