semaphore-sleepers.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * i386 and x86-64 semaphore implementation.
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. *
  6. * Portions Copyright 1999 Red Hat, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
  14. */
  15. #include <linux/config.h>
  16. #include <linux/sched.h>
  17. #include <linux/err.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 protected
  32. * by the spinlock in the semaphore's waitqueue head.
  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. fastcall void __up(struct semaphore *sem)
  50. {
  51. wake_up(&sem->wait);
  52. }
  53. fastcall void __sched __down(struct semaphore * sem)
  54. {
  55. struct task_struct *tsk = current;
  56. DECLARE_WAITQUEUE(wait, tsk);
  57. unsigned long flags;
  58. tsk->state = TASK_UNINTERRUPTIBLE;
  59. spin_lock_irqsave(&sem->wait.lock, flags);
  60. add_wait_queue_exclusive_locked(&sem->wait, &wait);
  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 in
  67. * the wait_queue_head.
  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_irqrestore(&sem->wait.lock, flags);
  75. schedule();
  76. spin_lock_irqsave(&sem->wait.lock, flags);
  77. tsk->state = TASK_UNINTERRUPTIBLE;
  78. }
  79. remove_wait_queue_locked(&sem->wait, &wait);
  80. wake_up_locked(&sem->wait);
  81. spin_unlock_irqrestore(&sem->wait.lock, flags);
  82. tsk->state = TASK_RUNNING;
  83. }
  84. fastcall int __sched __down_interruptible(struct semaphore * sem)
  85. {
  86. int retval = 0;
  87. struct task_struct *tsk = current;
  88. DECLARE_WAITQUEUE(wait, tsk);
  89. unsigned long flags;
  90. tsk->state = TASK_INTERRUPTIBLE;
  91. spin_lock_irqsave(&sem->wait.lock, flags);
  92. add_wait_queue_exclusive_locked(&sem->wait, &wait);
  93. sem->sleepers++;
  94. for (;;) {
  95. int sleepers = sem->sleepers;
  96. /*
  97. * With signals pending, this turns into
  98. * the trylock failure case - we won't be
  99. * sleeping, and we* can't get the lock as
  100. * it has contention. Just correct the count
  101. * and exit.
  102. */
  103. if (signal_pending(current)) {
  104. retval = -EINTR;
  105. sem->sleepers = 0;
  106. atomic_add(sleepers, &sem->count);
  107. break;
  108. }
  109. /*
  110. * Add "everybody else" into it. They aren't
  111. * playing, because we own the spinlock in
  112. * wait_queue_head. The "-1" is because we're
  113. * still hoping to get the semaphore.
  114. */
  115. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  116. sem->sleepers = 0;
  117. break;
  118. }
  119. sem->sleepers = 1; /* us - see -1 above */
  120. spin_unlock_irqrestore(&sem->wait.lock, flags);
  121. schedule();
  122. spin_lock_irqsave(&sem->wait.lock, flags);
  123. tsk->state = TASK_INTERRUPTIBLE;
  124. }
  125. remove_wait_queue_locked(&sem->wait, &wait);
  126. wake_up_locked(&sem->wait);
  127. spin_unlock_irqrestore(&sem->wait.lock, flags);
  128. tsk->state = TASK_RUNNING;
  129. return retval;
  130. }
  131. /*
  132. * Trylock failed - make sure we correct for
  133. * having decremented the count.
  134. *
  135. * We could have done the trylock with a
  136. * single "cmpxchg" without failure cases,
  137. * but then it wouldn't work on a 386.
  138. */
  139. fastcall int __down_trylock(struct semaphore * sem)
  140. {
  141. int sleepers;
  142. unsigned long flags;
  143. spin_lock_irqsave(&sem->wait.lock, flags);
  144. sleepers = sem->sleepers + 1;
  145. sem->sleepers = 0;
  146. /*
  147. * Add "everybody else" and us into it. They aren't
  148. * playing, because we own the spinlock in the
  149. * wait_queue_head.
  150. */
  151. if (!atomic_add_negative(sleepers, &sem->count)) {
  152. wake_up_locked(&sem->wait);
  153. }
  154. spin_unlock_irqrestore(&sem->wait.lock, flags);
  155. return 1;
  156. }