mutex.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 irq contexts
  30. *
  31. * These semantics are fully enforced when DEBUG_MUTEXES is
  32. * enabled. Furthermore, besides enforcing the above rules, the mutex
  33. * debugging code also implements a number of additional features
  34. * that make lock debugging easier and faster:
  35. *
  36. * - uses symbolic names of mutexes, whenever they are printed in debug output
  37. * - point-of-acquire tracking, symbolic lookup of function names
  38. * - list of all locks held in the system, printout of them
  39. * - owner tracking
  40. * - detects self-recursing locks and prints out all relevant info
  41. * - detects multi-task circular deadlocks and prints out all affected
  42. * locks and tasks (and only those tasks)
  43. */
  44. struct mutex {
  45. /* 1: unlocked, 0: locked, negative: locked, possible waiters */
  46. atomic_t count;
  47. spinlock_t wait_lock;
  48. struct list_head wait_list;
  49. #ifdef CONFIG_DEBUG_MUTEXES
  50. struct thread_info *owner;
  51. const char *name;
  52. void *magic;
  53. #endif
  54. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  55. struct lockdep_map dep_map;
  56. #endif
  57. };
  58. /*
  59. * This is the control structure for tasks blocked on mutex,
  60. * which resides on the blocked task's kernel stack:
  61. */
  62. struct mutex_waiter {
  63. struct list_head list;
  64. struct task_struct *task;
  65. #ifdef CONFIG_DEBUG_MUTEXES
  66. struct mutex *lock;
  67. void *magic;
  68. #endif
  69. };
  70. #ifdef CONFIG_DEBUG_MUTEXES
  71. # include <linux/mutex-debug.h>
  72. #else
  73. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  74. # define mutex_init(mutex) \
  75. do { \
  76. static struct lock_class_key __key; \
  77. \
  78. __mutex_init((mutex), #mutex, &__key); \
  79. } while (0)
  80. # define mutex_destroy(mutex) do { } while (0)
  81. #endif
  82. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  83. # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
  84. , .dep_map = { .name = #lockname }
  85. #else
  86. # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
  87. #endif
  88. #define __MUTEX_INITIALIZER(lockname) \
  89. { .count = ATOMIC_INIT(1) \
  90. , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
  91. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  92. __DEBUG_MUTEX_INITIALIZER(lockname) \
  93. __DEP_MAP_MUTEX_INITIALIZER(lockname) }
  94. #define DEFINE_MUTEX(mutexname) \
  95. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  96. extern void __mutex_init(struct mutex *lock, const char *name,
  97. struct lock_class_key *key);
  98. /**
  99. * mutex_is_locked - is the mutex locked
  100. * @lock: the mutex to be queried
  101. *
  102. * Returns 1 if the mutex is locked, 0 if unlocked.
  103. */
  104. static inline int fastcall mutex_is_locked(struct mutex *lock)
  105. {
  106. return atomic_read(&lock->count) != 1;
  107. }
  108. /*
  109. * See kernel/mutex.c for detailed documentation of these APIs.
  110. * Also see Documentation/mutex-design.txt.
  111. */
  112. extern void fastcall mutex_lock(struct mutex *lock);
  113. extern int fastcall mutex_lock_interruptible(struct mutex *lock);
  114. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  115. extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
  116. extern int mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass);
  117. #else
  118. # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
  119. # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
  120. #endif
  121. /*
  122. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  123. * not the down_trylock() convention!
  124. */
  125. extern int fastcall mutex_trylock(struct mutex *lock);
  126. extern void fastcall mutex_unlock(struct mutex *lock);
  127. #endif