2 * Copyright (c) 2008 Intel Corporation
3 * Author: Matthew Wilcox <willy@linux.intel.com>
5 * Distributed under the terms of the GNU GPL, version 2
7 * Counting semaphores allow up to <n> tasks to acquire the semaphore
10 #ifndef __LINUX_SEMAPHORE_H
11 #define __LINUX_SEMAPHORE_H
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
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.
24 struct list_head wait_list;
27 #define __SEMAPHORE_INITIALIZER(name, n) \
29 .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
31 .wait_list = LIST_HEAD_INIT((name).wait_list), \
34 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
35 struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
37 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
39 static inline void sema_init(struct semaphore *sem, int val)
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);
46 #define init_MUTEX(sem) sema_init(sem, 1)
47 #define init_MUTEX_LOCKED(sem) sema_init(sem, 0)
50 * Attempt to acquire the semaphore. If another task is already holding the
51 * semaphore, sleep until the semaphore is released.
53 extern void down(struct semaphore *sem);
56 * As down(), except the sleep may be interrupted by a signal. If it is,
57 * this function will return -EINTR.
59 extern int __must_check down_interruptible(struct semaphore *sem);
62 * As down_interruptible(), except the sleep may only be interrupted by
63 * signals which are fatal to this process.
65 extern int __must_check down_killable(struct semaphore *sem);
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.
72 extern int __must_check down_trylock(struct semaphore *sem);
75 * As down(), except this function will return -ETIME if it fails to
76 * acquire the semaphore within the specified number of jiffies.
78 extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
81 * Release the semaphore. Unlike mutexes, up() may be called from any
82 * context and even by tasks which have never called down().
84 extern void up(struct semaphore *sem);
86 #endif /* __LINUX_SEMAPHORE_H */