2 * BK Id: SCCS/s.semaphore.h 1.19 12/19/02 16:32:51 paulus
4 #ifndef _PPC_SEMAPHORE_H
5 #define _PPC_SEMAPHORE_H
8 * Swiped from asm-sparc/semaphore.h and modified
9 * -- Cort (cort@cs.nmt.edu)
11 * Stole some rw spinlock-based semaphore stuff from asm-alpha/semaphore.h
12 * -- Ani Joshi (ajoshi@unixbox.com)
14 * Remove spinlock-based RW semaphores; RW semaphore definitions are
15 * now in rwsem.h and we use the generic lib/rwsem.c implementation.
16 * Rework semaphores to use atomic_dec_if_positive.
17 * -- Paul Mackerras (paulus@samba.org)
22 #include <asm/atomic.h>
23 #include <asm/system.h>
24 #include <linux/wait.h>
25 #include <linux/rwsem.h>
29 * Note that any negative value of count is equivalent to 0,
30 * but additionally indicates that some process(es) might be
34 wait_queue_head_t wait;
41 # define __SEM_DEBUG_INIT(name) \
42 , (long)&(name).__magic
44 # define __SEM_DEBUG_INIT(name)
47 #define __SEMAPHORE_INITIALIZER(name, count) \
48 { ATOMIC_INIT(count), \
49 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
50 __SEM_DEBUG_INIT(name) }
52 #define __MUTEX_INITIALIZER(name) \
53 __SEMAPHORE_INITIALIZER(name, 1)
55 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
56 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
58 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
59 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
61 static inline void sema_init (struct semaphore *sem, int val)
63 atomic_set(&sem->count, val);
64 init_waitqueue_head(&sem->wait);
66 sem->__magic = (long)&sem->__magic;
70 static inline void init_MUTEX (struct semaphore *sem)
75 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
80 extern void __down(struct semaphore * sem);
81 extern int __down_interruptible(struct semaphore * sem);
82 extern void __up(struct semaphore * sem);
84 extern inline void down(struct semaphore * sem)
87 CHECK_MAGIC(sem->__magic);
91 * Try to get the semaphore, take the slow path if we fail.
93 if (atomic_dec_return(&sem->count) < 0)
98 extern inline int down_interruptible(struct semaphore * sem)
103 CHECK_MAGIC(sem->__magic);
106 if (atomic_dec_return(&sem->count) < 0)
107 ret = __down_interruptible(sem);
112 extern inline int down_trylock(struct semaphore * sem)
117 CHECK_MAGIC(sem->__magic);
120 ret = atomic_dec_if_positive(&sem->count) < 0;
125 extern inline void up(struct semaphore * sem)
128 CHECK_MAGIC(sem->__magic);
132 if (atomic_inc_return(&sem->count) <= 0)
136 static inline int sem_getcount(struct semaphore *sem)
138 return atomic_read(&sem->count);
141 #endif /* __KERNEL__ */
143 #endif /* !(_PPC_SEMAPHORE_H) */