2 * Generic semaphore code. Buyer beware. Do your own
3 * specific changes in <asm/semaphore-helper.h>
6 #include <linux/sched.h>
8 #include <linux/init.h>
9 #include <asm/semaphore-helper.h>
11 #ifndef CONFIG_RMW_INSNS
12 spinlock_t semaphore_wake_lock
;
16 * Semaphores are implemented using a two-way counter:
17 * The "count" variable is decremented for each process
18 * that tries to sleep, while the "waking" variable is
19 * incremented when the "up()" code goes to wake up waiting
22 * Notably, the inline "up()" and "down()" functions can
23 * efficiently test if they need to do any extra work (up
24 * needs to do something only if count was negative before
25 * the increment operation.
27 * waking_non_zero() (from asm/semaphore.h) must execute
30 * When __up() is called, the count was negative before
31 * incrementing it, and we need to wake up somebody.
33 * This routine adds one to the count of processes that need to
34 * wake up and exit. ALL waiting processes actually wake up but
35 * only the one that gets to the "waking" field first will gate
36 * through and acquire the semaphore. The others will go back
39 * Note that these functions are only called when there is
40 * contention on the lock, and as such all this is the
41 * "non-critical" part of the whole semaphore business. The
42 * critical part is the inline stuff in <asm/semaphore.h>
43 * where we want to avoid any extra jumps and calls.
45 void __up(struct semaphore
*sem
)
52 * Perform the "down" function. Return zero for semaphore acquired,
53 * return negative for signalled out of the function.
55 * If called from __down, the return is ignored and the wait loop is
56 * not interruptible. This means that a task waiting on a semaphore
57 * using "down()" cannot be killed until someone does an "up()" on
60 * If called from __down_interruptible, the return value gets checked
61 * upon return. If the return value is negative then the task continues
62 * with the negative value in the return register (it can be tested by
65 * Either form may be used in conjunction with "up()".
70 #define DOWN_HEAD(task_state) \
73 current->state = (task_state); \
74 add_wait_queue(&sem->wait, &wait); \
77 * Ok, we're set up. sem->count is known to be less than zero \
80 * We can let go the lock for purposes of waiting. \
81 * We re-acquire it after awaking so as to protect \
82 * all semaphore operations. \
84 * If "up()" is called before we call waking_non_zero() then \
85 * we will catch it right away. If it is called later then \
86 * we will have to go through a wakeup cycle to catch it. \
88 * Multiple waiters contend for the semaphore lock to see \
89 * who gets to gate through and who has to wait some more. \
93 #define DOWN_TAIL(task_state) \
94 current->state = (task_state); \
96 current->state = TASK_RUNNING; \
97 remove_wait_queue(&sem->wait, &wait);
99 void __sched
__down(struct semaphore
* sem
)
101 DECLARE_WAITQUEUE(wait
, current
);
103 DOWN_HEAD(TASK_UNINTERRUPTIBLE
)
104 if (waking_non_zero(sem
))
107 DOWN_TAIL(TASK_UNINTERRUPTIBLE
)
110 int __sched
__down_interruptible(struct semaphore
* sem
)
112 DECLARE_WAITQUEUE(wait
, current
);
115 DOWN_HEAD(TASK_INTERRUPTIBLE
)
117 ret
= waking_non_zero_interruptible(sem
, current
);
121 /* ret != 0 only if we get interrupted -arca */
126 DOWN_TAIL(TASK_INTERRUPTIBLE
)
130 int __down_trylock(struct semaphore
* sem
)
132 return waking_non_zero_trylock(sem
);