blob: a7125daaff90d672272f7817a98e2515d8a04dba [file] [log] [blame]
Matthew Wilcox64ac24e2008-03-07 21:55:58 -05001/*
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.
Matthew Wilcoxb17170b2008-03-14 14:35:22 -040018 * 'count' represents how many more tasks can acquire this semaphore.
19 * Tasks waiting for the lock are kept on the wait_list.
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050020 */
21struct semaphore {
22 spinlock_t lock;
Matthew Wilcoxb17170b2008-03-14 14:35:22 -040023 unsigned int count;
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050024 struct list_head wait_list;
25};
26
27#define __SEMAPHORE_INITIALIZER(name, n) \
28{ \
29 .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
30 .count = n, \
31 .wait_list = LIST_HEAD_INIT((name).wait_list), \
32}
33
34#define __DECLARE_SEMAPHORE_GENERIC(name, count) \
35 struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
36
37#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
38
39static inline void sema_init(struct semaphore *sem, int val)
40{
41 static struct lock_class_key __key;
42 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
43 lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
44}
45
46#define init_MUTEX(sem) sema_init(sem, 1)
47#define init_MUTEX_LOCKED(sem) sema_init(sem, 0)
48
49/*
50 * Attempt to acquire the semaphore. If another task is already holding the
51 * semaphore, sleep until the semaphore is released.
52 */
53extern void down(struct semaphore *sem);
54
55/*
56 * As down(), except the sleep may be interrupted by a signal. If it is,
57 * this function will return -EINTR.
58 */
59extern int __must_check down_interruptible(struct semaphore *sem);
60
61/*
Matthew Wilcoxf06d9682008-03-14 13:19:33 -040062 * As down_interruptible(), except the sleep may only be interrupted by
63 * signals which are fatal to this process.
64 */
65extern int __must_check down_killable(struct semaphore *sem);
66
67/*
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050068 * As down(), except this function will not sleep. It will return 0 if it
69 * acquired the semaphore and 1 if the semaphore was contended. This
70 * function may be called from any context, including interrupt and softirq.
71 */
72extern int __must_check down_trylock(struct semaphore *sem);
73
74/*
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040075 * As down(), except this function will return -ETIME if it fails to
76 * acquire the semaphore within the specified number of jiffies.
77 */
78extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
79
80/*
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050081 * Release the semaphore. Unlike mutexes, up() may be called from any
82 * context and even by tasks which have never called down().
83 */
84extern void up(struct semaphore *sem);
85
86#endif /* __LINUX_SEMAPHORE_H */