mutex.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifdef CONFIG_DEBUG_MUTEXES
  51. struct thread_info *owner;
  52. const char *name;
  53. void *magic;
  54. #endif
  55. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  56. struct lockdep_map dep_map;
  57. #endif
  58. };
  59. /*
  60. * This is the control structure for tasks blocked on mutex,
  61. * which resides on the blocked task's kernel stack:
  62. */
  63. struct mutex_waiter {
  64. struct list_head list;
  65. struct task_struct *task;
  66. #ifdef CONFIG_DEBUG_MUTEXES
  67. struct mutex *lock;
  68. void *magic;
  69. #endif
  70. };
  71. #ifdef CONFIG_DEBUG_MUTEXES
  72. # include <linux/mutex-debug.h>
  73. #else
  74. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  75. # define mutex_init(mutex) \
  76. do { \
  77. static struct lock_class_key __key; \
  78. \
  79. __mutex_init((mutex), #mutex, &__key); \
  80. } while (0)
  81. # define mutex_destroy(mutex) do { } while (0)
  82. #endif
  83. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  84. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  85. , .dep_map = { .name = #lockname }
  86. #else
  87. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  88. #endif
  89. #define __MUTEX_INITIALIZER(lockname) \
  90. { .count = ATOMIC_INIT(1) \
  91. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  92. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  93. __DEBUG_MUTEX_INITIALIZER(lockname) \
  94. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  95. #define DEFINE_MUTEX(mutexname) \
  96. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  97. extern void __mutex_init(struct mutex *lock, const char *name,
  98. struct lock_class_key *key);
  99. /**
  100. * mutex_is_locked - is the mutex locked
  101. * @lock: the mutex to be queried
  102. *
  103. * Returns 1 if the mutex is locked, 0 if unlocked.
  104. */
  105. static inline int mutex_is_locked(struct mutex *lock)
  106. {
  107. return atomic_read(&lock->count) != 1;
  108. }
  109. /*
  110. * See kernel/mutex.c for detailed documentation of these APIs.
  111. * Also see Documentation/mutex-design.txt.
  112. */
  113. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  114. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  115. extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
  116. unsigned int subclass);
  117. extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
  118. unsigned int subclass);
  119. #define mutex_lock(lock) mutex_lock_nested(lock, 0)
  120. #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
  121. #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
  122. #else
  123. extern void mutex_lock(struct mutex *lock);
  124. extern int __must_check mutex_lock_interruptible(struct mutex *lock);
  125. extern int __must_check mutex_lock_killable(struct mutex *lock);
  126. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  127. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  128. # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
  129. #endif
  130. /*
  131. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  132. * not the down_trylock() convention!
  133. *
  134. * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
  135. */
  136. extern int mutex_trylock(struct mutex *lock);
  137. extern void mutex_unlock(struct mutex *lock);
  138. #endif