Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / include / asm-cris / semaphore.h
blob31a4ac448195e640931b58eb4cc8089079b9605f
1 /* $Id: semaphore.h,v 1.3 2001/05/08 13:54:09 bjornw Exp $ */
3 /* On the i386 these are coded in asm, perhaps we should as well. Later.. */
5 #ifndef _CRIS_SEMAPHORE_H
6 #define _CRIS_SEMAPHORE_H
8 #define RW_LOCK_BIAS 0x01000000
10 #include <linux/wait.h>
11 #include <linux/spinlock.h>
12 #include <linux/rwsem.h>
14 #include <asm/system.h>
15 #include <asm/atomic.h>
18 * CRIS semaphores, implemented in C-only so far.
21 struct semaphore {
22 atomic_t count;
23 atomic_t waking;
24 wait_queue_head_t wait;
27 #define __SEMAPHORE_INITIALIZER(name, n) \
28 { \
29 .count = ATOMIC_INIT(n), \
30 .waking = ATOMIC_INIT(0), \
31 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
34 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
35 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
37 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
39 static inline void sema_init(struct semaphore *sem, int val)
41 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
44 static inline void init_MUTEX (struct semaphore *sem)
46 sema_init(sem, 1);
49 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
51 sema_init(sem, 0);
54 extern void __down(struct semaphore * sem);
55 extern int __down_interruptible(struct semaphore * sem);
56 extern int __down_trylock(struct semaphore * sem);
57 extern void __up(struct semaphore * sem);
59 /* notice - we probably can do cli/sti here instead of saving */
61 static inline void down(struct semaphore * sem)
63 unsigned long flags;
64 int failed;
66 might_sleep();
68 /* atomically decrement the semaphores count, and if its negative, we wait */
69 cris_atomic_save(sem, flags);
70 failed = --(sem->count.counter) < 0;
71 cris_atomic_restore(sem, flags);
72 if(failed) {
73 __down(sem);
78 * This version waits in interruptible state so that the waiting
79 * process can be killed. The down_interruptible routine
80 * returns negative for signalled and zero for semaphore acquired.
83 static inline int down_interruptible(struct semaphore * sem)
85 unsigned long flags;
86 int failed;
88 might_sleep();
90 /* atomically decrement the semaphores count, and if its negative, we wait */
91 cris_atomic_save(sem, flags);
92 failed = --(sem->count.counter) < 0;
93 cris_atomic_restore(sem, flags);
94 if(failed)
95 failed = __down_interruptible(sem);
96 return(failed);
99 static inline int down_trylock(struct semaphore * sem)
101 unsigned long flags;
102 int failed;
104 cris_atomic_save(sem, flags);
105 failed = --(sem->count.counter) < 0;
106 cris_atomic_restore(sem, flags);
107 if(failed)
108 failed = __down_trylock(sem);
109 return(failed);
114 * Note! This is subtle. We jump to wake people up only if
115 * the semaphore was negative (== somebody was waiting on it).
116 * The default case (no contention) will result in NO
117 * jumps for both down() and up().
119 static inline void up(struct semaphore * sem)
121 unsigned long flags;
122 int wakeup;
124 /* atomically increment the semaphores count, and if it was negative, we wake people */
125 cris_atomic_save(sem, flags);
126 wakeup = ++(sem->count.counter) <= 0;
127 cris_atomic_restore(sem, flags);
128 if(wakeup) {
129 __up(sem);
133 #endif