~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/include/asm-sh/semaphore.h

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #ifndef __ASM_SH_SEMAPHORE_H
  2 #define __ASM_SH_SEMAPHORE_H
  3 
  4 #include <linux/linkage.h>
  5 
  6 /*
  7  * SMP- and interrupt-safe semaphores.
  8  *
  9  * (C) Copyright 1996 Linus Torvalds
 10  *
 11  * SuperH verison by Niibe Yutaka
 12  *  (Currently no asm implementation but generic C code...)
 13  */
 14 
 15 #include <linux/spinlock.h>
 16 
 17 #include <asm/system.h>
 18 #include <asm/atomic.h>
 19 
 20 struct semaphore {
 21         atomic_t count;
 22         int sleepers;
 23         wait_queue_head_t wait;
 24 #if WAITQUEUE_DEBUG
 25         long __magic;
 26 #endif
 27 };
 28 
 29 #if WAITQUEUE_DEBUG
 30 # define __SEM_DEBUG_INIT(name) \
 31                 , (int)&(name).__magic
 32 #else
 33 # define __SEM_DEBUG_INIT(name)
 34 #endif
 35 
 36 #define __SEMAPHORE_INITIALIZER(name,count) \
 37 { ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
 38         __SEM_DEBUG_INIT(name) }
 39 
 40 #define __MUTEX_INITIALIZER(name) \
 41         __SEMAPHORE_INITIALIZER(name,1)
 42 
 43 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
 44         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
 45 
 46 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
 47 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
 48 
 49 extern inline void sema_init (struct semaphore *sem, int val)
 50 {
 51 /*
 52  *      *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
 53  *
 54  * i'd rather use the more flexible initialization above, but sadly
 55  * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
 56  */
 57         atomic_set(&sem->count, val);
 58         sem->sleepers = 0;
 59         init_waitqueue_head(&sem->wait);
 60 #if WAITQUEUE_DEBUG
 61         sem->__magic = (int)&sem->__magic;
 62 #endif
 63 }
 64 
 65 static inline void init_MUTEX (struct semaphore *sem)
 66 {
 67         sema_init(sem, 1);
 68 }
 69 
 70 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
 71 {
 72         sema_init(sem, 0);
 73 }
 74 
 75 #if 0
 76 asmlinkage void __down_failed(void /* special register calling convention */);
 77 asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
 78 asmlinkage int  __down_failed_trylock(void  /* params in registers */);
 79 asmlinkage void __up_wakeup(void /* special register calling convention */);
 80 #endif
 81 
 82 asmlinkage void __down(struct semaphore * sem);
 83 asmlinkage int  __down_interruptible(struct semaphore * sem);
 84 asmlinkage int  __down_trylock(struct semaphore * sem);
 85 asmlinkage void __up(struct semaphore * sem);
 86 extern struct rw_semaphore *__down_read(struct rw_semaphore *sem, int carry);
 87 extern struct rw_semaphore *__down_write(struct rw_semaphore *sem, int carry);
 88 asmlinkage struct rw_semaphore *__rwsem_wake(struct rw_semaphore *sem);
 89 
 90 extern spinlock_t semaphore_wake_lock;
 91 
 92 extern __inline__ void down(struct semaphore * sem)
 93 {
 94 #if WAITQUEUE_DEBUG
 95         CHECK_MAGIC(sem->__magic);
 96 #endif
 97 
 98         if (atomic_dec_return(&sem->count) < 0)
 99                 __down(sem);
100 }
101 
102 extern __inline__ int down_interruptible(struct semaphore * sem)
103 {
104         int ret = 0;
105 #if WAITQUEUE_DEBUG
106         CHECK_MAGIC(sem->__magic);
107 #endif
108 
109         if (atomic_dec_return(&sem->count) < 0)
110                 ret = __down_interruptible(sem);
111         return ret;
112 }
113 
114 extern __inline__ int down_trylock(struct semaphore * sem)
115 {
116         int ret = 0;
117 #if WAITQUEUE_DEBUG
118         CHECK_MAGIC(sem->__magic);
119 #endif
120 
121         if (atomic_dec_return(&sem->count) < 0)
122                 ret = __down_trylock(sem);
123         return ret;
124 }
125 
126 /*
127  * Note! This is subtle. We jump to wake people up only if
128  * the semaphore was negative (== somebody was waiting on it).
129  */
130 extern __inline__ void up(struct semaphore * sem)
131 {
132 #if WAITQUEUE_DEBUG
133         CHECK_MAGIC(sem->__magic);
134 #endif
135         if (atomic_inc_return(&sem->count) <= 0)
136                 __up(sem);
137 }
138 
139 /* rw mutexes (should that be mutices? =) -- throw rw
140  * spinlocks and semaphores together, and this is what we
141  * end up with...
142  *
143  * SuperH version by Niibe Yutaka
144  */
145 struct rw_semaphore {
146         atomic_t                count;
147         volatile unsigned char  write_bias_granted;
148         volatile unsigned char  read_bias_granted;
149         volatile unsigned char  pad1;
150         volatile unsigned char  pad2;
151         wait_queue_head_t       wait;
152         wait_queue_head_t       write_bias_wait;
153 #if WAITQUEUE_DEBUG
154         long                    __magic;
155         atomic_t                readers;
156         atomic_t                writers;
157 #endif
158 };
159 
160 #define RW_LOCK_BIAS             0x01000000
161 
162 #if WAITQUEUE_DEBUG
163 #define __RWSEM_DEBUG_INIT      , ATOMIC_INIT(0), ATOMIC_INIT(0)
164 #else
165 #define __RWSEM_DEBUG_INIT      /* */
166 #endif
167 
168 #define __RWSEM_INITIALIZER(name,count) \
169 { ATOMIC_INIT(count), 0, 0, 0, 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \
170         __WAIT_QUEUE_HEAD_INITIALIZER((name).write_bias_wait) \
171         __SEM_DEBUG_INIT(name) __RWSEM_DEBUG_INIT }
172 
173 #define __DECLARE_RWSEM_GENERIC(name,count) \
174         struct rw_semaphore name = __RWSEM_INITIALIZER(name,count)
175 
176 #define DECLARE_RWSEM(name) __DECLARE_RWSEM_GENERIC(name,RW_LOCK_BIAS)
177 #define DECLARE_RWSEM_READ_LOCKED(name) __DECLARE_RWSEM_GENERIC(name,RW_LOCK_BIAS-1)
178 #define DECLARE_RWSEM_WRITE_LOCKED(name) __DECLARE_RWSEM_GENERIC(name,0)
179 
180 extern inline void init_rwsem(struct rw_semaphore *sem)
181 {
182         atomic_set(&sem->count, RW_LOCK_BIAS);
183         sem->read_bias_granted = 0;
184         sem->write_bias_granted = 0;
185         init_waitqueue_head(&sem->wait);
186         init_waitqueue_head(&sem->write_bias_wait);
187 #if WAITQUEUE_DEBUG
188         sem->__magic = (long)&sem->__magic;
189         atomic_set(&sem->readers, 0);
190         atomic_set(&sem->writers, 0);
191 #endif
192 }
193 
194 extern inline void down_read(struct rw_semaphore *sem)
195 {
196         int saved = atomic_read(&sem->count), new;
197 #if WAITQUEUE_DEBUG
198         if (sem->__magic != (long)&sem->__magic)
199                 BUG();
200 #endif
201         if ((new = atomic_dec_return(&sem->count)) < 0)
202                 __down_read(sem, (new < 0 && saved >=0));
203 #if WAITQUEUE_DEBUG
204         if (sem->write_bias_granted)
205                 BUG();
206         if (atomic_read(&sem->writers))
207                 BUG();
208         atomic_inc(&sem->readers);
209 #endif
210 }
211 
212 extern inline void down_write(struct rw_semaphore *sem)
213 {
214         int saved = atomic_read(&sem->count), new;
215 #if WAITQUEUE_DEBUG
216         if (sem->__magic != (long)&sem->__magic)
217                 BUG();
218 #endif
219         if ((new = atomic_sub_return(RW_LOCK_BIAS, &sem->count)) != 0)
220                 __down_write(sem, (new < 0 && saved >=0));
221 #if WAITQUEUE_DEBUG
222         if (atomic_read(&sem->writers))
223                 BUG();
224         if (atomic_read(&sem->readers))
225                 BUG();
226         if (sem->read_bias_granted)
227                 BUG();
228         if (sem->write_bias_granted)
229                 BUG();
230         atomic_inc(&sem->writers);
231 #endif
232 }
233 
234 /* When a reader does a release, the only significant
235  * case is when there was a writer waiting, and we've
236  * bumped the count to 0: we must wake the writer up.
237  */
238 extern inline void __up_read(struct rw_semaphore *sem)
239 {
240         if (atomic_inc_return(&sem->count) == 0)
241                 __rwsem_wake(sem);
242 }
243 
244 /* releasing the writer is easy -- just release it and
245  * wake up any sleepers.
246  */
247 extern inline void __up_write(struct rw_semaphore *sem)
248 {
249         int saved = atomic_read(&sem->count), new;
250 
251         new = atomic_add_return(RW_LOCK_BIAS, &sem->count);
252         if (saved < 0 && new >= 0)
253                 __rwsem_wake(sem);
254 }
255 
256 extern inline void up_read(struct rw_semaphore *sem)
257 {
258 #if WAITQUEUE_DEBUG
259         if (sem->write_bias_granted)
260                 BUG();
261         if (atomic_read(&sem->writers))
262                 BUG();
263         atomic_dec(&sem->readers);
264 #endif
265         __up_read(sem);
266 }
267 
268 extern inline void up_write(struct rw_semaphore *sem)
269 {
270 #if WAITQUEUE_DEBUG
271         if (sem->read_bias_granted)
272                 BUG();
273         if (sem->write_bias_granted)
274                 BUG();
275         if (atomic_read(&sem->readers))
276                 BUG();
277         if (atomic_read(&sem->writers) != 1)
278                 BUG();
279         atomic_dec(&sem->writers);
280 #endif
281         __up_write(sem);
282 }
283 
284 #endif /* __ASM_SH_SEMAPHORE_H */
285 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.