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 |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 25 | * @waiters: rbtree root to enqueue waiters in priority order |
| 26 | * @waiters_leftmost: top waiter |
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; |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 31 | struct rb_root waiters; |
| 32 | struct rb_node *waiters_leftmost; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 33 | struct task_struct *owner; |
| 34 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
| 35 | int save_state; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 36 | const char *name, *file; |
| 37 | int line; |
| 38 | void *magic; |
| 39 | #endif |
| 40 | }; |
| 41 | |
| 42 | struct rt_mutex_waiter; |
| 43 | struct hrtimer_sleeper; |
| 44 | |
| 45 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 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 |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 59 | # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \ |
| 60 | , .name = #mutexname, .file = __FILE__, .line = __LINE__ |
Harvey Harrison | d5c003b | 2008-10-15 22:01:24 -0700 | [diff] [blame] | 61 | # define rt_mutex_init(mutex) __rt_mutex_init(mutex, __func__) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 62 | extern void rt_mutex_debug_task_free(struct task_struct *tsk); |
| 63 | #else |
| 64 | # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) |
| 65 | # define rt_mutex_init(mutex) __rt_mutex_init(mutex, NULL) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 66 | # define rt_mutex_debug_task_free(t) do { } while (0) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | #define __RT_MUTEX_INITIALIZER(mutexname) \ |
Thomas Gleixner | d209d74 | 2009-11-17 18:22:11 +0100 | [diff] [blame] | 70 | { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \ |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 71 | , .waiters = RB_ROOT \ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 72 | , .owner = NULL \ |
| 73 | __DEBUG_RT_MUTEX_INITIALIZER(mutexname)} |
| 74 | |
| 75 | #define DEFINE_RT_MUTEX(mutexname) \ |
| 76 | struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname) |
| 77 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 78 | /** |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 79 | * rt_mutex_is_locked - is the mutex locked |
| 80 | * @lock: the mutex to be queried |
| 81 | * |
| 82 | * Returns 1 if the mutex is locked, 0 if unlocked. |
| 83 | */ |
| 84 | static inline int rt_mutex_is_locked(struct rt_mutex *lock) |
| 85 | { |
| 86 | return lock->owner != NULL; |
| 87 | } |
| 88 | |
| 89 | extern void __rt_mutex_init(struct rt_mutex *lock, const char *name); |
| 90 | extern void rt_mutex_destroy(struct rt_mutex *lock); |
| 91 | |
| 92 | extern void rt_mutex_lock(struct rt_mutex *lock); |
| 93 | extern int rt_mutex_lock_interruptible(struct rt_mutex *lock, |
| 94 | int detect_deadlock); |
| 95 | extern int rt_mutex_timed_lock(struct rt_mutex *lock, |
| 96 | struct hrtimer_sleeper *timeout, |
| 97 | int detect_deadlock); |
| 98 | |
| 99 | extern int rt_mutex_trylock(struct rt_mutex *lock); |
| 100 | |
| 101 | extern void rt_mutex_unlock(struct rt_mutex *lock); |
| 102 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 103 | #endif |