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/sched.h>
11 #include <linux/wait.h>
12 #include <asm/semaphore.h>
13 #include <asm/semaphore-helper.h>
15 spinlock_t semaphore_wake_lock;
18 * Semaphores are implemented using a two-way counter:
19 * The "count" variable is decremented for each process
20 * that tries to sleep, while the "waking" variable is
21 * incremented when the "up()" code goes to wake up waiting
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.
29 * waking_non_zero() (from asm/semaphore.h) must execute
32 * When __up() is called, the count was negative before
33 * incrementing it, and we need to wake up somebody.
35 * This routine adds one to the count of processes that need to
36 * wake up and exit. ALL waiting processes actually wake up but
37 * only the one that gets to the "waking" field first will gate
38 * through and acquire the semaphore. The others will go back
41 * Note that these functions are only called when there is
42 * contention on the lock, and as such all this is the
43 * "non-critical" part of the whole semaphore business. The
44 * critical part is the inline stuff in <asm/semaphore.h>
45 * where we want to avoid any extra jumps and calls.
47 void __up(struct semaphore *sem)
54 * Perform the "down" function. Return zero for semaphore acquired,
55 * return negative for signalled out of the function.
57 * If called from __down, the return is ignored and the wait loop is
58 * not interruptible. This means that a task waiting on a semaphore
59 * using "down()" cannot be killed until someone does an "up()" on
62 * If called from __down_interruptible, the return value gets checked
63 * upon return. If the return value is negative then the task continues
64 * with the negative value in the return register (it can be tested by
67 * Either form may be used in conjunction with "up()".
72 struct task_struct *tsk = current; \
74 init_waitqueue_entry(&wait, tsk);
76 #define DOWN_HEAD(task_state) \
79 tsk->state = (task_state); \
80 add_wait_queue(&sem->wait, &wait); \
83 * Ok, we're set up. sem->count is known to be less than zero \
86 * We can let go the lock for purposes of waiting. \
87 * We re-acquire it after awaking so as to protect \
88 * all semaphore operations. \
90 * If "up()" is called before we call waking_non_zero() then \
91 * we will catch it right away. If it is called later then \
92 * we will have to go through a wakeup cycle to catch it. \
94 * Multiple waiters contend for the semaphore lock to see \
95 * who gets to gate through and who has to wait some more. \
99 #define DOWN_TAIL(task_state) \
100 tsk->state = (task_state); \
102 tsk->state = TASK_RUNNING; \
103 remove_wait_queue(&sem->wait, &wait);
105 void __down(struct semaphore * sem)
108 DOWN_HEAD(TASK_UNINTERRUPTIBLE)
109 if (waking_non_zero(sem))
112 DOWN_TAIL(TASK_UNINTERRUPTIBLE)
115 int __down_interruptible(struct semaphore * sem)
119 DOWN_HEAD(TASK_INTERRUPTIBLE)
121 ret = waking_non_zero_interruptible(sem, tsk);
125 /* ret != 0 only if we get interrupted -arca */
130 DOWN_TAIL(TASK_INTERRUPTIBLE)
134 int __down_trylock(struct semaphore * sem)
136 return waking_non_zero_trylock(sem);