rtmutex.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  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. #ifndef __LINUX_RT_MUTEX_H
  13. #define __LINUX_RT_MUTEX_H
  14. #include <linux/compiler.h>
  15. #include <linux/linkage.h>
  16. #include <linux/rbtree_types.h>
  17. #include <linux/spinlock_types_raw.h>
  18. extern int max_lock_depth; /* for sysctl */
  19. struct rt_mutex_base {
  20. raw_spinlock_t wait_lock;
  21. struct rb_root_cached waiters;
  22. struct task_struct *owner;
  23. };
  24. #define __RT_MUTEX_BASE_INITIALIZER(rtbasename) \
  25. { \
  26. .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(rtbasename.wait_lock), \
  27. .waiters = RB_ROOT_CACHED, \
  28. .owner = NULL \
  29. }
  30. /**
  31. * rt_mutex_base_is_locked - is the rtmutex locked
  32. * @lock: the mutex to be queried
  33. *
  34. * Returns true if the mutex is locked, false if unlocked.
  35. */
  36. static inline bool rt_mutex_base_is_locked(struct rt_mutex_base *lock)
  37. {
  38. return READ_ONCE(lock->owner) != NULL;
  39. }
  40. extern void rt_mutex_base_init(struct rt_mutex_base *rtb);
  41. /**
  42. * The rt_mutex structure
  43. *
  44. * @wait_lock: spinlock to protect the structure
  45. * @waiters: rbtree root to enqueue waiters in priority order;
  46. * caches top-waiter (leftmost node).
  47. * @owner: the mutex owner
  48. */
  49. struct rt_mutex {
  50. struct rt_mutex_base rtmutex;
  51. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  52. struct lockdep_map dep_map;
  53. #endif
  54. };
  55. struct rt_mutex_waiter;
  56. struct hrtimer_sleeper;
  57. #ifdef CONFIG_DEBUG_RT_MUTEXES
  58. extern void rt_mutex_debug_task_free(struct task_struct *tsk);
  59. #else
  60. static inline void rt_mutex_debug_task_free(struct task_struct *tsk) { }
  61. #endif
  62. #define rt_mutex_init(mutex) \
  63. do { \
  64. static struct lock_class_key __key; \
  65. __rt_mutex_init(mutex, __func__, &__key); \
  66. } while (0)
  67. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  68. #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
  69. .dep_map = { \
  70. .name = #mutexname, \
  71. .wait_type_inner = LD_WAIT_SLEEP, \
  72. }
  73. #else
  74. #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
  75. #endif
  76. #define __RT_MUTEX_INITIALIZER(mutexname) \
  77. { \
  78. .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex), \
  79. __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
  80. }
  81. #define DEFINE_RT_MUTEX(mutexname) \
  82. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  83. extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
  84. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  85. extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
  86. extern void _rt_mutex_lock_nest_lock(struct rt_mutex *lock, struct lockdep_map *nest_lock);
  87. #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
  88. #define rt_mutex_lock_nest_lock(lock, nest_lock) \
  89. do { \
  90. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  91. _rt_mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  92. } while (0)
  93. #else
  94. extern void rt_mutex_lock(struct rt_mutex *lock);
  95. #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
  96. #define rt_mutex_lock_nest_lock(lock, nest_lock) rt_mutex_lock(lock)
  97. #endif
  98. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  99. extern int rt_mutex_lock_killable(struct rt_mutex *lock);
  100. extern int rt_mutex_trylock(struct rt_mutex *lock);
  101. extern void rt_mutex_unlock(struct rt_mutex *lock);
  102. #endif