memory-barriers

Memory barriers and the TLB

风格不统一 提交于 2019-12-03 04:38:58
问题 Memory barriers guarantee that the data cache will be consistent. However, does it guarantee that the TLB will be consistent? I am seeing a problem where the JVM (java 7 update 1) sometimes crashes with memory errors (SIGBUS, SIGSEG) when passing a MappedByteBuffer between threads. e.g. final AtomicReference<MappedByteBuffer> mbbQueue = new AtomicReference<>(); // in a background thread. MappedByteBuffer map = raf.map(MapMode.READ_WRITE, offset, allocationSize); Thread.yield(); while (

Memory barriers in userspace? (Linux, x86-64)

可紊 提交于 2019-12-03 02:27:10
问题 It is easy to set memory barriers on the kernel side: the macros mb, wmb, rmb, etc. are always in place thanks to the Linux kernel headers. How to accomplish this on the user side? 回答1: Posix defines a number of functions as acting as memory barriers. Memory locations must not be concurrently accessed; to prevent this, use synchronization - and that synchronization will also work as a barrier. 回答2: You are looking for the full memory barrier atomic builtins of gcc. Please note the detail on

volatile variables and memory barrier in java

梦想与她 提交于 2019-12-02 21:15:29
I've got a data structure which consists of linked nodes. You can think of it as of a simple LinkedList. Each node of the list consists of some value and a next field pointing the other node or null if it is the last node. The first node works as a root, it has no value it only points to the next node. All the other nodes are practically immutable that is once they are created neither their value nor their next field change during lifetime, unless the structure is being disposed which relates to a specific situation. One (only one) thread adds new nodes to the front of the list. It is

Out of Order Execution and Memory Fences

放肆的年华 提交于 2019-12-02 20:50:24
I know that modern CPUs can execute out of order, However they always retire the results in-order, as described by wikipedia. "Out of Oder processors fill these "slots" in time with other instructions that are ready, then re-order the results at the end to make it appear that the instructions were processed as normal. " Now memory fences are said to be required when using multicore platforms, because owing to Out of Order execution, wrong value of x can be printed here. Processor #1: while f == 0 ; print x; // x might not be 42 here Processor #2: x = 42; // Memory fence required here f = 1 Now

Is the popular “volatile polled flag” pattern broken?

瘦欲@ 提交于 2019-12-02 20:47:07
Suppose that I want to use a boolean status flag for cooperative cancellation between threads. (I realize that one should preferably use CancellationTokenSource instead; that is not the point of this question.) private volatile bool _stopping; public void Start() { var thread = new Thread(() => { while (!_stopping) { // Do computation lasting around 10 seconds. } }); thread.Start(); } public void Stop() { _stopping = true; } Question : If I call Start() at 0s and Stop() at 3s on another thread, is the loop guaranteed to exit at the end of the current iteration at around 10s? The overwhelming

Memory barriers in userspace? (Linux, x86-64)

痞子三分冷 提交于 2019-12-02 17:22:00
It is easy to set memory barriers on the kernel side: the macros mb, wmb, rmb, etc. are always in place thanks to the Linux kernel headers. How to accomplish this on the user side? Posix defines a number of functions as acting as memory barriers. Memory locations must not be concurrently accessed; to prevent this, use synchronization - and that synchronization will also work as a barrier. You are looking for the full memory barrier atomic builtins of gcc. Please note the detail on the reference i gave here says, The [following] builtins are intended to be compatible with those described in the

Memory barriers and the TLB

痴心易碎 提交于 2019-12-02 17:08:52
Memory barriers guarantee that the data cache will be consistent. However, does it guarantee that the TLB will be consistent? I am seeing a problem where the JVM (java 7 update 1) sometimes crashes with memory errors (SIGBUS, SIGSEG) when passing a MappedByteBuffer between threads. e.g. final AtomicReference<MappedByteBuffer> mbbQueue = new AtomicReference<>(); // in a background thread. MappedByteBuffer map = raf.map(MapMode.READ_WRITE, offset, allocationSize); Thread.yield(); while (!inQueue.compareAndSet(null, map)); // the main thread. (more than 10x faster than using map() in the same

difference in mfence and asm volatile (“” : : : “memory”)

我的未来我决定 提交于 2019-12-02 16:57:26
As far as I have understood, mfence is a hardware memory barrier while asm volatile ("" : : : "memory") is a compiler barrier. But,can asm volatile ("" : : : "memory") be used in place of mfence. The reason I have got confused is this link Well, a memory barrier is only needed on architectures that have weak memory ordering. x86 and x64 don't have weak memory ordering. on x86/x64 all stores have a release fence and all loads have an acquire fence. so, you should only really need asm volatile ("" : : : "memory") For a good overview of both Intel and AMD as well as references to the relavent

Concurrent stores seen in a consistent order

半腔热情 提交于 2019-12-02 01:24:10
The Intel Architectures Software Developer's Manual, Aug. 2012, vol. 3A, sect. 8.2.2: Any two stores are seen in a consistent order by processors other than those performing the stores. But can this be so? The reason I ask is this: Consider a dual-core Intel i7 processor with HyperThreading. According to the Manual's vol. 1, Fig. 2-8, the i7's logical processors 0 and 1 share an L1/L2 cache, but its logical processors 2 and 3 share a different L1/L2 cache -- whereas all the logical processors share a single L3 cache. Suppose that logical processors 0 and 2 -- which do not share an L1/L2 cache

volatile vs memory barrier for interrupts

守給你的承諾、 提交于 2019-12-02 01:00:32
问题 Let x and y be variables that are shared between main code and interrupt code. My idea of volatile is that it is only and always needed for hardware variables and interrupt variables that are also used in main code. Every usage of x and y in the main code is guaranteed to be atomic by disabling interrupts. Do x and y really need to be volatile , or is it enough to put a memory barrier before using them to force reloading the variables from RAM? A) volatile bool x; volatile int y[100]; int