2 * Mutexes: blocking mutual exclusion locks
4 * started by Ingo Molnar:
6 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * This file contains the main data structure and API definitions.
10 #ifndef __LINUX_MUTEX_H
11 #define __LINUX_MUTEX_H
13 #include <linux/list.h>
14 #include <linux/spinlock_types.h>
15 #include <linux/linkage.h>
16 #include <linux/lockdep.h>
18 #include <asm/atomic.h>
21 * Simple, straightforward mutexes with strict semantics:
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
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:
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
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)
48 /* 1: unlocked, 0: locked, negative: locked, possible waiters */
51 struct list_head wait_list;
52 #ifdef CONFIG_DEBUG_MUTEXES
53 struct thread_info *owner;
57 #ifdef CONFIG_DEBUG_LOCK_ALLOC
58 struct lockdep_map dep_map;
63 * This is the control structure for tasks blocked on mutex,
64 * which resides on the blocked task's kernel stack:
67 struct list_head list;
68 struct task_struct *task;
69 #ifdef CONFIG_DEBUG_MUTEXES
75 #ifdef CONFIG_DEBUG_MUTEXES
76 # include <linux/mutex-debug.h>
78 # define __DEBUG_MUTEX_INITIALIZER(lockname)
79 # define mutex_init(mutex) \
81 static struct lock_class_key __key; \
83 __mutex_init((mutex), #mutex, &__key); \
85 # define mutex_destroy(mutex) do { } while (0)
88 #ifdef CONFIG_DEBUG_LOCK_ALLOC
89 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
90 , .dep_map = { .name = #lockname }
92 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
95 #define __MUTEX_INITIALIZER(lockname) \
96 { .count = ATOMIC_INIT(1) \
97 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
98 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
99 __DEBUG_MUTEX_INITIALIZER(lockname) \
100 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
102 #define DEFINE_MUTEX(mutexname) \
103 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
105 extern void __mutex_init(struct mutex *lock, const char *name,
106 struct lock_class_key *key);
109 * mutex_is_locked - is the mutex locked
110 * @lock: the mutex to be queried
112 * Returns 1 if the mutex is locked, 0 if unlocked.
114 static inline int fastcall mutex_is_locked(struct mutex *lock)
116 return atomic_read(&lock->count) != 1;
120 * See kernel/mutex.c for detailed documentation of these APIs.
121 * Also see Documentation/mutex-design.txt.
123 extern void fastcall mutex_lock(struct mutex *lock);
124 extern int fastcall mutex_lock_interruptible(struct mutex *lock);
126 #ifdef CONFIG_DEBUG_LOCK_ALLOC
127 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
128 extern int mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass);
130 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
131 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
135 * NOTE: mutex_trylock() follows the spin_trylock() convention,
136 * not the down_trylock() convention!
138 extern int fastcall mutex_trylock(struct mutex *lock);
139 extern void fastcall mutex_unlock(struct mutex *lock);