semaphore.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Just taken from alpha implementation.
  3. * This can't work well, perhaps.
  4. */
  5. /*
  6. * Generic semaphore code. Buyer beware. Do your own
  7. * specific changes in <asm/semaphore-helper.h>
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/sched.h>
  11. #include <linux/wait.h>
  12. #include <linux/init.h>
  13. #include <asm/semaphore.h>
  14. #include <asm/semaphore-helper.h>
  15. spinlock_t semaphore_wake_lock;
  16. /*
  17. * Semaphores are implemented using a two-way counter:
  18. * The "count" variable is decremented for each process
  19. * that tries to sleep, while the "waking" variable is
  20. * incremented when the "up()" code goes to wake up waiting
  21. * processes.
  22. *
  23. * Notably, the inline "up()" and "down()" functions can
  24. * efficiently test if they need to do any extra work (up
  25. * needs to do something only if count was negative before
  26. * the increment operation.
  27. *
  28. * waking_non_zero() (from asm/semaphore.h) must execute
  29. * atomically.
  30. *
  31. * When __up() is called, the count was negative before
  32. * incrementing it, and we need to wake up somebody.
  33. *
  34. * This routine adds one to the count of processes that need to
  35. * wake up and exit. ALL waiting processes actually wake up but
  36. * only the one that gets to the "waking" field first will gate
  37. * through and acquire the semaphore. The others will go back
  38. * to sleep.
  39. *
  40. * Note that these functions are only called when there is
  41. * contention on the lock, and as such all this is the
  42. * "non-critical" part of the whole semaphore business. The
  43. * critical part is the inline stuff in <asm/semaphore.h>
  44. * where we want to avoid any extra jumps and calls.
  45. */
  46. void __up(struct semaphore *sem)
  47. {
  48. wake_one_more(sem);
  49. wake_up(&sem->wait);
  50. }
  51. /*
  52. * Perform the "down" function. Return zero for semaphore acquired,
  53. * return negative for signalled out of the function.
  54. *
  55. * If called from __down, the return is ignored and the wait loop is
  56. * not interruptible. This means that a task waiting on a semaphore
  57. * using "down()" cannot be killed until someone does an "up()" on
  58. * the semaphore.
  59. *
  60. * If called from __down_interruptible, the return value gets checked
  61. * upon return. If the return value is negative then the task continues
  62. * with the negative value in the return register (it can be tested by
  63. * the caller).
  64. *
  65. * Either form may be used in conjunction with "up()".
  66. *
  67. */
  68. #define DOWN_VAR \
  69. struct task_struct *tsk = current; \
  70. wait_queue_t wait; \
  71. init_waitqueue_entry(&wait, tsk);
  72. #define DOWN_HEAD(task_state) \
  73. \
  74. \
  75. tsk->state = (task_state); \
  76. add_wait_queue(&sem->wait, &wait); \
  77. \
  78. /* \
  79. * Ok, we're set up. sem->count is known to be less than zero \
  80. * so we must wait. \
  81. * \
  82. * We can let go the lock for purposes of waiting. \
  83. * We re-acquire it after awaking so as to protect \
  84. * all semaphore operations. \
  85. * \
  86. * If "up()" is called before we call waking_non_zero() then \
  87. * we will catch it right away. If it is called later then \
  88. * we will have to go through a wakeup cycle to catch it. \
  89. * \
  90. * Multiple waiters contend for the semaphore lock to see \
  91. * who gets to gate through and who has to wait some more. \
  92. */ \
  93. for (;;) {
  94. #define DOWN_TAIL(task_state) \
  95. tsk->state = (task_state); \
  96. } \
  97. tsk->state = TASK_RUNNING; \
  98. remove_wait_queue(&sem->wait, &wait);
  99. void __sched __down(struct semaphore * sem)
  100. {
  101. DOWN_VAR
  102. DOWN_HEAD(TASK_UNINTERRUPTIBLE)
  103. if (waking_non_zero(sem))
  104. break;
  105. schedule();
  106. DOWN_TAIL(TASK_UNINTERRUPTIBLE)
  107. }
  108. int __sched __down_interruptible(struct semaphore * sem)
  109. {
  110. int ret = 0;
  111. DOWN_VAR
  112. DOWN_HEAD(TASK_INTERRUPTIBLE)
  113. ret = waking_non_zero_interruptible(sem, tsk);
  114. if (ret)
  115. {
  116. if (ret == 1)
  117. /* ret != 0 only if we get interrupted -arca */
  118. ret = 0;
  119. break;
  120. }
  121. schedule();
  122. DOWN_TAIL(TASK_INTERRUPTIBLE)
  123. return ret;
  124. }
  125. int __down_trylock(struct semaphore * sem)
  126. {
  127. return waking_non_zero_trylock(sem);
  128. }