1 /* SPDX-License-Identifier: GPL-2.0 */
3 * RT Mutexes: blocking mutual exclusion locks with PI support
5 * started by Ingo Molnar and Thomas Gleixner:
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
10 * This file contains the public data structure and API definitions.
13 #ifndef __LINUX_RT_MUTEX_H
14 #define __LINUX_RT_MUTEX_H
16 #include <linux/linkage.h>
17 #include <linux/rbtree.h>
18 #include <linux/spinlock_types.h>
20 extern int max_lock_depth; /* for sysctl */
23 * The rt_mutex structure
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
31 raw_spinlock_t wait_lock;
32 struct rb_root_cached waiters;
33 struct task_struct *owner;
34 #ifdef CONFIG_DEBUG_LOCK_ALLOC
35 struct lockdep_map dep_map;
39 struct rt_mutex_waiter;
40 struct hrtimer_sleeper;
42 #ifdef CONFIG_DEBUG_RT_MUTEXES
43 extern int rt_mutex_debug_check_no_locks_freed(const void *from,
45 extern void rt_mutex_debug_check_no_locks_held(struct task_struct *task);
47 static inline int rt_mutex_debug_check_no_locks_freed(const void *from,
52 # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
55 #ifdef CONFIG_DEBUG_RT_MUTEXES
57 # define rt_mutex_init(mutex) \
59 static struct lock_class_key __key; \
60 __rt_mutex_init(mutex, __func__, &__key); \
63 extern void rt_mutex_debug_task_free(struct task_struct *tsk);
65 # define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL, NULL)
66 # define rt_mutex_debug_task_free(t) do { } while (0)
69 #ifdef CONFIG_DEBUG_LOCK_ALLOC
70 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
71 , .dep_map = { .name = #mutexname }
73 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
76 #define __RT_MUTEX_INITIALIZER(mutexname) \
77 { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
78 , .waiters = RB_ROOT_CACHED \
80 __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)}
82 #define DEFINE_RT_MUTEX(mutexname) \
83 struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
86 * rt_mutex_is_locked - is the mutex locked
87 * @lock: the mutex to be queried
89 * Returns 1 if the mutex is locked, 0 if unlocked.
91 static inline int rt_mutex_is_locked(struct rt_mutex *lock)
93 return lock->owner != NULL;
96 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
97 extern void rt_mutex_destroy(struct rt_mutex *lock);
99 #ifdef CONFIG_DEBUG_LOCK_ALLOC
100 extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
101 #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
103 extern void rt_mutex_lock(struct rt_mutex *lock);
104 #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
107 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
108 extern int rt_mutex_trylock(struct rt_mutex *lock);
110 extern void rt_mutex_unlock(struct rt_mutex *lock);