Linux 6.16-rc6
[linux-block.git] / include / linux / semaphore.h
CommitLineData
3e456101 1/* SPDX-License-Identifier: GPL-2.0-only */
64ac24e7
MW
2/*
3 * Copyright (c) 2008 Intel Corporation
4 * Author: Matthew Wilcox <willy@linux.intel.com>
5 *
2dd6fd2e 6 * Please see kernel/locking/semaphore.c for documentation of these functions
64ac24e7
MW
7 */
8#ifndef __LINUX_SEMAPHORE_H
9#define __LINUX_SEMAPHORE_H
10
11#include <linux/list.h>
12#include <linux/spinlock.h>
13
714493cd 14/* Please don't access any members of this structure directly */
64ac24e7 15struct semaphore {
8292c9e1 16 raw_spinlock_t lock;
b17170b2 17 unsigned int count;
64ac24e7 18 struct list_head wait_list;
194a9b9e
LY
19
20#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
21 unsigned long last_holder;
22#endif
64ac24e7
MW
23};
24
194a9b9e
LY
25#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
26#define __LAST_HOLDER_SEMAPHORE_INITIALIZER \
27 , .last_holder = 0UL
28#else
29#define __LAST_HOLDER_SEMAPHORE_INITIALIZER
30#endif
31
64ac24e7
MW
32#define __SEMAPHORE_INITIALIZER(name, n) \
33{ \
8292c9e1 34 .lock = __RAW_SPIN_LOCK_UNLOCKED((name).lock), \
64ac24e7 35 .count = n, \
194a9b9e
LY
36 .wait_list = LIST_HEAD_INIT((name).wait_list) \
37 __LAST_HOLDER_SEMAPHORE_INITIALIZER \
64ac24e7
MW
38}
39
48380368
PZ
40/*
41 * Unlike mutexes, binary semaphores do not have an owner, so up() can
42 * be called in a different thread from the one which called down().
43 * It is also safe to call down_trylock() and up() from interrupt
44 * context.
45 */
46#define DEFINE_SEMAPHORE(_name, _n) \
47 struct semaphore _name = __SEMAPHORE_INITIALIZER(_name, _n)
febc88c5 48
64ac24e7
MW
49static inline void sema_init(struct semaphore *sem, int val)
50{
51 static struct lock_class_key __key;
52 *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
53 lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
54}
55
64ac24e7 56extern void down(struct semaphore *sem);
64ac24e7 57extern int __must_check down_interruptible(struct semaphore *sem);
f06d9686 58extern int __must_check down_killable(struct semaphore *sem);
64ac24e7 59extern int __must_check down_trylock(struct semaphore *sem);
f1241c87 60extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
64ac24e7 61extern void up(struct semaphore *sem);
194a9b9e 62extern unsigned long sem_last_holder(struct semaphore *sem);
64ac24e7
MW
63
64#endif /* __LINUX_SEMAPHORE_H */