首页 > 其他分享 >21 同步与互斥(七)互斥量

21 同步与互斥(七)互斥量

时间:2023-05-01 14:57:00浏览次数:46  
标签:count __ 同步 struct lock 互斥 mutex wait 21

1 简介

mutex相对于semaphore更加高效。

mutex在面对SMP时,如果mutex在别的CPU上运行,而“我”是唯一在等待这个mutex的进程。此时“我”是不会去休眠的,而是原地spin

2 mutex的结构和API

2.1 mutex结构

struct mutex my_mutex;
struct mutex {
	/* 1: unlocked, 0: locked, negative: locked, possible waiters */
	/* 1: unlock
	 * 0: lock
	 * -1:lock,有人wait
	 */
	atomic_t		count;					/* 1: unlocked, 0: locked, negative: locked, possible waiters,这里描述的是possible */
	spinlock_t		wait_lock;				/* 借助spinlock */
	struct list_head	wait_list;			/* 与spinlock一致,等待线程放于此 */
#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
	struct task_struct	*owner;				/* 调试和性能优化 */
#endif
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
	struct optimistic_spin_queue osq; /* Spinner MCS lock */
#endif
#ifdef CONFIG_DEBUG_MUTEXES
	void			*magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lockdep_map	dep_map;
#endif
};

2.2 mutex的API

  • mutex_init(mutex)

    初始化mutex

  • DEFINE_MUTEX(mutexname)

    定义并初始化一个mutexname

  • void mutex_lock(struct mutex *lock)

    获得mutex,如果无法获得会休眠

  • int mutex_lock_interruptible(struct mutex *lock)

    获得mutex,如果无法获得会休眠。此外还能被信号唤醒

    return

    • 0:成功获得mutex
    • -EINTR:被信号唤醒
  • int mutex_lock_killable(struct mutex *lock)

    获得mutex,如果无法获得会休眠。此外还能被fatal signal信号唤醒

    return

    • 0:成功获得mutex
    • -EINTR:被信号唤醒
  • int mutex_trylock(struct mutex *lock)

    尝试获取mutex,如果无法获得立即返回

    return

    • 1:获得了mutex
    • 0:没有获得mutex
  • void mutex_unlock(struct mutex *lock)

    释放mutex,唤醒其他等待同一个mutex的线程

3 mutex实现机制

mutex中存在两条路径,一条fastpath,一条slowpath

3.1 mutex_lock

mutex_lock -> __mutex_fastpath_lock | | -> mutex_set_owner

| -> __mutex_lock_slowpath |

void __sched mutex_lock(struct mutex *lock)
{
	might_sleep();
	/*
	 * The locking fastpath is the 1->0 transition from
	 * 'unlocked' into 'locked' state.
	 */
	__mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
	mutex_set_owner(lock);
}

