Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * RT Mutexes: blocking mutual exclusion locks with PI support |
| 3 | * |
| 4 | * started by Ingo Molnar and Thomas Gleixner: |
| 5 | * |
| 6 | * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 7 | * Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com> |
| 8 | * |
| 9 | * This file contains the public data structure and API definitions. |
| 10 | */ |
| 11 | |
| 12 | #ifndef __LINUX_RT_MUTEX_H |
| 13 | #define __LINUX_RT_MUTEX_H |
| 14 | |
| 15 | #include <linux/linkage.h> |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 16 | #include <linux/rbtree.h> |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 17 | #include <linux/spinlock_types.h> |
| 18 | |
Dave Young | 4f0e056 | 2010-03-10 15:24:09 -0800 | [diff] [blame] | 19 | extern int max_lock_depth; /* for sysctl */ |
| 20 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 21 | /** |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 22 | * The rt_mutex structure |
| 23 | * |
| 24 | * @wait_lock: spinlock to protect the structure |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 25 | * @waiters: rbtree root to enqueue waiters in priority order; |
| 26 | * caches top-waiter (leftmost node). |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 27 | * @owner: the mutex owner |
| 28 | */ |
| 29 | struct rt_mutex { |
Thomas Gleixner | d209d74 | 2009-11-17 18:22:11 +0100 | [diff] [blame] | 30 | raw_spinlock_t wait_lock; |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 31 | struct rb_root_cached waiters; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 32 | struct task_struct *owner; |
| 33 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
| 34 | int save_state; |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 35 | const char *name, *file; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 36 | int line; |
| 37 | void *magic; |
| 38 | #endif |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 40 | struct lockdep_map dep_map; |
| 41 | #endif |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | struct rt_mutex_waiter; |
| 45 | struct hrtimer_sleeper; |
| 46 | |
| 47 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 48 | extern int rt_mutex_debug_check_no_locks_freed(const void *from, |
| 49 | unsigned long len); |
| 50 | extern void rt_mutex_debug_check_no_locks_held(struct task_struct *task); |
| 51 | #else |
| 52 | static inline int rt_mutex_debug_check_no_locks_freed(const void *from, |
| 53 | unsigned long len) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | # define rt_mutex_debug_check_no_locks_held(task) do { } while (0) |
| 58 | #endif |
| 59 | |
| 60 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 61 | # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \ |
| 62 | , .name = #mutexname, .file = __FILE__, .line = __LINE__ |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 63 | |
| 64 | # define rt_mutex_init(mutex) \ |
| 65 | do { \ |
| 66 | static struct lock_class_key __key; \ |
| 67 | __rt_mutex_init(mutex, __func__, &__key); \ |
| 68 | } while (0) |
| 69 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 70 | extern void rt_mutex_debug_task_free(struct task_struct *tsk); |
| 71 | #else |
| 72 | # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 73 | # define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL, NULL) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 74 | # define rt_mutex_debug_task_free(t) do { } while (0) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 75 | #endif |
| 76 | |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 77 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 78 | #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \ |
| 79 | , .dep_map = { .name = #mutexname } |
| 80 | #else |
| 81 | #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) |
| 82 | #endif |
| 83 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 84 | #define __RT_MUTEX_INITIALIZER(mutexname) \ |
Thomas Gleixner | d209d74 | 2009-11-17 18:22:11 +0100 | [diff] [blame] | 85 | { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \ |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 86 | , .waiters = RB_ROOT_CACHED \ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 87 | , .owner = NULL \ |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 88 | __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \ |
| 89 | __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)} |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 90 | |
| 91 | #define DEFINE_RT_MUTEX(mutexname) \ |
| 92 | struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname) |
| 93 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 94 | /** |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 95 | * rt_mutex_is_locked - is the mutex locked |
| 96 | * @lock: the mutex to be queried |
| 97 | * |
| 98 | * Returns 1 if the mutex is locked, 0 if unlocked. |
| 99 | */ |
| 100 | static inline int rt_mutex_is_locked(struct rt_mutex *lock) |
| 101 | { |
| 102 | return lock->owner != NULL; |
| 103 | } |
| 104 | |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 105 | extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 106 | extern void rt_mutex_destroy(struct rt_mutex *lock); |
| 107 | |
| 108 | extern void rt_mutex_lock(struct rt_mutex *lock); |
Thomas Gleixner | c051b21 | 2014-05-22 03:25:50 +0000 | [diff] [blame] | 109 | extern int rt_mutex_lock_interruptible(struct rt_mutex *lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 110 | extern int rt_mutex_timed_lock(struct rt_mutex *lock, |
Thomas Gleixner | c051b21 | 2014-05-22 03:25:50 +0000 | [diff] [blame] | 111 | struct hrtimer_sleeper *timeout); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 112 | |
| 113 | extern int rt_mutex_trylock(struct rt_mutex *lock); |
| 114 | |
| 115 | extern void rt_mutex_unlock(struct rt_mutex *lock); |
| 116 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 117 | #endif |