mutex.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <asm/atomic.h>
  15. /*
  16. * Simple, straightforward mutexes with strict semantics:
  17. *
  18. * - only one task can hold the mutex at a time
  19. * - only the owner can unlock the mutex
  20. * - multiple unlocks are not permitted
  21. * - recursive locking is not permitted
  22. * - a mutex object must be initialized via the API
  23. * - a mutex object must not be initialized via memset or copying
  24. * - task may not exit with mutex held
  25. * - memory areas where held locks reside must not be freed
  26. * - held mutexes must not be reinitialized
  27. * - mutexes may not be used in irq contexts
  28. *
  29. * These semantics are fully enforced when DEBUG_MUTEXES is
  30. * enabled. Furthermore, besides enforcing the above rules, the mutex
  31. * debugging code also implements a number of additional features
  32. * that make lock debugging easier and faster:
  33. *
  34. * - uses symbolic names of mutexes, whenever they are printed in debug output
  35. * - point-of-acquire tracking, symbolic lookup of function names
  36. * - list of all locks held in the system, printout of them
  37. * - owner tracking
  38. * - detects self-recursing locks and prints out all relevant info
  39. * - detects multi-task circular deadlocks and prints out all affected
  40. * locks and tasks (and only those tasks)
  41. */
  42. struct mutex {
  43. /* 1: unlocked, 0: locked, negative: locked, possible waiters */
  44. atomic_t count;
  45. spinlock_t wait_lock;
  46. struct list_head wait_list;
  47. #ifdef CONFIG_DEBUG_MUTEXES
  48. struct thread_info *owner;
  49. struct list_head held_list;
  50. unsigned long acquire_ip;
  51. const char *name;
  52. void *magic;
  53. #endif
  54. };
  55. /*
  56. * This is the control structure for tasks blocked on mutex,
  57. * which resides on the blocked task's kernel stack:
  58. */
  59. struct mutex_waiter {
  60. struct list_head list;
  61. struct task_struct *task;
  62. #ifdef CONFIG_DEBUG_MUTEXES
  63. struct mutex *lock;
  64. void *magic;
  65. #endif
  66. };
  67. #ifdef CONFIG_DEBUG_MUTEXES
  68. # include <linux/mutex-debug.h>
  69. #else
  70. # define __DEBUG_MUTEX_INITIALIZER(lockname)
  71. # define mutex_init(mutex) __mutex_init(mutex, NULL)
  72. # define mutex_destroy(mutex) do { } while (0)
  73. # define mutex_debug_show_all_locks() do { } while (0)
  74. # define mutex_debug_show_held_locks(p) do { } while (0)
  75. # define mutex_debug_check_no_locks_held(task) do { } while (0)
  76. # define mutex_debug_check_no_locks_freed(from, to) do { } while (0)
  77. #endif
  78. #define __MUTEX_INITIALIZER(lockname) \
  79. { .count = ATOMIC_INIT(1) \
  80. , .wait_lock = SPIN_LOCK_UNLOCKED \
  81. , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
  82. __DEBUG_MUTEX_INITIALIZER(lockname) }
  83. #define DEFINE_MUTEX(mutexname) \
  84. struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
  85. extern void fastcall __mutex_init(struct mutex *lock, const char *name);
  86. /***
  87. * mutex_is_locked - is the mutex locked
  88. * @lock: the mutex to be queried
  89. *
  90. * Returns 1 if the mutex is locked, 0 if unlocked.
  91. */
  92. static inline int fastcall mutex_is_locked(struct mutex *lock)
  93. {
  94. return atomic_read(&lock->count) != 1;
  95. }
  96. /*
  97. * See kernel/mutex.c for detailed documentation of these APIs.
  98. * Also see Documentation/mutex-design.txt.
  99. */
  100. extern void fastcall mutex_lock(struct mutex *lock);
  101. extern int fastcall mutex_lock_interruptible(struct mutex *lock);
  102. /*
  103. * NOTE: mutex_trylock() follows the spin_trylock() convention,
  104. * not the down_trylock() convention!
  105. */
  106. extern int fastcall mutex_trylock(struct mutex *lock);
  107. extern void fastcall mutex_unlock(struct mutex *lock);
  108. #endif