special usb hub handling, IDE disks, and retries all over the place
[linux-2.4.git] / include / asm-ppc / semaphore.h
1 #ifndef _PPC_SEMAPHORE_H
2 #define _PPC_SEMAPHORE_H
3
4 /*
5  * Swiped from asm-sparc/semaphore.h and modified
6  * -- Cort (cort@cs.nmt.edu)
7  *
8  * Stole some rw spinlock-based semaphore stuff from asm-alpha/semaphore.h
9  * -- Ani Joshi (ajoshi@unixbox.com)
10  *
11  * Remove spinlock-based RW semaphores; RW semaphore definitions are
12  * now in rwsem.h and we use the generic lib/rwsem.c implementation.
13  * Rework semaphores to use atomic_dec_if_positive.
14  * -- Paul Mackerras (paulus@samba.org)
15  */
16
17 #ifdef __KERNEL__
18
19 #include <asm/atomic.h>
20 #include <asm/system.h>
21 #include <linux/wait.h>
22 #include <linux/rwsem.h>
23
24 struct semaphore {
25         /*
26          * Note that any negative value of count is equivalent to 0,
27          * but additionally indicates that some process(es) might be
28          * sleeping on `wait'.
29          */
30         atomic_t count;
31         wait_queue_head_t wait;
32 #if WAITQUEUE_DEBUG
33         long __magic;
34 #endif
35 };
36
37 #if WAITQUEUE_DEBUG
38 # define __SEM_DEBUG_INIT(name) \
39                 , (long)&(name).__magic
40 #else
41 # define __SEM_DEBUG_INIT(name)
42 #endif
43
44 #define __SEMAPHORE_INITIALIZER(name, count) \
45         { ATOMIC_INIT(count), \
46           __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
47           __SEM_DEBUG_INIT(name) }
48
49 #define __MUTEX_INITIALIZER(name) \
50         __SEMAPHORE_INITIALIZER(name, 1)
51
52 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
53         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
54
55 #define DECLARE_MUTEX(name)             __DECLARE_SEMAPHORE_GENERIC(name, 1)
56 #define DECLARE_MUTEX_LOCKED(name)      __DECLARE_SEMAPHORE_GENERIC(name, 0)
57
58 static inline void sema_init (struct semaphore *sem, int val)
59 {
60         atomic_set(&sem->count, val);
61         init_waitqueue_head(&sem->wait);
62 #if WAITQUEUE_DEBUG
63         sem->__magic = (long)&sem->__magic;
64 #endif
65 }
66
67 static inline void init_MUTEX (struct semaphore *sem)
68 {
69         sema_init(sem, 1);
70 }
71
72 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
73 {
74         sema_init(sem, 0);
75 }
76
77 extern void __down(struct semaphore * sem);
78 extern int  __down_interruptible(struct semaphore * sem);
79 extern void __up(struct semaphore * sem);
80
81 extern inline void down(struct semaphore * sem)
82 {
83 #if WAITQUEUE_DEBUG
84         CHECK_MAGIC(sem->__magic);
85 #endif
86
87         /*
88          * Try to get the semaphore, take the slow path if we fail.
89          */
90         if (atomic_dec_return(&sem->count) < 0)
91                 __down(sem);
92         smp_wmb();
93 }
94
95 extern inline int down_interruptible(struct semaphore * sem)
96 {
97         int ret = 0;
98
99 #if WAITQUEUE_DEBUG
100         CHECK_MAGIC(sem->__magic);
101 #endif
102
103         if (atomic_dec_return(&sem->count) < 0)
104                 ret = __down_interruptible(sem);
105         smp_wmb();
106         return ret;
107 }
108
109 extern inline int down_trylock(struct semaphore * sem)
110 {
111         int ret;
112
113 #if WAITQUEUE_DEBUG
114         CHECK_MAGIC(sem->__magic);
115 #endif
116
117         ret = atomic_dec_if_positive(&sem->count) < 0;
118         smp_wmb();
119         return ret;
120 }
121
122 extern inline void up(struct semaphore * sem)
123 {
124 #if WAITQUEUE_DEBUG
125         CHECK_MAGIC(sem->__magic);
126 #endif
127
128         smp_wmb();
129         if (atomic_inc_return(&sem->count) <= 0)
130                 __up(sem);
131 }
132
133 static inline int sem_getcount(struct semaphore *sem)
134 {
135         return atomic_read(&sem->count);
136 }
137
138 #endif /* __KERNEL__ */
139
140 #endif /* !(_PPC_SEMAPHORE_H) */