blob: dc368b8ce215ccc0f6a2b44a16dd2aaa603dbcd1 [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 *
Matthew Wilcox714493c2008-04-11 15:23:52 -04007 * Please see kernel/semaphore.c for documentation of these functions
Matthew Wilcox64ac24e2008-03-07 21:55:58 -05008 */
9#ifndef __LINUX_SEMAPHORE_H
10#define __LINUX_SEMAPHORE_H
11
12#include <linux/list.h>
13#include <linux/spinlock.h>
14
Matthew Wilcox714493c2008-04-11 15:23:52 -040015/* Please don't access any members of this structure directly */
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050016struct semaphore {
Thomas Gleixner8292c9e2010-02-24 09:50:22 +010017 raw_spinlock_t lock;
Matthew Wilcoxb17170b2008-03-14 14:35:22 -040018 unsigned int count;
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050019 struct list_head wait_list;
20};
21
22#define __SEMAPHORE_INITIALIZER(name, n) \
23{ \
Thomas Gleixner8292c9e2010-02-24 09:50:22 +010024 .lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050025 .count = n, \
26 .wait_list = LIST_HEAD_INIT((name).wait_list), \
27}
28
Thomas Gleixnerfebc88c2010-09-07 14:46:37 +020029#define DEFINE_SEMAPHORE(name) \
30 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
31
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050032static inline void sema_init(struct semaphore *sem, int val)
33{
34 static struct lock_class_key __key;
35 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
36 lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
37}
38
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050039extern void down(struct semaphore *sem);
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050040extern int __must_check down_interruptible(struct semaphore *sem);
Matthew Wilcoxf06d9682008-03-14 13:19:33 -040041extern int __must_check down_killable(struct semaphore *sem);
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050042extern int __must_check down_trylock(struct semaphore *sem);
Matthew Wilcoxf1241c82008-03-14 13:43:13 -040043extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
Matthew Wilcox64ac24e2008-03-07 21:55:58 -050044extern void up(struct semaphore *sem);
45
46#endif /* __LINUX_SEMAPHORE_H */