Commit | Line | Data |
---|---|---|
6053ee3b IM |
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> | |
a8b9ee73 | 15 | #include <linux/linkage.h> |
ef5d4707 | 16 | #include <linux/lockdep.h> |
6053ee3b IM |
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; | |
6053ee3b IM |
54 | const char *name; |
55 | void *magic; | |
56 | #endif | |
ef5d4707 IM |
57 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
58 | struct lockdep_map dep_map; | |
59 | #endif | |
6053ee3b IM |
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) | |
ef5d4707 IM |
79 | # define mutex_init(mutex) \ |
80 | do { \ | |
81 | static struct lock_class_key __key; \ | |
82 | \ | |
83 | __mutex_init((mutex), #mutex, &__key); \ | |
84 | } while (0) | |
6053ee3b | 85 | # define mutex_destroy(mutex) do { } while (0) |
6053ee3b IM |
86 | #endif |
87 | ||
ef5d4707 IM |
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 | ||
6053ee3b IM |
95 | #define __MUTEX_INITIALIZER(lockname) \ |
96 | { .count = ATOMIC_INIT(1) \ | |
6cfd76a2 | 97 | , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \ |
6053ee3b | 98 | , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \ |
ef5d4707 IM |
99 | __DEBUG_MUTEX_INITIALIZER(lockname) \ |
100 | __DEP_MAP_MUTEX_INITIALIZER(lockname) } | |
6053ee3b IM |
101 | |
102 | #define DEFINE_MUTEX(mutexname) \ | |
103 | struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) | |
104 | ||
ef5d4707 IM |
105 | extern void __mutex_init(struct mutex *lock, const char *name, |
106 | struct lock_class_key *key); | |
6053ee3b | 107 | |
45f8bde0 | 108 | /** |
6053ee3b IM |
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); | |
18d8362d | 124 | extern int __must_check fastcall mutex_lock_interruptible(struct mutex *lock); |
ef5d4707 IM |
125 | |
126 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
127 | extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass); | |
18d8362d AM |
128 | extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock, |
129 | unsigned int subclass); | |
ef5d4707 IM |
130 | #else |
131 | # define mutex_lock_nested(lock, subclass) mutex_lock(lock) | |
d63a5a74 | 132 | # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock) |
ef5d4707 IM |
133 | #endif |
134 | ||
6053ee3b IM |
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 |