rwsem-spinlock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* rwsem-spinlock.c: R/W semaphores: contention handling functions for
  2. * generic spinlock implementation
  3. *
  4. * Copyright (c) 2001 David Howells (dhowells@redhat.com).
  5. * - Derived partially from idea by Andrea Arcangeli <andrea@suse.de>
  6. * - Derived also from comments by Linus
  7. */
  8. #include <linux/rwsem.h>
  9. #include <linux/sched.h>
  10. #include <linux/module.h>
  11. struct rwsem_waiter {
  12. struct list_head list;
  13. struct task_struct *task;
  14. unsigned int flags;
  15. #define RWSEM_WAITING_FOR_READ 0x00000001
  16. #define RWSEM_WAITING_FOR_WRITE 0x00000002
  17. };
  18. int rwsem_is_locked(struct rw_semaphore *sem)
  19. {
  20. int ret = 1;
  21. unsigned long flags;
  22. if (spin_trylock_irqsave(&sem->wait_lock, flags)) {
  23. ret = (sem->activity != 0);
  24. spin_unlock_irqrestore(&sem->wait_lock, flags);
  25. }
  26. return ret;
  27. }
  28. EXPORT_SYMBOL(rwsem_is_locked);
  29. /*
  30. * initialise the semaphore
  31. */
  32. void __init_rwsem(struct rw_semaphore *sem, const char *name,
  33. struct lock_class_key *key)
  34. {
  35. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  36. /*
  37. * Make sure we are not reinitializing a held semaphore:
  38. */
  39. debug_check_no_locks_freed((void *)sem, sizeof(*sem));
  40. lockdep_init_map(&sem->dep_map, name, key, 0);
  41. #endif
  42. sem->activity = 0;
  43. spin_lock_init(&sem->wait_lock);
  44. INIT_LIST_HEAD(&sem->wait_list);
  45. }
  46. EXPORT_SYMBOL(__init_rwsem);
  47. /*
  48. * handle the lock release when processes blocked on it that can now run
  49. * - if we come here, then:
  50. * - the 'active count' _reached_ zero
  51. * - the 'waiting count' is non-zero
  52. * - the spinlock must be held by the caller
  53. * - woken process blocks are discarded from the list after having task zeroed
  54. * - writers are only woken if wakewrite is non-zero
  55. */
  56. static inline struct rw_semaphore *
  57. __rwsem_do_wake(struct rw_semaphore *sem, int wakewrite)
  58. {
  59. struct rwsem_waiter *waiter;
  60. struct task_struct *tsk;
  61. int woken;
  62. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  63. if (!wakewrite) {
  64. if (waiter->flags & RWSEM_WAITING_FOR_WRITE)
  65. goto out;
  66. goto dont_wake_writers;
  67. }
  68. /* if we are allowed to wake writers try to grant a single write lock
  69. * if there's a writer at the front of the queue
  70. * - we leave the 'waiting count' incremented to signify potential
  71. * contention
  72. */
  73. if (waiter->flags & RWSEM_WAITING_FOR_WRITE) {
  74. sem->activity = -1;
  75. list_del(&waiter->list);
  76. tsk = waiter->task;
  77. /* Don't touch waiter after ->task has been NULLed */
  78. smp_mb();
  79. waiter->task = NULL;
  80. wake_up_process(tsk);
  81. put_task_struct(tsk);
  82. goto out;
  83. }
  84. /* grant an infinite number of read locks to the front of the queue */
  85. dont_wake_writers:
  86. woken = 0;
  87. while (waiter->flags & RWSEM_WAITING_FOR_READ) {
  88. struct list_head *next = waiter->list.next;
  89. list_del(&waiter->list);
  90. tsk = waiter->task;
  91. smp_mb();
  92. waiter->task = NULL;
  93. wake_up_process(tsk);
  94. put_task_struct(tsk);
  95. woken++;
  96. if (list_empty(&sem->wait_list))
  97. break;
  98. waiter = list_entry(next, struct rwsem_waiter, list);
  99. }
  100. sem->activity += woken;
  101. out:
  102. return sem;
  103. }
  104. /*
  105. * wake a single writer
  106. */
  107. static inline struct rw_semaphore *
  108. __rwsem_wake_one_writer(struct rw_semaphore *sem)
  109. {
  110. struct rwsem_waiter *waiter;
  111. struct task_struct *tsk;
  112. sem->activity = -1;
  113. waiter = list_entry(sem->wait_list.next, struct rwsem_waiter, list);
  114. list_del(&waiter->list);
  115. tsk = waiter->task;
  116. smp_mb();
  117. waiter->task = NULL;
  118. wake_up_process(tsk);
  119. put_task_struct(tsk);
  120. return sem;
  121. }
  122. /*
  123. * get a read lock on the semaphore
  124. */
  125. void __sched __down_read(struct rw_semaphore *sem)
  126. {
  127. struct rwsem_waiter waiter;
  128. struct task_struct *tsk;
  129. unsigned long flags;
  130. spin_lock_irqsave(&sem->wait_lock, flags);
  131. if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
  132. /* granted */
  133. sem->activity++;
  134. spin_unlock_irqrestore(&sem->wait_lock, flags);
  135. goto out;
  136. }
  137. tsk = current;
  138. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  139. /* set up my own style of waitqueue */
  140. waiter.task = tsk;
  141. waiter.flags = RWSEM_WAITING_FOR_READ;
  142. get_task_struct(tsk);
  143. list_add_tail(&waiter.list, &sem->wait_list);
  144. /* we don't need to touch the semaphore struct anymore */
  145. spin_unlock_irqrestore(&sem->wait_lock, flags);
  146. /* wait to be given the lock */
  147. for (;;) {
  148. if (!waiter.task)
  149. break;
  150. schedule();
  151. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  152. }
  153. tsk->state = TASK_RUNNING;
  154. out:
  155. ;
  156. }
  157. /*
  158. * trylock for reading -- returns 1 if successful, 0 if contention
  159. */
  160. int __down_read_trylock(struct rw_semaphore *sem)
  161. {
  162. unsigned long flags;
  163. int ret = 0;
  164. spin_lock_irqsave(&sem->wait_lock, flags);
  165. if (sem->activity >= 0 && list_empty(&sem->wait_list)) {
  166. /* granted */
  167. sem->activity++;
  168. ret = 1;
  169. }
  170. spin_unlock_irqrestore(&sem->wait_lock, flags);
  171. return ret;
  172. }
  173. /*
  174. * get a write lock on the semaphore
  175. * - we increment the waiting count anyway to indicate an exclusive lock
  176. */
  177. void __sched __down_write_nested(struct rw_semaphore *sem, int subclass)
  178. {
  179. struct rwsem_waiter waiter;
  180. struct task_struct *tsk;
  181. unsigned long flags;
  182. spin_lock_irqsave(&sem->wait_lock, flags);
  183. if (sem->activity == 0 && list_empty(&sem->wait_list)) {
  184. /* granted */
  185. sem->activity = -1;
  186. spin_unlock_irqrestore(&sem->wait_lock, flags);
  187. goto out;
  188. }
  189. tsk = current;
  190. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  191. /* set up my own style of waitqueue */
  192. waiter.task = tsk;
  193. waiter.flags = RWSEM_WAITING_FOR_WRITE;
  194. get_task_struct(tsk);
  195. list_add_tail(&waiter.list, &sem->wait_list);
  196. /* we don't need to touch the semaphore struct anymore */
  197. spin_unlock_irqrestore(&sem->wait_lock, flags);
  198. /* wait to be given the lock */
  199. for (;;) {
  200. if (!waiter.task)
  201. break;
  202. schedule();
  203. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  204. }
  205. tsk->state = TASK_RUNNING;
  206. out:
  207. ;
  208. }
  209. void __sched __down_write(struct rw_semaphore *sem)
  210. {
  211. __down_write_nested(sem, 0);
  212. }
  213. /*
  214. * trylock for writing -- returns 1 if successful, 0 if contention
  215. */
  216. int __down_write_trylock(struct rw_semaphore *sem)
  217. {
  218. unsigned long flags;
  219. int ret = 0;
  220. spin_lock_irqsave(&sem->wait_lock, flags);
  221. if (sem->activity == 0 && list_empty(&sem->wait_list)) {
  222. /* granted */
  223. sem->activity = -1;
  224. ret = 1;
  225. }
  226. spin_unlock_irqrestore(&sem->wait_lock, flags);
  227. return ret;
  228. }
  229. /*
  230. * release a read lock on the semaphore
  231. */
  232. void __up_read(struct rw_semaphore *sem)
  233. {
  234. unsigned long flags;
  235. spin_lock_irqsave(&sem->wait_lock, flags);
  236. if (--sem->activity == 0 && !list_empty(&sem->wait_list))
  237. sem = __rwsem_wake_one_writer(sem);
  238. spin_unlock_irqrestore(&sem->wait_lock, flags);
  239. }
  240. /*
  241. * release a write lock on the semaphore
  242. */
  243. void __up_write(struct rw_semaphore *sem)
  244. {
  245. unsigned long flags;
  246. spin_lock_irqsave(&sem->wait_lock, flags);
  247. sem->activity = 0;
  248. if (!list_empty(&sem->wait_list))
  249. sem = __rwsem_do_wake(sem, 1);
  250. spin_unlock_irqrestore(&sem->wait_lock, flags);
  251. }
  252. /*
  253. * downgrade a write lock into a read lock
  254. * - just wake up any readers at the front of the queue
  255. */
  256. void __downgrade_write(struct rw_semaphore *sem)
  257. {
  258. unsigned long flags;
  259. spin_lock_irqsave(&sem->wait_lock, flags);
  260. sem->activity = 1;
  261. if (!list_empty(&sem->wait_list))
  262. sem = __rwsem_do_wake(sem, 0);
  263. spin_unlock_irqrestore(&sem->wait_lock, flags);
  264. }