mutex.h 3.3 KB

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