xref: /linux-6.15/include/linux/rtmutex.h (revision 830e6acc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * RT Mutexes: blocking mutual exclusion locks with PI support
4  *
5  * started by Ingo Molnar and Thomas Gleixner:
6  *
7  *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <[email protected]>
8  *  Copyright (C) 2006, Timesys Corp., Thomas Gleixner <[email protected]>
9  *
10  * This file contains the public data structure and API definitions.
11  */
12 
13 #ifndef __LINUX_RT_MUTEX_H
14 #define __LINUX_RT_MUTEX_H
15 
16 #include <linux/linkage.h>
17 #include <linux/rbtree.h>
18 #include <linux/spinlock_types.h>
19 
20 extern int max_lock_depth; /* for sysctl */
21 
22 struct rt_mutex_base {
23 	raw_spinlock_t		wait_lock;
24 	struct rb_root_cached   waiters;
25 	struct task_struct	*owner;
26 };
27 
28 #define __RT_MUTEX_BASE_INITIALIZER(rtbasename)				\
29 {									\
30 	.wait_lock = __RAW_SPIN_LOCK_UNLOCKED(rtbasename.wait_lock),	\
31 	.waiters = RB_ROOT_CACHED,					\
32 	.owner = NULL							\
33 }
34 
35 extern void rt_mutex_base_init(struct rt_mutex_base *rtb);
36 
37 /**
38  * The rt_mutex structure
39  *
40  * @wait_lock:	spinlock to protect the structure
41  * @waiters:	rbtree root to enqueue waiters in priority order;
42  *              caches top-waiter (leftmost node).
43  * @owner:	the mutex owner
44  */
45 struct rt_mutex {
46 	struct rt_mutex_base	rtmutex;
47 #ifdef CONFIG_DEBUG_LOCK_ALLOC
48 	struct lockdep_map	dep_map;
49 #endif
50 };
51 
52 struct rt_mutex_waiter;
53 struct hrtimer_sleeper;
54 
55 #ifdef CONFIG_DEBUG_RT_MUTEXES
56 extern void rt_mutex_debug_task_free(struct task_struct *tsk);
57 #else
58 static inline void rt_mutex_debug_task_free(struct task_struct *tsk) { }
59 #endif
60 
61 #define rt_mutex_init(mutex) \
62 do { \
63 	static struct lock_class_key __key; \
64 	__rt_mutex_init(mutex, __func__, &__key); \
65 } while (0)
66 
67 #ifdef CONFIG_DEBUG_LOCK_ALLOC
68 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)	\
69 	.dep_map = {					\
70 		.name = #mutexname,			\
71 		.wait_type_inner = LD_WAIT_SLEEP,	\
72 	}
73 #else
74 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
75 #endif
76 
77 #define __RT_MUTEX_INITIALIZER(mutexname)				\
78 {									\
79 	.rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex),	\
80 	__DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)			\
81 }
82 
83 #define DEFINE_RT_MUTEX(mutexname) \
84 	struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
85 
86 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
87 
88 #ifdef CONFIG_DEBUG_LOCK_ALLOC
89 extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
90 #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
91 #else
92 extern void rt_mutex_lock(struct rt_mutex *lock);
93 #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
94 #endif
95 
96 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
97 extern int rt_mutex_trylock(struct rt_mutex *lock);
98 
99 extern void rt_mutex_unlock(struct rt_mutex *lock);
100 
101 #endif
102