这是在x86更好的写屏障:锁+ A​​DDL或xchgl? [英] Which is a better write barrier on x86: lock+addl or xchgl?

查看:202
本文介绍了这是在x86更好的写屏障:锁+ A​​DDL或xchgl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux内核使用锁; ADDL $ 0,0(%% ESP)为写屏障,而RE2库使用 xchgl(0%),0%为写屏障。有什么区别和哪个好?

The Linux kernel uses lock; addl $0,0(%%esp) as write barrier, while the RE2 library uses xchgl (%0),%0 as write barrier. What's the difference and which is better?

86是否也需要阅读障碍的指令? RE2定义它的读屏障功能作为一个无操作在x86而Linux将其定义为两种 lfence 或取决于SSE2是否可用无操作。如果是 lfence 要求?

Does x86 also require read barrier instructions? RE2 defines its read barrier function as a no-op on x86 while Linux defines it as either lfence or no-op depending on whether SSE2 is available. When is lfence required?

推荐答案

锁; ADDL $ 0,0(%% ESP)是我们测试锁的状态,0速度的情况下在(%% ESP)地址变量。因为我们添加0值来锁定变量和零标志被设置为1,如果在地址变量的锁值(%%尤)为0

The "lock; addl $0,0(%%esp)" is faster in case that we testing the 0 state of lock variable at (%%esp) address. Because we add 0 value to lock variable and the zero flag is set to 1 if the lock value of variable at address (%%esp) is 0.

lfence 英特尔数据表:

执行一个序列化操作上
  所有负载从内存中的指令,
  之前的LFENCE发行
  指令。这种序列化
  操作保证每一个负载
  这说明美元的程序P $ pcedes
  订购LFENCE指令
  任何负载之前全局可见
  后面的LFENCE指令
  指令是全局可见的。

Performs a serializing operation on all load-from-memory instructions that were issued prior the LFENCE instruction. This serializing operation guarantees that every load instruction that precedes in program order the LFENCE instruction is globally visible before any load instruction that follows the LFENCE instruction is globally visible.

例如:像'MOV'存储器写指令是原子(他们并不需要锁定preFIX),如果有正确对齐。但这个指令在CPU缓存正常执行,并且不会在这一刻所有其他线程全局可见,因为存储栅栏必须先pformed $ P $。

For instance: memory write instruction like 'mov' are atomic (they don't need lock prefix) if there are properly aligned. But this instruction is normally executed in CPU cache and will not be globally visible at this moment for all other threads, because memory fence must be preformed first.

编辑:

所以,这两个指令之间的主要区别是, xchgl 指令不会对有条件的标志任何影响。当然,我们可以测试是锁定CMPXCHG 指令锁变量的状态,但是,这仍然比更复杂的锁定加$ 1,0 指令。

So the main difference between these two instructions is that xchgl instruction will not have any effect on the conditional flags. Certainly we can test the lock variable state with lock cmpxchg instruction but this is still more complex than with lock add $0 instruction.

这篇关于这是在x86更好的写屏障:锁+ A​​DDL或xchgl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