2 * Just taken from alpha implementation.
3 * This can't work well, perhaps.
6 * Generic semaphore code. Buyer beware. Do your own
7 * specific changes in <asm/semaphore-helper.h>
10 #include <linux/errno.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>
17 spinlock_t semaphore_wake_lock;
20 * Semaphores are implemented using a two-way counter:
21 * The "count" variable is decremented for each process
22 * that tries to sleep, while the "waking" variable is
23 * incremented when the "up()" code goes to wake up waiting
26 * Notably, the inline "up()" and "down()" functions can
27 * efficiently test if they need to do any extra work (up
28 * needs to do something only if count was negative before
29 * the increment operation.
31 * waking_non_zero() (from asm/semaphore.h) must execute
34 * When __up() is called, the count was negative before
35 * incrementing it, and we need to wake up somebody.
37 * This routine adds one to the count of processes that need to
38 * wake up and exit. ALL waiting processes actually wake up but
39 * only the one that gets to the "waking" field first will gate
40 * through and acquire the semaphore. The others will go back
43 * Note that these functions are only called when there is
44 * contention on the lock, and as such all this is the
45 * "non-critical" part of the whole semaphore business. The
46 * critical part is the inline stuff in <asm/semaphore.h>
47 * where we want to avoid any extra jumps and calls.
49 void __up(struct semaphore *sem)
56 * Perform the "down" function. Return zero for semaphore acquired,
57 * return negative for signalled out of the function.
59 * If called from __down, the return is ignored and the wait loop is
60 * not interruptible. This means that a task waiting on a semaphore
61 * using "down()" cannot be killed until someone does an "up()" on
64 * If called from __down_interruptible, the return value gets checked
65 * upon return. If the return value is negative then the task continues
66 * with the negative value in the return register (it can be tested by
69 * Either form may be used in conjunction with "up()".
74 struct task_struct *tsk = current; \
76 init_waitqueue_entry(&wait, tsk);
78 #define DOWN_HEAD(task_state) \
81 tsk->state = (task_state); \
82 add_wait_queue(&sem->wait, &wait); \
85 * Ok, we're set up. sem->count is known to be less than zero \
88 * We can let go the lock for purposes of waiting. \
89 * We re-acquire it after awaking so as to protect \
90 * all semaphore operations. \
92 * If "up()" is called before we call waking_non_zero() then \
93 * we will catch it right away. If it is called later then \
94 * we will have to go through a wakeup cycle to catch it. \
96 * Multiple waiters contend for the semaphore lock to see \
97 * who gets to gate through and who has to wait some more. \
101 #define DOWN_TAIL(task_state) \
102 tsk->state = (task_state); \
104 tsk->state = TASK_RUNNING; \
105 remove_wait_queue(&sem->wait, &wait);
107 void __sched __down(struct semaphore * sem)
110 DOWN_HEAD(TASK_UNINTERRUPTIBLE)
111 if (waking_non_zero(sem))
114 DOWN_TAIL(TASK_UNINTERRUPTIBLE)
117 int __sched __down_interruptible(struct semaphore * sem)
121 DOWN_HEAD(TASK_INTERRUPTIBLE)
123 ret = waking_non_zero_interruptible(sem, tsk);
127 /* ret != 0 only if we get interrupted -arca */
132 DOWN_TAIL(TASK_INTERRUPTIBLE)
136 int __down_trylock(struct semaphore * sem)
138 return waking_non_zero_trylock(sem);