semaphore.c 3.7 KB

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