| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * This file is subject to the terms and conditions of the GNU General Public | 
 | 3 |  * License.  See the file "COPYING" in the main directory of this archive | 
 | 4 |  * for more details. | 
 | 5 |  * | 
 | 6 |  * Copyright (C) 1996  Linus Torvalds | 
 | 7 |  * Copyright (C) 1998, 99, 2000, 01, 04  Ralf Baechle | 
 | 8 |  * Copyright (C) 1999, 2000, 01  Silicon Graphics, Inc. | 
 | 9 |  * Copyright (C) 2000, 01 MIPS Technologies, Inc. | 
 | 10 |  * | 
 | 11 |  * In all honesty, little of the old MIPS code left - the PPC64 variant was | 
 | 12 |  * just looking nice and portable so I ripped it.  Credits to whoever wrote | 
 | 13 |  * it. | 
 | 14 |  */ | 
 | 15 | #ifndef __ASM_SEMAPHORE_H | 
 | 16 | #define __ASM_SEMAPHORE_H | 
 | 17 |  | 
 | 18 | /* | 
 | 19 |  * Remove spinlock-based RW semaphores; RW semaphore definitions are | 
 | 20 |  * now in rwsem.h and we use the generic lib/rwsem.c implementation. | 
 | 21 |  * Rework semaphores to use atomic_dec_if_positive. | 
 | 22 |  * -- Paul Mackerras (paulus@samba.org) | 
 | 23 |  */ | 
 | 24 |  | 
 | 25 | #ifdef __KERNEL__ | 
 | 26 |  | 
 | 27 | #include <asm/atomic.h> | 
 | 28 | #include <asm/system.h> | 
 | 29 | #include <linux/wait.h> | 
 | 30 | #include <linux/rwsem.h> | 
 | 31 |  | 
 | 32 | struct semaphore { | 
 | 33 | 	/* | 
 | 34 | 	 * Note that any negative value of count is equivalent to 0, | 
 | 35 | 	 * but additionally indicates that some process(es) might be | 
 | 36 | 	 * sleeping on `wait'. | 
 | 37 | 	 */ | 
 | 38 | 	atomic_t count; | 
 | 39 | 	wait_queue_head_t wait; | 
 | 40 | }; | 
 | 41 |  | 
 | 42 | #define __SEMAPHORE_INITIALIZER(name, n)				\ | 
 | 43 | {									\ | 
 | 44 | 	.count		= ATOMIC_INIT(n),				\ | 
 | 45 | 	.wait		= __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)	\ | 
 | 46 | } | 
 | 47 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define __DECLARE_SEMAPHORE_GENERIC(name, count) \ | 
 | 49 | 	struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) | 
 | 50 |  | 
 | 51 | #define DECLARE_MUTEX(name)		__DECLARE_SEMAPHORE_GENERIC(name, 1) | 
 | 52 | #define DECLARE_MUTEX_LOCKED(name)	__DECLARE_SEMAPHORE_GENERIC(name, 0) | 
 | 53 |  | 
 | 54 | static inline void sema_init (struct semaphore *sem, int val) | 
 | 55 | { | 
 | 56 | 	atomic_set(&sem->count, val); | 
 | 57 | 	init_waitqueue_head(&sem->wait); | 
 | 58 | } | 
 | 59 |  | 
 | 60 | static inline void init_MUTEX (struct semaphore *sem) | 
 | 61 | { | 
 | 62 | 	sema_init(sem, 1); | 
 | 63 | } | 
 | 64 |  | 
 | 65 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) | 
 | 66 | { | 
 | 67 | 	sema_init(sem, 0); | 
 | 68 | } | 
 | 69 |  | 
 | 70 | extern void __down(struct semaphore * sem); | 
 | 71 | extern int  __down_interruptible(struct semaphore * sem); | 
 | 72 | extern void __up(struct semaphore * sem); | 
 | 73 |  | 
 | 74 | static inline void down(struct semaphore * sem) | 
 | 75 | { | 
 | 76 | 	might_sleep(); | 
 | 77 |  | 
 | 78 | 	/* | 
 | 79 | 	 * Try to get the semaphore, take the slow path if we fail. | 
 | 80 | 	 */ | 
 | 81 | 	if (unlikely(atomic_dec_return(&sem->count) < 0)) | 
 | 82 | 		__down(sem); | 
 | 83 | } | 
 | 84 |  | 
 | 85 | static inline int down_interruptible(struct semaphore * sem) | 
 | 86 | { | 
 | 87 | 	int ret = 0; | 
 | 88 |  | 
 | 89 | 	might_sleep(); | 
 | 90 |  | 
 | 91 | 	if (unlikely(atomic_dec_return(&sem->count) < 0)) | 
 | 92 | 		ret = __down_interruptible(sem); | 
 | 93 | 	return ret; | 
 | 94 | } | 
 | 95 |  | 
 | 96 | static inline int down_trylock(struct semaphore * sem) | 
 | 97 | { | 
 | 98 | 	return atomic_dec_if_positive(&sem->count) < 0; | 
 | 99 | } | 
 | 100 |  | 
 | 101 | static inline void up(struct semaphore * sem) | 
 | 102 | { | 
 | 103 | 	if (unlikely(atomic_inc_return(&sem->count) <= 0)) | 
 | 104 | 		__up(sem); | 
 | 105 | } | 
 | 106 |  | 
 | 107 | #endif /* __KERNEL__ */ | 
 | 108 |  | 
 | 109 | #endif /* __ASM_SEMAPHORE_H */ |