semaphore.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * ARM semaphore implementation, taken from
  3. *
  4. * i386 semaphore implementation.
  5. *
  6. * (C) Copyright 1999 Linus Torvalds
  7. * (C) Copyright 2003 Ian Molton (ARM26 mods)
  8. *
  9. * Modified for ARM by Russell King
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/config.h>
  17. #include <linux/sched.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <asm/semaphore.h>
  21. /*
  22. * Semaphores are implemented using a two-way counter:
  23. * The "count" variable is decremented for each process
  24. * that tries to acquire the semaphore, while the "sleeping"
  25. * variable is a count of such acquires.
  26. *
  27. * Notably, the inline "up()" and "down()" functions can
  28. * efficiently test if they need to do any extra work (up
  29. * needs to do something only if count was negative before
  30. * the increment operation.
  31. *
  32. * "sleeping" and the contention routine ordering is
  33. * protected by the semaphore spinlock.
  34. *
  35. * Note that these functions are only called when there is
  36. * contention on the lock, and as such all this is the
  37. * "non-critical" part of the whole semaphore business. The
  38. * critical part is the inline stuff in <asm/semaphore.h>
  39. * where we want to avoid any extra jumps and calls.
  40. */
  41. /*
  42. * Logic:
  43. * - only on a boundary condition do we need to care. When we go
  44. * from a negative count to a non-negative, we wake people up.
  45. * - when we go from a non-negative count to a negative do we
  46. * (a) synchronize with the "sleeper" count and (b) make sure
  47. * that we're on the wakeup list before we synchronize so that
  48. * we cannot lose wakeup events.
  49. */
  50. void __up(struct semaphore *sem)
  51. {
  52. wake_up(&sem->wait);
  53. }
  54. static DEFINE_SPINLOCK(semaphore_lock);
  55. void __sched __down(struct semaphore * sem)
  56. {
  57. struct task_struct *tsk = current;
  58. DECLARE_WAITQUEUE(wait, tsk);
  59. tsk->state = TASK_UNINTERRUPTIBLE;
  60. add_wait_queue_exclusive(&sem->wait, &wait);
  61. spin_lock_irq(&semaphore_lock);
  62. sem->sleepers++;
  63. for (;;) {
  64. int sleepers = sem->sleepers;
  65. /*
  66. * Add "everybody else" into it. They aren't
  67. * playing, because we own the spinlock.
  68. */
  69. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  70. sem->sleepers = 0;
  71. break;
  72. }
  73. sem->sleepers = 1; /* us - see -1 above */
  74. spin_unlock_irq(&semaphore_lock);
  75. schedule();
  76. tsk->state = TASK_UNINTERRUPTIBLE;
  77. spin_lock_irq(&semaphore_lock);
  78. }
  79. spin_unlock_irq(&semaphore_lock);
  80. remove_wait_queue(&sem->wait, &wait);
  81. tsk->state = TASK_RUNNING;
  82. wake_up(&sem->wait);
  83. }
  84. int __sched __down_interruptible(struct semaphore * sem)
  85. {
  86. int retval = 0;
  87. struct task_struct *tsk = current;
  88. DECLARE_WAITQUEUE(wait, tsk);
  89. tsk->state = TASK_INTERRUPTIBLE;
  90. add_wait_queue_exclusive(&sem->wait, &wait);
  91. spin_lock_irq(&semaphore_lock);
  92. sem->sleepers ++;
  93. for (;;) {
  94. int sleepers = sem->sleepers;
  95. /*
  96. * With signals pending, this turns into
  97. * the trylock failure case - we won't be
  98. * sleeping, and we* can't get the lock as
  99. * it has contention. Just correct the count
  100. * and exit.
  101. */
  102. if (signal_pending(current)) {
  103. retval = -EINTR;
  104. sem->sleepers = 0;
  105. atomic_add(sleepers, &sem->count);
  106. break;
  107. }
  108. /*
  109. * Add "everybody else" into it. They aren't
  110. * playing, because we own the spinlock. The
  111. * "-1" is because we're still hoping to get
  112. * the lock.
  113. */
  114. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  115. sem->sleepers = 0;
  116. break;
  117. }
  118. sem->sleepers = 1; /* us - see -1 above */
  119. spin_unlock_irq(&semaphore_lock);
  120. schedule();
  121. tsk->state = TASK_INTERRUPTIBLE;
  122. spin_lock_irq(&semaphore_lock);
  123. }
  124. spin_unlock_irq(&semaphore_lock);
  125. tsk->state = TASK_RUNNING;
  126. remove_wait_queue(&sem->wait, &wait);
  127. wake_up(&sem->wait);
  128. return retval;
  129. }
  130. /*
  131. * Trylock failed - make sure we correct for
  132. * having decremented the count.
  133. *
  134. * We could have done the trylock with a
  135. * single "cmpxchg" without failure cases,
  136. * but then it wouldn't work on a 386.
  137. */
  138. int __down_trylock(struct semaphore * sem)
  139. {
  140. int sleepers;
  141. unsigned long flags;
  142. spin_lock_irqsave(&semaphore_lock, flags);
  143. sleepers = sem->sleepers + 1;
  144. sem->sleepers = 0;
  145. /*
  146. * Add "everybody else" and us into it. They aren't
  147. * playing, because we own the spinlock.
  148. */
  149. if (!atomic_add_negative(sleepers, &sem->count))
  150. wake_up(&sem->wait);
  151. spin_unlock_irqrestore(&semaphore_lock, flags);
  152. return 1;
  153. }
  154. /*
  155. * The semaphore operations have a special calling sequence that
  156. * allow us to do a simpler in-line version of them. These routines
  157. * need to convert that sequence back into the C sequence when
  158. * there is contention on the semaphore.
  159. *
  160. * ip contains the semaphore pointer on entry. Save the C-clobbered
  161. * registers (r0 to r3 and lr), but not ip, as we use it as a return
  162. * value in some cases..
  163. */
  164. asm(" .section .sched.text , #alloc, #execinstr \n\
  165. .align 5 \n\
  166. .globl __down_failed \n\
  167. __down_failed: \n\
  168. stmfd sp!, {r0 - r3, lr} \n\
  169. mov r0, ip \n\
  170. bl __down \n\
  171. ldmfd sp!, {r0 - r3, pc}^ \n\
  172. \n\
  173. .align 5 \n\
  174. .globl __down_interruptible_failed \n\
  175. __down_interruptible_failed: \n\
  176. stmfd sp!, {r0 - r3, lr} \n\
  177. mov r0, ip \n\
  178. bl __down_interruptible \n\
  179. mov ip, r0 \n\
  180. ldmfd sp!, {r0 - r3, pc}^ \n\
  181. \n\
  182. .align 5 \n\
  183. .globl __down_trylock_failed \n\
  184. __down_trylock_failed: \n\
  185. stmfd sp!, {r0 - r3, lr} \n\
  186. mov r0, ip \n\
  187. bl __down_trylock \n\
  188. mov ip, r0 \n\
  189. ldmfd sp!, {r0 - r3, pc}^ \n\
  190. \n\
  191. .align 5 \n\
  192. .globl __up_wakeup \n\
  193. __up_wakeup: \n\
  194. stmfd sp!, {r0 - r3, lr} \n\
  195. mov r0, ip \n\
  196. bl __up \n\
  197. ldmfd sp!, {r0 - r3, pc}^ \n\
  198. ");
  199. EXPORT_SYMBOL(__down_failed);
  200. EXPORT_SYMBOL(__down_interruptible_failed);
  201. EXPORT_SYMBOL(__down_trylock_failed);
  202. EXPORT_SYMBOL(__up_wakeup);