blob: 6a735c72f23f1c2fd3b983af1c47d9b275879ff9 [file] [log] [blame]
Ingo Molnar6053ee32006-01-09 15:59:19 -08001/*
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>
David S. Millera8b9ee72006-01-11 00:15:16 -080015#include <linux/linkage.h>
Ingo Molnaref5d4702006-07-03 00:24:55 -070016#include <linux/lockdep.h>
Ingo Molnar6053ee32006-01-09 15:59:19 -080017
18#include <asm/atomic.h>
19
20/*
21 * Simple, straightforward mutexes with strict semantics:
22 *
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
33 *
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:
38 *
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
42 * - owner tracking
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)
46 */
47struct mutex {
48 /* 1: unlocked, 0: locked, negative: locked, possible waiters */
49 atomic_t count;
50 spinlock_t wait_lock;
51 struct list_head wait_list;
52#ifdef CONFIG_DEBUG_MUTEXES
53 struct thread_info *owner;
Ingo Molnar6053ee32006-01-09 15:59:19 -080054 const char *name;
55 void *magic;
56#endif
Ingo Molnaref5d4702006-07-03 00:24:55 -070057#ifdef CONFIG_DEBUG_LOCK_ALLOC
58 struct lockdep_map dep_map;
59#endif
Ingo Molnar6053ee32006-01-09 15:59:19 -080060};
61
62/*
63 * This is the control structure for tasks blocked on mutex,
64 * which resides on the blocked task's kernel stack:
65 */
66struct mutex_waiter {
67 struct list_head list;
68 struct task_struct *task;
69#ifdef CONFIG_DEBUG_MUTEXES
70 struct mutex *lock;
71 void *magic;
72#endif
73};
74
75#ifdef CONFIG_DEBUG_MUTEXES
76# include <linux/mutex-debug.h>
77#else
78# define __DEBUG_MUTEX_INITIALIZER(lockname)
Ingo Molnaref5d4702006-07-03 00:24:55 -070079# define mutex_init(mutex) \
80do { \
81 static struct lock_class_key __key; \
82 \
83 __mutex_init((mutex), #mutex, &__key); \
84} while (0)
Ingo Molnar6053ee32006-01-09 15:59:19 -080085# define mutex_destroy(mutex) do { } while (0)
Ingo Molnar6053ee32006-01-09 15:59:19 -080086#endif
87
Ingo Molnaref5d4702006-07-03 00:24:55 -070088#ifdef CONFIG_DEBUG_LOCK_ALLOC
89# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
90 , .dep_map = { .name = #lockname }
91#else
92# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
93#endif
94
Ingo Molnar6053ee32006-01-09 15:59:19 -080095#define __MUTEX_INITIALIZER(lockname) \
96 { .count = ATOMIC_INIT(1) \
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080097 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
Ingo Molnar6053ee32006-01-09 15:59:19 -080098 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
Ingo Molnaref5d4702006-07-03 00:24:55 -070099 __DEBUG_MUTEX_INITIALIZER(lockname) \
100 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
Ingo Molnar6053ee32006-01-09 15:59:19 -0800101
102#define DEFINE_MUTEX(mutexname) \
103 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
104
Ingo Molnaref5d4702006-07-03 00:24:55 -0700105extern void __mutex_init(struct mutex *lock, const char *name,
106 struct lock_class_key *key);
Ingo Molnar6053ee32006-01-09 15:59:19 -0800107
Robert P. J. Day45f8bde2007-01-26 00:57:09 -0800108/**
Ingo Molnar6053ee32006-01-09 15:59:19 -0800109 * mutex_is_locked - is the mutex locked
110 * @lock: the mutex to be queried
111 *
112 * Returns 1 if the mutex is locked, 0 if unlocked.
113 */
114static inline int fastcall mutex_is_locked(struct mutex *lock)
115{
116 return atomic_read(&lock->count) != 1;
117}
118
119/*
120 * See kernel/mutex.c for detailed documentation of these APIs.
121 * Also see Documentation/mutex-design.txt.
122 */
Ingo Molnaref5d4702006-07-03 00:24:55 -0700123#ifdef CONFIG_DEBUG_LOCK_ALLOC
124extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
Andrew Morton18d83622007-05-09 02:33:39 -0700125extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
126 unsigned int subclass);
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200127
128#define mutex_lock(lock) mutex_lock_nested(lock, 0)
129#define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
Ingo Molnaref5d4702006-07-03 00:24:55 -0700130#else
Peter Zijlstrae4564f72007-10-11 22:11:12 +0200131extern void fastcall mutex_lock(struct mutex *lock);
132extern int __must_check fastcall mutex_lock_interruptible(struct mutex *lock);
133
Ingo Molnaref5d4702006-07-03 00:24:55 -0700134# define mutex_lock_nested(lock, subclass) mutex_lock(lock)
NeilBrownd63a5a72006-12-08 02:36:17 -0800135# define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
Ingo Molnaref5d4702006-07-03 00:24:55 -0700136#endif
137
Ingo Molnar6053ee32006-01-09 15:59:19 -0800138/*
139 * NOTE: mutex_trylock() follows the spin_trylock() convention,
140 * not the down_trylock() convention!
141 */
142extern int fastcall mutex_trylock(struct mutex *lock);
143extern void fastcall mutex_unlock(struct mutex *lock);
144
145#endif