2 * Semaphore implementation Copyright (c) 2001 Matthew Wilcox, Hewlett-Packard
5 #include <linux/sched.h>
6 #include <linux/spinlock.h>
7 #include <linux/errno.h>
8 #include <linux/init.h>
11 * Semaphores are complex as we wish to avoid using two variables.
12 * `count' has multiple roles, depending on its value. If it is positive
13 * or zero, there are no waiters. The functions here will never be
14 * called; see <asm/semaphore.h>
16 * When count is -1 it indicates there is at least one task waiting
19 * When count is less than that, there are '- count - 1' wakeups
20 * pending. ie if it has value -3, there are 2 wakeups pending.
22 * Note that these functions are only called when there is contention
23 * on the lock, and as such all this is the "non-critical" part of the
24 * whole semaphore business. The critical part is the inline stuff in
25 * <asm/semaphore.h> where we want to avoid any extra jumps and calls.
27 void __up(struct semaphore
*sem
)
33 #define wakers(count) (-1 - count)
37 DECLARE_WAITQUEUE(wait, current); \
39 /* Note that someone is waiting */ \
40 if (sem->count == 0) \
43 /* protected by the sentry still -- use unlocked version */ \
44 wait.flags = WQ_FLAG_EXCLUSIVE; \
45 __add_wait_queue_tail(&sem->wait, &wait); \
47 spin_unlock_irq(&sem->sentry); \
50 spin_lock_irq(&sem->sentry); \
51 if (wakers(sem->count) == 0 && ret == 0) \
52 goto lost_race; /* Someone stole our wakeup */ \
53 __remove_wait_queue(&sem->wait, &wait); \
54 current->state = TASK_RUNNING; \
55 if (!waitqueue_active(&sem->wait) && (sem->count < 0)) \
56 sem->count = wakers(sem->count);
58 #define UPDATE_COUNT \
59 sem->count += (sem->count < 0) ? 1 : - 1;
62 void __sched
__down(struct semaphore
* sem
)
67 set_task_state(current
, TASK_UNINTERRUPTIBLE
);
68 /* we can _read_ this without the sentry */
78 int __sched
__down_interruptible(struct semaphore
* sem
)
83 set_task_state(current
, TASK_INTERRUPTIBLE
);
84 /* we can _read_ this without the sentry */
88 if (signal_pending(current
)) {