semaphore.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * linux/arch/m32r/semaphore.c
  3. * orig : i386 2.6.4
  4. *
  5. * M32R semaphore implementation.
  6. *
  7. * Copyright (c) 2002 - 2004 Hitoshi Yamamoto
  8. */
  9. /*
  10. * i386 semaphore implementation.
  11. *
  12. * (C) Copyright 1999 Linus Torvalds
  13. *
  14. * Portions Copyright 1999 Red Hat, Inc.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
  22. */
  23. #include <linux/config.h>
  24. #include <linux/sched.h>
  25. #include <linux/err.h>
  26. #include <linux/init.h>
  27. #include <asm/semaphore.h>
  28. /*
  29. * Semaphores are implemented using a two-way counter:
  30. * The "count" variable is decremented for each process
  31. * that tries to acquire the semaphore, while the "sleeping"
  32. * variable is a count of such acquires.
  33. *
  34. * Notably, the inline "up()" and "down()" functions can
  35. * efficiently test if they need to do any extra work (up
  36. * needs to do something only if count was negative before
  37. * the increment operation.
  38. *
  39. * "sleeping" and the contention routine ordering is protected
  40. * by the spinlock in the semaphore's waitqueue head.
  41. *
  42. * Note that these functions are only called when there is
  43. * contention on the lock, and as such all this is the
  44. * "non-critical" part of the whole semaphore business. The
  45. * critical part is the inline stuff in <asm/semaphore.h>
  46. * where we want to avoid any extra jumps and calls.
  47. */
  48. /*
  49. * Logic:
  50. * - only on a boundary condition do we need to care. When we go
  51. * from a negative count to a non-negative, we wake people up.
  52. * - when we go from a non-negative count to a negative do we
  53. * (a) synchronize with the "sleeper" count and (b) make sure
  54. * that we're on the wakeup list before we synchronize so that
  55. * we cannot lose wakeup events.
  56. */
  57. asmlinkage void __up(struct semaphore *sem)
  58. {
  59. wake_up(&sem->wait);
  60. }
  61. asmlinkage void __sched __down(struct semaphore * sem)
  62. {
  63. struct task_struct *tsk = current;
  64. DECLARE_WAITQUEUE(wait, tsk);
  65. unsigned long flags;
  66. tsk->state = TASK_UNINTERRUPTIBLE;
  67. spin_lock_irqsave(&sem->wait.lock, flags);
  68. add_wait_queue_exclusive_locked(&sem->wait, &wait);
  69. sem->sleepers++;
  70. for (;;) {
  71. int sleepers = sem->sleepers;
  72. /*
  73. * Add "everybody else" into it. They aren't
  74. * playing, because we own the spinlock in
  75. * the wait_queue_head.
  76. */
  77. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  78. sem->sleepers = 0;
  79. break;
  80. }
  81. sem->sleepers = 1; /* us - see -1 above */
  82. spin_unlock_irqrestore(&sem->wait.lock, flags);
  83. schedule();
  84. spin_lock_irqsave(&sem->wait.lock, flags);
  85. tsk->state = TASK_UNINTERRUPTIBLE;
  86. }
  87. remove_wait_queue_locked(&sem->wait, &wait);
  88. wake_up_locked(&sem->wait);
  89. spin_unlock_irqrestore(&sem->wait.lock, flags);
  90. tsk->state = TASK_RUNNING;
  91. }
  92. asmlinkage int __sched __down_interruptible(struct semaphore * sem)
  93. {
  94. int retval = 0;
  95. struct task_struct *tsk = current;
  96. DECLARE_WAITQUEUE(wait, tsk);
  97. unsigned long flags;
  98. tsk->state = TASK_INTERRUPTIBLE;
  99. spin_lock_irqsave(&sem->wait.lock, flags);
  100. add_wait_queue_exclusive_locked(&sem->wait, &wait);
  101. sem->sleepers++;
  102. for (;;) {
  103. int sleepers = sem->sleepers;
  104. /*
  105. * With signals pending, this turns into
  106. * the trylock failure case - we won't be
  107. * sleeping, and we* can't get the lock as
  108. * it has contention. Just correct the count
  109. * and exit.
  110. */
  111. if (signal_pending(current)) {
  112. retval = -EINTR;
  113. sem->sleepers = 0;
  114. atomic_add(sleepers, &sem->count);
  115. break;
  116. }
  117. /*
  118. * Add "everybody else" into it. They aren't
  119. * playing, because we own the spinlock in
  120. * wait_queue_head. The "-1" is because we're
  121. * still hoping to get the semaphore.
  122. */
  123. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  124. sem->sleepers = 0;
  125. break;
  126. }
  127. sem->sleepers = 1; /* us - see -1 above */
  128. spin_unlock_irqrestore(&sem->wait.lock, flags);
  129. schedule();
  130. spin_lock_irqsave(&sem->wait.lock, flags);
  131. tsk->state = TASK_INTERRUPTIBLE;
  132. }
  133. remove_wait_queue_locked(&sem->wait, &wait);
  134. wake_up_locked(&sem->wait);
  135. spin_unlock_irqrestore(&sem->wait.lock, flags);
  136. tsk->state = TASK_RUNNING;
  137. return retval;
  138. }
  139. /*
  140. * Trylock failed - make sure we correct for
  141. * having decremented the count.
  142. *
  143. * We could have done the trylock with a
  144. * single "cmpxchg" without failure cases,
  145. * but then it wouldn't work on a 386.
  146. */
  147. asmlinkage int __down_trylock(struct semaphore * sem)
  148. {
  149. int sleepers;
  150. unsigned long flags;
  151. spin_lock_irqsave(&sem->wait.lock, flags);
  152. sleepers = sem->sleepers + 1;
  153. sem->sleepers = 0;
  154. /*
  155. * Add "everybody else" and us into it. They aren't
  156. * playing, because we own the spinlock in the
  157. * wait_queue_head.
  158. */
  159. if (!atomic_add_negative(sleepers, &sem->count)) {
  160. wake_up_locked(&sem->wait);
  161. }
  162. spin_unlock_irqrestore(&sem->wait.lock, flags);
  163. return 1;
  164. }