ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-parisc / semaphore.h
1 /*    SMP- and interrupt-safe semaphores.
2  *    PA-RISC version by Matthew Wilcox
3  *
4  *    Linux/PA-RISC Project (http://www.parisc-linux.org/)
5  *    Copyright (C) 1996 Linus Torvalds
6  *    Copyright (C) 1999-2001 Matthew Wilcox < willy at debian d0T org >
7  *    Copyright (C) 2000 Grant Grundler < grundler a debian org >
8  *
9  *    This program is free software; you can redistribute it and/or modify
10  *    it under the terms of the GNU General Public License as published by
11  *    the Free Software Foundation; either version 2 of the License, or
12  *    (at your option) any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  *    You should have received a copy of the GNU General Public License
20  *    along with this program; if not, write to the Free Software
21  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #ifndef _ASM_PARISC_SEMAPHORE_H
25 #define _ASM_PARISC_SEMAPHORE_H
26
27 #include <linux/spinlock.h>
28 #include <linux/wait.h>
29 #include <linux/rwsem.h>
30
31 #include <asm/system.h>
32
33 /*
34  * The `count' is initialised to the number of people who are allowed to
35  * take the lock.  (Normally we want a mutex, so this is `1').  if
36  * `count' is positive, the lock can be taken.  if it's 0, no-one is
37  * waiting on it.  if it's -1, at least one task is waiting.
38  */
39 struct semaphore {
40         spinlock_t      sentry;
41         int             count;
42         wait_queue_head_t wait;
43 #if WAITQUEUE_DEBUG
44         long __magic;
45 #endif
46 };
47
48 #if WAITQUEUE_DEBUG
49 # define __SEM_DEBUG_INIT(name) \
50                 , (long)&(name).__magic
51 #else
52 # define __SEM_DEBUG_INIT(name)
53 #endif
54
55 #define __SEMAPHORE_INITIALIZER(name,count) \
56 { SPIN_LOCK_UNLOCKED, count, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
57         __SEM_DEBUG_INIT(name) }
58
59 #define __MUTEX_INITIALIZER(name) \
60         __SEMAPHORE_INITIALIZER(name,1)
61
62 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
63         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
64
65 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
66 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
67
68 extern inline void sema_init (struct semaphore *sem, int val)
69 {
70         *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
71 }
72
73 static inline void init_MUTEX (struct semaphore *sem)
74 {
75         sema_init(sem, 1);
76 }
77
78 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
79 {
80         sema_init(sem, 0);
81 }
82
83 static inline int sem_getcount(struct semaphore *sem)
84 {
85         return sem->count;
86 }
87
88 asmlinkage void __down(struct semaphore * sem);
89 asmlinkage int  __down_interruptible(struct semaphore * sem);
90 asmlinkage void __up(struct semaphore * sem);
91
92 /* Semaphores can be `tried' from irq context.  So we have to disable
93  * interrupts while we're messing with the semaphore.  Sorry.
94  */
95
96 extern __inline__ void down(struct semaphore * sem)
97 {
98 #if WAITQUEUE_DEBUG
99         CHECK_MAGIC(sem->__magic);
100 #endif
101         might_sleep();
102         spin_lock_irq(&sem->sentry);
103         if (sem->count > 0) {
104                 sem->count--;
105         } else {
106                 __down(sem);
107         }
108         spin_unlock_irq(&sem->sentry);
109 }
110
111 extern __inline__ int down_interruptible(struct semaphore * sem)
112 {
113         int ret = 0;
114 #if WAITQUEUE_DEBUG
115         CHECK_MAGIC(sem->__magic);
116 #endif
117         might_sleep();
118         spin_lock_irq(&sem->sentry);
119         if (sem->count > 0) {
120                 sem->count--;
121         } else {
122                 ret = __down_interruptible(sem);
123         }
124         spin_unlock_irq(&sem->sentry);
125         return ret;
126 }
127
128 /*
129  * down_trylock returns 0 on success, 1 if we failed to get the lock.
130  * May not sleep, but must preserve irq state
131  */
132 extern __inline__ int down_trylock(struct semaphore * sem)
133 {
134         int flags, count;
135 #if WAITQUEUE_DEBUG
136         CHECK_MAGIC(sem->__magic);
137 #endif
138
139         spin_lock_irqsave(&sem->sentry, flags);
140         count = sem->count - 1;
141         if (count >= 0)
142                 sem->count = count;
143         spin_unlock_irqrestore(&sem->sentry, flags);
144         return (count < 0);
145 }
146
147 /*
148  * Note! This is subtle. We jump to wake people up only if
149  * the semaphore was negative (== somebody was waiting on it).
150  */
151 extern __inline__ void up(struct semaphore * sem)
152 {
153         int flags;
154 #if WAITQUEUE_DEBUG
155         CHECK_MAGIC(sem->__magic);
156 #endif
157         spin_lock_irqsave(&sem->sentry, flags);
158         if (sem->count < 0) {
159                 __up(sem);
160         } else {
161                 sem->count++;
162         }
163         spin_unlock_irqrestore(&sem->sentry, flags);
164 }
165
166 #endif /* _ASM_PARISC_SEMAPHORE_H */