]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/linux/mutex.h
[PATCH] hrtimer: clean up mktime and make arguments const
[mirror_ubuntu-jammy-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
16 #include <asm/atomic.h>
17
18 /*
19 * Simple, straightforward mutexes with strict semantics:
20 *
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
31 *
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:
36 *
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
40 * - owner tracking
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)
44 */
45 struct mutex {
46 /* 1: unlocked, 0: locked, negative: locked, possible waiters */
47 atomic_t count;
48 spinlock_t wait_lock;
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;
54 const char *name;
55 void *magic;
56 #endif
57 };
58
59 /*
60 * This is the control structure for tasks blocked on mutex,
61 * which resides on the blocked task's kernel stack:
62 */
63 struct mutex_waiter {
64 struct list_head list;
65 struct task_struct *task;
66 #ifdef CONFIG_DEBUG_MUTEXES
67 struct mutex *lock;
68 void *magic;
69 #endif
70 };
71
72 #ifdef CONFIG_DEBUG_MUTEXES
73 # include <linux/mutex-debug.h>
74 #else
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)
82 #endif
83
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) }
89
90 #define DEFINE_MUTEX(mutexname) \
91 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
92
93 extern void fastcall __mutex_init(struct mutex *lock, const char *name);
94
95 /***
96 * mutex_is_locked - is the mutex locked
97 * @lock: the mutex to be queried
98 *
99 * Returns 1 if the mutex is locked, 0 if unlocked.
100 */
101 static inline int fastcall mutex_is_locked(struct mutex *lock)
102 {
103 return atomic_read(&lock->count) != 1;
104 }
105
106 /*
107 * See kernel/mutex.c for detailed documentation of these APIs.
108 * Also see Documentation/mutex-design.txt.
109 */
110 extern void fastcall mutex_lock(struct mutex *lock);
111 extern int fastcall mutex_lock_interruptible(struct mutex *lock);
112 /*
113 * NOTE: mutex_trylock() follows the spin_trylock() convention,
114 * not the down_trylock() convention!
115 */
116 extern int fastcall mutex_trylock(struct mutex *lock);
117 extern void fastcall mutex_unlock(struct mutex *lock);
118
119 #endif