* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / include / asm-ppc / semaphore.h
blobb73bd59613ec6be8706dae71d6ac4e96a78c92a7
1 #ifndef _PPC_SEMAPHORE_H
2 #define _PPC_SEMAPHORE_H
4 /*
5 * Swiped from asm-sparc/semaphore.h and modified
6 * -- Cort (cort@cs.nmt.edu)
7 */
9 #ifdef __KERNEL__
11 #include <asm/atomic.h>
12 #include <linux/wait.h>
14 struct semaphore {
15 atomic_t count;
16 atomic_t waking;
17 wait_queue_head_t wait;
18 #if WAITQUEUE_DEBUG
19 long __magic;
20 #endif
23 #if WAITQUEUE_DEBUG
24 # define __SEM_DEBUG_INIT(name) \
25 , (long)&(name).__magic
26 #else
27 # define __SEM_DEBUG_INIT(name)
28 #endif
30 #define __SEMAPHORE_INITIALIZER(name,count) \
31 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
32 __SEM_DEBUG_INIT(name) }
34 #define __MUTEX_INITIALIZER(name) \
35 __SEMAPHORE_INITIALIZER(name,1)
37 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
38 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
40 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
41 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
43 extern inline void sema_init (struct semaphore *sem, int val)
45 atomic_set(&sem->count, val);
46 atomic_set(&sem->waking, 0);
47 init_waitqueue_head(&sem->wait);
48 #if WAITQUEUE_DEBUG
49 sem->__magic = (long)&sem->__magic;
50 #endif
53 static inline void init_MUTEX (struct semaphore *sem)
55 sema_init(sem, 1);
58 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
60 sema_init(sem, 0);
63 extern void __down(struct semaphore * sem);
64 extern int __down_interruptible(struct semaphore * sem);
65 extern int __down_trylock(struct semaphore * sem);
66 extern void __up(struct semaphore * sem);
68 extern inline void down(struct semaphore * sem)
70 if (atomic_dec_return(&sem->count) >= 0)
71 wmb();
72 else
73 __down(sem);
76 extern inline int down_interruptible(struct semaphore * sem)
78 int ret = 0;
80 if (atomic_dec_return(&sem->count) >= 0)
81 wmb();
82 else
83 ret = __down_interruptible(sem);
84 return ret;
87 extern inline int down_trylock(struct semaphore * sem)
89 int ret = 0;
91 if (atomic_dec_return(&sem->count) >= 0)
92 wmb();
93 else
94 ret = __down_trylock(sem);
95 return ret;
98 extern inline void up(struct semaphore * sem)
100 mb();
101 if (atomic_inc_return(&sem->count) <= 0)
102 __up(sem);
105 #endif /* __KERNEL__ */
107 #endif /* !(_PPC_SEMAPHORE_H) */