Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Intel Corporation |
| 3 | * Author: Matthew Wilcox <willy@linux.intel.com> |
| 4 | * |
| 5 | * Distributed under the terms of the GNU GPL, version 2 |
| 6 | * |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 7 | * Please see kernel/semaphore.c for documentation of these functions |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 8 | */ |
| 9 | #ifndef __LINUX_SEMAPHORE_H |
| 10 | #define __LINUX_SEMAPHORE_H |
| 11 | |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | |
Matthew Wilcox | 714493c | 2008-04-11 15:23:52 -0400 | [diff] [blame] | 15 | /* Please don't access any members of this structure directly */ |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 16 | struct semaphore { |
| 17 | spinlock_t lock; |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame] | 18 | unsigned int count; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 19 | struct list_head wait_list; |
| 20 | }; |
| 21 | |
| 22 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
| 23 | { \ |
| 24 | .lock = __SPIN_LOCK_UNLOCKED((name).lock), \ |
| 25 | .count = n, \ |
| 26 | .wait_list = LIST_HEAD_INIT((name).wait_list), \ |
| 27 | } |
| 28 | |
| 29 | #define __DECLARE_SEMAPHORE_GENERIC(name, count) \ |
| 30 | struct semaphore name = __SEMAPHORE_INITIALIZER(name, count) |
| 31 | |
| 32 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1) |
| 33 | |
| 34 | static inline void sema_init(struct semaphore *sem, int val) |
| 35 | { |
| 36 | static struct lock_class_key __key; |
| 37 | *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val); |
| 38 | lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0); |
| 39 | } |
| 40 | |
| 41 | #define init_MUTEX(sem) sema_init(sem, 1) |
| 42 | #define init_MUTEX_LOCKED(sem) sema_init(sem, 0) |
| 43 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 44 | extern void down(struct semaphore *sem); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 45 | extern int __must_check down_interruptible(struct semaphore *sem); |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 46 | extern int __must_check down_killable(struct semaphore *sem); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 47 | extern int __must_check down_trylock(struct semaphore *sem); |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 48 | extern int __must_check down_timeout(struct semaphore *sem, long jiffies); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 49 | extern void up(struct semaphore *sem); |
| 50 | |
| 51 | #endif /* __LINUX_SEMAPHORE_H */ |