1 #ifndef _PPC64_SEMAPHORE_H
2 #define _PPC64_SEMAPHORE_H
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)
13 #include <asm/atomic.h>
14 #include <asm/system.h>
15 #include <linux/wait.h>
16 #include <linux/rwsem.h>
20 * Note that any negative value of count is equivalent to 0,
21 * but additionally indicates that some process(es) might be
25 wait_queue_head_t wait;
26 #ifdef WAITQUEUE_DEBUG
31 #ifdef WAITQUEUE_DEBUG
32 # define __SEM_DEBUG_INIT(name) \
33 , (long)&(name).__magic
35 # define __SEM_DEBUG_INIT(name)
38 #define __SEMAPHORE_INITIALIZER(name, count) \
39 { ATOMIC_INIT(count), \
40 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
41 __SEM_DEBUG_INIT(name) }
43 #define __MUTEX_INITIALIZER(name) \
44 __SEMAPHORE_INITIALIZER(name, 1)
46 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
47 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
49 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
50 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
52 static inline void sema_init (struct semaphore *sem, int val)
54 atomic_set(&sem->count, val);
55 init_waitqueue_head(&sem->wait);
56 #ifdef WAITQUEUE_DEBUG
57 sem->__magic = (long)&sem->__magic;
61 static inline void init_MUTEX (struct semaphore *sem)
66 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
71 extern void __down(struct semaphore * sem);
72 extern int __down_interruptible(struct semaphore * sem);
73 extern void __up(struct semaphore * sem);
75 static inline void down(struct semaphore * sem)
77 #ifdef WAITQUEUE_DEBUG
78 CHECK_MAGIC(sem->__magic);
83 * Try to get the semaphore, take the slow path if we fail.
85 if (unlikely(atomic_dec_return(&sem->count) < 0))
89 static inline int down_interruptible(struct semaphore * sem)
93 #ifdef WAITQUEUE_DEBUG
94 CHECK_MAGIC(sem->__magic);
98 if (unlikely(atomic_dec_return(&sem->count) < 0))
99 ret = __down_interruptible(sem);
103 static inline int down_trylock(struct semaphore * sem)
105 #ifdef WAITQUEUE_DEBUG
106 CHECK_MAGIC(sem->__magic);
109 return atomic_dec_if_positive(&sem->count) < 0;
112 static inline void up(struct semaphore * sem)
114 #ifdef WAITQUEUE_DEBUG
115 CHECK_MAGIC(sem->__magic);
118 if (unlikely(atomic_inc_return(&sem->count) <= 0))
122 #endif /* __KERNEL__ */
124 #endif /* !(_PPC64_SEMAPHORE_H) */