1 /* On the i386 these are coded in asm, perhaps we should as well. Later.. */
3 #ifndef _CRIS_SEMAPHORE_H
4 #define _CRIS_SEMAPHORE_H
6 #define RW_LOCK_BIAS 0x01000000
8 #include <linux/wait.h>
9 #include <linux/spinlock.h>
10 #include <linux/rwsem.h>
12 #include <asm/system.h>
13 #include <asm/atomic.h>
16 * CRIS semaphores, implemented in C-only so far.
19 int printk(const char *fmt, ...);
22 int count; /* not atomic_t since we do the atomicity here already */
24 wait_queue_head_t wait;
31 # define __SEM_DEBUG_INIT(name) , (long)&(name).__magic
33 # define __SEM_DEBUG_INIT(name)
36 #define __SEMAPHORE_INITIALIZER(name,count) \
37 { count, ATOMIC_INIT(0), \
38 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
39 __SEM_DEBUG_INIT(name) }
41 #define __MUTEX_INITIALIZER(name) \
42 __SEMAPHORE_INITIALIZER(name,1)
44 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
45 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
47 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
48 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
50 extern inline void sema_init(struct semaphore *sem, int val)
52 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
55 extern inline void init_MUTEX (struct semaphore *sem)
60 extern inline void init_MUTEX_LOCKED (struct semaphore *sem)
65 extern void __down(struct semaphore * sem);
66 extern int __down_interruptible(struct semaphore * sem);
67 extern int __down_trylock(struct semaphore * sem);
68 extern void __up(struct semaphore * sem);
70 /* notice - we probably can do cli/sti here instead of saving */
72 extern inline void down(struct semaphore * sem)
78 CHECK_MAGIC(sem->__magic);
81 /* atomically decrement the semaphores count, and if its negative, we wait */
84 failed = --(sem->count) < 0;
92 * This version waits in interruptible state so that the waiting
93 * process can be killed. The down_interruptible routine
94 * returns negative for signalled and zero for semaphore acquired.
97 extern inline int down_interruptible(struct semaphore * sem)
103 CHECK_MAGIC(sem->__magic);
106 /* atomically decrement the semaphores count, and if its negative, we wait */
109 failed = --(sem->count) < 0;
110 restore_flags(flags);
112 failed = __down_interruptible(sem);
116 extern inline int down_trylock(struct semaphore * sem)
122 CHECK_MAGIC(sem->__magic);
127 failed = --(sem->count) < 0;
128 restore_flags(flags);
130 failed = __down_trylock(sem);
135 * Note! This is subtle. We jump to wake people up only if
136 * the semaphore was negative (== somebody was waiting on it).
137 * The default case (no contention) will result in NO
138 * jumps for both down() and up().
140 extern inline void up(struct semaphore * sem)
146 CHECK_MAGIC(sem->__magic);
149 /* atomically increment the semaphores count, and if it was negative, we wake people */
152 wakeup = ++(sem->count) <= 0;
153 restore_flags(flags);
159 static inline int sem_getcount(struct semaphore *sem)