Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ppc / kernel / semaphore.c
blob2fe429b27c142728c4477bfb8e7f07a7d9b45f62
1 /*
2 * PowerPC-specific semaphore code.
4 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * April 2001 - Reworked by Paul Mackerras <paulus@samba.org>
12 * to eliminate the SMP races in the old version between the updates
13 * of `count' and `waking'. Now we use negative `count' values to
14 * indicate that some process(es) are waiting for the semaphore.
17 #include <linux/sched.h>
18 #include <linux/init.h>
19 #include <asm/atomic.h>
20 #include <asm/semaphore.h>
21 #include <asm/errno.h>
24 * Atomically update sem->count.
25 * This does the equivalent of the following:
27 * old_count = sem->count;
28 * tmp = MAX(old_count, 0) + incr;
29 * sem->count = tmp;
30 * return old_count;
32 static inline int __sem_update_count(struct semaphore *sem, int incr)
34 int old_count, tmp;
36 __asm__ __volatile__("\n"
37 "1: lwarx %0,0,%3\n"
38 " srawi %1,%0,31\n"
39 " andc %1,%0,%1\n"
40 " add %1,%1,%4\n"
41 PPC405_ERR77(0,%3)
42 " stwcx. %1,0,%3\n"
43 " bne 1b"
44 : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
45 : "r" (&sem->count), "r" (incr), "m" (sem->count)
46 : "cc");
48 return old_count;
51 void __up(struct semaphore *sem)
54 * Note that we incremented count in up() before we came here,
55 * but that was ineffective since the result was <= 0, and
56 * any negative value of count is equivalent to 0.
57 * This ends up setting count to 1, unless count is now > 0
58 * (i.e. because some other cpu has called up() in the meantime),
59 * in which case we just increment count.
61 __sem_update_count(sem, 1);
62 wake_up(&sem->wait);
66 * Note that when we come in to __down or __down_interruptible,
67 * we have already decremented count, but that decrement was
68 * ineffective since the result was < 0, and any negative value
69 * of count is equivalent to 0.
70 * Thus it is only when we decrement count from some value > 0
71 * that we have actually got the semaphore.
73 void __sched __down(struct semaphore *sem)
75 struct task_struct *tsk = current;
76 DECLARE_WAITQUEUE(wait, tsk);
78 tsk->state = TASK_UNINTERRUPTIBLE;
79 add_wait_queue_exclusive(&sem->wait, &wait);
80 smp_wmb();
83 * Try to get the semaphore. If the count is > 0, then we've
84 * got the semaphore; we decrement count and exit the loop.
85 * If the count is 0 or negative, we set it to -1, indicating
86 * that we are asleep, and then sleep.
88 while (__sem_update_count(sem, -1) <= 0) {
89 schedule();
90 tsk->state = TASK_UNINTERRUPTIBLE;
92 remove_wait_queue(&sem->wait, &wait);
93 tsk->state = TASK_RUNNING;
96 * If there are any more sleepers, wake one of them up so
97 * that it can either get the semaphore, or set count to -1
98 * indicating that there are still processes sleeping.
100 wake_up(&sem->wait);
103 int __sched __down_interruptible(struct semaphore * sem)
105 int retval = 0;
106 struct task_struct *tsk = current;
107 DECLARE_WAITQUEUE(wait, tsk);
109 tsk->state = TASK_INTERRUPTIBLE;
110 add_wait_queue_exclusive(&sem->wait, &wait);
111 smp_wmb();
113 while (__sem_update_count(sem, -1) <= 0) {
114 if (signal_pending(current)) {
116 * A signal is pending - give up trying.
117 * Set sem->count to 0 if it is negative,
118 * since we are no longer sleeping.
120 __sem_update_count(sem, 0);
121 retval = -EINTR;
122 break;
124 schedule();
125 tsk->state = TASK_INTERRUPTIBLE;
127 tsk->state = TASK_RUNNING;
128 remove_wait_queue(&sem->wait, &wait);
129 wake_up(&sem->wait);
130 return retval;