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/rwsem.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/init.h>
15 #include <asm/semaphore.h>
16 #include <asm/semaphore-helper.h>
18 spinlock_t semaphore_wake_lock;
21 * Semaphores are implemented using a two-way counter:
22 * The "count" variable is decremented for each process
23 * that tries to sleep, while the "waking" variable is
24 * incremented when the "up()" code goes to wake up waiting
27 * Notably, the inline "up()" and "down()" functions can
28 * efficiently test if they need to do any extra work (up
29 * needs to do something only if count was negative before
30 * the increment operation.
32 * waking_non_zero() (from asm/semaphore.h) must execute
35 * When __up() is called, the count was negative before
36 * incrementing it, and we need to wake up somebody.
38 * This routine adds one to the count of processes that need to
39 * wake up and exit. ALL waiting processes actually wake up but
40 * only the one that gets to the "waking" field first will gate
41 * through and acquire the semaphore. The others will go back
44 * Note that these functions are only called when there is
45 * contention on the lock, and as such all this is the
46 * "non-critical" part of the whole semaphore business. The
47 * critical part is the inline stuff in <asm/semaphore.h>
48 * where we want to avoid any extra jumps and calls.
50 void __up(struct semaphore *sem)
57 * Perform the "down" function. Return zero for semaphore acquired,
58 * return negative for signalled out of the function.
60 * If called from __down, the return is ignored and the wait loop is
61 * not interruptible. This means that a task waiting on a semaphore
62 * using "down()" cannot be killed until someone does an "up()" on
65 * If called from __down_interruptible, the return value gets checked
66 * upon return. If the return value is negative then the task continues
67 * with the negative value in the return register (it can be tested by
70 * Either form may be used in conjunction with "up()".
75 struct task_struct *tsk = current; \
77 init_waitqueue_entry(&wait, tsk);
79 #define DOWN_HEAD(task_state) \
82 tsk->state = (task_state); \
83 add_wait_queue(&sem->wait, &wait); \
86 * Ok, we're set up. sem->count is known to be less than zero \
89 * We can let go the lock for purposes of waiting. \
90 * We re-acquire it after awaking so as to protect \
91 * all semaphore operations. \
93 * If "up()" is called before we call waking_non_zero() then \
94 * we will catch it right away. If it is called later then \
95 * we will have to go through a wakeup cycle to catch it. \
97 * Multiple waiters contend for the semaphore lock to see \
98 * who gets to gate through and who has to wait some more. \
102 #define DOWN_TAIL(task_state) \
103 tsk->state = (task_state); \
105 tsk->state = TASK_RUNNING; \
106 remove_wait_queue(&sem->wait, &wait);
108 void __sched __down(struct semaphore * sem)
111 DOWN_HEAD(TASK_UNINTERRUPTIBLE)
112 if (waking_non_zero(sem))
115 DOWN_TAIL(TASK_UNINTERRUPTIBLE)
118 int __sched __down_interruptible(struct semaphore * sem)
122 DOWN_HEAD(TASK_INTERRUPTIBLE)
124 ret = waking_non_zero_interruptible(sem, tsk);
128 /* ret != 0 only if we get interrupted -arca */
133 DOWN_TAIL(TASK_INTERRUPTIBLE)
137 int __down_trylock(struct semaphore * sem)
139 return waking_non_zero_trylock(sem);