1 /* $Id: semaphore.c,v 1.7 2001/04/18 21:06:05 davem Exp $ */
3 /* sparc32 semaphore implementation, based on i386 version */
5 #include <linux/sched.h>
6 #include <linux/errno.h>
7 #include <linux/init.h>
9 #include <asm/semaphore.h>
12 * Semaphores are implemented using a two-way counter:
13 * The "count" variable is decremented for each process
14 * that tries to acquire the semaphore, while the "sleeping"
15 * variable is a count of such acquires.
17 * Notably, the inline "up()" and "down()" functions can
18 * efficiently test if they need to do any extra work (up
19 * needs to do something only if count was negative before
20 * the increment operation.
22 * "sleeping" and the contention routine ordering is
23 * protected by the semaphore spinlock.
25 * Note that these functions are only called when there is
26 * contention on the lock, and as such all this is the
27 * "non-critical" part of the whole semaphore business. The
28 * critical part is the inline stuff in <asm/semaphore.h>
29 * where we want to avoid any extra jumps and calls.
34 * - only on a boundary condition do we need to care. When we go
35 * from a negative count to a non-negative, we wake people up.
36 * - when we go from a non-negative count to a negative do we
37 * (a) synchronize with the "sleeper" count and (b) make sure
38 * that we're on the wakeup list before we synchronize so that
39 * we cannot lose wakeup events.
42 void __up(struct semaphore
*sem
)
47 static DEFINE_SPINLOCK(semaphore_lock
);
49 void __sched
__down(struct semaphore
* sem
)
51 struct task_struct
*tsk
= current
;
52 DECLARE_WAITQUEUE(wait
, tsk
);
53 tsk
->state
= TASK_UNINTERRUPTIBLE
;
54 add_wait_queue_exclusive(&sem
->wait
, &wait
);
56 spin_lock_irq(&semaphore_lock
);
59 int sleepers
= sem
->sleepers
;
62 * Add "everybody else" into it. They aren't
63 * playing, because we own the spinlock.
65 if (!atomic24_add_negative(sleepers
- 1, &sem
->count
)) {
69 sem
->sleepers
= 1; /* us - see -1 above */
70 spin_unlock_irq(&semaphore_lock
);
73 tsk
->state
= TASK_UNINTERRUPTIBLE
;
74 spin_lock_irq(&semaphore_lock
);
76 spin_unlock_irq(&semaphore_lock
);
77 remove_wait_queue(&sem
->wait
, &wait
);
78 tsk
->state
= TASK_RUNNING
;
82 int __sched
__down_interruptible(struct semaphore
* sem
)
85 struct task_struct
*tsk
= current
;
86 DECLARE_WAITQUEUE(wait
, tsk
);
87 tsk
->state
= TASK_INTERRUPTIBLE
;
88 add_wait_queue_exclusive(&sem
->wait
, &wait
);
90 spin_lock_irq(&semaphore_lock
);
93 int sleepers
= sem
->sleepers
;
96 * With signals pending, this turns into
97 * the trylock failure case - we won't be
98 * sleeping, and we* can't get the lock as
99 * it has contention. Just correct the count
102 if (signal_pending(current
)) {
105 atomic24_add(sleepers
, &sem
->count
);
110 * Add "everybody else" into it. They aren't
111 * playing, because we own the spinlock. The
112 * "-1" is because we're still hoping to get
115 if (!atomic24_add_negative(sleepers
- 1, &sem
->count
)) {
119 sem
->sleepers
= 1; /* us - see -1 above */
120 spin_unlock_irq(&semaphore_lock
);
123 tsk
->state
= TASK_INTERRUPTIBLE
;
124 spin_lock_irq(&semaphore_lock
);
126 spin_unlock_irq(&semaphore_lock
);
127 tsk
->state
= TASK_RUNNING
;
128 remove_wait_queue(&sem
->wait
, &wait
);
134 * Trylock failed - make sure we correct for
135 * having decremented the count.
137 int __down_trylock(struct semaphore
* sem
)
142 spin_lock_irqsave(&semaphore_lock
, flags
);
143 sleepers
= sem
->sleepers
+ 1;
147 * Add "everybody else" and us into it. They aren't
148 * playing, because we own the spinlock.
150 if (!atomic24_add_negative(sleepers
, &sem
->count
))
153 spin_unlock_irqrestore(&semaphore_lock
, flags
);