DEFINE_MUTEX

2017-01-07 11:23:15来源:CSDN作者:wh8_2011人点击

DEFINE_MUTEX宏包含在  include/linux/mutex.h文件中


#ifdef CONFIG_DEBUG_MUTEXES
# include <linux/mutex-debug.h>
#else
# define __DEBUG_MUTEX_INITIALIZER(lockname)
/**
 * mutex_init - initialize the mutex
 * @mutex: the mutex to be initialized
 *
 * Initialize the mutex to unlocked state.
 *
 * It is not allowed to initialize an already locked mutex.
 */
# define mutex_init(mutex) /
do { /
static struct lock_class_key __key;/
/
__mutex_init((mutex), #mutex, &__key);/
} while (0)
# define mutex_destroy(mutex) do { } while (0)
#endif


#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define __DEP_MAP_MUTEX_INITIALIZER(lockname) /
, .dep_map = { .name = #lockname }
#else
# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
#endif


#define __MUTEX_INITIALIZER(lockname) /
{ .count = ATOMIC_INIT(1) /
, .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) /
, .wait_list = LIST_HEAD_INIT(lockname.wait_list) /
__DEBUG_MUTEX_INITIALIZER(lockname) /
__DEP_MAP_MUTEX_INITIALIZER(lockname) }


#define DEFINE_MUTEX(mutexname) /
struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)

mutex的结构如下图:

struct mutex {
/* 1: unlocked, 0: locked, negative: locked, possible waiters */
atomic_t count;
spinlock_t wait_lock;
struct list_headwait_list;
#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
struct thread_info*owner;
#endif
#ifdef CONFIG_DEBUG_MUTEXES
const char *name;
void *magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_mapdep_map;
#endif
};


DEFINE_MUTEX的展开:

struct mutex mutexname = {
.count = ATOMIC_INIT(1),
.wait_lock = __SPIN_LOCK_UNLOCKED(mutexname.wait_lock),
.wait_list = LIST_HEAD_INIT(mutexname.wait_list)}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台