ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / semaphore.c
1 /*
2  * 
3  *
4  * PowerPC-specific semaphore code.
5  *
6  * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  *
13  * April 2001 - Reworked by Paul Mackerras <paulus@samba.org>
14  * to eliminate the SMP races in the old version between the updates
15  * of `count' and `waking'.  Now we use negative `count' values to
16  * indicate that some process(es) are waiting for the semaphore.
17  */
18
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <asm/atomic.h>
22 #include <asm/semaphore.h>
23 #include <asm/errno.h>
24
25 /*
26  * Atomically update sem->count.
27  * This does the equivalent of the following:
28  *
29  *      old_count = sem->count;
30  *      tmp = MAX(old_count, 0) + incr;
31  *      sem->count = tmp;
32  *      return old_count;
33  */
34 static inline int __sem_update_count(struct semaphore *sem, int incr)
35 {
36         int old_count, tmp;
37
38         __asm__ __volatile__("\n"
39 "1:     lwarx   %0,0,%3\n"
40 "       srawi   %1,%0,31\n"
41 "       andc    %1,%0,%1\n"
42 "       add     %1,%1,%4\n"
43 "       stwcx.  %1,0,%3\n"
44 "       bne     1b"
45         : "=&r" (old_count), "=&r" (tmp), "=m" (sem->count)
46         : "r" (&sem->count), "r" (incr), "m" (sem->count)
47         : "cc");
48
49         return old_count;
50 }
51
52 void __up(struct semaphore *sem)
53 {
54         /*
55          * Note that we incremented count in up() before we came here,
56          * but that was ineffective since the result was <= 0, and
57          * any negative value of count is equivalent to 0.
58          * This ends up setting count to 1, unless count is now > 0
59          * (i.e. because some other cpu has called up() in the meantime),
60          * in which case we just increment count.
61          */
62         __sem_update_count(sem, 1);
63         wake_up(&sem->wait);
64 }
65
66 /*
67  * Note that when we come in to __down or __down_interruptible,
68  * we have already decremented count, but that decrement was
69  * ineffective since the result was < 0, and any negative value
70  * of count is equivalent to 0.
71  * Thus it is only when we decrement count from some value > 0
72  * that we have actually got the semaphore.
73  */
74 void __sched __down(struct semaphore *sem)
75 {
76         struct task_struct *tsk = current;
77         DECLARE_WAITQUEUE(wait, tsk);
78
79         __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
80         add_wait_queue_exclusive(&sem->wait, &wait);
81
82         /*
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.
87          */
88         while (__sem_update_count(sem, -1) <= 0) {
89                 schedule();
90                 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
91         }
92         remove_wait_queue(&sem->wait, &wait);
93         __set_task_state(tsk, TASK_RUNNING);
94
95         /*
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.
99          */
100         wake_up(&sem->wait);
101 }
102
103 int __sched __down_interruptible(struct semaphore * sem)
104 {
105         int retval = 0;
106         struct task_struct *tsk = current;
107         DECLARE_WAITQUEUE(wait, tsk);
108
109         __set_task_state(tsk, TASK_INTERRUPTIBLE);
110         add_wait_queue_exclusive(&sem->wait, &wait);
111
112         while (__sem_update_count(sem, -1) <= 0) {
113                 if (signal_pending(current)) {
114                         /*
115                          * A signal is pending - give up trying.
116                          * Set sem->count to 0 if it is negative,
117                          * since we are no longer sleeping.
118                          */
119                         __sem_update_count(sem, 0);
120                         retval = -EINTR;
121                         break;
122                 }
123                 schedule();
124                 set_task_state(tsk, TASK_INTERRUPTIBLE);
125         }
126         remove_wait_queue(&sem->wait, &wait);
127         __set_task_state(tsk, TASK_RUNNING);
128
129         wake_up(&sem->wait);
130         return retval;
131 }