Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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>
 15 #include <linux/rt_lock.h>
 16 #include <linux/linkage.h>
 17 #include <linux/lockdep.h>
 18 
 19 #include <asm/atomic.h>
 20 
 21 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 22 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
 23                 , .dep_map = { .name = #lockname }
 24 #else
 25 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
 26 #endif
 27 
 28 #ifdef CONFIG_PREEMPT_RT
 29 
 30 #include <linux/rtmutex.h>
 31 
 32 struct mutex {
 33         struct rt_mutex         lock;
 34 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 35         struct lockdep_map      dep_map;
 36 #endif
 37 };
 38 
 39 
 40 #define __MUTEX_INITIALIZER(mutexname)                                  \
 41         {                                                               \
 42                 .lock = __RT_MUTEX_INITIALIZER(mutexname.lock)          \
 43                 __DEP_MAP_MUTEX_INITIALIZER(mutexname)                  \
 44         }
 45 
 46 #define DEFINE_MUTEX(mutexname)                                         \
 47         struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
 48 
 49 extern void
 50 _mutex_init(struct mutex *lock, char *name, struct lock_class_key *key);
 51 
 52 extern void __lockfunc _mutex_lock(struct mutex *lock);
 53 extern int __lockfunc _mutex_lock_interruptible(struct mutex *lock);
 54 extern int __lockfunc _mutex_lock_killable(struct mutex *lock);
 55 extern void __lockfunc _mutex_lock_nested(struct mutex *lock, int subclass);
 56 extern int __lockfunc _mutex_lock_interruptible_nested(struct mutex *lock, int subclass);
 57 extern int __lockfunc _mutex_lock_killable_nested(struct mutex *lock, int subclass);
 58 extern int __lockfunc _mutex_trylock(struct mutex *lock);
 59 extern void __lockfunc _mutex_unlock(struct mutex *lock);
 60 
 61 #define mutex_is_locked(l)              rt_mutex_is_locked(&(l)->lock)
 62 #define mutex_lock(l)                   _mutex_lock(l)
 63 #define mutex_lock_interruptible(l)     _mutex_lock_interruptible(l)
 64 #define mutex_lock_killable(l)          _mutex_lock_killable(l)
 65 #define mutex_trylock(l)                _mutex_trylock(l)
 66 #define mutex_unlock(l)                 _mutex_unlock(l)
 67 #define mutex_destroy(l)                rt_mutex_destroy(&(l)->lock)
 68 
 69 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 70 # define mutex_lock_nested(l, s)        _mutex_lock_nested(l, s)
 71 # define mutex_lock_interruptible_nested(l, s) \
 72                                         _mutex_lock_interruptible_nested(l, s)
 73 # define mutex_lock_killable_nested(l, s) \
 74                                         _mutex_lock_killable_nested(l, s)
 75 #else
 76 # define mutex_lock_nested(l, s)        _mutex_lock(l)
 77 # define mutex_lock_interruptible_nested(l, s) \
 78                                         _mutex_lock_interruptible(l)
 79 # define mutex_lock_killable_nested(l, s) \
 80                                         _mutex_lock_killable(l)
 81 #endif
 82 
 83 # define mutex_init(mutex)                              \
 84 do {                                                    \
 85         static struct lock_class_key __key;             \
 86                                                         \
 87         _mutex_init((mutex), #mutex, &__key);           \
 88 } while (0)
 89 
 90 #else
 91 /*
 92  * Simple, straightforward mutexes with strict semantics:
 93  *
 94  * - only one task can hold the mutex at a time
 95  * - only the owner can unlock the mutex
 96  * - multiple unlocks are not permitted
 97  * - recursive locking is not permitted
 98  * - a mutex object must be initialized via the API
 99  * - a mutex object must not be initialized via memset or copying
100  * - task may not exit with mutex held
101  * - memory areas where held locks reside must not be freed
102  * - held mutexes must not be reinitialized
103  * - mutexes may not be used in hardware or software interrupt
104  *   contexts such as tasklets and timers
105  *
106  * These semantics are fully enforced when DEBUG_MUTEXES is
107  * enabled. Furthermore, besides enforcing the above rules, the mutex
108  * debugging code also implements a number of additional features
109  * that make lock debugging easier and faster:
110  *
111  * - uses symbolic names of mutexes, whenever they are printed in debug output
112  * - point-of-acquire tracking, symbolic lookup of function names
113  * - list of all locks held in the system, printout of them
114  * - owner tracking
115  * - detects self-recursing locks and prints out all relevant info
116  * - detects multi-task circular deadlocks and prints out all affected
117  *   locks and tasks (and only those tasks)
118  */
119 struct mutex {
120         /* 1: unlocked, 0: locked, negative: locked, possible waiters */
121         atomic_t                count;
122         spinlock_t              wait_lock;
123         struct list_head        wait_list;
124 #ifdef CONFIG_DEBUG_MUTEXES
125         struct thread_info      *owner;
126         const char              *name;
127         void                    *magic;
128 #endif
129 #ifdef CONFIG_DEBUG_LOCK_ALLOC
130         struct lockdep_map      dep_map;
131 #endif
132 };
133 
134 /*
135  * This is the control structure for tasks blocked on mutex,
136  * which resides on the blocked task's kernel stack:
137  */
138 struct mutex_waiter {
139         struct list_head        list;
140         struct task_struct      *task;
141 #ifdef CONFIG_DEBUG_MUTEXES
142         struct mutex            *lock;
143         void                    *magic;
144 #endif
145 };
146 
147 #ifdef CONFIG_DEBUG_MUTEXES
148 # include <linux/mutex-debug.h>
149 #else
150 # define __DEBUG_MUTEX_INITIALIZER(lockname)
151 # define mutex_init(mutex) \
152 do {                                                    \
153         static struct lock_class_key __key;             \
154                                                         \
155         __mutex_init((mutex), #mutex, &__key);          \
156 } while (0)
157 # define mutex_destroy(mutex)                           do { } while (0)
158 #endif
159 
160 #define __MUTEX_INITIALIZER(lockname) \
161                 { .count = ATOMIC_INIT(1) \
162                 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
163                 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
164                 __DEBUG_MUTEX_INITIALIZER(lockname) \
165                 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
166 
167 #define DEFINE_MUTEX(mutexname) \
168         struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
169 
170 extern void __mutex_init(struct mutex *lock, const char *name,
171                          struct lock_class_key *key);
172 
173 /**
174  * mutex_is_locked - is the mutex locked
175  * @lock: the mutex to be queried
176  *
177  * Returns 1 if the mutex is locked, 0 if unlocked.
178  */
179 static inline int mutex_is_locked(struct mutex *lock)
180 {
181         return atomic_read(&lock->count) != 1;
182 }
183 
184 /*
185  * See kernel/mutex.c for detailed documentation of these APIs.
186  * Also see Documentation/mutex-design.txt.
187  */
188 #ifdef CONFIG_DEBUG_LOCK_ALLOC
189 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
190 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
191                                         unsigned int subclass);
192 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
193                                         unsigned int subclass);
194 
195 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
196 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
197 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
198 #else
199 extern void mutex_lock(struct mutex *lock);
200 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
201 extern int __must_check mutex_lock_killable(struct mutex *lock);
202 
203 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
204 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
205 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
206 #endif
207 
208 /*
209  * NOTE: mutex_trylock() follows the spin_trylock() convention,
210  *       not the down_trylock() convention!
211  */
212 extern int mutex_trylock(struct mutex *lock);
213 extern void mutex_unlock(struct mutex *lock);
214 
215 #endif
216 #endif
217 
  This page was automatically generated by the LXR engine.