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