blob: 6fd615a0eea94278fa1d6d9a1ae711f4b60e347a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnar23f78d4a2006-06-27 02:54:53 -07002/*
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 <mingo@redhat.com>
8 * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
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>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010017#include <linux/rbtree.h>
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070018#include <linux/spinlock_types.h>
19
Dave Young4f0e0562010-03-10 15:24:09 -080020extern int max_lock_depth; /* for sysctl */
21
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080022/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070023 * The rt_mutex structure
24 *
25 * @wait_lock: spinlock to protect the structure
Davidlohr Buesoa23ba902017-09-08 16:15:01 -070026 * @waiters: rbtree root to enqueue waiters in priority order;
27 * caches top-waiter (leftmost node).
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070028 * @owner: the mutex owner
29 */
30struct rt_mutex {
Thomas Gleixnerd209d742009-11-17 18:22:11 +010031 raw_spinlock_t wait_lock;
Davidlohr Buesoa23ba902017-09-08 16:15:01 -070032 struct rb_root_cached waiters;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070033 struct task_struct *owner;
34#ifdef CONFIG_DEBUG_RT_MUTEXES
35 int save_state;
Davidlohr Buesoa23ba902017-09-08 16:15:01 -070036 const char *name, *file;
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070037 int line;
38 void *magic;
39#endif
Peter Zijlstraf5694782016-09-19 12:15:37 +020040#ifdef CONFIG_DEBUG_LOCK_ALLOC
41 struct lockdep_map dep_map;
42#endif
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070043};
44
45struct rt_mutex_waiter;
46struct hrtimer_sleeper;
47
48#ifdef CONFIG_DEBUG_RT_MUTEXES
Ingo Molnare7eebaf2006-06-27 02:54:55 -070049 extern int rt_mutex_debug_check_no_locks_freed(const void *from,
50 unsigned long len);
51 extern void rt_mutex_debug_check_no_locks_held(struct task_struct *task);
52#else
53 static inline int rt_mutex_debug_check_no_locks_freed(const void *from,
54 unsigned long len)
55 {
56 return 0;
57 }
58# define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
59#endif
60
61#ifdef CONFIG_DEBUG_RT_MUTEXES
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070062# define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
63 , .name = #mutexname, .file = __FILE__, .line = __LINE__
Peter Zijlstraf5694782016-09-19 12:15:37 +020064
65# define rt_mutex_init(mutex) \
66do { \
67 static struct lock_class_key __key; \
68 __rt_mutex_init(mutex, __func__, &__key); \
69} while (0)
70
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070071 extern void rt_mutex_debug_task_free(struct task_struct *tsk);
72#else
73# define __DEBUG_RT_MUTEX_INITIALIZER(mutexname)
Peter Zijlstraf5694782016-09-19 12:15:37 +020074# define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL, NULL)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070075# define rt_mutex_debug_task_free(t) do { } while (0)
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070076#endif
77
Peter Zijlstraf5694782016-09-19 12:15:37 +020078#ifdef CONFIG_DEBUG_LOCK_ALLOC
79#define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
80 , .dep_map = { .name = #mutexname }
81#else
82#define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
83#endif
84
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070085#define __RT_MUTEX_INITIALIZER(mutexname) \
Thomas Gleixnerd209d742009-11-17 18:22:11 +010086 { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
Davidlohr Buesoa23ba902017-09-08 16:15:01 -070087 , .waiters = RB_ROOT_CACHED \
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070088 , .owner = NULL \
Peter Zijlstraf5694782016-09-19 12:15:37 +020089 __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
90 __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)}
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070091
92#define DEFINE_RT_MUTEX(mutexname) \
93 struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
94
Robert P. J. Day45f8bde2007-01-26 00:57:09 -080095/**
Ingo Molnar23f78d4a2006-06-27 02:54:53 -070096 * rt_mutex_is_locked - is the mutex locked
97 * @lock: the mutex to be queried
98 *
99 * Returns 1 if the mutex is locked, 0 if unlocked.
100 */
101static inline int rt_mutex_is_locked(struct rt_mutex *lock)
102{
103 return lock->owner != NULL;
104}
105
Peter Zijlstraf5694782016-09-19 12:15:37 +0200106extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700107extern void rt_mutex_destroy(struct rt_mutex *lock);
108
Peter Rosin62cedf32018-07-20 10:39:13 +0200109#ifdef CONFIG_DEBUG_LOCK_ALLOC
110extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
111#define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
112#else
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700113extern void rt_mutex_lock(struct rt_mutex *lock);
Peter Rosin62cedf32018-07-20 10:39:13 +0200114#define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
115#endif
116
Thomas Gleixnerc051b212014-05-22 03:25:50 +0000117extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700118extern int rt_mutex_timed_lock(struct rt_mutex *lock,
Thomas Gleixnerc051b212014-05-22 03:25:50 +0000119 struct hrtimer_sleeper *timeout);
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700120
121extern int rt_mutex_trylock(struct rt_mutex *lock);
122
123extern void rt_mutex_unlock(struct rt_mutex *lock);
124
Ingo Molnar23f78d4a2006-06-27 02:54:53 -0700125#endif