| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | 
|---|
| 2 | /* | 
|---|
| 3 | * Copyright (c) 2008 Intel Corporation | 
|---|
| 4 | * Author: Matthew Wilcox <willy@linux.intel.com> | 
|---|
| 5 | * | 
|---|
| 6 | * Please see kernel/locking/semaphore.c for documentation of these functions | 
|---|
| 7 | */ | 
|---|
| 8 | #ifndef __LINUX_SEMAPHORE_H | 
|---|
| 9 | #define __LINUX_SEMAPHORE_H | 
|---|
| 10 |  | 
|---|
| 11 | #include <linux/list.h> | 
|---|
| 12 | #include <linux/spinlock.h> | 
|---|
| 13 |  | 
|---|
| 14 | /* Please don't access any members of this structure directly */ | 
|---|
| 15 | struct semaphore { | 
|---|
| 16 | raw_spinlock_t		lock; | 
|---|
| 17 | unsigned int		count; | 
|---|
| 18 | struct list_head	wait_list; | 
|---|
| 19 |  | 
|---|
| 20 | #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER | 
|---|
| 21 | unsigned long		last_holder; | 
|---|
| 22 | #endif | 
|---|
| 23 | }; | 
|---|
| 24 |  | 
|---|
| 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 |  | 
|---|
| 32 | #define __SEMAPHORE_INITIALIZER(name, n)				\ | 
|---|
| 33 | {									\ | 
|---|
| 34 | .lock		= __RAW_SPIN_LOCK_UNLOCKED((name).lock),	\ | 
|---|
| 35 | .count		= n,						\ | 
|---|
| 36 | .wait_list	= LIST_HEAD_INIT((name).wait_list)		\ | 
|---|
| 37 | __LAST_HOLDER_SEMAPHORE_INITIALIZER				\ | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 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) | 
|---|
| 48 |  | 
|---|
| 49 | static 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 |  | 
|---|
| 56 | extern void down(struct semaphore *sem); | 
|---|
| 57 | extern int __must_check down_interruptible(struct semaphore *sem); | 
|---|
| 58 | extern int __must_check down_killable(struct semaphore *sem); | 
|---|
| 59 | extern int __must_check down_trylock(struct semaphore *sem); | 
|---|
| 60 | extern int __must_check down_timeout(struct semaphore *sem, long jiffies); | 
|---|
| 61 | extern void up(struct semaphore *sem); | 
|---|
| 62 | extern unsigned long sem_last_holder(struct semaphore *sem); | 
|---|
| 63 |  | 
|---|
| 64 | #endif /* __LINUX_SEMAPHORE_H */ | 
|---|
| 65 |  | 
|---|