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 int printk(const char *fmt, ...);
24 int count; /* not atomic_t since we do the atomicity here already */
26 wait_queue_head_t wait;
33 # define __SEM_DEBUG_INIT(name) , (long)&(name).__magic
35 # define __SEM_DEBUG_INIT(name)
38 #define __SEMAPHORE_INITIALIZER(name,count) \
39 { count, ATOMIC_INIT(0), \
40 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
41 __SEM_DEBUG_INIT(name) }
43 #define __MUTEX_INITIALIZER(name) \
44 __SEMAPHORE_INITIALIZER(name,1)
46 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
47 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
49 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
50 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
52 extern inline void sema_init(struct semaphore *sem, int val)
54 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
57 extern inline void init_MUTEX (struct semaphore *sem)
62 extern inline void init_MUTEX_LOCKED (struct semaphore *sem)
67 extern void __down(struct semaphore * sem);
68 extern int __down_interruptible(struct semaphore * sem);
69 extern int __down_trylock(struct semaphore * sem);
70 extern void __up(struct semaphore * sem);
72 /* notice - we probably can do cli/sti here instead of saving */
74 extern inline void down(struct semaphore * sem)
80 CHECK_MAGIC(sem->__magic);
84 /* atomically decrement the semaphores count, and if its negative, we wait */
85 local_save_flags(flags);
87 failed = --(sem->count) < 0;
88 local_irq_restore(flags);
95 * This version waits in interruptible state so that the waiting
96 * process can be killed. The down_interruptible routine
97 * returns negative for signalled and zero for semaphore acquired.
100 extern inline int down_interruptible(struct semaphore * sem)
106 CHECK_MAGIC(sem->__magic);
110 /* atomically decrement the semaphores count, and if its negative, we wait */
111 local_save_flags(flags);
113 failed = --(sem->count) < 0;
114 local_irq_restore(flags);
116 failed = __down_interruptible(sem);
120 extern inline int down_trylock(struct semaphore * sem)
126 CHECK_MAGIC(sem->__magic);
129 local_save_flags(flags);
131 failed = --(sem->count) < 0;
132 local_irq_restore(flags);
134 failed = __down_trylock(sem);
139 * Note! This is subtle. We jump to wake people up only if
140 * the semaphore was negative (== somebody was waiting on it).
141 * The default case (no contention) will result in NO
142 * jumps for both down() and up().
144 extern inline void up(struct semaphore * sem)
150 CHECK_MAGIC(sem->__magic);
153 /* atomically increment the semaphores count, and if it was negative, we wake people */
154 local_save_flags(flags);
156 wakeup = ++(sem->count) <= 0;
157 local_irq_restore(flags);