mutex.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Mutexes: blocking mutual exclusion locks
  3. *
  4. * started by Ingo Molnar:
  5. *
  6. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  7. *
  8. * This file contains the main data structure and API definitions.
  9. */
  10. #ifndef __LINUX_MUTEX_H
  11. #define __LINUX_MUTEX_H
  12. #include <asm/current.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock_types.h>
  15. #include <linux/linkage.h>
  16. #include <linux/lockdep.h>
  17. #include <linux/atomic.h>
  18. #include <asm/processor.h>
  19. #include <linux/osq_lock.h>
  20. #include <linux/debug_locks.h>
  21. struct ww_acquire_ctx;
  22. /*
  23. * Simple, straightforward mutexes with strict semantics:
  24. *
  25. * - only one task can hold the mutex at a time
  26. * - only the owner can unlock the mutex
  27. * - multiple unlocks are not permitted
  28. * - recursive locking is not permitted
  29. * - a mutex object must be initialized via the API
  30. * - a mutex object must not be initialized via memset or copying
  31. * - task may not exit with mutex held
  32. * - memory areas where held locks reside must not be freed
  33. * - held mutexes must not be reinitialized
  34. * - mutexes may not be used in hardware or software interrupt
  35. * contexts such as tasklets and timers
  36. *
  37. * These semantics are fully enforced when DEBUG_MUTEXES is
  38. * enabled. Furthermore, besides enforcing the above rules, the mutex
  39. * debugging code also implements a number of additional features
  40. * that make lock debugging easier and faster:
  41. *
  42. * - uses symbolic names of mutexes, whenever they are printed in debug output
  43. * - point-of-acquire tracking, symbolic lookup of function names
  44. * - list of all locks held in the system, printout of them
  45. * - owner tracking
  46. * - detects self-recursing locks and prints out all relevant info
  47. * - detects multi-task circular deadlocks and prints out all affected
  48. * locks and tasks (and only those tasks)
  49. */
  50. struct mutex {
  51. atomic_long_t owner;
  52. spinlock_t wait_lock;
  53. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  54. struct optimistic_spin_queue osq; /* Spinner MCS lock */
  55. #endif
  56. struct list_head wait_list;
  57. #ifdef CONFIG_DEBUG_MUTEXES
  58. void *magic;
  59. #endif
  60. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  61. struct lockdep_map dep_map;
  62. #endif
  63. };
  64. static inline struct task_struct *__mutex_owner(struct mutex *lock)
  65. {
  66. return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x07);
  67. }
  68. /*
  69. * This is the control structure for tasks blocked on mutex,
  70. * which resides on the blocked task's kernel stack:
  71. */
  72. struct mutex_waiter {
  73. struct list_head list;
  74. struct task_struct *task;
  75. struct ww_acquire_ctx *ww_ctx;
  76. #ifdef CONFIG_DEBUG_MUTEXES
  77. void *magic;
  78. #endif
  79. };
  80. #ifdef CONFIG_DEBUG_MUTEXES
  81. #define __DEBUG_MUTEX_INITIALIZER(lockname) \
  82. , .magic = &lockname
  83. extern void mutex_destroy(struct mutex *lock);
  84. #else
  85. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  86. static inline void mutex_destroy(struct mutex *lock) {}
  87. #endif
  88. /**
  89. * mutex_init - initialize the mutex
  90. * @mutex: the mutex to be initialized
  91. *
  92. * Initialize the mutex to unlocked state.
  93. *
  94. * It is not allowed to initialize an already locked mutex.
  95. */
  96. #define mutex_init(mutex) \
  97. do { \
  98. static struct lock_class_key __key; \
  99. \
  100. __mutex_init((mutex), #mutex, &__key); \
  101. } while (0)
  102. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  103. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  104. , .dep_map = { .name = #lockname }
  105. #else
  106. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  107. #endif
  108. #define __MUTEX_INITIALIZER(lockname) \
  109. { .owner = ATOMIC_LONG_INIT(0) \
  110. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  111. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  112. __DEBUG_MUTEX_INITIALIZER(lockname) \
  113. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  114. #define DEFINE_MUTEX(mutexname) \
  115. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  116. extern void __mutex_init(struct mutex *lock, const char *name,
  117. struct lock_class_key *key);
  118. /**
  119. * mutex_is_locked - is the mutex locked
  120. * @lock: the mutex to be queried
  121. *
  122. * Returns 1 if the mutex is locked, 0 if unlocked.
  123. */
  124. static inline int mutex_is_locked(struct mutex *lock)
  125. {
  126. /*
  127. * XXX think about spin_is_locked
  128. */
  129. return __mutex_owner(lock) != NULL;
  130. }
  131. /*
  132. * See kernel/locking/mutex.c for detailed documentation of these APIs.
  133. * Also see Documentation/locking/mutex-design.txt.
  134. */
  135. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  136. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  137. extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
  138. extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
  139. unsigned int subclass);
  140. extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
  141. unsigned int subclass);
  142. extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
  143. #define mutex_lock(lock) mutex_lock_nested(lock, 0)
  144. #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
  145. #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
  146. #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
  147. #define mutex_lock_nest_lock(lock, nest_lock) \
  148. do { \
  149. typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
  150. _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  151. } while (0)
  152. #else
  153. extern void mutex_lock(struct mutex *lock);
  154. extern int __must_check mutex_lock_interruptible(struct mutex *lock);
  155. extern int __must_check mutex_lock_killable(struct mutex *lock);
  156. extern void mutex_lock_io(struct mutex *lock);
  157. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  158. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  159. # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
  160. # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
  161. # define mutex_lock_io_nested(lock, subclass) mutex_lock(lock)
  162. #endif
  163. /*
  164. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  165. * not the down_trylock() convention!
  166. *
  167. * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  168. */
  169. extern int mutex_trylock(struct mutex *lock);
  170. extern void mutex_unlock(struct mutex *lock);
  171. extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
  172. /*
  173. * These values are chosen such that FAIL and SUCCESS match the
  174. * values of the regular mutex_trylock().
  175. */
  176. enum mutex_trylock_recursive_enum {
  177. MUTEX_TRYLOCK_FAILED = 0,
  178. MUTEX_TRYLOCK_SUCCESS = 1,
  179. MUTEX_TRYLOCK_RECURSIVE,
  180. };
  181. /**
  182. * mutex_trylock_recursive - trylock variant that allows recursive locking
  183. * @lock: mutex to be locked
  184. *
  185. * This function should not be used, _ever_. It is purely for hysterical GEM
  186. * raisins, and once those are gone this will be removed.
  187. *
  188. * Returns:
  189. * MUTEX_TRYLOCK_FAILED - trylock failed,
  190. * MUTEX_TRYLOCK_SUCCESS - lock acquired,
  191. * MUTEX_TRYLOCK_RECURSIVE - we already owned the lock.
  192. */
  193. static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum
  194. mutex_trylock_recursive(struct mutex *lock)
  195. {
  196. if (unlikely(__mutex_owner(lock) == current))
  197. return MUTEX_TRYLOCK_RECURSIVE;
  198. return mutex_trylock(lock);
  199. }
  200. #endif /* __LINUX_MUTEX_H */