Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | }; |
| 33 | |
| 34 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
| 35 | { \ |
| 36 | .count = ATOMIC_INIT(n), \ |
| 37 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
| 38 | } |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define __DECLARE_SEMAPHORE_GENERIC(name, count) \ |
| 41 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) |
| 42 | |
| 43 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1) |
| 44 | #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0) |
| 45 | |
| 46 | static inline void sema_init (struct semaphore *sem, int val) |
| 47 | { |
| 48 | atomic_set(&sem->count, val); |
| 49 | init_waitqueue_head(&sem->wait); |
| 50 | } |
| 51 | |
| 52 | static inline void init_MUTEX (struct semaphore *sem) |
| 53 | { |
| 54 | sema_init(sem, 1); |
| 55 | } |
| 56 | |
| 57 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) |
| 58 | { |
| 59 | sema_init(sem, 0); |
| 60 | } |
| 61 | |
| 62 | extern void __down(struct semaphore * sem); |
| 63 | extern int __down_interruptible(struct semaphore * sem); |
| 64 | extern void __up(struct semaphore * sem); |
| 65 | |
| 66 | extern inline void down(struct semaphore * sem) |
| 67 | { |
| 68 | might_sleep(); |
| 69 | |
| 70 | /* |
| 71 | * Try to get the semaphore, take the slow path if we fail. |
| 72 | */ |
| 73 | if (atomic_dec_return(&sem->count) < 0) |
| 74 | __down(sem); |
| 75 | smp_wmb(); |
| 76 | } |
| 77 | |
| 78 | extern inline int down_interruptible(struct semaphore * sem) |
| 79 | { |
| 80 | int ret = 0; |
| 81 | |
| 82 | might_sleep(); |
| 83 | |
| 84 | if (atomic_dec_return(&sem->count) < 0) |
| 85 | ret = __down_interruptible(sem); |
| 86 | smp_wmb(); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | extern inline int down_trylock(struct semaphore * sem) |
| 91 | { |
| 92 | int ret; |
| 93 | |
| 94 | ret = atomic_dec_if_positive(&sem->count) < 0; |
| 95 | smp_wmb(); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | extern inline void up(struct semaphore * sem) |
| 100 | { |
| 101 | smp_wmb(); |
| 102 | if (atomic_inc_return(&sem->count) <= 0) |
| 103 | __up(sem); |
| 104 | } |
| 105 | |
| 106 | #endif /* __KERNEL__ */ |
| 107 | |
| 108 | #endif /* !(_PPC_SEMAPHORE_H) */ |