mutex.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <linux/list.h>
  13. #include <linux/spinlock_types.h>
  14. #include <linux/linkage.h>
  15. #include <linux/lockdep.h>
  16. #include <asm/atomic.h>
  17. /*
  18. * Simple, straightforward mutexes with strict semantics:
  19. *
  20. * - only one task can hold the mutex at a time
  21. * - only the owner can unlock the mutex
  22. * - multiple unlocks are not permitted
  23. * - recursive locking is not permitted
  24. * - a mutex object must be initialized via the API
  25. * - a mutex object must not be initialized via memset or copying
  26. * - task may not exit with mutex held
  27. * - memory areas where held locks reside must not be freed
  28. * - held mutexes must not be reinitialized
  29. * - mutexes may not be used in hardware or software interrupt
  30. * contexts such as tasklets and timers
  31. *
  32. * These semantics are fully enforced when DEBUG_MUTEXES is
  33. * enabled. Furthermore, besides enforcing the above rules, the mutex
  34. * debugging code also implements a number of additional features
  35. * that make lock debugging easier and faster:
  36. *
  37. * - uses symbolic names of mutexes, whenever they are printed in debug output
  38. * - point-of-acquire tracking, symbolic lookup of function names
  39. * - list of all locks held in the system, printout of them
  40. * - owner tracking
  41. * - detects self-recursing locks and prints out all relevant info
  42. * - detects multi-task circular deadlocks and prints out all affected
  43. * locks and tasks (and only those tasks)
  44. */
  45. struct mutex {
  46. /* 1: unlocked, 0: locked, negative: locked, possible waiters */
  47. atomic_t count;
  48. spinlock_t wait_lock;
  49. struct list_head wait_list;
  50. #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
  51. struct thread_info *owner;
  52. #endif
  53. #ifdef CONFIG_DEBUG_MUTEXES
  54. const char *name;
  55. void *magic;
  56. #endif
  57. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  58. struct lockdep_map dep_map;
  59. #endif
  60. };
  61. /*
  62. * This is the control structure for tasks blocked on mutex,
  63. * which resides on the blocked task's kernel stack:
  64. */
  65. struct mutex_waiter {
  66. struct list_head list;
  67. struct task_struct *task;
  68. #ifdef CONFIG_DEBUG_MUTEXES
  69. void *magic;
  70. #endif
  71. };
  72. #ifdef CONFIG_DEBUG_MUTEXES
  73. # include <linux/mutex-debug.h>
  74. #else
  75. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  76. # define mutex_init(mutex) \
  77. do { \
  78. static struct lock_class_key __key; \
  79. \
  80. __mutex_init((mutex), #mutex, &__key); \
  81. } while (0)
  82. # define mutex_destroy(mutex) do { } while (0)
  83. #endif
  84. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  85. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  86. , .dep_map = { .name = #lockname }
  87. #else
  88. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  89. #endif
  90. #define __MUTEX_INITIALIZER(lockname) \
  91. { .count = ATOMIC_INIT(1) \
  92. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  93. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  94. __DEBUG_MUTEX_INITIALIZER(lockname) \
  95. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  96. #define DEFINE_MUTEX(mutexname) \
  97. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  98. extern void __mutex_init(struct mutex *lock, const char *name,
  99. struct lock_class_key *key);
  100. /**
  101. * mutex_is_locked - is the mutex locked
  102. * @lock: the mutex to be queried
  103. *
  104. * Returns 1 if the mutex is locked, 0 if unlocked.
  105. */
  106. static inline int mutex_is_locked(struct mutex *lock)
  107. {
  108. return atomic_read(&lock->count) != 1;
  109. }
  110. /*
  111. * See kernel/mutex.c for detailed documentation of these APIs.
  112. * Also see Documentation/mutex-design.txt.
  113. */
  114. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  115. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  116. extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
  117. unsigned int subclass);
  118. extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
  119. unsigned int subclass);
  120. #define mutex_lock(lock) mutex_lock_nested(lock, 0)
  121. #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
  122. #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
  123. #else
  124. extern void mutex_lock(struct mutex *lock);
  125. extern int __must_check mutex_lock_interruptible(struct mutex *lock);
  126. extern int __must_check mutex_lock_killable(struct mutex *lock);
  127. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  128. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  129. # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
  130. #endif
  131. /*
  132. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  133. * not the down_trylock() convention!
  134. *
  135. * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  136. */
  137. extern int mutex_trylock(struct mutex *lock);
  138. extern void mutex_unlock(struct mutex *lock);
  139. #endif