2024年3月9日发(作者:)

/**

* 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)

void __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)

{

atomic_long_set(&lock->owner, 0);

spin_lock_init(&lock->wait_lock);

INIT_LIST_HEAD(&lock->wait_list);

#ifdef CONFIG_MUTEX_SPIN_ON_OWNER

osq_lock_init(&lock->osq);

#endif

debug_mutex_init(lock, name, key);

}

EXPORT_SYMBOL(__mutex_init);

mutex加锁

#define mutex_lock(lock) mutex_lock_nested(lock, 0)

void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass)

{

__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);

}

static int __sched

__mutex_lock(struct mutex *lock, long state, unsigned int subclass,

struct lockdep_map *nest_lock, unsigned long ip)

{

return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);

}

/*

* Lock a mutex (possibly interruptible), slowpath:

*/

static __always_inline int __sched

__mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,

struct lockdep_map *nest_lock, unsigned long ip,

struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)

{

struct mutex_waiter waiter;

bool first = false;

struct ww_mutex *ww;

int ret;

if (!use_ww_ctx)

ww_ctx = NULL;