semaphore.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* $Id: semaphore.c,v 1.7 2001/04/18 21:06:05 davem Exp $ */
  2. /* sparc32 semaphore implementation, based on i386 version */
  3. #include <linux/sched.h>
  4. #include <linux/errno.h>
  5. #include <linux/init.h>
  6. #include <asm/semaphore.h>
  7. /*
  8. * Semaphores are implemented using a two-way counter:
  9. * The "count" variable is decremented for each process
  10. * that tries to acquire the semaphore, while the "sleeping"
  11. * variable is a count of such acquires.
  12. *
  13. * Notably, the inline "up()" and "down()" functions can
  14. * efficiently test if they need to do any extra work (up
  15. * needs to do something only if count was negative before
  16. * the increment operation.
  17. *
  18. * "sleeping" and the contention routine ordering is
  19. * protected by the semaphore spinlock.
  20. *
  21. * Note that these functions are only called when there is
  22. * contention on the lock, and as such all this is the
  23. * "non-critical" part of the whole semaphore business. The
  24. * critical part is the inline stuff in <asm/semaphore.h>
  25. * where we want to avoid any extra jumps and calls.
  26. */
  27. /*
  28. * Logic:
  29. * - only on a boundary condition do we need to care. When we go
  30. * from a negative count to a non-negative, we wake people up.
  31. * - when we go from a non-negative count to a negative do we
  32. * (a) synchronize with the "sleeper" count and (b) make sure
  33. * that we're on the wakeup list before we synchronize so that
  34. * we cannot lose wakeup events.
  35. */
  36. void __up(struct semaphore *sem)
  37. {
  38. wake_up(&sem->wait);
  39. }
  40. static DEFINE_SPINLOCK(semaphore_lock);
  41. void __sched __down(struct semaphore * sem)
  42. {
  43. struct task_struct *tsk = current;
  44. DECLARE_WAITQUEUE(wait, tsk);
  45. tsk->state = TASK_UNINTERRUPTIBLE;
  46. add_wait_queue_exclusive(&sem->wait, &wait);
  47. spin_lock_irq(&semaphore_lock);
  48. sem->sleepers++;
  49. for (;;) {
  50. int sleepers = sem->sleepers;
  51. /*
  52. * Add "everybody else" into it. They aren't
  53. * playing, because we own the spinlock.
  54. */
  55. if (!atomic24_add_negative(sleepers - 1, &sem->count)) {
  56. sem->sleepers = 0;
  57. break;
  58. }
  59. sem->sleepers = 1; /* us - see -1 above */
  60. spin_unlock_irq(&semaphore_lock);
  61. schedule();
  62. tsk->state = TASK_UNINTERRUPTIBLE;
  63. spin_lock_irq(&semaphore_lock);
  64. }
  65. spin_unlock_irq(&semaphore_lock);
  66. remove_wait_queue(&sem->wait, &wait);
  67. tsk->state = TASK_RUNNING;
  68. wake_up(&sem->wait);
  69. }
  70. int __sched __down_interruptible(struct semaphore * sem)
  71. {
  72. int retval = 0;
  73. struct task_struct *tsk = current;
  74. DECLARE_WAITQUEUE(wait, tsk);
  75. tsk->state = TASK_INTERRUPTIBLE;
  76. add_wait_queue_exclusive(&sem->wait, &wait);
  77. spin_lock_irq(&semaphore_lock);
  78. sem->sleepers ++;
  79. for (;;) {
  80. int sleepers = sem->sleepers;
  81. /*
  82. * With signals pending, this turns into
  83. * the trylock failure case - we won't be
  84. * sleeping, and we* can't get the lock as
  85. * it has contention. Just correct the count
  86. * and exit.
  87. */
  88. if (signal_pending(current)) {
  89. retval = -EINTR;
  90. sem->sleepers = 0;
  91. atomic24_add(sleepers, &sem->count);
  92. break;
  93. }
  94. /*
  95. * Add "everybody else" into it. They aren't
  96. * playing, because we own the spinlock. The
  97. * "-1" is because we're still hoping to get
  98. * the lock.
  99. */
  100. if (!atomic24_add_negative(sleepers - 1, &sem->count)) {
  101. sem->sleepers = 0;
  102. break;
  103. }
  104. sem->sleepers = 1; /* us - see -1 above */
  105. spin_unlock_irq(&semaphore_lock);
  106. schedule();
  107. tsk->state = TASK_INTERRUPTIBLE;
  108. spin_lock_irq(&semaphore_lock);
  109. }
  110. spin_unlock_irq(&semaphore_lock);
  111. tsk->state = TASK_RUNNING;
  112. remove_wait_queue(&sem->wait, &wait);
  113. wake_up(&sem->wait);
  114. return retval;
  115. }
  116. /*
  117. * Trylock failed - make sure we correct for
  118. * having decremented the count.
  119. */
  120. int __down_trylock(struct semaphore * sem)
  121. {
  122. int sleepers;
  123. unsigned long flags;
  124. spin_lock_irqsave(&semaphore_lock, flags);
  125. sleepers = sem->sleepers + 1;
  126. sem->sleepers = 0;
  127. /*
  128. * Add "everybody else" and us into it. They aren't
  129. * playing, because we own the spinlock.
  130. */
  131. if (!atomic24_add_negative(sleepers, &sem->count))
  132. wake_up(&sem->wait);
  133. spin_unlock_irqrestore(&semaphore_lock, flags);
  134. return 1;
  135. }