VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / include / asm-mips / semaphore.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1996  Linus Torvalds
7  * Copyright (C) 1998, 99, 2000, 01, 04  Ralf Baechle
8  * Copyright (C) 1999, 2000, 01  Silicon Graphics, Inc.
9  * Copyright (C) 2000, 01 MIPS Technologies, Inc.
10  *
11  * In all honesty, little of the old MIPS code left - the PPC64 variant was
12  * just looking nice and portable so I ripped it.  Credits to whoever wrote
13  * it.
14  */
15 #ifndef __ASM_SEMAPHORE_H
16 #define __ASM_SEMAPHORE_H
17
18 /*
19  * Remove spinlock-based RW semaphores; RW semaphore definitions are
20  * now in rwsem.h and we use the generic lib/rwsem.c implementation.
21  * Rework semaphores to use atomic_dec_if_positive.
22  * -- Paul Mackerras (paulus@samba.org)
23  */
24
25 #ifdef __KERNEL__
26
27 #include <asm/atomic.h>
28 #include <asm/system.h>
29 #include <linux/wait.h>
30 #include <linux/rwsem.h>
31
32 struct semaphore {
33         /*
34          * Note that any negative value of count is equivalent to 0,
35          * but additionally indicates that some process(es) might be
36          * sleeping on `wait'.
37          */
38         atomic_t count;
39         wait_queue_head_t wait;
40 #ifdef WAITQUEUE_DEBUG
41         long __magic;
42 #endif
43 };
44
45 #ifdef WAITQUEUE_DEBUG
46 # define __SEM_DEBUG_INIT(name) \
47                 , (long)&(name).__magic
48 #else
49 # define __SEM_DEBUG_INIT(name)
50 #endif
51
52 #define __SEMAPHORE_INITIALIZER(name, count) \
53         { ATOMIC_INIT(count), \
54           __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
55           __SEM_DEBUG_INIT(name) }
56
57 #define __MUTEX_INITIALIZER(name) \
58         __SEMAPHORE_INITIALIZER(name, 1)
59
60 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
61         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
62
63 #define DECLARE_MUTEX(name)             __DECLARE_SEMAPHORE_GENERIC(name, 1)
64 #define DECLARE_MUTEX_LOCKED(name)      __DECLARE_SEMAPHORE_GENERIC(name, 0)
65
66 static inline void sema_init (struct semaphore *sem, int val)
67 {
68         atomic_set(&sem->count, val);
69         init_waitqueue_head(&sem->wait);
70 #ifdef WAITQUEUE_DEBUG
71         sem->__magic = (long)&sem->__magic;
72 #endif
73 }
74
75 static inline void init_MUTEX (struct semaphore *sem)
76 {
77         sema_init(sem, 1);
78 }
79
80 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
81 {
82         sema_init(sem, 0);
83 }
84
85 extern void __down(struct semaphore * sem);
86 extern int  __down_interruptible(struct semaphore * sem);
87 extern void __up(struct semaphore * sem);
88
89 static inline void down(struct semaphore * sem)
90 {
91 #ifdef WAITQUEUE_DEBUG
92         CHECK_MAGIC(sem->__magic);
93 #endif
94         might_sleep();
95
96         /*
97          * Try to get the semaphore, take the slow path if we fail.
98          */
99         if (unlikely(atomic_dec_return(&sem->count) < 0))
100                 __down(sem);
101 }
102
103 static inline int down_interruptible(struct semaphore * sem)
104 {
105         int ret = 0;
106
107 #ifdef WAITQUEUE_DEBUG
108         CHECK_MAGIC(sem->__magic);
109 #endif
110         might_sleep();
111
112         if (unlikely(atomic_dec_return(&sem->count) < 0))
113                 ret = __down_interruptible(sem);
114         return ret;
115 }
116
117 static inline int down_trylock(struct semaphore * sem)
118 {
119 #ifdef WAITQUEUE_DEBUG
120         CHECK_MAGIC(sem->__magic);
121 #endif
122
123         return atomic_dec_if_positive(&sem->count) < 0;
124 }
125
126 static inline void up(struct semaphore * sem)
127 {
128 #ifdef WAITQUEUE_DEBUG
129         CHECK_MAGIC(sem->__magic);
130 #endif
131
132         if (unlikely(atomic_inc_return(&sem->count) <= 0))
133                 __up(sem);
134 }
135
136 #endif /* __KERNEL__ */
137
138 #endif /* __ASM_SEMAPHORE_H */