EXPORT_SYMBOL(mutex_lock);
static inline void
__mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *))
{
	/* 原子操作减
	 * 如果之前count为1,即mutex未被占用。直接返回。
	 */
	if (unlikely(atomic_dec_return_acquire(count) < 0))
		fail_fn(count);			// 如果count值为0,即mutex被占用。则调用__mutex_lock_slowpath。走slow路径
}
__visible void __sched
__mutex_lock_slowpath(atomic_t *lock_count)
{
	/* container_of 从成员推出指针head */
	struct mutex *lock = container_of(lock_count, struct mutex, count);

	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
			    NULL, _RET_IP_, NULL, 0);
}
/*
 * 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 task_struct *task = current;
	struct mutex_waiter waiter;
	unsigned long flags;
	int ret;

	/* 传入值为0 */
	if (use_ww_ctx) {
		struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
			return -EALREADY;
	}
	
	/* 禁止抢占 */
	preempt_disable();
	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);

	/* 优化操作
	 * 如果是在另外一个CPU上的进程在使用,则不休眠,尝试等待一会
	 */
	if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) {
		/* got the lock, yay! */
		preempt_enable();
		return 0;
	}

	/* 上锁 */
	spin_lock_mutex(&lock->wait_lock, flags);

	/*
	 * Once more, try to acquire the lock. Only try-lock the mutex if
	 * it is unlocked to reduce unnecessary xchg() operations.
	 */
	/* 再次判定如果mutex没有被锁,并且count为1,则跳过等待 
     */
	if (!mutex_is_locked(lock) &&
	    (atomic_xchg_acquire(&lock->count, 0) == 1))
		goto skip_wait;

	debug_mutex_lock_common(lock, &waiter);
	debug_mutex_add_waiter(lock, &waiter, task);

	/* add waiting tasks to the end of the waitqueue (FIFO): */
	/* 将当前进程放入wait_list
	 * 这里是FIFO,即先等待的先获得mutex
	 */
	list_add_tail(&waiter.list, &lock->wait_list);
	waiter.task = task;

	lock_contended(&lock->dep_map, ip);

	for (;;) {
		/*
		 * Lets try to take the lock again - this is needed even if
		 * we get here for the first time (shortly after failing to
		 * acquire the lock), to make sure that we get a wakeup once
		 * it's unlocked. Later on, if we sleep, this is the
		 * operation that gives us the lock. We xchg it to -1, so
		 * that when we release the lock, we properly wake up the
		 * other waiters. We only attempt the xchg if the count is
		 * non-negative in order to avoid unnecessary xchg operations:
		 */
		/* 如果count为1,则意为着mutex可用,break */
		if (atomic_read(&lock->count) >= 0 &&
		    (atomic_xchg_acquire(&lock->count, -1) == 1))
			break;

		/*
		 * got a signal? (This code gets eliminated in the
		 * TASK_UNINTERRUPTIBLE case.)
		 */
		/* 获得到信号,就退出 */
		if (unlikely(signal_pending_state(state, task))) {
			ret = -EINTR;
			goto err;
		}

		if (use_ww_ctx && ww_ctx->acquired > 0) {
			ret = __ww_mutex_lock_check_stamp(lock, ww_ctx);
			if (ret)
				goto err;
		}

		/* 把当前进程设为非RUNNING */
		__set_task_state(task, state);

		/* didn't get the lock, go to sleep: */
		spin_unlock_mutex(&lock->wait_lock, flags);		// 解锁
		schedule_preempt_disabled();					// 开始调度
		spin_lock_mutex(&lock->wait_lock, flags);		// 被信号或者mutex_unlock唤醒,上锁
	}
	/* 设置当前进程为RUNNING */
	__set_task_state(task, TASK_RUNNING);

	/* 删除mutex中wait_list中的进程 */
	mutex_remove_waiter(lock, &waiter, task);

	/* set it to 0 if there are no waiters left: */
	/* 如果wait_list为空,表示已经没人等待这个mutex了。将count设为0 */
	if (likely(list_empty(&lock->wait_list)))
		atomic_set(&lock->count, 0);
	debug_mutex_free_waiter(&waiter);

skip_wait:
	/* got the lock - cleanup and rejoice! */
	lock_acquired(&lock->dep_map, ip);
	mutex_set_owner(lock);

	/* use_ww_ctx为0 */
	if (use_ww_ctx) {
		struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
		ww_mutex_set_context_slowpath(ww, ww_ctx);
	}

	spin_unlock_mutex(&lock->wait_lock, flags);
	preempt_enable();
	return 0;

err:
	mutex_remove_waiter(lock, &waiter, task);
	spin_unlock_mutex(&lock->wait_lock, flags);
	debug_mutex_free_waiter(&waiter);
	mutex_release(&lock->dep_map, 1, ip);
	preempt_enable();
	return ret;
}

3.2 mutex_unlock

mutex_unlock -> __mutex_fastpath_unlock

void __sched mutex_unlock(struct mutex *lock)
{
	/*
	 * The unlocking fastpath is the 0->1 transition from 'locked'
	 * into 'unlocked' state:
	 */
#ifndef CONFIG_DEBUG_MUTEXES
	/*
	 * When debugging is enabled we must not clear the owner before time,
	 * the slow path will always be taken, and that clears the owner field
	 * after verifying that it was indeed current.
	 */
	mutex_clear_owner(lock);
#endif
	__mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
}

EXPORT_SYMBOL(mutex_unlock);
static inline void
__mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *))
{
	/* count加1后如果count还是小于等于1,则表示设备当前有人等待。不能直接放回
	 * 需要调用slowpath
	 */
	if (unlikely(atomic_inc_return_release(count) <= 0))
		fail_fn(count);
}
__visible void
__mutex_unlock_slowpath(atomic_t *lock_count)
{
	struct mutex *lock = container_of(lock_count, struct mutex, count);

	__mutex_unlock_common_slowpath(lock, 1);
}
static inline void
__mutex_unlock_common_slowpath(struct mutex *lock, int nested)
{
	unsigned long flags;
	WAKE_Q(wake_q);

	/*
	 * As a performance measurement, release the lock before doing other
	 * wakeup related duties to follow. This allows other tasks to acquire
	 * the lock sooner, while still handling cleanups in past unlock calls.
	 * This can be done as we do not enforce strict equivalence between the
	 * mutex counter and wait_list.
	 *
	 *
	 * Some architectures leave the lock unlocked in the fastpath failure
	 * case, others need to leave it locked. In the later case we have to
	 * unlock it here - as the lock counter is currently 0 or negative.
	 */
	if (__mutex_slowpath_needs_to_unlock())
		atomic_set(&lock->count, 1);			// count被置为1

	spin_lock_mutex(&lock->wait_lock, flags);
	mutex_release(&lock->dep_map, nested, _RET_IP_);
	debug_mutex_unlock(lock);

	/* 从wait_list中取出第一个进程 */
	if (!list_empty(&lock->wait_list)) {
		/* get the first entry from the wait-list: */
		struct mutex_waiter *waiter =
				list_entry(lock->wait_list.next,
					   struct mutex_waiter, list);

		debug_mutex_wake_waiter(lock, waiter);
		wake_q_add(&wake_q, waiter->task);
	}

	spin_unlock_mutex(&lock->wait_lock, flags);
	wake_up_q(&wake_q);							// 唤醒wait进程
}

