semaphore.c 5.7 KB

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