Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / asm-ppc64 / semaphore.h
blobaefe7753ea41698685912a6044112e8e13ddb991
1 #ifndef _PPC64_SEMAPHORE_H
2 #define _PPC64_SEMAPHORE_H
4 /*
5 * Remove spinlock-based RW semaphores; RW semaphore definitions are
6 * now in rwsem.h and we use the generic lib/rwsem.c implementation.
7 * Rework semaphores to use atomic_dec_if_positive.
8 * -- Paul Mackerras (paulus@samba.org)
9 */
11 #ifdef __KERNEL__
13 #include <asm/atomic.h>
14 #include <asm/system.h>
15 #include <linux/wait.h>
16 #include <linux/rwsem.h>
18 struct semaphore {
20 * Note that any negative value of count is equivalent to 0,
21 * but additionally indicates that some process(es) might be
22 * sleeping on `wait'.
24 atomic_t count;
25 wait_queue_head_t wait;
28 #define __SEMAPHORE_INITIALIZER(name, n) \
29 { \
30 .count = ATOMIC_INIT(n), \
31 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
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 static inline void sema_init (struct semaphore *sem, int val)
45 atomic_set(&sem->count, val);
46 init_waitqueue_head(&sem->wait);
49 static inline void init_MUTEX (struct semaphore *sem)
51 sema_init(sem, 1);
54 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
56 sema_init(sem, 0);
59 extern void __down(struct semaphore * sem);
60 extern int __down_interruptible(struct semaphore * sem);
61 extern void __up(struct semaphore * sem);
63 static inline void down(struct semaphore * sem)
65 might_sleep();
68 * Try to get the semaphore, take the slow path if we fail.
70 if (unlikely(atomic_dec_return(&sem->count) < 0))
71 __down(sem);
74 static inline int down_interruptible(struct semaphore * sem)
76 int ret = 0;
78 might_sleep();
80 if (unlikely(atomic_dec_return(&sem->count) < 0))
81 ret = __down_interruptible(sem);
82 return ret;
85 static inline int down_trylock(struct semaphore * sem)
87 return atomic_dec_if_positive(&sem->count) < 0;
90 static inline void up(struct semaphore * sem)
92 if (unlikely(atomic_inc_return(&sem->count) <= 0))
93 __up(sem);
96 #endif /* __KERNEL__ */
98 #endif /* !(_PPC64_SEMAPHORE_H) */