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