1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2008 Intel Corporation
4*4882a593Smuzhiyun * Author: Matthew Wilcox <willy@linux.intel.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Please see kernel/locking/semaphore.c for documentation of these functions
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun #ifndef __LINUX_SEMAPHORE_H
9*4882a593Smuzhiyun #define __LINUX_SEMAPHORE_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/list.h>
12*4882a593Smuzhiyun #include <linux/spinlock.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /* Please don't access any members of this structure directly */
15*4882a593Smuzhiyun struct semaphore {
16*4882a593Smuzhiyun raw_spinlock_t lock;
17*4882a593Smuzhiyun unsigned int count;
18*4882a593Smuzhiyun struct list_head wait_list;
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define __SEMAPHORE_INITIALIZER(name, n) \
22*4882a593Smuzhiyun { \
23*4882a593Smuzhiyun .lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
24*4882a593Smuzhiyun .count = n, \
25*4882a593Smuzhiyun .wait_list = LIST_HEAD_INIT((name).wait_list), \
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define DEFINE_SEMAPHORE(name) \
29*4882a593Smuzhiyun struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
30*4882a593Smuzhiyun
sema_init(struct semaphore * sem,int val)31*4882a593Smuzhiyun static inline void sema_init(struct semaphore *sem, int val)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun static struct lock_class_key __key;
34*4882a593Smuzhiyun *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
35*4882a593Smuzhiyun lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun extern void down(struct semaphore *sem);
39*4882a593Smuzhiyun extern int __must_check down_interruptible(struct semaphore *sem);
40*4882a593Smuzhiyun extern int __must_check down_killable(struct semaphore *sem);
41*4882a593Smuzhiyun extern int __must_check down_trylock(struct semaphore *sem);
42*4882a593Smuzhiyun extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
43*4882a593Smuzhiyun extern void up(struct semaphore *sem);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #endif /* __LINUX_SEMAPHORE_H */
46