semaphore.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2008 Intel Corporation
  3. * Author: Matthew Wilcox <willy@linux.intel.com>
  4. *
  5. * Distributed under the terms of the GNU GPL, version 2
  6. */
  7. #include <linux/compiler.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/semaphore.h>
  12. #include <linux/spinlock.h>
  13. /*
  14. * Some notes on the implementation:
  15. *
  16. * down_trylock() and up() can be called from interrupt context.
  17. * So we have to disable interrupts when taking the lock.
  18. *
  19. * The ->count variable defines how many more tasks can acquire the
  20. * semaphore. If it's zero, there may be tasks waiting on the list.
  21. */
  22. static noinline void __down(struct semaphore *sem);
  23. static noinline int __down_interruptible(struct semaphore *sem);
  24. static noinline int __down_killable(struct semaphore *sem);
  25. static noinline int __down_timeout(struct semaphore *sem, long jiffies);
  26. static noinline void __up(struct semaphore *sem);
  27. void down(struct semaphore *sem)
  28. {
  29. unsigned long flags;
  30. spin_lock_irqsave(&sem->lock, flags);
  31. if (likely(sem->count > 0))
  32. sem->count--;
  33. else
  34. __down(sem);
  35. spin_unlock_irqrestore(&sem->lock, flags);
  36. }
  37. EXPORT_SYMBOL(down);
  38. int down_interruptible(struct semaphore *sem)
  39. {
  40. unsigned long flags;
  41. int result = 0;
  42. spin_lock_irqsave(&sem->lock, flags);
  43. if (likely(sem->count > 0))
  44. sem->count--;
  45. else
  46. result = __down_interruptible(sem);
  47. spin_unlock_irqrestore(&sem->lock, flags);
  48. return result;
  49. }
  50. EXPORT_SYMBOL(down_interruptible);
  51. int down_killable(struct semaphore *sem)
  52. {
  53. unsigned long flags;
  54. int result = 0;
  55. spin_lock_irqsave(&sem->lock, flags);
  56. if (likely(sem->count > 0))
  57. sem->count--;
  58. else
  59. result = __down_killable(sem);
  60. spin_unlock_irqrestore(&sem->lock, flags);
  61. return result;
  62. }
  63. EXPORT_SYMBOL(down_killable);
  64. /**
  65. * down_trylock - try to acquire the semaphore, without waiting
  66. * @sem: the semaphore to be acquired
  67. *
  68. * Try to acquire the semaphore atomically. Returns 0 if the mutex has
  69. * been acquired successfully and 1 if it is contended.
  70. *
  71. * NOTE: This return value is inverted from both spin_trylock and
  72. * mutex_trylock! Be careful about this when converting code.
  73. *
  74. * Unlike mutex_trylock, this function can be used from interrupt context,
  75. * and the semaphore can be released by any task or interrupt.
  76. */
  77. int down_trylock(struct semaphore *sem)
  78. {
  79. unsigned long flags;
  80. int count;
  81. spin_lock_irqsave(&sem->lock, flags);
  82. count = sem->count - 1;
  83. if (likely(count >= 0))
  84. sem->count = count;
  85. spin_unlock_irqrestore(&sem->lock, flags);
  86. return (count < 0);
  87. }
  88. EXPORT_SYMBOL(down_trylock);
  89. int down_timeout(struct semaphore *sem, long jiffies)
  90. {
  91. unsigned long flags;
  92. int result = 0;
  93. spin_lock_irqsave(&sem->lock, flags);
  94. if (likely(sem->count > 0))
  95. sem->count--;
  96. else
  97. result = __down_timeout(sem, jiffies);
  98. spin_unlock_irqrestore(&sem->lock, flags);
  99. return result;
  100. }
  101. EXPORT_SYMBOL(down_timeout);
  102. void up(struct semaphore *sem)
  103. {
  104. unsigned long flags;
  105. spin_lock_irqsave(&sem->lock, flags);
  106. if (likely(list_empty(&sem->wait_list)))
  107. sem->count++;
  108. else
  109. __up(sem);
  110. spin_unlock_irqrestore(&sem->lock, flags);
  111. }
  112. EXPORT_SYMBOL(up);
  113. /* Functions for the contended case */
  114. struct semaphore_waiter {
  115. struct list_head list;
  116. struct task_struct *task;
  117. int up;
  118. };
  119. /*
  120. * Because this function is inlined, the 'state' parameter will be
  121. * constant, and thus optimised away by the compiler. Likewise the
  122. * 'timeout' parameter for the cases without timeouts.
  123. */
  124. static inline int __sched __down_common(struct semaphore *sem, long state,
  125. long timeout)
  126. {
  127. struct task_struct *task = current;
  128. struct semaphore_waiter waiter;
  129. list_add_tail(&waiter.list, &sem->wait_list);
  130. waiter.task = task;
  131. waiter.up = 0;
  132. for (;;) {
  133. if (state == TASK_INTERRUPTIBLE && signal_pending(task))
  134. goto interrupted;
  135. if (state == TASK_KILLABLE && fatal_signal_pending(task))
  136. goto interrupted;
  137. if (timeout <= 0)
  138. goto timed_out;
  139. __set_task_state(task, state);
  140. spin_unlock_irq(&sem->lock);
  141. timeout = schedule_timeout(timeout);
  142. spin_lock_irq(&sem->lock);
  143. if (waiter.up)
  144. return 0;
  145. }
  146. timed_out:
  147. list_del(&waiter.list);
  148. return -ETIME;
  149. interrupted:
  150. list_del(&waiter.list);
  151. return -EINTR;
  152. }
  153. static noinline void __sched __down(struct semaphore *sem)
  154. {
  155. __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  156. }
  157. static noinline int __sched __down_interruptible(struct semaphore *sem)
  158. {
  159. return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  160. }
  161. static noinline int __sched __down_killable(struct semaphore *sem)
  162. {
  163. return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
  164. }
  165. static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies)
  166. {
  167. return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies);
  168. }
  169. static noinline void __sched __up(struct semaphore *sem)
  170. {
  171. struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list,
  172. struct semaphore_waiter, list);
  173. list_del(&waiter->list);
  174. waiter->up = 1;
  175. wake_up_process(waiter->task);
  176. }