标签:count,__,同步,struct,lock,互斥,mutex,wait,21
From: https://www.cnblogs.com/burnk/p/17366529.html

相关文章

  • 2023年最新FL21水果音乐制作软件FL Studio 21中文版强悍来袭
    FLStudio21是一款无可挑剔并且适用于多种领域的音频编辑软件。这款软件支持多声道混音器和VST插件,包括上百种乐器和效果插件,还为大家提供了一个音符编辑器,可以根据作曲家的要求编辑不同乐器的节奏,如鼓、钹、锣、钢琴、筝、扬琴等的节奏。内置许多电子合成音色,只有斯泰鲁能让人兴......
  • CVE-2021-27239 漏洞复现
    在此感谢tolele师傅的帮助参考链接https://toleleyjl.github.io/2023/04/09/CVE-2021-27239%E6%BC%8F%E6%B4%9E%E5%A4%8D%E7%8E%B0%E8%AE%B0%E5%BD%95/https://toleleyjl.github.io/2023/02/16/CVE-2021-34991%E5%A4%8D%E7%8E%B0/https://xuanxuanblingbling.github.io/io......
  • CF521D Shop
    CF521DShop注意到选定的操作数可以少于\(m\),因此所有对乘积有负贡献的操作直接扔掉(在本题中,只有满足\(b_i<a_i\)的赋值操作对乘积是负贡献的)。假设我们框定了选择的操作集合,如何决定顺序?先做所有赋值操作,再做所有加操作,再做所有乘操作是最优的,而每种类型操作内部的顺序无......
  • cocoa线程同步synchronized
    synchronized关键字   1.synchronized方法:通过在方法声明中加入synchronized关键字来声明synchronized方法。如:publicsynchronizedvoidaccessVal(intnewVal);synchronized方法控制对类成员变量的访问:每个类实例对应一把锁,每个synchroni......
  • HDU 2108 Shape of HDU (判断凹凸)
    ShapeofHDUTimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):10350    AcceptedSubmission(s):4796ProblemDescription话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,“徐队”......
  • Linux用ntpdate自动同步时间后使用crontab定时任务更新
     两种同步linux系统的时间方法:(1)开启ntpd服务,即配置ntp服务器实现时间同步。(2)利用ntp客户端程序,即ntpdate同步时间。注意:因为ntp服务器本来就会与上层时间服务器进行同步,所以在默认的情况下,ntp服务器不可以使用ntpdate,也就是说ntpdate和ntpd不能同时启用。上一篇已经介绍......
  • 20 同步与互斥(六)semaphore
    1简介semaphore信号量;需要注意的是信号量不是信号。信号量是一种同步、互斥机制2semaphore的结构和API2.1semaphore结构structsemaphoresem;structsemaphore{ raw_spinlock_t lock; //semaphore借助与spinlock实现 unsignedint count; //允许多少人使......
  • 4 月 21 日测试题解
    4月21日测试题解T1\({\color{green}{\text{100pts}}}\text{/100pts}\)题意给出平面上的两条线段,求线段之间的距离。\(\text{|线段端点坐标|}\le10^4\)。思路一开始想的是分讨,但是又怕自己写挂了,所以就写了三分套三分。至少这个不怕少讨论一个情况。既然是三分套三分,......
  • 19 同步与互斥(五)自旋锁
    1简介自旋锁:通俗的说就是自己在原地打转,一直等待资源可用。不会休眠对临界资源的访问时,因为自旋锁的存在,某段临界资源被占用后。其他事件将无法访问,即进行所谓的自旋,“原地打转”注意:在自旋锁的临界区不能使用可能引起进程调度的函数(因为抢占已经被禁止了)。示例: copy_to_......
  • [CEOI2021] Newspapers
    模拟赛没有判\(n=1\),喜提\(0\)分。感谢每个subtask都放\(n=1\)的善良出题人。看到题感觉A的操作好像比较弱小,唯一的用处似乎只能用来排除B在哪些位置,那这样就有一个暴力了,直接记录当前还有哪些点上可能有B,然后直接跑bfs,就可以通过第一档分了。看到第二档分似乎比较......