a107aebd91483739bebf3bf097a7ddadad53785b
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' 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.
27 struct list_head wait_list
;
30 #define __SEMAPHORE_INITIALIZER(name, n) \
32 .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
34 .wait_list = LIST_HEAD_INIT((name).wait_list), \
37 #define __DECLARE_SEMAPHORE_GENERIC(name, count) \
38 struct semaphore name = __SEMAPHORE_INITIALIZER(name, count)
40 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name, 1)
42 static inline void sema_init(struct semaphore
*sem
, int val
)
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);
49 #define init_MUTEX(sem) sema_init(sem, 1)
50 #define init_MUTEX_LOCKED(sem) sema_init(sem, 0)
53 * Attempt to acquire the semaphore. If another task is already holding the
54 * semaphore, sleep until the semaphore is released.
56 extern void down(struct semaphore
*sem
);
59 * As down(), except the sleep may be interrupted by a signal. If it is,
60 * this function will return -EINTR.
62 extern int __must_check
down_interruptible(struct semaphore
*sem
);
65 * As down_interruptible(), except the sleep may only be interrupted by
66 * signals which are fatal to this process.
68 extern int __must_check
down_killable(struct semaphore
*sem
);
71 * As down(), except this function will not sleep. It will return 0 if it
72 * acquired the semaphore and 1 if the semaphore was contended. This
73 * function may be called from any context, including interrupt and softirq.
75 extern int __must_check
down_trylock(struct semaphore
*sem
);
78 * As down(), except this function will return -ETIME if it fails to
79 * acquire the semaphore within the specified number of jiffies.
81 extern int __must_check
down_timeout(struct semaphore
*sem
, long jiffies
);
84 * Release the semaphore. Unlike mutexes, up() may be called from any
85 * context and even by tasks which have never called down().
87 extern void up(struct semaphore
*sem
);
89 #endif /* __LINUX_SEMAPHORE_H */
This page took 0.032216 seconds and 4 git commands to generate.