semaphore.c 3.7 KB

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