semaphore.c 5.7 KB

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