rtmutex.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/linkage.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/spinlock_types.h>
  17. extern int max_lock_depth; /* for sysctl */
  18. /**
  19. * The rt_mutex structure
  20. *
  21. * @wait_lock: spinlock to protect the structure
  22. * @waiters: rbtree root to enqueue waiters in priority order;
  23. * caches top-waiter (leftmost node).
  24. * @owner: the mutex owner
  25. */
  26. struct rt_mutex {
  27. raw_spinlock_t wait_lock;
  28. struct rb_root_cached waiters;
  29. struct task_struct *owner;
  30. #ifdef CONFIG_DEBUG_RT_MUTEXES
  31. int save_state;
  32. const char *name, *file;
  33. int line;
  34. void *magic;
  35. #endif
  36. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  37. struct lockdep_map dep_map;
  38. #endif
  39. };
  40. struct rt_mutex_waiter;
  41. struct hrtimer_sleeper;
  42. #ifdef CONFIG_DEBUG_RT_MUTEXES
  43. extern int rt_mutex_debug_check_no_locks_freed(const void *from,
  44. unsigned long len);
  45. extern void rt_mutex_debug_check_no_locks_held(struct task_struct *task);
  46. #else
  47. static inline int rt_mutex_debug_check_no_locks_freed(const void *from,
  48. unsigned long len)
  49. {
  50. return 0;
  51. }
  52. # define rt_mutex_debug_check_no_locks_held(task) do { } while (0)
  53. #endif
  54. #ifdef CONFIG_DEBUG_RT_MUTEXES
  55. # define __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  56. , .name = #mutexname, .file = __FILE__, .line = __LINE__
  57. # define rt_mutex_init(mutex) \
  58. do { \
  59. static struct lock_class_key __key; \
  60. __rt_mutex_init(mutex, __func__, &__key); \
  61. } while (0)
  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, NULL)
  66. # define rt_mutex_debug_task_free(t) do { } while (0)
  67. #endif
  68. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  69. #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname) \
  70. , .dep_map = { .name = #mutexname }
  71. #else
  72. #define __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)
  73. #endif
  74. #define __RT_MUTEX_INITIALIZER(mutexname) \
  75. { .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(mutexname.wait_lock) \
  76. , .waiters = RB_ROOT_CACHED \
  77. , .owner = NULL \
  78. __DEBUG_RT_MUTEX_INITIALIZER(mutexname) \
  79. __DEP_MAP_RT_MUTEX_INITIALIZER(mutexname)}
  80. #define DEFINE_RT_MUTEX(mutexname) \
  81. struct rt_mutex mutexname = __RT_MUTEX_INITIALIZER(mutexname)
  82. /**
  83. * rt_mutex_is_locked - is the mutex locked
  84. * @lock: the mutex to be queried
  85. *
  86. * Returns 1 if the mutex is locked, 0 if unlocked.
  87. */
  88. static inline int rt_mutex_is_locked(struct rt_mutex *lock)
  89. {
  90. return lock->owner != NULL;
  91. }
  92. extern void __rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key);
  93. extern void rt_mutex_destroy(struct rt_mutex *lock);
  94. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  95. extern void rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass);
  96. #define rt_mutex_lock(lock) rt_mutex_lock_nested(lock, 0)
  97. #else
  98. extern void rt_mutex_lock(struct rt_mutex *lock);
  99. #define rt_mutex_lock_nested(lock, subclass) rt_mutex_lock(lock)
  100. #endif
  101. extern int rt_mutex_lock_interruptible(struct rt_mutex *lock);
  102. extern int rt_mutex_timed_lock(struct rt_mutex *lock,
  103. struct hrtimer_sleeper *timeout);
  104. extern int rt_mutex_trylock(struct rt_mutex *lock);
  105. extern void rt_mutex_unlock(struct rt_mutex *lock);
  106. #endif