semaphore.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * arch/v850/kernel/semaphore.c -- Semaphore support
  3. *
  4. * Copyright (C) 1998-2000 IBM Corporation
  5. * Copyright (C) 1999 Linus Torvalds
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * This file is a copy of the s390 version, arch/s390/kernel/semaphore.c
  12. * Author(s): Martin Schwidefsky
  13. * which was derived from the i386 version, linux/arch/i386/kernel/semaphore.c
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/sched.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. int __down_trylock(struct semaphore * sem)
  133. {
  134. unsigned long flags;
  135. int sleepers;
  136. spin_lock_irqsave(&semaphore_lock, flags);
  137. sleepers = sem->sleepers + 1;
  138. sem->sleepers = 0;
  139. /*
  140. * Add "everybody else" and us into it. They aren't
  141. * playing, because we own the spinlock.
  142. */
  143. if (!atomic_add_negative(sleepers, &sem->count))
  144. wake_up(&sem->wait);
  145. spin_unlock_irqrestore(&semaphore_lock, flags);
  146. return 1;
  147. }