aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/semaphore.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/semaphore.h')
-rw-r--r--include/linux/semaphore.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/include/linux/semaphore.h b/include/linux/semaphore.h
new file mode 100644
index 000000000000..b3c691b089b2
--- /dev/null
+++ b/include/linux/semaphore.h
@@ -0,0 +1,77 @@
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 *
7 * Counting semaphores allow up to <n> tasks to acquire the semaphore
8 * simultaneously.
9 */
10#ifndef __LINUX_SEMAPHORE_H
11#define __LINUX_SEMAPHORE_H
12
13#include <linux/list.h>
14#include <linux/spinlock.h>
15
16/*
17 * The spinlock controls access to the other members of the semaphore.
18 * 'count' is decremented by every task which calls down*() and incremented
19 * by every call to up(). Thus, if it is positive, it indicates how many
20 * more tasks may acquire the lock. If it is negative, it indicates how
21 * many tasks are waiting for the lock. Tasks waiting for the lock are
22 * kept on the wait_list.
23 */
24struct semaphore {
25 spinlock_t lock;
26 int count;
27 struct list_head wait_list;
28};
29
30#define __SEMAPHORE_INITIALIZER(name, n) \
31{ \
32 .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
33 .count = n, \
34 .wait_list = LIST_HEAD_INIT((name).wait_list), \
35}
36
37#define __DECLARE_SEMAPHORE_GENERIC(name, count) \
38 struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
39
40#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
41
42static inline void sema_init(struct semaphore *sem, int val)
43{
44 static struct lock_class_key __key;
45 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
46 lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
47}
48
49#define init_MUTEX(sem) sema_init(sem, 1)
50#define init_MUTEX_LOCKED(sem) sema_init(sem, 0)
51
52/*
53 * Attempt to acquire the semaphore. If another task is already holding the
54 * semaphore, sleep until the semaphore is released.
55 */
56extern void down(struct semaphore *sem);
57
58/*
59 * As down(), except the sleep may be interrupted by a signal. If it is,
60 * this function will return -EINTR.
61 */
62extern int __must_check down_interruptible(struct semaphore *sem);
63
64/*
65 * As down(), except this function will not sleep. It will return 0 if it
66 * acquired the semaphore and 1 if the semaphore was contended. This
67 * function may be called from any context, including interrupt and softirq.
68 */
69extern int __must_check down_trylock(struct semaphore *sem);
70
71/*
72 * Release the semaphore. Unlike mutexes, up() may be called from any
73 * context and even by tasks which have never called down().
74 */
75extern void up(struct semaphore *sem);
76
77#endif /* __LINUX_SEMAPHORE_H */