semaphore.c 4.8 KB

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