semaphore.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * i386 semaphore implementation.
  3.  *
  4.  * (C) Copyright 1999 Linus Torvalds
  5.  *
  6.  * Portions Copyright 1999 Red Hat, Inc.
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version
  11.  * 2 of the License, or (at your option) any later version.
  12.  *
  13.  * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@redhat.com>
  14.  */
  15. #include <linux/config.h>
  16. #include <linux/sched.h>
  17. #include <asm/semaphore.h>
  18. /*
  19.  * Semaphores are implemented using a two-way counter:
  20.  * The "count" variable is decremented for each process
  21.  * that tries to acquire the semaphore, while the "sleeping"
  22.  * variable is a count of such acquires.
  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.  * "sleeping" and the contention routine ordering is
  30.  * protected by the semaphore spinlock.
  31.  *
  32.  * Note that these functions are only called when there is
  33.  * contention on the lock, and as such all this is the
  34.  * "non-critical" part of the whole semaphore business. The
  35.  * critical part is the inline stuff in <asm/semaphore.h>
  36.  * where we want to avoid any extra jumps and calls.
  37.  */
  38. /*
  39.  * Logic:
  40.  *  - only on a boundary condition do we need to care. When we go
  41.  *    from a negative count to a non-negative, we wake people up.
  42.  *  - when we go from a non-negative count to a negative do we
  43.  *    (a) synchronize with the "sleeper" count and (b) make sure
  44.  *    that we're on the wakeup list before we synchronize so that
  45.  *    we cannot lose wakeup events.
  46.  */
  47. void __up(struct semaphore *sem)
  48. {
  49. wake_up(&sem->wait);
  50. }
  51. static spinlock_t semaphore_lock = SPIN_LOCK_UNLOCKED;
  52. void __down(struct semaphore * sem)
  53. {
  54. struct task_struct *tsk = current;
  55. DECLARE_WAITQUEUE(wait, tsk);
  56. tsk->state = TASK_UNINTERRUPTIBLE;
  57. add_wait_queue_exclusive(&sem->wait, &wait);
  58. spin_lock_irq(&semaphore_lock);
  59. sem->sleepers++;
  60. for (;;) {
  61. int sleepers = sem->sleepers;
  62. /*
  63.  * Add "everybody else" into it. They aren't
  64.  * playing, because we own the spinlock.
  65.  */
  66. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  67. sem->sleepers = 0;
  68. break;
  69. }
  70. sem->sleepers = 1; /* us - see -1 above */
  71. spin_unlock_irq(&semaphore_lock);
  72. schedule();
  73. tsk->state = TASK_UNINTERRUPTIBLE;
  74. spin_lock_irq(&semaphore_lock);
  75. }
  76. spin_unlock_irq(&semaphore_lock);
  77. remove_wait_queue(&sem->wait, &wait);
  78. tsk->state = TASK_RUNNING;
  79. wake_up(&sem->wait);
  80. }
  81. int __down_interruptible(struct semaphore * sem)
  82. {
  83. int retval = 0;
  84. struct task_struct *tsk = current;
  85. DECLARE_WAITQUEUE(wait, tsk);
  86. tsk->state = TASK_INTERRUPTIBLE;
  87. add_wait_queue_exclusive(&sem->wait, &wait);
  88. spin_lock_irq(&semaphore_lock);
  89. sem->sleepers ++;
  90. for (;;) {
  91. int sleepers = sem->sleepers;
  92. /*
  93.  * With signals pending, this turns into
  94.  * the trylock failure case - we won't be
  95.  * sleeping, and we* can't get the lock as
  96.  * it has contention. Just correct the count
  97.  * and exit.
  98.  */
  99. if (signal_pending(current)) {
  100. retval = -EINTR;
  101. sem->sleepers = 0;
  102. atomic_add(sleepers, &sem->count);
  103. break;
  104. }
  105. /*
  106.  * Add "everybody else" into it. They aren't
  107.  * playing, because we own the spinlock. The
  108.  * "-1" is because we're still hoping to get
  109.  * the lock.
  110.  */
  111. if (!atomic_add_negative(sleepers - 1, &sem->count)) {
  112. sem->sleepers = 0;
  113. break;
  114. }
  115. sem->sleepers = 1; /* us - see -1 above */
  116. spin_unlock_irq(&semaphore_lock);
  117. schedule();
  118. tsk->state = TASK_INTERRUPTIBLE;
  119. spin_lock_irq(&semaphore_lock);
  120. }
  121. spin_unlock_irq(&semaphore_lock);
  122. tsk->state = TASK_RUNNING;
  123. remove_wait_queue(&sem->wait, &wait);
  124. wake_up(&sem->wait);
  125. return retval;
  126. }
  127. /*
  128.  * Trylock failed - make sure we correct for
  129.  * having decremented the count.
  130.  *
  131.  * We could have done the trylock with a
  132.  * single "cmpxchg" without failure cases,
  133.  * but then it wouldn't work on a 386.
  134.  */
  135. int __down_trylock(struct semaphore * sem)
  136. {
  137. int sleepers;
  138. unsigned long flags;
  139. spin_lock_irqsave(&semaphore_lock, flags);
  140. sleepers = sem->sleepers + 1;
  141. sem->sleepers = 0;
  142. /*
  143.  * Add "everybody else" and us into it. They aren't
  144.  * playing, because we own the spinlock.
  145.  */
  146. if (!atomic_add_negative(sleepers, &sem->count))
  147. wake_up(&sem->wait);
  148. spin_unlock_irqrestore(&semaphore_lock, flags);
  149. return 1;
  150. }
  151. /*
  152.  * The semaphore operations have a special calling sequence that
  153.  * allow us to do a simpler in-line version of them. These routines
  154.  * need to convert that sequence back into the C sequence when
  155.  * there is contention on the semaphore.
  156.  *
  157.  * %ecx contains the semaphore pointer on entry. Save the C-clobbered
  158.  * registers (%eax, %edx and %ecx) except %eax when used as a return
  159.  * value..
  160.  */
  161. asm(
  162. ".textn"
  163. ".align 4n"
  164. ".globl __down_failedn"
  165. "__down_failed:nt"
  166. #if defined(CONFIG_FRAME_POINTER)
  167. "pushl %ebpnt"
  168. "movl  %esp,%ebpnt"
  169. #endif
  170. "pushl %eaxnt"
  171. "pushl %edxnt"
  172. "pushl %ecxnt"
  173. "call __downnt"
  174. "popl %ecxnt"
  175. "popl %edxnt"
  176. "popl %eaxnt"
  177. #if defined(CONFIG_FRAME_POINTER)
  178. "movl %ebp,%espnt"
  179. "popl %ebpnt"
  180. #endif
  181. "ret"
  182. );
  183. asm(
  184. ".textn"
  185. ".align 4n"
  186. ".globl __down_failed_interruptiblen"
  187. "__down_failed_interruptible:nt"
  188. #if defined(CONFIG_FRAME_POINTER)
  189. "pushl %ebpnt"
  190. "movl  %esp,%ebpnt"
  191. #endif
  192. "pushl %edxnt"
  193. "pushl %ecxnt"
  194. "call __down_interruptiblent"
  195. "popl %ecxnt"
  196. "popl %edxnt"
  197. #if defined(CONFIG_FRAME_POINTER)
  198. "movl %ebp,%espnt"
  199. "popl %ebpnt"
  200. #endif
  201. "ret"
  202. );
  203. asm(
  204. ".textn"
  205. ".align 4n"
  206. ".globl __down_failed_trylockn"
  207. "__down_failed_trylock:nt"
  208. #if defined(CONFIG_FRAME_POINTER)
  209. "pushl %ebpnt"
  210. "movl  %esp,%ebpnt"
  211. #endif
  212. "pushl %edxnt"
  213. "pushl %ecxnt"
  214. "call __down_trylocknt"
  215. "popl %ecxnt"
  216. "popl %edxnt"
  217. #if defined(CONFIG_FRAME_POINTER)
  218. "movl %ebp,%espnt"
  219. "popl %ebpnt"
  220. #endif
  221. "ret"
  222. );
  223. asm(
  224. ".textn"
  225. ".align 4n"
  226. ".globl __up_wakeupn"
  227. "__up_wakeup:nt"
  228. "pushl %eaxnt"
  229. "pushl %edxnt"
  230. "pushl %ecxnt"
  231. "call __upnt"
  232. "popl %ecxnt"
  233. "popl %edxnt"
  234. "popl %eaxnt"
  235. "ret"
  236. );
  237. /*
  238.  * rw spinlock fallbacks
  239.  */
  240. #if defined(CONFIG_SMP)
  241. asm(
  242. ".textn"
  243. ".align 4n"
  244. ".globl __write_lock_failedn"
  245. "__write_lock_failed:nt"
  246. LOCK "addl $" RW_LOCK_BIAS_STR ",(%eax)n"
  247. "1: rep; nopnt"
  248. "cmpl $" RW_LOCK_BIAS_STR ",(%eax)nt"
  249. "jne 1bnt"
  250. LOCK "subl $" RW_LOCK_BIAS_STR ",(%eax)nt"
  251. "jnz __write_lock_failednt"
  252. "ret"
  253. );
  254. asm(
  255. ".textn"
  256. ".align 4n"
  257. ".globl __read_lock_failedn"
  258. "__read_lock_failed:nt"
  259. LOCK "incl (%eax)n"
  260. "1: rep; nopnt"
  261. "cmpl $1,(%eax)nt"
  262. "js 1bnt"
  263. LOCK "decl (%eax)nt"
  264. "js __read_lock_failednt"
  265. "ret"
  266. );
  267. #endif