semaphore.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Semaphore implementation Copyright (c) 2001 Matthew Wilcox, Hewlett-Packard
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/errno.h>
  7. #include <linux/init.h>
  8. /*
  9. * Semaphores are complex as we wish to avoid using two variables.
  10. * `count' has multiple roles, depending on its value. If it is positive
  11. * or zero, there are no waiters. The functions here will never be
  12. * called; see <asm/semaphore.h>
  13. *
  14. * When count is -1 it indicates there is at least one task waiting
  15. * for the semaphore.
  16. *
  17. * When count is less than that, there are '- count - 1' wakeups
  18. * pending. ie if it has value -3, there are 2 wakeups pending.
  19. *
  20. * Note that these functions are only called when there is contention
  21. * on the lock, and as such all this is the "non-critical" part of the
  22. * whole semaphore business. The critical part is the inline stuff in
  23. * <asm/semaphore.h> where we want to avoid any extra jumps and calls.
  24. */
  25. void __up(struct semaphore *sem)
  26. {
  27. sem->count--;
  28. wake_up(&sem->wait);
  29. }
  30. #define wakers(count) (-1 - count)
  31. #define DOWN_HEAD \
  32. int ret = 0; \
  33. DECLARE_WAITQUEUE(wait, current); \
  34. \
  35. /* Note that someone is waiting */ \
  36. if (sem->count == 0) \
  37. sem->count = -1; \
  38. \
  39. /* protected by the sentry still -- use unlocked version */ \
  40. wait.flags = WQ_FLAG_EXCLUSIVE; \
  41. __add_wait_queue_tail(&sem->wait, &wait); \
  42. lost_race: \
  43. spin_unlock_irq(&sem->sentry); \
  44. #define DOWN_TAIL \
  45. spin_lock_irq(&sem->sentry); \
  46. if (wakers(sem->count) == 0 && ret == 0) \
  47. goto lost_race; /* Someone stole our wakeup */ \
  48. __remove_wait_queue(&sem->wait, &wait); \
  49. current->state = TASK_RUNNING; \
  50. if (!waitqueue_active(&sem->wait) && (sem->count < 0)) \
  51. sem->count = wakers(sem->count);
  52. #define UPDATE_COUNT \
  53. sem->count += (sem->count < 0) ? 1 : - 1;
  54. void __sched __down(struct semaphore * sem)
  55. {
  56. DOWN_HEAD
  57. for(;;) {
  58. set_task_state(current, TASK_UNINTERRUPTIBLE);
  59. /* we can _read_ this without the sentry */
  60. if (sem->count != -1)
  61. break;
  62. schedule();
  63. }
  64. DOWN_TAIL
  65. UPDATE_COUNT
  66. }
  67. int __sched __down_interruptible(struct semaphore * sem)
  68. {
  69. DOWN_HEAD
  70. for(;;) {
  71. set_task_state(current, TASK_INTERRUPTIBLE);
  72. /* we can _read_ this without the sentry */
  73. if (sem->count != -1)
  74. break;
  75. if (signal_pending(current)) {
  76. ret = -EINTR;
  77. break;
  78. }
  79. schedule();
  80. }
  81. DOWN_TAIL
  82. if (!ret) {
  83. UPDATE_COUNT
  84. }
  85. return ret;
  86. }