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
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.
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
15 #ifndef __ASM_SEMAPHORE_H
16 #define __ASM_SEMAPHORE_H
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)
27 #include <asm/atomic.h>
28 #include <asm/system.h>
29 #include <linux/wait.h>
30 #include <linux/rwsem.h>
34 * Note that any negative value of count is equivalent to 0,
35 * but additionally indicates that some process(es) might be
39 wait_queue_head_t wait
;
42 #define __SEMAPHORE_INITIALIZER(name, n) \
44 .count = ATOMIC_INIT(n), \
45 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
48 #define __MUTEX_INITIALIZER(name) \
49 __SEMAPHORE_INITIALIZER(name, 1)
51 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
52 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
54 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
55 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name, 0)
57 static inline void sema_init (struct semaphore
*sem
, int val
)
59 atomic_set(&sem
->count
, val
);
60 init_waitqueue_head(&sem
->wait
);
63 static inline void init_MUTEX (struct semaphore
*sem
)
68 static inline void init_MUTEX_LOCKED (struct semaphore
*sem
)
73 extern void __down(struct semaphore
* sem
);
74 extern int __down_interruptible(struct semaphore
* sem
);
75 extern void __up(struct semaphore
* sem
);
77 static inline void down(struct semaphore
* sem
)
82 * Try to get the semaphore, take the slow path if we fail.
84 if (unlikely(atomic_dec_return(&sem
->count
) < 0))
88 static inline int down_interruptible(struct semaphore
* sem
)
94 if (unlikely(atomic_dec_return(&sem
->count
) < 0))
95 ret
= __down_interruptible(sem
);
99 static inline int down_trylock(struct semaphore
* sem
)
101 return atomic_dec_if_positive(&sem
->count
) < 0;
104 static inline void up(struct semaphore
* sem
)
106 if (unlikely(atomic_inc_return(&sem
->count
) <= 0))
110 #endif /* __KERNEL__ */
112 #endif /* __ASM_SEMAPHORE_H */