Simplify semaphore implementation
[pandora-kernel.git] / include / linux / semaphore.h
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' represents how many more tasks can acquire this semaphore.
19  * Tasks waiting for the lock are kept on the wait_list.
20  */
21 struct semaphore {
22         spinlock_t              lock;
23         unsigned int            count;
24         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
39 static 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  */
53 extern 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  */
59 extern int __must_check down_interruptible(struct semaphore *sem);
60
61 /*
62  * As down_interruptible(), except the sleep may only be interrupted by
63  * signals which are fatal to this process.
64  */
65 extern int __must_check down_killable(struct semaphore *sem);
66
67 /*
68  * 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  */
72 extern int __must_check down_trylock(struct semaphore *sem);
73
74 /*
75  * As down(), except this function will return -ETIME if it fails to
76  * acquire the semaphore within the specified number of jiffies.
77  */
78 extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
79
80 /*
81  * Release the semaphore.  Unlike mutexes, up() may be called from any
82  * context and even by tasks which have never called down().
83  */
84 extern void up(struct semaphore *sem);
85
86 #endif /* __LINUX_SEMAPHORE_H */