[PATCH] fix semaphore handling in __unregister_chrdev_region
[linux/fpc-iii.git] / include / asm-cris / semaphore.h
blob605aa7eaaaf8d4190c767ba7920c2ddab8a9d886
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, ...);
23 struct semaphore {
24 atomic_t count;
25 atomic_t waking;
26 wait_queue_head_t wait;
29 #define __SEMAPHORE_INITIALIZER(name, n) \
30 { \
31 .count = ATOMIC_INIT(n), \
32 .waking = ATOMIC_INIT(0), \
33 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
36 #define __MUTEX_INITIALIZER(name) \
37 __SEMAPHORE_INITIALIZER(name,1)
39 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
40 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
42 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
43 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
45 extern inline void sema_init(struct semaphore *sem, int val)
47 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
50 extern inline void init_MUTEX (struct semaphore *sem)
52 sema_init(sem, 1);
55 extern inline void init_MUTEX_LOCKED (struct semaphore *sem)
57 sema_init(sem, 0);
60 extern void __down(struct semaphore * sem);
61 extern int __down_interruptible(struct semaphore * sem);
62 extern int __down_trylock(struct semaphore * sem);
63 extern void __up(struct semaphore * sem);
65 /* notice - we probably can do cli/sti here instead of saving */
67 extern inline void down(struct semaphore * sem)
69 unsigned long flags;
70 int failed;
72 might_sleep();
74 /* atomically decrement the semaphores count, and if its negative, we wait */
75 local_save_flags(flags);
76 local_irq_disable();
77 failed = --(sem->count.counter) < 0;
78 local_irq_restore(flags);
79 if(failed) {
80 __down(sem);
85 * This version waits in interruptible state so that the waiting
86 * process can be killed. The down_interruptible routine
87 * returns negative for signalled and zero for semaphore acquired.
90 extern inline int down_interruptible(struct semaphore * sem)
92 unsigned long flags;
93 int failed;
95 might_sleep();
97 /* atomically decrement the semaphores count, and if its negative, we wait */
98 local_save_flags(flags);
99 local_irq_disable();
100 failed = --(sem->count.counter) < 0;
101 local_irq_restore(flags);
102 if(failed)
103 failed = __down_interruptible(sem);
104 return(failed);
107 extern inline int down_trylock(struct semaphore * sem)
109 unsigned long flags;
110 int failed;
112 local_save_flags(flags);
113 local_irq_disable();
114 failed = --(sem->count.counter) < 0;
115 local_irq_restore(flags);
116 if(failed)
117 failed = __down_trylock(sem);
118 return(failed);
122 * Note! This is subtle. We jump to wake people up only if
123 * the semaphore was negative (== somebody was waiting on it).
124 * The default case (no contention) will result in NO
125 * jumps for both down() and up().
127 extern inline void up(struct semaphore * sem)
129 unsigned long flags;
130 int wakeup;
132 /* atomically increment the semaphores count, and if it was negative, we wake people */
133 local_save_flags(flags);
134 local_irq_disable();
135 wakeup = ++(sem->count.counter) <= 0;
136 local_irq_restore(flags);
137 if(wakeup) {
138 __up(sem);
142 #endif