]> git.karo-electronics.de Git - mv-sheeva.git/blob - include/linux/semaphore.h
Generic semaphore implementation
[mv-sheeva.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' 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  */
24 struct 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
42 static 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  */
56 extern 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  */
62 extern 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  */
69 extern 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  */
75 extern void up(struct semaphore *sem);
76
77 #endif /* __LINUX_SEMAPHORE_H */