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