Linux教程

Linux----barrier

本文主要是介绍Linux----barrier,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

首先贴一段代码吧:

E:\整理分类\源码\linux-5.14.14\arch\arm64\kernel\process.c 

/*
 * Thread switching.
 */
__notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
				struct task_struct *next)
{
	struct task_struct *last;

	fpsimd_thread_switch(next); /* 切换浮点寄存器 */
	tls_thread_switch(next);  /* 切换线程本地存储相关寄存器 */
	hw_breakpoint_thread_switch(next);  /* 切换调试寄存器 */
	contextidr_thread_switch(next); /* 上下文标识符寄存器设置为下一个进程的进程号 */
	entry_task_switch(next);
	ssbs_thread_switch(next);
	erratum_1418040_thread_switch(prev, next);
	ptrauth_thread_switch_user(next);
	compat_thread_switch(next);

	/*
	 * Complete any pending TLB or cache maintenance on this CPU in case
	 * the thread migrates to a different CPU.
	 * This full barrier is also required by the membarrier system
	 * call.
	 */
	dsb(ish); /* 数据同步屏障,确保屏障前面的缓存维护操作和页表缓存维护操作执行完 */

	/*
	 * MTE thread switching must happen after the DSB above to ensure that
	 * any asynchronous tag check faults have been logged in the TFSR*_EL1
	 * registers.
	 */
	mte_thread_switch(next);
	/* avoid expensive SCTLR_EL1 accesses if no change */
	if (prev->thread.sctlr_user != next->thread.sctlr_user)
		update_sctlr_el1(next->thread.sctlr_user);

	/* the actual thread switch */
	last = cpu_switch_to(prev, next); /* 切换同用寄存器 */

	return last;
}
#define dsb(opt)    asm volatile("dsb " #opt : : : "memory")

主要看这个数据同步屏障

看这个关键字 volatile

D:\Program Files\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xkeycheck.h

#if defined(volatile)
#define volatile EMIT WARNING C4005
#error The C++ Standard Library forbids macroizing the keyword "volatile". \
Enable warning C4005 to find the forbidden define.
#endif // volatile

禁止编译器优化,指令按需执行,去gcc里面看看编译器优化:

我先研究下。。。

这篇关于Linux----barrier的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!