Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * RT Mutexes: blocking mutual exclusion locks with PI support
0004  *
0005  * started by Ingo Molnar and Thomas Gleixner:
0006  *
0007  *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
0008  *  Copyright (C) 2006, Timesys Corp., Thomas Gleixner <tglx@timesys.com>
0009  *
0010  * This file contains the public data structure and API definitions.
0011  */
0012 
0013 #ifndef __LINUX_RT_MUTEX_H
0014 #define __LINUX_RT_MUTEX_H
0015 
0016 #include <linux/compiler.h>
0017 #include <linux/linkage.h>
0018 #include <linux/rbtree_types.h>
0019 #include <linux/spinlock_types_raw.h>
0020 
0021 extern int max_lock_depth; /* for sysctl */
0022 
0023 struct rt_mutex_base {
0024     raw_spinlock_t      wait_lock;
0025     struct rb_root_cached   waiters;
0026     struct task_struct  *owner;
0027 };
0028 
0029 #define __RT_MUTEX_BASE_INITIALIZER(rtbasename)             \
0030 {                                   \
0031     .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(rtbasename.wait_lock),    \
0032     .waiters = RB_ROOT_CACHED,                  \
0033     .owner = NULL                           \
0034 }
0035 
0036 /**
0037  * rt_mutex_base_is_locked - is the rtmutex locked
0038  * @lock: the mutex to be queried
0039  *
0040  * Returns true if the mutex is locked, false if unlocked.
0041  */
0042 static inline bool rt_mutex_base_is_locked(struct rt_mutex_base *lock)
0043 {
0044     return READ_ONCE(lock->owner) != NULL;
0045 }
0046 
0047 extern void rt_mutex_base_init(struct rt_mutex_base *rtb);
0048 
0049 /**
0050  * The rt_mutex structure
0051  *
0052  * @wait_lock:  spinlock to protect the structure
0053  * @waiters:    rbtree root to enqueue waiters in priority order;
0054  *              caches top-waiter (leftmost node).
0055  * @owner:  the mutex owner
0056  */
0057 struct rt_mutex {
0058     struct rt_mutex_base    rtmutex;
0059 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0060     struct lockdep_map  dep_map;
0061 #endif
0062 };
0063 
0064 struct rt_mutex_waiter;
0065 struct hrtimer_sleeper;
0066 
0067 #ifdef CONFIG_DEBUG_RT_MUTEXES
0068 extern void rt_mutex_debug_task_free(struct task_struct *tsk);
0069 #else
0070 static inline void rt_mutex_debug_task_free(struct task_struct *tsk) { }
0071 #endif
0072 
0073 #define rt_mutex_init(mutex) \
0074 do { \
0075     static struct lock_class_key __key; \
0076     __rt_mutex_init(mutex, __func__, &__key); \
0077 } while (0)
0078 
0079 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0080 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)   \
0081     .dep_map = {                    \
0082         .name = #mutexname,         \
0083         .wait_type_inner = LD_WAIT_SLEEP,   \
0084     }
0085 #else
0086 #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
0087 #endif
0088 
0089 #define __RT_MUTEX_INITIALIZER(mutexname)               \
0090 {                                   \
0091     .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex),  \
0092     __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)           \
0093 }
0094 
0095 #define DEFINE_RT_MUTEX(mutexname) \
0096     struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
0097 
0098 extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
0099 
0100 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0101 extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
0102 extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock);
0103 #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
0104 #define rt_mutex_lock_nest_lock(lock, nest_lock)            \
0105     do {                                \
0106         typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
0107         _rt_mutex_lock_nest_lock(lock, &(nest_lock)->dep_map);  \
0108     } while (0)
0109 
0110 #else
0111 extern void rt_mutex_lock(struct rt_mutex *lock);
0112 #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
0113 #define rt_mutex_lock_nest_lock(lock, nest_lock) rt_mutex_lock(lock)
0114 #endif
0115 
0116 extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
0117 extern int rt_mutex_lock_killable(struct rt_mutex *lock);
0118 extern int rt_mutex_trylock(struct rt_mutex *lock);
0119 
0120 extern void rt_mutex_unlock(struct rt_mutex *lock);
0121 
0122 #endif