semaphore.c 3.9 KB

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