Home | History | Annotate | Download | only in linux
      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 (at) 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 #include <linux/lockdep.h>
     17 
     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  */
     47 struct 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;
     54 	const char 		*name;
     55 	void			*magic;
     56 #endif
     57 #ifdef CONFIG_DEBUG_LOCK_ALLOC
     58 	struct lockdep_map	dep_map;
     59 #endif
     60 };
     61 
     62 /*
     63  * This is the control structure for tasks blocked on mutex,
     64  * which resides on the blocked task's kernel stack:
     65  */
     66 struct 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)
     79 # define mutex_init(mutex) \
     80 do {							\
     81 	static struct lock_class_key __key;		\
     82 							\
     83 	__mutex_init((mutex), #mutex, &__key);		\
     84 } while (0)
     85 # define mutex_destroy(mutex)				do { } while (0)
     86 #endif
     87 
     88 #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 
     95 #define __MUTEX_INITIALIZER(lockname) \
     96 		{ .count = ATOMIC_INIT(1) \
     97 		, .wait_lock = SPIN_LOCK_UNLOCKED \
     98 		, .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
     99 		__DEBUG_MUTEX_INITIALIZER(lockname) \
    100 		__DEP_MAP_MUTEX_INITIALIZER(lockname) }
    101 
    102 #define DEFINE_MUTEX(mutexname) \
    103 	struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
    104 
    105 extern void __mutex_init(struct mutex *lock, const char *name,
    106 			 struct lock_class_key *key);
    107 
    108 /***
    109  * 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  */
    114 static 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  */
    123 extern void fastcall mutex_lock(struct mutex *lock);
    124 extern int fastcall mutex_lock_interruptible(struct mutex *lock);
    125 
    126 #ifdef CONFIG_DEBUG_LOCK_ALLOC
    127 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
    128 #else
    129 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
    130 #endif
    131 
    132 /*
    133  * NOTE: mutex_trylock() follows the spin_trylock() convention,
    134  *       not the down_trylock() convention!
    135  */
    136 extern int fastcall mutex_trylock(struct mutex *lock);
    137 extern void fastcall mutex_unlock(struct mutex *lock);
    138 
    139 #endif
    140