I have seen some doc about Linux memory barrier. It mentioned that there are read memory barrier and write memory barrier. It is easy for me to understand the meaning of the write memory barrier, I do not see necessity of the read memory barrier!
我看过一些关于Linux内存障碍的文档。它提到有读记忆障碍和写记忆障碍。我很容易理解写记忆障碍的含义,我看不出读记忆障碍的必要性!
Is it just to keep the read order of memory area? if the order is reverse I think the value still should be right, isn't it?
它只是为了保持内存区域的读取顺序吗?如果顺序相反的话,我认为这个值应该是对的,不是吗?
So is the read memory barrier : read operation barrier read operation or write operation barrier read operation?
那么读内存屏障:读操作屏障读操作还是写操作屏障读操作?
It confuses me!
它迷惑我!
2 个解决方案
#1
2
A read memory barrier forces reads before it to finish before reads after it. This is useful, for example, with memory mapped I/O, where a read from memory is actually a read from some device connected to the system.
读内存障碍迫使读之前完成,读之后完成。这很有用,例如,对于内存映射I/O,从内存读取实际上是连接到系统的某个设备的读取。
MMIO reads need to be performed in program order because they might have side effects on the device you are reading from. For example, if the device is streaming data, reading the value could cause the the device to replace it with a new value. It could also cause an action which may trigger a change at a different MMIO location. Therefore, reads to these two locations must be ordered, since the results depend on the order they are performed.
MMIO读取需要按程序顺序执行,因为它们可能对您正在读取的设备有副作用。例如,如果设备是流数据,读取该值可能导致设备用一个新值替换它。它还可能导致一个动作,该动作可能在不同的MMIO位置触发一个更改。因此,必须对这两个位置的读取进行排序,因为结果取决于执行它们的顺序。
#2
1
Even reads that have no side effects have to be ordered sometimes. Consider the following idiom:
即使是没有副作用的阅读,有时也需要下命令。考虑下面的成语:
extern some_flag_type message_is_ready_flag;
extern some message_type message_data;
while(message_is_ready_flag) // First read
continue;
... = message_data; // Second read
Assume some other thread will write to message_data first, and then writes to message_is_ready_flag.
假设其他一些线程将首先写入message_data,然后写入message_is_ready_flag。
If in the code above, the second read happens first, it might not read the intended value. Note that the writing thread needs to use a corresponding writer barrier. That's why the Linux documentation notes that "read barriers should normally be paired with write barriers".
如果在上面的代码中,第二次读取发生在前面,那么它可能不会读取预期的值。注意,写入线程需要使用相应的写入器屏障。这就是为什么Linux文档指出“读屏障通常应该与写屏障同时存在”。
#1
2
A read memory barrier forces reads before it to finish before reads after it. This is useful, for example, with memory mapped I/O, where a read from memory is actually a read from some device connected to the system.
读内存障碍迫使读之前完成,读之后完成。这很有用,例如,对于内存映射I/O,从内存读取实际上是连接到系统的某个设备的读取。
MMIO reads need to be performed in program order because they might have side effects on the device you are reading from. For example, if the device is streaming data, reading the value could cause the the device to replace it with a new value. It could also cause an action which may trigger a change at a different MMIO location. Therefore, reads to these two locations must be ordered, since the results depend on the order they are performed.
MMIO读取需要按程序顺序执行,因为它们可能对您正在读取的设备有副作用。例如,如果设备是流数据,读取该值可能导致设备用一个新值替换它。它还可能导致一个动作,该动作可能在不同的MMIO位置触发一个更改。因此,必须对这两个位置的读取进行排序,因为结果取决于执行它们的顺序。
#2
1
Even reads that have no side effects have to be ordered sometimes. Consider the following idiom:
即使是没有副作用的阅读,有时也需要下命令。考虑下面的成语:
extern some_flag_type message_is_ready_flag;
extern some message_type message_data;
while(message_is_ready_flag) // First read
continue;
... = message_data; // Second read
Assume some other thread will write to message_data first, and then writes to message_is_ready_flag.
假设其他一些线程将首先写入message_data,然后写入message_is_ready_flag。
If in the code above, the second read happens first, it might not read the intended value. Note that the writing thread needs to use a corresponding writer barrier. That's why the Linux documentation notes that "read barriers should normally be paired with write barriers".
如果在上面的代码中,第二次读取发生在前面,那么它可能不会读取预期的值。注意,写入线程需要使用相应的写入器屏障。这就是为什么Linux文档指出“读屏障通常应该与写屏障同时存在”。