xref: /linux-6.15/include/linux/rtmutex.h (revision 2d445c3e)
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 /**
23  * The rt_mutex structure
24  *
25  * @wait_lock:	spinlock to protect the structure
26  * @waiters:	rbtree root to enqueue waiters in priority order;
27  *              caches top-waiter (leftmost node).
28  * @owner:	the mutex owner
29  */
30 struct rt_mutex {
31 	raw_spinlock_t		wait_lock;
32 	struct rb_root_cached   waiters;
33 	struct task_struct	*owner;
34 #ifdef CONFIG_DEBUG_RT_MUTEXES
35 	const char		*name;
36 #endif
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
38 	struct lockdep_map	dep_map;
39 #endif
40 };
41 
42 struct rt_mutex_waiter;
43 struct hrtimer_sleeper;
44 
45 #ifdef CONFIG_DEBUG_RT_MUTEXES
46  extern int rt_mutex_debug_check_no_locks_freed(const void *from,
47 						unsigned long len);
48  extern void rt_mutex_debug_check_no_locks_held(struct task_struct *task);
49 #else
50  static inline int rt_mutex_debug_check_no_locks_freed(const void *from,
51 						       unsigned long len)
52  {
53 	return 0;
54  }
55 # define rt_mutex_debug_check_no_locks_held(task)	do { } while (0)
56 #endif
57 
58 #ifdef CONFIG_DEBUG_RT_MUTEXES
59 # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
60 	, .name = #mutexname
61 
62 # define rt_mutex_init(mutex) \
63 do { \
64 	static struct lock_class_key __key; \
65 	__rt_mutex_init(mutex, __func__, &__key); \
66 } while (0)
67 
68  extern void rt_mutex_debug_task_free(struct task_struct *tsk);
69 #else
70 # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
71 # define rt_mutex_init(mutex)			__rt_mutex_init(mutex, NULL, NULL)
72 # define rt_mutex_debug_task_free(t)			do { } while (0)
73 #endif
74 
75 #ifdef CONFIG_DEBUG_LOCK_ALLOC
76 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
77 	, .dep_map = { .name = #mutexname }
78 #else
79 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
80 #endif
81 
82 #define __RT_MUTEX_INITIALIZER(mutexname) \
83 	{ .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
84 	, .waiters = RB_ROOT_CACHED \
85 	, .owner = NULL \
86 	__DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
87 	__DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)}
88 
89 #define DEFINE_RT_MUTEX(mutexname) \
90 	struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
91 
92 /**
93  * rt_mutex_is_locked - is the mutex locked
94  * @lock: the mutex to be queried
95  *
96  * Returns 1 if the mutex is locked, 0 if unlocked.
97  */
98 static inline int rt_mutex_is_locked(struct rt_mutex *lock)
99 {
100 	return lock->owner != NULL;
101 }
102 
103 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
104 extern void rt_mutex_destroy(struct rt_mutex *lock);
105 
106 #ifdef CONFIG_DEBUG_LOCK_ALLOC
107 extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
108 #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
109 #else
110 extern void rt_mutex_lock(struct rt_mutex *lock);
111 #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
112 #endif
113 
114 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
115 extern int rt_mutex_trylock(struct rt_mutex *lock);
116 
117 extern void rt_mutex_unlock(struct rt_mutex *lock);
118 
119 #endif
120