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>
17 #include <asm/atomic.h>
20 * Simple, straightforward mutexes with strict semantics:
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
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:
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
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)
47 /* 1: unlocked, 0: locked, negative: locked, possible waiters */
50 struct list_head wait_list;
51 #ifdef CONFIG_DEBUG_MUTEXES
52 struct thread_info *owner;
59 * This is the control structure for tasks blocked on mutex,
60 * which resides on the blocked task's kernel stack:
63 struct list_head list;
64 struct task_struct *task;
65 #ifdef CONFIG_DEBUG_MUTEXES
71 #ifdef CONFIG_DEBUG_MUTEXES
72 # include <linux/mutex-debug.h>
74 # define __DEBUG_MUTEX_INITIALIZER(lockname)
75 # define mutex_init(mutex) __mutex_init(mutex, NULL)
76 # define mutex_destroy(mutex) do { } while (0)
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) }
85 #define DEFINE_MUTEX(mutexname) \
86 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
88 extern void fastcall __mutex_init(struct mutex *lock, const char *name);
91 * mutex_is_locked - is the mutex locked
92 * @lock: the mutex to be queried
94 * Returns 1 if the mutex is locked, 0 if unlocked.
96 static inline int fastcall mutex_is_locked(struct mutex *lock)
98 return atomic_read(&lock->count) != 1;
102 * See kernel/mutex.c for detailed documentation of these APIs.
103 * Also see Documentation/mutex-design.txt.
105 extern void fastcall mutex_lock(struct mutex *lock);
106 extern int fastcall mutex_lock_interruptible(struct mutex *lock);
108 * NOTE: mutex_trylock() follows the spin_trylock() convention,
109 * not the down_trylock() convention!
111 extern int fastcall mutex_trylock(struct mutex *lock);
112 extern void fastcall mutex_unlock(struct mutex *lock);