patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-cris / semaphore.h
1 /* $Id: semaphore.h,v 1.3 2001/05/08 13:54:09 bjornw Exp $ */
2
3 /* On the i386 these are coded in asm, perhaps we should as well. Later.. */
4
5 #ifndef _CRIS_SEMAPHORE_H
6 #define _CRIS_SEMAPHORE_H
7
8 #define RW_LOCK_BIAS             0x01000000
9
10 #include <linux/wait.h>
11 #include <linux/spinlock.h>
12 #include <linux/rwsem.h>
13
14 #include <asm/system.h>
15 #include <asm/atomic.h>
16
17 /*
18  * CRIS semaphores, implemented in C-only so far. 
19  */
20
21 int printk(const char *fmt, ...);
22
23 struct semaphore {
24         atomic_t count;
25         atomic_t waking;
26         wait_queue_head_t wait;
27 #if WAITQUEUE_DEBUG
28         long __magic;
29 #endif
30 };
31
32 #if WAITQUEUE_DEBUG
33 # define __SEM_DEBUG_INIT(name)         , (long)&(name).__magic
34 #else
35 # define __SEM_DEBUG_INIT(name)
36 #endif
37
38 #define __SEMAPHORE_INITIALIZER(name,count)             \
39         { ATOMIC_INIT(count), ATOMIC_INIT(0),           \
40           __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)    \
41           __SEM_DEBUG_INIT(name) }
42
43 #define __MUTEX_INITIALIZER(name) \
44         __SEMAPHORE_INITIALIZER(name,1)
45
46 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
47         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
48
49 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
50 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
51
52 extern inline void sema_init(struct semaphore *sem, int val)
53 {
54         *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
55 }
56
57 extern inline void init_MUTEX (struct semaphore *sem)
58 {
59         sema_init(sem, 1);
60 }
61
62 extern inline void init_MUTEX_LOCKED (struct semaphore *sem)
63 {
64         sema_init(sem, 0);
65 }
66
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);
71
72 /* notice - we probably can do cli/sti here instead of saving */
73
74 extern inline void down(struct semaphore * sem)
75 {
76         unsigned long flags;
77         int failed;
78
79 #if WAITQUEUE_DEBUG
80         CHECK_MAGIC(sem->__magic);
81 #endif
82         might_sleep();
83
84         /* atomically decrement the semaphores count, and if its negative, we wait */
85         local_save_flags(flags);
86         local_irq_disable();
87         failed = --(sem->count.counter) < 0;
88         local_irq_restore(flags);
89         if(failed) {
90                 __down(sem);
91         }
92 }
93
94 /*
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.
98  */
99
100 extern inline int down_interruptible(struct semaphore * sem)
101 {
102         unsigned long flags;
103         int failed;
104
105 #if WAITQUEUE_DEBUG
106         CHECK_MAGIC(sem->__magic);
107 #endif
108         might_sleep();
109
110         /* atomically decrement the semaphores count, and if its negative, we wait */
111         local_save_flags(flags);
112         local_irq_disable();
113         failed = --(sem->count.counter) < 0;
114         local_irq_restore(flags);
115         if(failed)
116                 failed = __down_interruptible(sem);
117         return(failed);
118 }
119
120 extern inline int down_trylock(struct semaphore * sem)
121 {
122         unsigned long flags;
123         int failed;
124
125 #if WAITQUEUE_DEBUG
126         CHECK_MAGIC(sem->__magic);
127 #endif
128
129         local_save_flags(flags);
130         local_irq_disable();
131         failed = --(sem->count.counter) < 0;
132         local_irq_restore(flags);
133         if(failed)
134                 failed = __down_trylock(sem);
135         return(failed);
136 }
137
138 /*
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().
143  */
144 extern inline void up(struct semaphore * sem)
145 {  
146         unsigned long flags;
147         int wakeup;
148
149 #if WAITQUEUE_DEBUG
150         CHECK_MAGIC(sem->__magic);
151 #endif
152
153         /* atomically increment the semaphores count, and if it was negative, we wake people */
154         local_save_flags(flags);
155         local_irq_disable();
156         wakeup = ++(sem->count.counter) <= 0;
157         local_irq_restore(flags);
158         if(wakeup) {
159                 __up(sem);
160         }
161 }
162
163 #endif