Nugget
atomic.h
1 // Copyright (c) Electronic Arts Inc. All rights reserved.
4 
5 
6 #ifndef EASTL_ATOMIC_H
7 #define EASTL_ATOMIC_H
8 
9 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
10  #pragma once
11 #endif
12 
13 
15 //
16 // Below is the documentation of the API of the eastl::atomic<T> library.
17 // This includes class and free functions.
18 // Anything marked with a '+' in front of the name is an extension to the std API.
19 //
20 
21 
23 //
24 // eastl::atomic<T> memory_order API
25 //
26 // See below for full explanations on the memory orders and their guarantees.
27 //
28 // - eastl::memory_order_relaxed
29 // - eastl::memory_order_acquire
30 // - eastl::memory_order_release
31 // - eastl::memory_order_acq_rel
32 // - eastl::memory_order_seq_cst
33 // - +eastl::memory_order_read_depends
34 //
35 
36 
38 //
39 // eastl::atomic<T> class API
40 //
41 // All jargon and prerequisite knowledge is explained below.
42 //
43 // Unless otherwise specified all orders except read_depends is a valid order
44 // on the given operation.
45 // Unless otherwise specified all operations are valid on all types T.
46 // If no order is provided, seq_cst memory ordering is used for the operation.
47 //
48 // - atomic() : Value-initializes the underlying object as T{}.
49 //
50 // - atomic(T) : Initializes the underlying object with a copy of T.
51 //
52 // - T operator=(T) : Atomically assigns T as store(T, seq_cst).
53 //
54 // - is_lock_free() : true if the operations are lockfree. Always true for eastl.
55 //
56 // - store(T, order) : Atomically stores T affecting memory according to order.
57 // : Valid orders are relaxed, release, and seq_cst.
58 //
59 // - T load(order) : Atomically loads T affecting memory according to order.
60 // : Valid orders are relaxed, acquire, and seq_cst.
61 // : If T is a pointer type, read_depends is another valid order.
62 //
63 // - operator T() : Atomically loads T as load(T, seq_cst).
64 //
65 // - T exchange(T, order) : Atomically performs a RMW that replaces the current value with T.
66 // : Memory is affected according to order.
67 // : Returns the previous value stored before the RMW operation.
68 //
69 // - bool compare_exchange_weak(T&, T, successOrder, failOrder)
70 // : Atomically compares the value stored with that of T& and if equal replaces it with T.
71 // : This is a RMW operation.
72 // : If the comparison fails, loads the observed value into T&. This is a load operation.
73 // : Memory is affected in the RMW operation according to successOrder.
74 // : Memory is affected in the load operation according to failOrder.
75 // : failOrder cannot be a stronger order than successOrder.
76 // : Returns true or false if the comparison succeeded and T was stored into the atomic object.
77 // :
78 // : The weak variant may fail even if the observed value of the atomic object equals T&.
79 // : This can yield performance gains on platforms with ld/str exclusive pair instructions especially
80 // : when the compare_exchange operation is done in a loop.
81 // : Only the bool return value can be used to determine if the operation was successful.
82 //
83 // - bool compare_exchange_weak(T&, T, order)
84 // : Same as the above except that order is used for both the RMW and the load operation.
85 // : If order == acq_rel then the order of the load operation equals acquire.
86 // : If order == release then the order of the load operation equals relaxed.
87 //
88 // - bool compare_exchange_strong(T&, T, successOrder, failOrder)
89 // - bool compare_exchange_strong(T&, T, order)
90 // : This operation is the same as the above weak variants
91 // : expect that it will not fail spuriously if the value stored equals T&.
92 //
93 // The below operations are only valid for Integral types.
94 //
95 // - T fetch_add(T, order)
96 // : Atomically performs a RMW that increments the value stored with T.
97 // : Returns the previous value stored before the RMW operation.
98 // - T fetch_sub(T, order)
99 // : Atomically performs a RMW that decrements the value stored with T.
100 // : Returns the previous value stored before the RMW operation.
101 // - T fetch_and(T, order)
102 // : Atomically performs a RMW that bit-wise and's the value stored with T.
103 // : Returns the previous value stored before the RMW operation.
104 // - T fetch_or(T, order)
105 // : Atomically performs a RMW that bit-wise or's the value stored with T.
106 // : Returns the previous value stored before the RMW operation.
107 // - T fetch_xor(T, order)
108 // : Atomically performs a RMW that bit-wise xor's the value stored with T.
109 // : Returns the previous value stored before the RMW operation.
110 //
111 // - +T add_fetch(T, order)
112 // : Atomically performs a RMW that increments the value stored with T.
113 // : Returns the new updated value after the operation.
114 // - +T sub_fetch(T, order)
115 // : Atomically performs a RMW that decrements the value stored with T.
116 // : Returns the new updated value after the operation.
117 // - +T and_fetch(T, order)
118 // : Atomically performs a RMW that bit-wise and's the value stored with T.
119 // : Returns the new updated value after the operation.
120 // - +T or_fetch(T, order)
121 // : Atomically performs a RMW that bit-wise or's the value stored with T.
122 // : Returns the new updated value after the operation.
123 // - +T xor_fetch(T, order)
124 // : Atomically performs a RMW that bit-wise xor's the value stored with T.
125 // : Returns the new updated value after the operation.
126 //
127 // - T operator++/--()
128 // : Atomically increments or decrements the atomic value by one.
129 // : Returns the previous value stored before the RMW operation.
130 // : Memory is affected according to seq_cst ordering.
131 //
132 // - T ++/--operator()
133 // : Atomically increments or decrements the atomic value by one.
134 // : Returns the new updated value after the RMW operation.
135 // : Memory is affected according to seq_cst ordering.
136 //
137 // - T operator+=/-=/&=/|=/^=(T)
138 // : Atomically adds, subtracts, bitwise and/or/xor the atomic object with T.
139 // : Returns the new updated value after the operation.
140 // : Memory is affected according to seq_cst ordering.
141 //
142 //
143 // The below operations are only valid for Pointer types
144 //
145 // - T* fetch_add(ptrdiff_t val, order)
146 // : Atomically performs a RMW that increments the value store with sizeof(T) * val
147 // : Returns the previous value stored before the RMW operation.
148 // - T* fetch_sub(ptrdiff_t val, order)
149 // : Atomically performs a RMW that decrements the value store with sizeof(T) * val
150 // : Returns the previous value stored before the RMW operation.
151 //
152 // - +T* add_fetch(ptrdiff_t val, order)
153 // : Atomically performs a RMW that increments the value store with sizeof(T) * val
154 // : Returns the new updated value after the operation.
155 // - +T* sub_fetch(ptrdiff_t val, order)
156 // : Atomically performs a RMW that decrements the value store with sizeof(T) * val
157 // : Returns the new updated value after the operation.
158 //
159 // - T* operator++/--()
160 // : Atomically increments or decrements the atomic value by sizeof(T) * 1.
161 // : Returns the previous value stored before the RMW operation.
162 // : Memory is affected according to seq_cst ordering.
163 //
164 // - T* ++/--operator()
165 // : Atomically increments or decrements the atomic value by sizeof(T) * 1.
166 // : Returns the new updated value after the RMW operation.
167 // : Memory is affected according to seq_cst ordering.
168 //
169 //
170 // - +EASTL_ATOMIC_HAS_[len]BIT Macro Definitions
171 // These macros provide the ability to compile-time switch on the availability of support for the specific
172 // bit width of an atomic object.
173 // Example:
174 //
175 // #if defined(EASTL_ATOMIC_HAS_128BIT)
176 // #endif
177 //
178 // Indicates the support for 128-bit atomic operations on an eastl::atomic<T> object.
179 //
180 
181 
183 //
184 // eastl::atomic_flag class API
185 //
186 // Unless otherwise specified all orders except read_depends is a valid order
187 // on the given operation.
188 //
189 // - atomic_flag() : Initializes the flag to false.
190 //
191 // - clear(order)
192 // : Atomically stores the value false to the flag.
193 // : Valid orders are relaxed, release, and seq_cst.
194 //
195 // - bool test_and_set(order)
196 // : Atomically exchanges flag with true and returns the previous value that was held.
197 //
198 // - bool test(order)
199 // : Atomically loads the flag value.
200 // : Valid orders are relaxed, acquire, and seq_cst.
201 //
202 
203 
205 //
206 // eastl::atomic standalone free function API
207 //
208 // All class methods have a standalone free function that takes a pointer to the
209 // atomic object as the first argument. These functions just call the correct method
210 // on the atomic object for the given operation.
211 // These functions come in two variants, a non-explicit and an explicit variant
212 // that take on the form atomic_op() and atomic_op_explicit() respectively.
213 // The non-explicit variants take no order arguments and thus are all seq_cst.
214 // The explicit variants take an order argument.
215 // Only the standalone functions that do not have a class method equivalent pair will be
216 // documented here which includes all new extensions to the std API.
217 //
218 // - +compiler_barrier()
219 // : Read-Write Compiler Barrier.
220 // - +compiler_barrier_data_dependency(const T&)
221 // : Read-Write Compiler Barrier.
222 // : Applies a fake input dependency on const T& so the compiler believes said variable is used.
223 // : Useful for example when writing benchmark or testing code with local variables that must not get dead-store eliminated.
224 // - +cpu_pause()
225 // : Prevents speculative memory order violations in spin-wait loops.
226 // : Allows giving up core resources, execution units, to other threads while in spin-wait loops.
227 // - atomic_thread_fence(order)
228 // : Read docs below.
229 // - atomic_signal_fence(order)
230 // : Prevents reordering with a signal handler.
231 // - +atomic_load_cond(const eastl::atomic<T>*, Predicate)
232 // : continuously loads the atomic object until Predicate is true
233 // : will properly ensure the spin-wait loop is optimal
234 // : very useful when needing to spin-wait for some condition to be true which is common is many lock-free algorithms
235 // : Memory is affected according to seq_cst ordering.
236 // - +atomic_load_cond_explicit(const eastl::atomic<T>*, Predicate, Order)
237 // : Same as above but takes an order for how memory is affected
238 //
239 
240 
242 //
243 // Deviations from the standard. This does not include new features added:
244 //
245 // 1.
246 // Description: Atomics are always lock free
247 // Reasoning : We don't want people to fall into performance traps where implicit locking
248 // is done. If your user defined type is large enough to not support atomic
249 // instructions then your user code should do the locking.
250 //
251 // 2.
252 // Description: Atomic objects can not be volatile
253 // Reasoning : Volatile objects do not make sense in the context of eastl::atomic<T>.
254 // Use the given memory orders to get the ordering you need.
255 // Atomic objects have to become visible on the bus. See below for details.
256 //
257 // 3.
258 // Description: Consume memory order is not supported
259 // Reasoning : See below for the reasoning.
260 //
261 // 4.
262 // Description: ATOMIC_INIT() macros and the ATOMIC_LOCK_FREE macros are not implemented
263 // Reasoning : Use the is_lock_free() method instead of the macros.
264 // ATOMIC_INIT() macros aren't needed since the default constructor value initializes.
265 //
266 // 5.
267 // Description: compare_exchange failure memory order cannot be stronger than success memory order
268 // Reasoning : Besides the argument that it ideologically does not make sense that a failure
269 // of the atomic operation shouldn't have a stricter ordering guarantee than the
270 // success of it; if that is required then just make the whole operation stronger.
271 // This ability was added and allowed in C++17 only which makes supporting multiple
272 // C++ versions harder when using the compiler provided intrinsics since their behaviour
273 // is reliant on the C++ version being compiled. Also makes it harder to reason about code
274 // using these atomic ops since C++ versions vary the behaviour. We have also noticed
275 // that versions of compilers that say they support C++17 do not properly adhere to this
276 // new requirement in their intrinsics. Thus we will not support this.
277 //
278 // 6.
279 // Description: All memory orders are distinct types instead of enum values
280 // Reasoning : This will not affect how the API is used in user code.
281 // It allows us to statically assert on invalid memory orders since they are compile-time types
282 // instead of potentially runtime enum values.
283 // Allows for more efficient code gen without the use of switch statements or if-else conditionals
284 // on the memory order enum values on compilers that do not provide intrinsics that take in a
285 // memory order, such as MSVC, especially in debug and debug-opt builds.
286 //
287 
288 
290 //
291 // ******** DISCLAIMER ********
292 //
293 // This documentation is not meant to provide rigorous proofs on the memory models
294 // of specific architectures or the C++ memory model introduced in C++11. It is not
295 // meant to provide formal mathematical definitions and logic that shows that a given
296 // implementation adheres to the C++ memory model. This isn't meant to be some infallible
297 // oracle on memory models, barriers, observers, and architecture implementation details.
298 // What I do hope a reader gets out of this is the following. An understanding of the C++
299 // memory model and how that relates to implementations on various architectures. Various
300 // phenomena and ways that compilers and architectures can steer away from a sequentially
301 // consistent system. To provide examples on how to use this library with common patterns
302 // that will be seen in many code bases. Lastly I would like to provide insight and
303 // further readings into the lesser known topics that aren't shared outside people
304 // who live in this space and why certain things are done the way they are
305 // such as cumulativity of memory barriers as one example. Sometimes specifying barriers
306 // as LDLD/LDST/STST/STLD doesn't actually cut it, and finer grain semantics are needed
307 // to describe cumulativity of memory barriers.
308 //
309 // ******** Layout of the Documentation ********
310 //
311 // This document will first go through a variety of different hardware architectures with examples of the various kinds of
312 // reordering that is allowed by these architectures. We will use the memory barriers provided by the hardware to "fix" these
313 // examples.
314 // Then we will introduce the C++ memory model and revisit the examples using the platform agnostic abstract memory model to "fix"
315 // them.
316 // The hope here is that we get a sense of the various types of architectures and weak memory consistency provided by them and thus
317 // an appreciation for the design of the C++ abstract memory model.
318 //
319 // ******** REFERENCES ********
320 // [1] Dekker's mutual exclusion algorithm made RW-safe
321 // [2] Handling Memory Ordering in Multithreaded Applications with Oracle Solaris
322 // [3] Evaluating the Cost of Atomic Operations on Modern Architectures
323 // [4] A Tutorial Introduction to the ARM and POWER Relaxed Memory Models
324 // [5] Memory Barriers: a Hardware View for Software Hackers
325 // [6] Memory Model = Instruction Reordering + Store Atomicity
326 // [7] ArMOR: Defending Against Memory Consistency Model Mismatches in Heterogeneous Architectures
327 // [8] Weak Memory Models: Balancing Definitional Simplicity and Implementation Flexibility
328 // [9] Repairing Sequential Consistency in C/C++11
329 // [10] A high-level operational semantics for hardware weak memory models
330 // [11] x86-TSO: A Rigorous and Usable Programmer's Model for x86 Multiprocessors
331 // [12] Simplifying ARM Concurrency: Multicopy-Atomic Axiomatic and Operational Models for ARMv8
332 // [13] Mixed-size Concurrency: ARM, POWER, C/C++11, and SC
333 // [14] P0668R4: Revising the C++ memory model
334 // [15] Constructing a Weak Memory Model
335 // [16] The Superfluous Load Queue
336 // [17] P0190R1: Proposal for New memory_order_consume Definition
337 //
338 // ******** What does it mean to be Atomic? ********
339 //
340 // The word atomic has been overloaded and can mean a lot of different things depending on the context,
341 // so let's digest it.
342 //
343 // The first attribute for something to be atomic is that concurrent stores and loads
344 // must not tear or shear. This means if two threads write 0x01 and 0x02 at the same time
345 // then the only values that should ever be observed is 0x01 or 0x02. We can only see
346 // the whole write of 0x01 or 0x02, not 0x03 as an example. Many algorithms rely on
347 // this property; only very few such a Dekker's algorithm for mutual exclusion don't.
348 // Well actually a recent paper, [1], showed that Dekker's isn't safe without atomic
349 // loads and stores so this property is pretty fundamental and also hard to prove that
350 // your algorithm is safe without this property on loads and stores.
351 //
352 // We need to ensure the compiler emits a single load instruction.
353 // If we are doing 64-bit loads on a 32-bit platform, we need to ensure the load is one
354 // instruction instead of 2 32-bit loads into two registers.
355 // Another example is if we have this struct, struct { int32_t i; int32_t k; }, even on
356 // a 64-bit system we have to ensure the compiler does one 64-bit load and not two
357 // 32-bit loads for each individual member.
358 //
359 // We also need to ensure the correct instruction is emitted. A general load instruction
360 // to do a 64-bit load on a 32-bit platform may perform a 64-bit load but it may not
361 // be atomic, it may be turned into two 32-bit loads behind the scenes in the cpu.
362 // For example on ARMv7 we would have to use ldrexd not ldrd for 64-bit loads
363 // on a 32-bit ARMv7 core.
364 //
365 // An operation may be considered atomic if multiple sub-operations are done as one
366 // transactional unit. This is commonly known as a Read-Modify-Write, RMW, operation.
367 // Take a simple add operation; it is actually a load from memory into a register,
368 // a modification of said register and then a store back to memory. If two threads
369 // concurrently execute this add operation on the same memory location; any interleaving
370 // of the 3 sub-operations is possible. It is possible that if the initial value is 0,
371 // the result may be 1 because each thread executed in lockstep both loading 0, adding 1
372 // and then storing 1. A RMW operation may be considered atomic if the whole sequence of
373 // sub-operations are serialized as one transactional unit.
374 //
375 // Atomicity may also refer to the order in which memory operations are observed and the
376 // dependencies between memory operations to different memory locations. As a quick example
377 // into the very thing we will be deep diving into that is not very intuitive. If I do, [STORE(A, 2); STORE(B, 1);],
378 // in one thread and another thread does, [r0 = LOAD(B); r1 = LOAD(A);]; if r0 == 1, thus we observed
379 // the store to B, will we observe r1 == 2. Our intuition tells us that well A was stored
380 // first and then B, so if I read the new value of B then I must also read the new value
381 // of A since the store to A happened before B so if I can see B then I must be able to
382 // see everything before B which includes A.
383 // This highlights the ordering of memory operations and why memory barriers and memory
384 // models are so heavily attached to atomic operations because one could classify something
385 // is atomic if the dependency highlighted in the above example is allowed to be maintained.
386 //
387 // This is what people mean when you hear that volatile does NOT mean atomicity of the operation.
388 // Usually people imply a lot of implicit assumptions when they mark a variable as volatile.
389 // All volatile gives us is the ability to tell the compiler it may not assume anything
390 // about the state of that memory location. This means the compiler must always emit a load
391 // or store instruction, cannot perform constant folding, dead-store elimination, or
392 // do any sort of code movement on volatile variables.
393 //
394 // ******** Preliminary Basics ********
395 //
396 // It is expected that the reader understands what a cache is, how it is organized and how data
397 // is chunked into cachelines. It is helpful if the reader understands basic cache coherency
398 // protocols such as MSI or MESI.
399 // It is expected the reader understands alignment, especially natural alignment
400 // of the processor and why alignment is important for data access.
401 // The reader should have some understanding of how a processor executes instructions,
402 // basics of what Out-of-Order execution means and basics of what speculative execution means.
403 // It is expected that the reader has an understanding of threading, multi-threaded programming
404 // and the use of concurrency primitives such as mutexes.
405 // Memory Barrier, Barrier, Memory Fence and Fence are all interchangeable synonyms.
406 //
407 // Independent memory operations can be performed or observed, depending on your perspective,
408 // in any order as long as the local cpu thinks its execution is happening in program order.
409 // This can be a problem for inter-cpu communications and thus we need some way to enforce
410 // that the compiler does not reorder instructions and that the cpu also does not reorder
411 // instructions. This is what a barrier is, it is an enforcement of ordering on memory instructions,
412 // so as the name suggests a barrier. Barriers can be one-sided or both-sided which means
413 // the barrier enforces a partial order above or below or on both sides of said barrier.
414 //
415 // Processors will use tricks such as out-of-order execution, memory instruction buffering and
416 // combining, speculative loads and speculative execution, branch prediction and many types of caching even
417 // in various interconnects from the cpu to the memory itself. One key thing to note is that cpus
418 // do not physically reorder the instruction stream. Instructions are dispatched and retired
419 // in-order but executed out-of-order. Memory barriers will prevent these tricks from happening
420 // by controlling the interaction of multiple cpus.
421 //
422 // Compilers will morph your code and physically move instructions around as long as the program
423 // has the same observed behaviour. This is becoming increasingly true with more optimization techniques
424 // such as Link Time Optimization becoming the norm where once people assumed compilers couldn't assume
425 // something outside the given TU and now because they have the whole program view they know everything.
426 // This means the compiler does indeed alter the instruction stream
427 // and compiler barriers are a way to tell them to not move any memory instructions across the barrier.
428 // This does not prevent a compiler from doing optimizations such as constant folding, merging of
429 // overlapping loads, or even dead store elimination. Compiler barriers are also very cheap and
430 // have zero impact on anything that the compiler knows isn't visible in memory such as local variables
431 // whose addresses do not escape the function even if their address is taken. You can think of it
432 // in terms of a sequence point as used with "volatile" qualified variables to denote a place in code where
433 // things must be stable and the compiler doesn't cache any variables in registers or do any reordering.
434 //
435 // Memory Barriers come in many flavours that instill a partial or full ordering on memory operations.
436 // Some memory operations themselves have implicit ordering guarantees already, for example
437 // Total-Store Order, TSO, architectures like x86 guarantee that a store operation cannot be reordered with a
438 // previous store operation thus a memory barrier that only orders stores is not needed
439 // on this architecture other than ensuring the compiler doesn't do any shenanigans.
440 // Considering we have 4 permutations of memory operations; a common way to describe an ordering
441 // is via Load-Load/LDLD, Load-Store/LDST, Store-Store/STST or Store-Load/STLD notation. You read this
442 // notation as follows; STLD memory barrier means a load cannot be reordered with a previous store.
443 // For example, on TSO architecture we can say all stores provide a STST memory barrier,
444 // since a store cannot be reordered with a previous store.
445 //
446 // Memory Barriers in itself are not a magic bullet, they come with caveats that must be known.
447 // Each cpu architecture also has its own flavours and guarantees provided by said memory barriers.
448 // There is no guarantee that memory instructions specified before a memory barrier will complete,
449 // be written to memory or fully propagated throughout the rest of the system, when the memory barrier
450 // instruction completes. The memory barrier creates a point in that local cpus queue of memory instructions
451 // whereby they must not cross. There is no guarantee that using a memory barrier on one cpu will have
452 // any effect at all on another remote cpu's observed view of memory. This also implies that executing
453 // a memory barrier does not hinder, incur, stall or enforce any other cpus to serialize with each other cpu.
454 // In order for a remote cpu to observe the correct effects it must also use a matching memory barrier.
455 // This means code communicating in 2 threads through memory must both be employing the use of memory barriers.
456 // For example, a store memory barrier that only orders stores, STST, in one thread must be paired with a load memory barrier
457 // that only orders loads, LDLD, in the other thread trying to observe those stores in the correct order.
458 //
459 // ******** Memory Types && Devices ********
460 //
461 // eastl::atomic<T> and accompanying memory barriers ONLY ORDER MEMORY to cpu-to-cpu communication through whatever the
462 // processor designates as normal cacheable memory. It does not order memory to devices. It does not provide any DMA ordering guarantees.
463 // It does not order memory with other memory types such as Write Combining. It strictly orders memory only to shared memory that is used
464 // to communicate between cpus only.
465 //
466 // ******** Sequentially Consistent Machine ********
467 //
468 // The most intuitive as well as the model people naturally expect a concurrent system to have is Sequential Consistency.
469 // You may have or definitely have heard this term if you dealt with any type of distributed system. Lamport's definition
470 // articulates this consistency model the best.
471 // Leslie Lamport: "the result of any execution is the same as if the operations of all the processors were executed in some
472 // sequential order, and the operations of each individual processor appear in this sequence in the order
473 // specified by its program".
474 //
475 // A Sequentially Consistent machine is modelled as follows:
476 //
477 // ------------ ------------
478 // | Thread 0 | ... | Thread N |
479 // ------------ ------------
480 // | | | |
481 // | | | |
482 // ----------------------------------------
483 // | |
484 // | Shared Memory |
485 // | |
486 // ----------------------------------------
487 //
488 // This is a sequentially consistent machine. Each thread is executing instructions in program order which does loads and stores
489 // that are serialized in some order to the shared memory. This means all communication is done through the shared memory with one cpu
490 // doing one access at a time. This system has a couple key properties.
491 //
492 // 1. There is no local cpu memory reordering. Each cpu executes instructions in program order and all loads and stores must complete,
493 // be visible in the shared memory or be visible in a register before starting the next instruction.
494 // 2. Each memory operation becomes visible to all cpus at the same time. If a store hits the shared memory, then all subsequent loads
495 // from every other cpu will always see the latest store.
496 //
497 // A Sequentially Consistent machine has, Single-Copy Store Atomicity: All stores must become visible to all cores in the system at the same time.
498 //
499 // ******** Adding Caches ********
500 //
501 // Caches by nature implicitly add the potential for memory reordering. A centralized shared snoopy bus that we all learned in school
502 // makes it easy to implement sequential consistency with caches. Writes and reads are all serialized in a total order via the cache bus transaction
503 // ordering. Every modern day bus is not inorder, and most certainly not a shared centralized bus. Cache coherency guarantees that all memory operations
504 // will be propagated eventually to all parties, but it doesn't guarantee in what order or in what time frame. Once you add
505 // caches, various levels of caching and various interconnects between remote cpus, you inevitably run into the issue where
506 // some cpus observe the effects of a store before other cpus. Obviously we have weakly-ordered and strongly-ordered cpus with
507 // caches so why is that? The short answer is, where is the onus put, is it on the programmer or the hardware. Does the hardware
508 // have dependency tracking, is it able to determine when a memory order violation occurs such as rolling back its speculative execution
509 // and also how far along the chain of interconnects does the hardware wait before it determines that the memory operation has
510 // been acknowledged or is considered to satisfy its memory ordering guarantees. Again this is a very high level view of the system
511 // as a whole, but the takeaway is yes; caches do add the potential for reordering but other supporting hardware determines whether
512 // that is observable by the programmer. There is also some debate whether weakly-ordered processors are actually more performant
513 // than strongly-ordered cpus eluding to the fact that the hardware has a better picture of what is a violation versus the programmer
514 // having to emit far more barriers on weakly-ordered architectures in multi-threaded code which may actually not be needed because the
515 // hardware didn't commit a violation but it may have and we as the programmer cannot rely on may haves.
516 //
517 // ******** Store Buffers ********
518 //
519 // Obviously having all stores serialize results in unnecessary stalls. Store buffers alleviate this issue.
520 // Store buffers are simple fixed size structures that sit between the cpu and the memory hierarchy. This allows
521 // each cpu to record its write in the store buffer and then move onto the next instruction. The store buffer will
522 // eventually be flushed to the resulting memory hierarchy in FIFO order. How and when this flushing occurs is irrelevant to the
523 // understanding of a store buffer. A read from an address will grab the most recent write to the same address in the store buffer.
524 //
525 // The introduction of a store buffer is our first dive into weaker memory consistency. The addition of this hardware turns the consistency model weaker,
526 // into one that is commonly known as TSO, Total-Store Order. This is the exact model used by x86 cpus and we will see what this means
527 // and what new effects are observed with the addition of the store buffer. Below is a diagram of how the machine may now look.
528 // This type of store buffer is known as a FIFO store buffer, FIFO write buffer, or Load/Store Queue in some literature. This type of
529 // store buffer introduces STLD reordering but still prevents STST reordering. We will take a look at another type of store buffer later.
530 // Even with this store buffer, stores to the same address can still be merged so that only the latest store is written to the cache assuming
531 // no other intermediary stores happen. x86 cpus do write merging even for consecutive stores, i.e. storing to A and A+1 can be merged into one two-byte store.
532 //
533 // ------------ ------------
534 // | Thread 0 | ... | Thread N |
535 // ------------ ------------
536 // | | | |
537 // | | | |
538 // | Store | | Store |
539 // | Buffer | | Buffer |
540 // | | | |
541 // ----------------------------------------
542 // | |
543 // | Shared Memory |
544 // | |
545 // ----------------------------------------
546 //
547 // ---- Store-Buffering / Dekker's Example ----
548 // This is a very common litmus test that showcases the introduction of STLD reordering. It is called Store-Buffering example because it is the only weaker
549 // behaviour observed under TSO and also called Dekker's Example as it famously breaks Dekker's mutual exclusion algorithm.
550 //
551 // ---------------------------
552 // Initial State:
553 // x = 0; y = 0;
554 // ---------------------------
555 // Thread 0 | Thread 1
556 // ---------------------------
557 // STORE(x, 1) | STORE(y, 1)
558 // r0 = LOAD(y) | r1 = LOAD(x)
559 // ---------------------------
560 // Observed: r0 = 0 && r1 = 0
561 // ---------------------------
562 //
563 // We would normally assume that any interleaving of the two threads cannot possibly end up with both loads reading 0. We assume that the observed outcome
564 // of r0 = 0 && r1 = 0 to be impossible, clearly that is not the case. Let's start by understanding the example with no reordering possible. Both threads
565 // run and their first instruction is to write the value 1 into either x or y, the next instruction then loads from the opposite variable. This means no
566 // matter the interleaving, one of the loads always executes after the other thread's store to that variable.
567 // We could observe r0 = 1 && r1 = 1 if both threads execute in lockstep.
568 // We could observe r0 = 0 && r1 = 1 if thread 0 executes and then thread 1 executes.
569 // We could observe r0 = 1 && r1 = 0 if thread 1 executes and then thread 0 executes.
570 // Since the stores always execute before that load in the other thread, one thread must always at least observe a store, so let's see why store buffers break this.
571 //
572 // What will happen is that STORE(x, 1) is stored to the store buffer but not made globally visible yet.
573 // STORE(y, 1) is written to the store buffer and also is not made globally visible yet.
574 // Both loads now read the initial state of x and y which is 0. We got the r0 = 0 && r1 = 0 outcome and just observed a Store-Load reordering.
575 // It has appeared as if the loads have been reordered with the previous stores and thus executed before the stores.
576 // Notice even if we execute the instructions in order, a series of other hardware side effects made it appear as if the instructions have been reordered.
577 // We can solve this by placing a Store-Load barrier after the store and before the load as follows.
578 //
579 // ---------------------------
580 // Thread 0 | Thread 1
581 // ---------------------------
582 // STORE(x, 1) | STORE(y, 1)
583 // STLD BARRIER | STLD BARRIER
584 // r0 = LOAD(y) | r1 = LOAD(x)
585 // ---------------------------
586 //
587 // This STLD barrier effectively will flush the store buffer into the memory hierarchy ensuring all stores in the buffer are visible to all other cpus at the same time
588 // before executing the load instruction. Again nothing prevents a potential hardware from speculatively executing the load even with the STLD barrier, the hardware will have to do
589 // a proper rollback if it detected a memory order violation otherwise it can continue on with its speculative load. The barrier just delimits a stability point.
590 //
591 // Most hardware does not provide granular barrier semantics such as STLD. Most provide a write memory barrier which only orders stores, STST, a read memory barrier
592 // which only orders loads, LDLD, and then a full memory barrier which is all 4 permutations. So on x86 we will have to use the mfence, memory fence, instruction
593 // which is a full memory barrier to get our desired STLD requirements.
594 //
595 // TSO also has the property that we call, Multi-Copy Store Atomicity. This means a cpu sees its own stores before they become visible to other cpus,
596 // by forwarding them from the store buffer, but a store becomes visible to all other cpus at the same time when flushed from the store buffer.
597 //
598 //
599 // Let's look at a non-FIFO store buffer now as seen in ARM cpus as an example and we will use a standard Message Passing example to see how it manifests in even weaker consistency.
600 // A store buffer on ARM as an example allows write merging even with adjacent stores, is not a FIFO queue, any stores in the small hardware hash table may be ejected at any point
601 // due to a collision eviction or the availability of cachelines in the cache hierarchy meaning that stores may bypass the buffer entirely if that cacheline is already owned by that cpu.
602 // There is no guarantee that stores will be completed in order as in the FIFO case.
603 //
604 // ---------------------------
605 // Initial State:
606 // x = 0; y = 0;
607 // ---------------------------
608 // Thread 0 | Thread 1
609 // ---------------------------
610 // STORE(x, 1) | while(LOAD(y) == 0);
611 // STORE(y, 1) | r0 = LOAD(x)
612 // ---------------------------
613 // Observed: r0 = 0
614 // ---------------------------
615 //
616 // This is a classic Message Passing example that is very commonly used in production code. We store some values and then set a flag, STORE(y, 1) in this case.
617 // The other thread waits until the flag is observed and then reads the value out of x. If we observed the flag then we should obviously see all stores before the flag was set.
618 // Given our familiarity with TSO consistency above we know this definitely works on TSO and it is impossible to observe the load of x returning 0 under that consistency model.
619 // Let's see how this breaks with a non-FIFO store buffer.
620 //
621 // Thread 0 executes the STORE(x, 1) but the cacheline for x is not in thread 0's cache so we write to the store buffer and wait for the cacheline.
622 // Thread 1 executes the LOAD(y) and it also does not have y in its cacheline so it waits before completing the load.
623 // Thread 0 moves on to STORE(y, 1). It owns this cacheline, hypothetically, so it may bypass the store buffer and store directly to the cache.
624 // Thread 0 receives a message that Thread 1 needs y's cacheline, so it transfers the now modified cacheline to Thread 1.
625 // Thread 1 completes the load with the updated value of y = 1 and branches out of the while loop since we saw the new value of y.
626 // Thread 1 executes LOAD(x) which will return 0 since Thread 0 still hasn't flushed its store buffer waiting for x's cacheline.
627 // Thread 0 receives x's cacheline and now flushes x = 1 to the cache. Thread 1 will also have invalidated its cacheline for x that it brought in via the previous load.
628 //
629 // We have now fallen victim to STST reordering, allowing Thread 1 to observe a load of x returning 0. Not only does this store buffer allow STLD reordering due to the nature of
630 // buffering stores, but it also allows another reordering; that of Store-Store reordering. It was observed as if Thread 0 executed STORE(y, 1) before STORE(x, 1) which completely
631 // broke our simple message passing scenario.
632 //
633 // ---------------------------
634 // Thread 0 | Thread 1
635 // ---------------------------
636 // STORE(x, 1) | while(LOAD(y) == 0);
637 // STST BARRIER |
638 // STORE(y, 1) | r0 = LOAD(x)
639 // ---------------------------
640 //
641 // The STST memory barrier effectively ensures that the cpu will flush its store buffer before executing any subsequent stores. That is not entirely true, the cpu is still allowed
642 // to continue and execute stores to the store buffer as long as it doesn't flush them to the cache before the previous stores are flushed to the cache. If nothing becomes
643 // globally visible out of order then we are good.
644 // The example above will change how the processor executes due to the STST memory barrier. Thread 0 will execute STORE(y, 1), write to the store buffer and mark all current entries. Even though it owns the cacheline
645 // it cannot write the store to the cache until all marked entries, which are all the previous stores, are flushed to the cache. We have now fixed the message passing code by adding
646 // a STST or write memory barrier and thus it is no longer possible to observe the load of x returning 0.
647 //
648 // ******** Invalidation Queues ********
649 //
650 // Due to the cache coherency protocol in play, a write to a cacheline will have to send invalidation messages to all other cpus that may have that cacheline as well.
651 // Immediately executing and responding to invalidation messages can cause quite a stall especially if the cache is busy at the moment with other requests.
652 // The longer we wait to invalidate the cacheline, the longer the remote cpu doing the write is stalled waiting on us. We don't like this very much.
653 // Invalidation Queues are just that, we queue up the action of actually invalidating the cacheline but immediately respond to the request saying we did it anyway.
654 // Now the remote cpu thinks we invalidated said cacheline but actually it may very well still be in our cache ready to be read from. We just got weaker again, let's
655 // see how this manifests in code by starting from the end of our previous example.
656 //
657 // ---------------------------
658 // Initial State:
659 // x = 0; y = 0;
660 // ---------------------------
661 // Thread 0 | Thread 1
662 // ---------------------------
663 // STORE(x, 1) | while(LOAD(y) == 0);
664 // STST BARRIER |
665 // STORE(y, 1) | r0 = LOAD(x)
666 // ---------------------------
667 // Observed: r0 = 0
668 // ---------------------------
669 //
670 // Thread 1 receives the invalidate x's cacheline message and queues it because it is busy.
671 // Thread 1 receives the invalidate y's cacheline message, but we don't have that cacheline so acknowledge immediately.
672 // Thread 1 executes LOAD(y), loads in y's cacheline and branches out of the loop.
673 // Thread 1 executes LOAD(x), and loads from the cache the old value of x because the invalidation message is still sitting in the invalidation queue.
674 //
675 // We have just again observed the load of x returning 0 but from a different type of reordering now on the reader side.
676 // This is a form of LDLD, Load-Load, reordering as it appears as if LOAD(x) was executed before LOAD(y). This can be fixed as follows.
677 //
678 // ---------------------------
679 // Thread 0 | Thread 1
680 // ---------------------------
681 // STORE(x, 1) | while(LOAD(y) == 0);
682 // STST BARRIER | LDLD BARRIER
683 // STORE(y, 1) | r0 = LOAD(x)
684 // ---------------------------
685 //
686 // The LDLD memory barrier essentially marks all entries currently in the invalidation queue. Any subsequent load must wait until all the marked entries have been
687 // processed. This ensures once we observe y = 1, we process all entries that came before y and that way we observe all the stores that happened before y.
688 // The insertion of the read memory barrier creates the required memory barrier pairing as discussed above and ensures that now our code executes as expected.
689 //
690 // It must be made clear that these are not the only hardware structure additions or ways that can relax STST, STLD and LDLD orderings. These are merely
691 // 2 structures that are common and ones that I choose to use as examples of how hardware can reduce ordering guarantees. Knowing how the hardware does this
692 // isn't always entirely clear but having a model that tells us what operations can be reordered is all we need to be able to reason about our code when executing on that hardware.
693 //
694 // ******** Load Buffering ********
695 //
696 // The analog of the Store Buffering example, this litmus test has two threads read from two different locations and then write to the other locations.
697 // The outcome of having LDST reordering is allowed and observable on many processors such as ARM.
698 //
699 // ---------------------------
700 // Initial State:
701 // x = 0; y = 0;
702 // ---------------------------
703 // Thread 0 | Thread 1
704 // ---------------------------
705 // r0 = LOAD(x) | r1 = LOAD(y)
706 // STORE(y, 1) | STORE(x, 1)
707 // ---------------------------
708 // Observed: r0 = 1 && r1 = 1
709 // ---------------------------
710 //
711 // This is possible because the processor does not have to wait for the other cpu's cacheline to arrive before storing into the cache.
712 // Assume Thread 0 owns y's cacheline and Thread 1 owns x's cacheline.
713 // The processor may execute the load and thus buffer the load waiting for the cacheline to arrive.
714 // The processor may continue onto the store and since each cpu owns their respective cacheline, store the result into the cache.
715 // The cpus now receive the cachelines for x and y with the now modified value.
716 // We have just observed the loads returning 1 and thus observed LDST reordering.
717 //
718 // To forbid such outcome it suffices to add any full memory barrier to both threads or a local Read-After-Write/Read-To-Write dependency or a control dependency.
719 //
720 // -------------------------------
721 // Thread 0 | Thread 1
722 // -------------------------------
723 // r0 = LOAD(x) | r1 = LOAD(y)
724 // if (r0 == 1) | if (r1 == 1)
725 // STORE(y, 1) | STORE(x, 1)
726 // -------------------------------
727 //
728 // -----------------------------------------------------
729 // Thread 0 | Thread 1
730 // -----------------------------------------------------
731 // r0 = LOAD(x) | r1 = LOAD(y)
732 // STORE(&(y + r0 - r1), 1) | STORE(&(x + r1 - r1), 1)
733 // -----------------------------------------------------
734 //
735 // Both fixes above ensure that both writes cannot be committed, made globally visible, until their program source code order preceding reads have been fully satisfied.
736 //
737 // ******** Compiler Barriers ********
738 //
739 // Compiler barriers are both-sided barriers that prevent loads and stores from moving down past the compiler barrier and
740 // loads and stores from moving up above the compiler barrier. Here we will see the various ways our code may be subject
741 // to compiler optimizations and why compiler barriers are needed. Note as stated above, compiler barriers may not
742 // prevent all compiler optimizations or transformations. Compiler barriers are usually implemented by reloading all
743 // variables that are currently cached in registers and flushing all stores in registers back to memory.
744 // This list isn't exhaustive but will hopefully try to outline what compiler barriers protect against and what they don't.
745 //
746 // Compiler may reorder loads.
747 // LOAD A; LOAD B; -> LOAD B; LOAD A;
748 // LOAD A; operation on A; LOAD B; operation on B; -> LOAD A; LOAD B; operation on A; operation on B
749 //
750 // Insert a compiler barrier in between the two loads to guarantee that they are kept in order.
751 // LOAD A; COMPILER_BARRIER; LOAD B;
752 // LOAD A; operation on A; COMPILER_BARRIER; LOAD B; operation on B;
753 //
754 // The same with stores.
755 // STORE(A, 1); STORE(B, 1); -> STORE(B, 1); STORE(A, 1);
756 // operations and STORE result into A; operations and STORE result int B; -> all operations; STORE result into B; STORE result into A;
757 //
758 // Insert a compiler barrier in between the two stores to guarantee that they are kept in order.
759 // It is not required that the multiple stores to A before the barrier are not merged into one final store.
760 // It is not required that the store to B after the barrier be written to memory, it may be cached in a register for some indeterminate
761 // amount of time as an example.
762 // STORE(A, 1); COMPILER_BARRIER; STORE(B, 1);
763 //
764 // The compiler is allowed to merge overlapping loads and stores.
765 // Inserting a compiler barrier here will not prevent the compiler from doing this optimization as doing one wider load/store is
766 // technically still abiding by the guarantee that the loads/stores are not reordered with each other.
767 // LOAD A[0]; LOAD A[1]; -> A single wider LOAD instruction
768 // STORE(A[0], 1); STORE(A[1], 2); -> A single wider STORE instruction
769 //
770 // Compilers do not have to reload the values pointers point to. This is especially common with RISC architectures with lots
771 // of general purpose registers or even compiler optimizations such as inlining or Link-Time Optimization.
772 // int i = *ptr; Do bunch of operations; if (*ptr) { do more; }
773 // It is entirely possible the compiler may remove the last if statement because it can keep the *ptr in a register
774 // and it may infer from the operations done on i that i is never 0.
775 //
776 // int i = *ptr; Do bunch of operations; COMPILER_BARRIER; if (*ptr) { do more; }
777 // Inserting a compiler barrier at that location will cause the compiler to have reload *ptr thus keeping the if statement assuming
778 // no other optimizations take place, such as the compiler knowing that *ptr is always greater than 0.
779 //
780 // The compiler is within its rights to also merge and reload loads as much as it pleases.
781 //
782 // while (int tmp = LOAD(A))
783 // process_tmp(tmp)
784 //
785 // Will be merged and transformed to
786 //
787 // if (int tmp = LOAD(A))
788 // for (;;) process_tmp(tmp)
789 //
790 // Inserting a compiler barrier will ensure that LOAD(A) is always reloaded and thus the unwanted transformation is avoided.
791 //
792 // while (int tmp = LOAD(A))
793 // {
794 // process_tmp(tmp)
795 // COMPILER_BARRIER
796 // }
797 //
798 // Under heavy register pressure scenarios, say the loop body was larger, the compiler may reload A as follows.
799 // Compiler barriers cannot prevent this from happening, even if we put it after process_tmp as above;
800 // the compiler still kept those loads above the barrier so it satisfied its contract even though it reloaded
801 // from A more than once.
802 //
803 // while (int tmp = LOAD(A))
804 // process_tmp(LOAD(A))
805 //
806 // In the above transformation it is possible that another cpu stores 0 into A. When we reload A for process_tmp, we pass 0
807 // to process_tmp() which it would actually never expect to observe. Because if we observed 0, the while loop condition
808 // would never be satisfied. If the compiler under register pressure instead stored and loaded tmp from its stack slot, that is fine
809 // because we are just storing and loading the original observed value from A. Obviously that is slower than just reloading from
810 // A again so an optimizing compiler may not do the stack slot store. This is an unwanted transformation which eastl::atomic<T> prevents
811 // even on relaxed loads.
812 //
813 // The compiler is allowed to do dead-store elimination if it knows that value has already been stored, or that only the last store
814 // needs to be stored. The compiler does not assume or know that these variables are shared variables.
815 //
816 // STORE(A, 1); STORE(A, 1);
817 // OPERATIONS; -> OPERATIONS;
818 // STORE(A, 1);
819 //
820 // The compiler is well within its rights to omit the second store to A. Assuming we are doing some fancy lockfree communication
821 // with another cpu and the last store is meant to ensure the ending value is 1 even if another cpu changed A in between; that
822 // assumption will not be satisfied. A compiler barrier will not prevent the last store from being dead-store removed.
823 //
824 // STORE(A, 1);
825 // OPERATIONS;
826 // STORE(A, 2);
827 //
828 // Assuming these stores are meant to denote some state changes to communicate with a remote cpu. The compiler is allowed to
829 // transform this as follows without a compiler barrier. Insert a compiler barrier between the two stores to prevent the transformation.
830 // Something like this will also require memory barriers, but that is not the point of this section.
831 //
832 // STORE(A, 2);
833 // OPERATIONS;
834 //
835 // The compiler is also allowed to invent stores as it may please.
836 // First on many RISC architectures storing an immediate value either involves loading the immediate from the .data section
837 // or combing a variety of load upper immediate and add or or immediate instructions to get our constant in a register and then
838 // doing a single 32-bit store instruction from said register. Some ISAs have 16-bit stores with immediate value so that a store
839 // may be broken into 2 16-bit store immediate values causing shearing. To reduce instruction dependencies it may also decide
840 // to do two add immediates and then two 16-bit stores again causing shearing.
841 //
842 // lui $t0, 1 # t0 == 0x00010000
843 // ori $a0, $t0, 8 # t0 == 0x00010008
844 // strw $t0, 0($a1) # store t0 into address at a1
845 // ->
846 // ori $a0, $t0, 1 # t0 == 0x00000001
847 // ori $a0, $t1, 8 # t0 == 0x00000008
848 // strhw $t0, 0($a1) # store t0 lower half at a1
849 // strhw $t1, 2($a1) # store t1 upper half at a1
850 //
851 // The above shows a potential transformation that a compiler barrier cannot solve for us.
852 //
853 // A compiler may also introduce stores to save on branching. Let's see.
854 //
855 // if (a)
856 // STORE(X, 10);
857 // else
858 // STORE(X, 20);
859 //
860 // STORE(X, 20);
861 // if (a)
862 // STORE(X, 10);
863 //
864 // This is a very common optimization as it saves a potentially more expensive branch instruction but breaks multi-threaded code.
865 // This is also another case where a compiler barrier doesn't give us the granularity we need.
866 // The branches may even be completely removed with the compiler instead choosing to use conditional move operations which would
867 // actually be compliant since there would be one store only done, an extra store wouldn't have been added.
868 //
869 // You are now probably thinking that compiler barriers are useful and are definitely needed to tell the compiler to calm down
870 // and guarantee our hardware guarantees are valid because the code we wrote is the instructions that were emitted.
871 // But there are definitely lots of caveats where compiler barriers do not at all provide the guarantees we still need.
872 // This where eastl::atomic<T> comes into play, and under the relaxed memory ordering section it will be explained
873 // what the standard guarantees and how we achieve those guarantees, like ensuring the compiler never does dead-store elimination or reloads.
874 //
875 // ******** Control Dependencies ********
876 //
877 // Control dependencies are implicit local cpu ordering of memory instructions due to branching instructions, specifically
878 // only conditional branches. The problem is compilers do not understand control dependencies, and control dependencies
879 // are incredibly hard to understand. This is meant to make the reader aware they exist and to never use them
880 // because they shouldn't be needed at all with eastl::atomic<T>. Also control dependencies are categorized as LDLD or LDST,
881 // store control dependencies inherently do not make sense since the conditional branch loads and compares two values.
882 //
883 // A LDLD control dependency is an anti-pattern since it is not guaranteed that any architecture will detect the memory-order violation.
884 // r0 = LOAD(A);
885 // if (r0)
886 // r1 = LOAD(B)
887 //
888 // Given those sequence of instructions, it is entirely possible that a cpu attempts to speculatively predict and load the value of B
889 // before the branch instruction has finished executing. It is entirely allowed that the cpu loads from B, assume B is in cache and A
890 // is not in cache, before A. It is allowed, that even if the cpu was correct in it's prediction that it doesn't reload B and change the
891 // fact that it speculatively got lucky.
892 //
893 // This is also what the x86 pause instruction inserted into spin wait loops is meant to solve.
894 // LOOP:
895 // r0 = LOAD(A);
896 // if (!r0) pause; goto LOOP;
897 //
898 // In the above spin loop, after a couple of iterations the processor will fill the pipeline with speculated cmp and load instructions.
899 // x86 will catch a memory order violation if it sees that an external store was done to A and thus must flush the entire
900 // pipeline of all the speculated load A. Pause instruction tells the cpu to not do speculative loads so that the pipeline is not
901 // filled with all said speculative load instructions. This ensures we do not incur the costly pipeline flushes from memory order
902 // violations which are likely to occur in tight spin wait loops. This also allows other threads on the same physical core to use the
903 // core's resources better since our speculative nature won't be hogging it all.
904 //
905 // A LDST control dependency is a true dependency in which the cpu cannot make a store visible to the system and other cpus until it
906 // knows its prediction is correct. Thus a LDST ordering is guaranteed and can be always relied upon as in the following example.
907 //
908 // r0 = LOAD(A);
909 // if (r0)
910 // STORE(B, 1);
911 //
912 // The fun part comes in with how does the compiler actually break all of this.
913 // First is that if the compiler can ensure that the value of A in the LDST example is always not zero, then it is always within its
914 // rights to completely remove the if statement which would lend us with no control dependency.
915 //
916 // Things get more fun when we deal with conditionals with else and else if statements where the compiler might be able to employ
917 // invariant code motion optimizations. Take this example.
918 //
919 // r0 = LOAD(A);
920 // r1 = LOAD(B);
921 // if (r0)
922 // STORE(B, 1);
923 // /* MORE CODE */
924 // else if (r1)
925 // STORE(B, 1);
926 // /* MORE CODE */
927 // else
928 // STORE(B, 1);
929 // /* MORE CODE */
930 //
931 // If we were trying to be smart and entirely rely on the control dependency to ensure order, ya well just don't the compiler
932 // is always smarter. The compiler is well within its rights to move all the STORE(B, 1) up and above all the conditionals breaking
933 // our reliance on the LDST control dependency.
934 //
935 // Things can get even more complicated especially in C++ when values may come from constexpr, inline, inline constexpr, static const, etc,
936 // variables and thus the compiler will do all sorts of transformations to reduce, remove, augment and change all your conditional code since
937 // it knows the values of the expressions or even parts of it at compile time. Even more aggressive optimizations like LTO might break code that was being cautious.
938 // Even adding simple short circuiting logic or your classic likely/unlikely macros can alter conditionals in ways you didn't expect.
939 // In short know enough about control dependencies to know not to ever use them.
940 //
941 // ******** Multi-Copy Store Atomicity && Barrier Cumulativity ********
942 //
943 // Single-Copy Store Atomicity: All stores must become visible to all cores in the system at the same time.
944 //
945 // Multi-Copy Store Atomicity : This means a cpu sees its own stores before they become visible to other cpus, by forwarding them from the store buffer,
946 // but a store becomes visible to all other cpus at the same time when flushed from the store buffer.
947 //
948 // Non-Atomic Store Atomicity : A store becomes visible to different cpus at different times.
949 //
950 // Those are the above variations of Store Atomicity. Most processors have Non-Atomic Store Atomicity and thus you must program to that lowest common denominator.
951 // We can use barriers, with some caveats, to restore Multi-Copy Store Atomicity to a Non-Atomic system though we need to define a new granular definition for
952 // memory barriers to define this behaviour. Simple LDLD/LDST/STST/STLD definition is not enough to categorize memory barriers at this level. Let's start off
953 // with a simple example that breaks under a Non-Atomic Store Atomicity system and what potential hardware features allow this behaviour to be observed.
954 //
955 // NOTE: For all the below examples we assume no compile reordering and that the processor also executes the instructions with no local reorderings to make the examples simpler,
956 // to only show off the effects of Multi-Copy Store Atomicity. This is why we don't add any address dependencies, or mark explicit LDLD/LDST memory barriers.
957 // Thus you may assume all LDLD and LDST pairs have an address dependency between them, so that they are not reordered by the compiler or the local cpu.
958 //
959 // ---------------------------------------------------------------------------------------------------------
960 // Write-To-Read Causality, WRC, Litmus Test
961 // ---------------------------------------------------------------------------------------------------------
962 // Initial State:
963 // X = 0; Y = 0;
964 // ---------------------------------------------------------------------------------------------------------
965 // Thread 0 | Thread 1 | Thread 2
966 // ---------------------------------------------------------------------------------------------------------
967 // STORE(X, 1) | r0 = LOAD(X) | r1 = LOAD(Y)
968 // | STORE(Y, r0) | r2 = LOAD(X)
969 // ---------------------------------------------------------------------------------------------------------
970 // Observed: r0 = 1 && r1 = 1 && r2 = 0
971 // ---------------------------------------------------------------------------------------------------------
972 //
973 // Let's go over this example in detail and whether the outcome shown above can be observed. In this example Thread 0 stores 1 into X. If Thread 1 observes the write to X,
974 // it stores the observed value into Y. Thread 2 loads from Y then X. This means if the load from Y returns 1, then we intuitively know the global store order
975 // was 1 to X and then 1 to Y. So is it possible then that the load from X in Thread 2 can return 0 in that case? Under a Multi-Copy Store Atomicity system, that would be
976 // impossible because once 1 was stored to X all cpus see that store so if Thread 2 saw the store to Y which can only happen after the store to X was observed, then
977 // Thread 2 must also have observed the store to X and return 1. As you may well have figured out, it is possible under a Non-Atomic Store Atomicity system to still
978 // observe the load from X returning 0 even if the above load from Y returned 1 in Thread 2. This completely breaks our intuition of causality. Let's now understand what hardware may cause this.
979 //
980 // This is possible on cpus that have Simultaneous Multi-Threading, SMT or HyperThreading in Intel parlance, which share resources such as store buffers or L1 cache.
981 // We are accustomed to the x86 way of SMT where each logical core shares Execution Units on the physical core but each logical core has their own statically partitioned
982 // cache and store buffer that is not visible to the other cpus. It is possible on cpus like ARMv7 or POWER, POWER9 supports 4 and even 8 threads per physical core, so
983 // to save on die space though yet enable this large number of threads per physical core it is common for these logical cores to all use the same store buffer or L1 cache
984 // per physical core on these processors. Let's take the above example and rerun it with this knowledge to get the observed behaviour outlined above.
985 //
986 // Assume Thread 0, Thread 1, and Thread 2 run on cpu 0, cpu 1, and cpu 2 respectively. Assume that cpu 0 and cpu 1 are two logical cores on the same physical core so this processor
987 // has an SMT value of 2. Thread 0 will store 1 into X. This store may be in the store buffer or in the L1 cache that cpu 1 also shares with cpu 0, thus cpu 1 has early access to cpu 0's stores.
988 // Thread 1 loads X which it observed as 1 early and then stores 1 into Y. Thread 2 may see the load from Y returning 1 but now the load from X returning 0 all because cpu 1 got early
989 // access to cpu 0 store due to sharing a L1 cache or store buffer.
990 // We will come back on how to fix this example with the proper memory barriers for the Non-Atomic Store Atomicity systems, but we need to detour first.
991 //
992 // We need to take a deeper dive into memory barriers to understand how to restore Multi-Copy Store Atomicity from a Non-Atomic Store Atomicity system.
993 // Let's start with a motivating example and we will be using the POWER architecture throughout this example because it encompasses all the possible observable behaviour.
994 // ARMv7 technically allows Non-Atomic Store Atomicity behaviour but no consumer ARMv7 chip actually observes this behaviour.
995 // ARMv8 reworked its model to specifically say it is a Multi-Copy Store Atomicity system.
996 // POWER is one of the last few popular consumer architectures that are guaranteed to have Non-Atomic Store Atomicity observable behaviour, thus we will be using it for the following examples.
997 //
998 // To preface, POWER has two types of memory barriers called lwsync and sync. The following table lists the guarantees provided by TSO, x86, and the lwsync instruction.
999 // The table gives a hint as to why using our previous definition of LDLD/LDST/STST/STLD isn't granular enough to categorize memory barrier instructions.
1000 //
1001 // TSO: | POWER lwsync memory barrier:
1002 // LDLD : YES | LDLD : YES
1003 // LDST : YES | LDST : YES
1004 // STST : YES | STST : YES
1005 // STLD : NO | STLD : NO
1006 // A cumulative : YES | A cumulative : YES
1007 // B cumulative : YES | B cumulative : YES
1008 // IRIW : YES | IRIW : NO
1009 //
1010 // The TSO memory model provided by x86 seems to be exactly the same as POWER if we add lwsync memory barrier instructions in between each of the memory instructions.
1011 // This provides us the exact same ordering guarantees as the TSO memory model. If we just looked at the 4 permutations of reorderings we would be inclined to assume that
1012 // TSO has the exact same ordering as sprinkling lwsync in our code in between every pair of memory instructions. That is not the case because memory barrier causality and cumulativity differ in subtle ways.
1013 // In this case they differ by the implicit guarantees from the TSO memory model versus those provided by the POWER lwsync memory barrier.
1014 // So the lwsync memory barrier prevents reordering with instructions that have causality but does not prevent reordering with instructions that are completely independent.
1015 // Let's dive into these concepts a bit more.
1016 //
1017 // Non-Atomic Store Atomicity architectures are prone to behaviours such as the non-causal outcome of the WRC test above. Architectures such as POWER defines memory barriers to enforce
1018 // ordering with respect to memory accesses in remote cpus other than the cpu actually issuing the memory barrier. This is known as memory barrier cumulativity.
1019 // How does the memory barrier issued on my cpu affect the view of memory accesses done by remote cpuss.
1020 //
1021 // Cumulative memory barriers are defined as follows - Take your time this part is very non-trivial:
1022 // A-Cumulative: We denote group A as the set of memory instructions in this cpu or other cpus that are ordered before the memory barrier in this cpu.
1023 // A-Cumulativity requires that memory instructions from any cpu that have performed prior to a memory load before the memory barrier on this cpu are also members of group A.
1024 // B-Cumulative: We denote group B as the set of memory instructions in this cpu or other cpus that are ordered after the memory barrier in this cpu.
1025 // B-Cumulativity requires that memory instructions from any cpu that perform after a load and including the load in that cpu that returns the value of a store in group B are
1026 // also members of group B.
1027 // IRIW : enforces a global ordering even for memory instructions that have no causality. The memory instructions are completely independent.
1028 //
1029 // ---------------------------------------------------------------------------------------------------------
1030 // WRC Litmus Test
1031 // ---------------------------------------------------------------------------------------------------------
1032 // Thread 0 | Thread 1 | Thread 2
1033 // ---------------------------------------------------------------------------------------------------------
1034 // {i} : STORE(X, 1) | {ii} : r0 = LOAD(X) | {v} : r1 = LOAD(Y)
1035 // | {iii} : lwsync |
1036 // | {iv} : STORE(Y, r0) | {vi} : r2 = LOAD(X)
1037 // ---------------------------------------------------------------------------------------------------------
1038 // Outcome: r0 = 1 && r1 = 1 && r2 = 1
1039 //
1040 // Group A of {iii} : {i} && {ii}
1041 //
1042 // Group B of {iii} : {iv} && {v} && {vi}
1043 // ---------------------------------------------------------------------------------------------------------
1044 //
1045 // Using the WRC test again and inserting a POWER lwsync, don't concern yourself with why the memory barrier was inserted at that spot right now, we now see the distinctions of group A and group B.
1046 // It demonstrates the A and B Cumulative nature of the lwsync instruction, {iii}. First group A, initially consists of {ii} and group B initially consists of {iv} from the local cpu that issued the lwsync.
1047 // Since {ii} reads from {i} and assume {i} happens before {ii}, by definition of A-Cumulativity {i} is included in group A.
1048 // Similarly {v} reads from {iv} and assume {iv} happens before {v}, then {v} is included in group B by definition of B-Cumulativity.
1049 // {vi} is also included in group B since it happens after {v} by definition of B-Cumulativity.
1050 //
1051 // WRC litmus test represents a scenario where only a A-Cumulative memory barrier is needed. The lwsync not only provides the needed local LDST memory barrier for the local thread but also ensures
1052 // that any write Thread 1 has read from before the memory barrier is kept in order with any write Thread 1 does after the memory barrier as far as any other thread observes.
1053 // In other words it ensures that any write that has propagated to Thread 1 before the memory barrier is propagated to any other thread before the second store after the memory barrier in Thread 1
1054 // can propagate to other threads in the system. This is exactly the definition of A-Cumulativity and what we need to ensure that causality is maintained in the WRC Litmus Test example.
1055 // With that lwsync in place it is now impossible to observe r0 = 1 && r1 = 1 && r2 = 0. The lwsync has restored causal ordering. Let's look at an example that requires B-Cumulativity.
1056 //
1057 // ---------------------------------------------------------------------------------------------------------
1058 // Example 2 from POWER manual
1059 // ---------------------------------------------------------------------------------------------------------
1060 // Initial State:
1061 // X = 0; Y = 0; Z = 0
1062 // ---------------------------------------------------------------------------------------------------------
1063 // Thread 0 | Thread 1 | Thread 2
1064 // ---------------------------------------------------------------------------------------------------------
1065 // STORE(X, 1) | r0 = LOAD(Y) | r1 = LOAD(Z)
1066 // STORE(Y, 1) | STORE(Z, r0) | r2 = LOAD(X)
1067 // ---------------------------------------------------------------------------------------------------------
1068 // Observed: r0 = 1 && r1 = 1 && r2 = 0
1069 // ---------------------------------------------------------------------------------------------------------
1070 //
1071 // This example is very similar to WRC except that we kinda extended the Message Passing through an additional shared variable instead.
1072 // Think of this as Thread 0 writing some data into X, setting flag Y, Thread 1 waiting for flag Y then writing flag Z, and finally Thread 2 waiting for flag Z before reading the data.
1073 // Take a minute to digest the above example and think about where a memory barrier, lwsync, should be placed. Don't peek at the solution below.
1074 //
1075 // ---------------------------------------------------------------------------------------------------------
1076 // Example 2 from POWER manual
1077 // ---------------------------------------------------------------------------------------------------------
1078 // Thread 0 | Thread 1 | Thread 2
1079 // ---------------------------------------------------------------------------------------------------------
1080 // STORE(X, 1) | r0 = LOAD(Y) | r1 = LOAD(Z)
1081 // lwsync | |
1082 // STORE(Y, 1) | STORE(Z, r0) | r2 = LOAD(X)
1083 // ---------------------------------------------------------------------------------------------------------
1084 //
1085 // First the lwsync provides the needed local STST memory barrier for the local thread, thus the lwsync here ensures that the store to X propagates to Thread 1 before the store to Y.
1086 // B-Cumulativity applied to all operations after the memory barrier ensure that the store to X is
1087 // kept in order with respect to the store to Z as far as all other threads participating in the dependency chain are concerned. This is the exact definition of B-Cumulativity.
1088 // With this one lwsync the outcome outlined above is impossible to observe. If r0 = 1 && r1 = 1 then r2 must be properly observed to be 1.
1089 //
1090 // We know that lwsync only provides A-Cumulativity and B-Cumulativity. Now we will look at examples that have no causality constraints thus we need to grab heavier memory barriers
1091 // that ensures in short we will say makes a store become visible to all processors, even those not on the dependency chains. Let's get to the first example.
1092 //
1093 // ---------------------------------------------------------------------------------------------------------
1094 // Independent Reads of Independent Writes, IRIW, coined by Doug Lea
1095 // ---------------------------------------------------------------------------------------------------------
1096 // Initial State:
1097 // X = 0; Y = 0;
1098 // ---------------------------------------------------------------------------------------------------------
1099 // Thread 0 | Thread 1 | Thread 2 | Thread 3
1100 // ---------------------------------------------------------------------------------------------------------
1101 // STORE(X, 1) | r0 = LOAD(X) | STORE(Y, 1) | r2 = LOAD(Y)
1102 // | r1 = LOAD(Y) | | r3 = LOAD(X)
1103 // ---------------------------------------------------------------------------------------------------------
1104 // Observed: r0 = 1 && r1 = 0 && r2 = 1 && r3 = 0
1105 // ---------------------------------------------------------------------------------------------------------
1106 //
1107 // The IRIW example above clearly shows that writes can be propagated to different cpus in completely different orders.
1108 // Thread 1 sees the store to X but not the store to Y while Thread 3 sees the store to Y but not the store to X, the complete opposite.
1109 // Also to the keen eye you may have noticed this example is a slight modification of the Store Buffer example so try to guess where the memory barriers would go.
1110 //
1111 // ---------------------------------------------------------------------------------------------------------
1112 // Independent Reads of Independent Writes, IRIW, coined by Doug Lea
1113 // ---------------------------------------------------------------------------------------------------------
1114 // Thread 0 | Thread 1 | Thread 2 | Thread 3
1115 // ---------------------------------------------------------------------------------------------------------
1116 // STORE(X, 1) | r0 = LOAD(X) | STORE(Y, 1) | r2 = LOAD(Y)
1117 // | sync | | sync
1118 // | r1 = LOAD(Y) | | r3 = LOAD(X)
1119 // ---------------------------------------------------------------------------------------------------------
1120 //
1121 // To ensure that the above observation is forbidden we need to add a full sync memory barrier on both the reading threads. Think of sync as restoring sequential consistency.
1122 // The sync memory barrier ensures that any writes that Thread 1 has read from before the memory barrier are fully propagated to all threads before the reads are satisfied after the memory barrier.
1123 // The same can be said for Thread 3. This is why the sync memory barrier is needed because there is no partial causal ordering here or anything that can be considered for our A and B Cumulativity definitions.
1124 // We must ensure that all writes have been propagated to all cpus before proceeding. This gives way to the difference between sync and lwsync with regards to visibility of writes and cumulativity.
1125 // sync guarantees that all program-order previous stores must have been propagated to all other cpus before the memory instructions after the memory barrier.
1126 // lwsync does not ensure that stores before the memory barrier have actually propagated to any other cpu before memory instructions after the memory barrier, but it will keep stores before and after the
1127 // lwsync in order as far as other cpus are concerned that are within the dependency chain.
1128 //
1129 // Fun fact while ARMv7 claims to be Non-Atomic Store Atomicity no mainstream ARM implementation that I have seen has shown cases of Non-Atomic Store Atomicity.
1130 // It's allowed by the ARMv7 memory model and thus you have to program to that. ARMv8 changes this and states that it has Multi-Copy Store Atomicity.
1131 //
1132 // ******** Release-Acquire Semantics ********
1133 //
1134 // The most useful and common cases where Release-Acquire Semantics are used in every day code is in message passing and mutexes. Let's get onto some examples and the C++ definition of Release-Acquire.
1135 //
1136 // ACQUIRE:
1137 // An Acquire operation is a one-way memory barrier whereby all loads and stores after the acquire operation cannot move up and above the acquire operation.
1138 // Loads and stores before the acquire operation can move down past the acquire operation. An acquire operation should always be paired with a Release operation on the SAME atomic object.
1139 //
1140 // RELEASE:
1141 // A Release operation is a one-way memory barrier whereby all loads and stores before the release operation cannot move down and below the release operation.
1142 // Loads and stores after the release operation can move up and above the release operation. A release operation should always be paired with an Acquire operation on the SAME atomic object.
1143 //
1144 // Release-Acquire pair does not create a full memory barrier but it guarantees that all memory instructions before a Release operation on an atomic object M are visible after an Acquire
1145 // operation on that same atomic object M. Thus these semantics usually are enough to preclude the need for any other memory barriers.
1146 // The synchronization is established only between the threads Releasing and Acquiring the same atomic object M.
1147 //
1148 // ---------------------------------------------------
1149 // Critical Section
1150 // ---------------------------------------------------
1151 // Thread 0 | Thread 1
1152 // ---------------------------------------------------
1153 // mtx.lock() - Acquire | mtx.lock() - Acquire
1154 // STORE(X, 1) | r0 = LOAD(X)
1155 // mtx.unlock() - Release | mtx.unlock() - Release
1156 // ---------------------------------------------------
1157 //
1158 // A mutex only requires Release-Acquire semantics to protect the critical section. We do not care if operations above the lock leak into the critical section or that operations below the unlock leak into the
1159 // critical section because they are outside the protected region of the lock()/unlock() pair. Release-Acquire semantics does guarantee that everything inside the critical section cannot leak out.
1160 // Thus all accesses of all previous critical sections for the mutex are guaranteed to have completed and be visible when the mutex is handed off to the next party due to the Release-Acquire chaining.
1161 // This also means that mutexes do not provide or restore Multi-Copy Store Atomicity to any memory instructions outside the mutex, like the IRIW example since it does not emit full memory barriers.
1162 //
1163 // ------------------------------------------------------
1164 // Message Passing
1165 // ------------------------------------------------------
1166 // Thread 0 | Thread 1
1167 // ------------------------------------------------------
1168 // STORE(DATA, 1) | while (!LOAD_ACQUIRE(FLAG))
1169 // |
1170 // STORE_RELEASE(FLAG, 1) | r0 = LOAD(DATA)
1171 // ------------------------------------------------------
1172 //
1173 // This is a common message passing idiom that also shows the use of Release-Acquire semantics. It should be obvious by the definitions outlined above why this works.
1174 // An Acquire operation attached to a load needs to provide a LDLD and LDST memory barrier according to our definition of acquire. This is provided by default on x86 TSO thus no memory barrier is emitted.
1175 // A Release operation attached to a store needs to provide a STST and LDST memory barrier according to our definition of release. This is provided by default on x86 TSO thus no memory barrier is emitted.
1176 //
1177 // A couple of things of note here. One is that by attaching the semantics of a memory model directly to the memory instruction/operation itself we can take advantage of the fact the some processors
1178 // already provide guarantees between memory instructions and thus we do not have to emit memory barriers. Another thing of note is that the memory model is directly attached to the operation,
1179 // so you must do the Release-Acquire pairing on the SAME object which in this case is the FLAG variable. Doing an Acquire or Release on a separate object has no guarantee to observe an Acquire or Release on a different object.
1180 // This better encapsulates the meaning of the code and also allows the processor to potentially do more optimizations since a stand alone memory barrier will order all memory instructions of a given type before and after the barrier.
1181 // Where as the memory ordering attached to the load or store tells the processor that it only has to order memory instructions in relation to that specific load or store with the given memory order.
1182 //
1183 //
1184 // ---------------------------------------------------------------------------------------------------------
1185 // Release Attached to a Store VS. Standalone Fence
1186 // ---------------------------------------------------------------------------------------------------------
1187 // STORE(DATA, 1) | STORE(DATA, 1)
1188 // | ATOMIC_THREAD_FENCE_RELEASE()
1189 // STORE_RELEASE(FLAG, 1) | STORE_RELAXED(FLAG, 1)
1190 // STORE_RELAXED(VAR, 2) | STORE_RELAXED(VAR, 2)
1191 // ---------------------------------------------------------------------------------------------------------
1192 // ARMv8 Assembly
1193 // ---------------------------------------------------------------------------------------------------------
1194 // str 1, DATA | str 1, DATA
1195 // | dmb ish
1196 // stlr 1, FLAG | str 1, FLAG
1197 // str 2, VAR | str 2, VAR
1198 // ---------------------------------------------------------------------------------------------------------
1199 //
1200 // In the above example the release is attached to the FLAG variable, thus synchronization only needs to be guaranteed for that atomic variable.
1201 // It is entirely possible for the VAR relaxed store to be reordered above the release store.
1202 // In the fence version, since the fence is standalone, there is no notion where the release is meant to be attached to thus the fence must prevent all subsequent relaxed stores
1203 // from being reordered above the fence. The fence provides a stronger guarantee whereby now the VAR relaxed store cannot be moved up and above the release operation.
1204 // Also notice the ARMv8 assembly is different, the release fence must use the stronger dmb ish barrier instead of the dedicated release store instruction.
1205 // We dive more into fences provided by eastl::atomic<T> below.
1206 //
1207 // Release-Acquire semantics also have the property that it must chain through multiple dependencies which is where our knowledge from the previous section comes into play.
1208 // Everything on the Release-Acquire dependency chain must be visible to the next hop in the chain.
1209 //
1210 // ---------------------------------------------------------------------------------------------------------
1211 // Example 2 from POWER manual
1212 // ---------------------------------------------------------------------------------------------------------
1213 // Thread 0 | Thread 1 | Thread 2
1214 // ---------------------------------------------------------------------------------------------------------
1215 // STORE(X, 1) | r0 = LOAD_ACQUIRE(Y) | r1 = LOAD_ACQUIRE(Z)
1216 // STORE_RELEASE(Y, 1) | STORE_RELEASE(Z, r0) | r2 = LOAD(X)
1217 // ---------------------------------------------------------------------------------------------------------
1218 //
1219 // ---------------------------------------------------------------------------------------------------------
1220 // Write-To-Read Causality, WRC, Litmus Test
1221 // ---------------------------------------------------------------------------------------------------------
1222 // Thread 0 | Thread 1 | Thread 2
1223 // ---------------------------------------------------------------------------------------------------------
1224 // STORE(X, 1) | r0 = LOAD(X) | r1 = LOAD_ACQUIRE(Y)
1225 // | STORE_RELEASE(Y, r0) | r2 = LOAD(X)
1226 // ---------------------------------------------------------------------------------------------------------
1227 //
1228 // You may notice both of these examples from the previous section. We replaced the standalone POWER memory barrier instructions with Release-Acquire semantics attached directly to the operations where we want causality preserved.
1229 // We have transformed those examples to use the eastl::atomic<T> memory model.
1230 // Take a moment to digest these examples in relation to the definition of Release-Acquire semantics.
1231 //
1232 // The Acquire chain can be satisfied by reading the value from the store release or any later stored headed by that release operation. The following examples will make this clearer.
1233 //
1234 // ------------------------------------------------------
1235 // Release Sequence Headed
1236 // ------------------------------------------------------
1237 // Initial State:
1238 // DATA = 0; FLAG = 0;
1239 // ------------------------------------------------------
1240 // Thread 0 | Thread 1
1241 // ------------------------------------------------------
1242 // STORE(DATA, 1) | r0 = LOAD_ACQUIRE(FLAG)
1243 // |
1244 // STORE_RELEASE(FLAG, 1) | r1 = LOAD(DATA)
1245 // STORE_RELAXED(FLAG, 3) |
1246 // ------------------------------------------------------
1247 // Observed: r0 = 3 && r1 = 0
1248 // ------------------------------------------------------
1249 //
1250 // In the above example we may read the value 3 from FLAG which was not the release store, but it was headed by that release store. Thus we observed a later store and therefore it is still valid to then observe r1 = 1.
1251 // The stores to FLAG from the STORE_RELEASE up to but not including the next STORE_RELEASE operation make up the release sequence headed by the first release store operation. Any store on that sequence can be used to enforce
1252 // causality on the load acquire.
1253 //
1254 // ******** Consume is currently not useful ********
1255 //
1256 // Consume is a weaker form of an acquire barrier and creates the Release-Consume barrier pairing.
1257 // Consume states that a load operation on an atomic object M cannot allow any loads or stores dependent on the value loaded by the operation to be reordered before the operation.
1258 // To understand consume we must first understand dependent loads.
1259 // You might encounter this being called a data dependency or an address dependency in some literature.
1260 //
1261 // --------------------------------------------------------------
1262 // Address Dependency
1263 // --------------------------------------------------------------
1264 // Initial State:
1265 // DATA = 0; PTR = nullptr;
1266 // --------------------------------------------------------------
1267 // Thread 0 | Thread 1
1268 // --------------------------------------------------------------
1269 // STORE(DATA, 1) | r0 = LOAD(PTR) - typeof(r0) = int*
1270 // |
1271 // STORE(PTR, &DATA) | r1 = LOAD(r0) - typeof(r1) = int
1272 // --------------------------------------------------------------
1273 //
1274 // There is a clear dependency here where we cannot load from *int until we actually read the int* from memory.
1275 // Now it is possible for Thread 1's load from *ptr to be observed before the store to DATA, therefore it can lead to r0 = &DATA && r1 = 0.
1276 // While this is a failure of causality, it is allowed by some cpus such as the DEC Alpha and I believe Blackfin as well.
1277 // Thus a data dependency memory barrier must be inserted between the data dependent loads in Thread 1. Note that this would equate to a nop on any processor other than the DEC Alpha.
1278 //
1279 // This can occur for a variety of hardware reasons. We learned about invalidation queues. It is possible that the invalidation for DATA gets buffered in Thread 1. DEC Alpha allows the Thread 1
1280 // load from PTR to continue without marking the entries in its invalidation queue. Thus the subsequent load is allowed to return the old cached value of DATA instead of waiting for the
1281 // marked entries in the invalidation queue to be processed. It is a design decision of the processor not to do proper dependency tracking here and instead relying on the programmer to insert memory barriers.
1282 //
1283 // This data dependent ordering guarantee is useful because in places where we were using an Acquire memory barrier we can reduce it to this Consume memory barrier without any hardware barriers actually emitted on every modern processor.
1284 // Let's take the above example, translate it to Acquire and Consume memory barriers and then translate it to the ARMv7 assembly and see the difference.
1285 //
1286 // --------------------------------------------------------------- ---------------------------------------------------------------
1287 // Address Dependency - Release-Acquire Address Dependency - Release-Acquire - ARMv7 Assembly
1288 // --------------------------------------------------------------- ---------------------------------------------------------------
1289 // Thread 0 | Thread 1 Thread 0 | Thread 1
1290 // --------------------------------------------------------------- ---------------------------------------------------------------
1291 // STORE(DATA, 1) | r0 = LOAD_ACQUIRE(PTR) STORE(DATA, 1) | r0 = LOAD(PTR)
1292 // | dmb ish | dmb ish
1293 // STORE_RELEASE(PTR, &DATA) | r1 = LOAD(r0) STORE(PTR, &DATA) | r1 = LOAD(r0)
1294 // --------------------------------------------------------------- ---------------------------------------------------------------
1295 //
1296 // To get Release-Acquire semantics on ARMv7 we need to emit dmb ish; memory barriers.
1297 //
1298 // --------------------------------------------------------------- ---------------------------------------------------------------
1299 // Address Dependency - Release-Consume Address Dependency - Release-Consume - ARMv7 Assembly
1300 // --------------------------------------------------------------- ---------------------------------------------------------------
1301 // Thread 0 | Thread 1 Thread 0 | Thread 1
1302 // --------------------------------------------------------------- ---------------------------------------------------------------
1303 // STORE(DATA, 1) | r0 = LOAD_CONSUME(PTR) STORE(DATA, 1) | r0 = LOAD(PTR)
1304 // | dmb ish |
1305 // STORE_RELEASE(PTR, &DATA) | r1 = LOAD(r0) STORE(PTR, &DATA) | r1 = LOAD(r0)
1306 // --------------------------------------------------------------- ---------------------------------------------------------------
1307 //
1308 // Data Dependencies can not only be created by read-after-write/RAW on registers, but also by RAW on memory locations too. Let's look at some more elaborate examples.
1309 //
1310 // --------------------------------------------------------------- ---------------------------------------------------------------
1311 // Address Dependency on Registers - Release-Consume - ARMv7 Address Dependency on Memory - Release-Consume - ARMv7
1312 // --------------------------------------------------------------- ---------------------------------------------------------------
1313 // Thread 0 | Thread 1 Thread 0 | Thread 1
1314 // --------------------------------------------------------------- ---------------------------------------------------------------
1315 // STORE(DATA, 1) | r0 = LOAD(PTR) STORE(DATA, 1) | r0 = LOAD(PTR)
1316 // | r1 = r0 + 0 | STORE(TEMP, r0)
1317 // dmb ish | r2 = r1 - 0 dmb ish | r1 = LOAD(TEMP)
1318 // STORE(PTR, &DATA) | r3 = LOAD(r2) STORE(PTR, &DATA) | r2 = LOAD(r1)
1319 // --------------------------------------------------------------- ---------------------------------------------------------------
1320 //
1321 // The above shows a more elaborate example of how data dependent dependencies flow through RAW chains either through memory or through registers.
1322 //
1323 // Notice by identifying that this is a data dependent operation and asking for a consume ordering, we can completely eliminate the memory barrier on Thread 1 since we know ARMv7 does not reorder data dependent loads. Neat.
1324 // Unfortunately every major compiler upgrades a consume to an acquire ordering, because the consume ordering in the standard has a stronger guarantee and requires the compiler to do complicated dependency tracking.
1325 // Dependency chains in source code must be mapped to dependency chains at the machine instruction level until a std::kill_dependency in the source code.
1326 //
1327 // ----------------------------------------------------------------
1328 // Non-Address Dependency && Multiple Chains
1329 // ----------------------------------------------------------------
1330 // Initial State:
1331 // std::atomic<int> FLAG; int DATA[1] = 0;
1332 // ----------------------------------------------------------------
1333 // Thread 0 | Thread 1
1334 // ----------------------------------------------------------------
1335 // STORE(DATA[0], 1) | int f = LOAD_CONSUME(FLAG)
1336 // | int x = f
1337 // | if (x) return Func(x);
1338 // |
1339 // STORE_RELEASE(FLAG, 1) | Func(int y) return DATA[y - y]
1340 // ----------------------------------------------------------------
1341 //
1342 // This example is really concise but there is a lot going on. Let's digest it.
1343 // First is that the standard allows consume ordering even on what we will call not true machine level dependencies like a ptr load and then a load from that ptr as shown in the previous examples.
1344 // Here the dependency is between two ints, and the dependency chain on Thread 1 is as follows. f -> x -> y -> DATA[y - y]. The standard requires that source code dependencies on the loaded value
1345 // from consume flow thru assignments and even thru function calls. Also notice we added a dependency on the dereference of DATA with the value loaded from consume which while it does nothing actually abides by the standard
1346 // by enforcing a source code data dependent load on the consume operation. You may see this referred to as artificial data dependencies in other texts.
1347 // If we assume the compiler is able to track all these dependencies, the question is how do we enforce these dependencies at the machine instruction level. Let's go back to our ptr dependent load example.
1348 //
1349 // ----------------------------------------------------------------
1350 // addi r0, pc, offset;
1351 // ldr r1, 0(r0);
1352 // ldr r2, 0(r1);
1353 // ----------------------------------------------------------------
1354 //
1355 // The above pseudo assembly does a pc relative calculation to find the address of ptr. We then load ptr and then continue the dependency chain by loading the int from the loaded ptr.
1356 // Thus r0 has type of int**, which we use to load r1 an int* which we use to load our final value of r2 which is the int.
1357 // The key observation here is that most instructions provided by most architectures only allow moving from a base register + offset into a destination register.
1358 // This allows for trivial capturing of data dependent loads through pointers. But how do we capture the data dependency of DATA[y - y]. We would need something like this.
1359 //
1360 // ----------------------------------------------------------------
1361 // sub r1, r0, r0; // Assume r0 holds y from the Consume Operation
1362 // add r3, r1, r2; // Assume r2 holds the address of DATA[0]
1363 // ldr r4, 0(r3);
1364 // ----------------------------------------------------------------
1365 //
1366 // We cannot use two registers as both arguments to the load instruction. Thus to accomplish this you noticed we had to add indirect data dependencies through registers to compute the final address from the consume
1367 // load of y and then load from the final computed address. The compiler would have to recognize all these dependencies and enforce that they be maintained in the generated assembly.
1368 // The compiler must ensure the entire syntactic, source code, data-dependency chain is enforced in the generated assembly, no matter how long such chain may be.
1369 // Because of this and other issues, every major compiler unilaterally promotes consume to an acquire operation across the board. Read reference [15] for more information.
1370 // This completely removes the actual usefulness of consume for the pointer dependent case which is used quite heavily in concurrent read heavy data structures where updates are published via pointer swaps.
1371 //
1372 // ******** read_depends use case - Release-ReadDepends Semantics ********
1373 //
1374 // eastl::atomic<T> provides a weaker read_depends operation that only encapsulates the pointer dependency case above. Loading from a pointer and then loading the value from the loaded pointer.
1375 // The read_depends operation can be used on loads from only an eastl::atomic<T*> type. The return pointer of the load must and can only be used to then further load values. And that is it.
1376 // If you are unsure, upgrade this load to an acquire operation.
1377 //
1378 // MyStruct* ptr = gAtomicPtr.load(memory_order_read_depends);
1379 // int a = ptr->a;
1380 // int b = ptr->b;
1381 // return a + b;
1382 //
1383 // The loads from ptr after the gAtomicPtr load ensure that the correct values of a and b are observed. This pairs with a Release operation on the writer side by releasing gAtomicPtr.
1384 //
1385 //
1386 // As said above the returned pointer from a .load(memory_order_read_depends) can only be used to then further load values.
1387 // Dereferencing(*) and Arrow Dereferencing(->) are valid operations on return values from .load(memory_order_read_depends).
1388 //
1389 // MyStruct* ptr = gAtomicPtr.load(memory_order_read_depends);
1390 // int a = ptr->a; - VALID
1391 // int a = *ptr; - VALID
1392 //
1393 // Since dereferencing is just indexing via some offset from some base address, this also means addition and subtraction of constants is ok.
1394 //
1395 // int* ptr = gAtomicPtr.load(memory_order_read_depends);
1396 // int a = *(ptr + 1) - VALID
1397 // int a = *(ptr - 1) - VALID
1398 //
1399 // Casts also work correctly since casting is just offsetting a pointer depending on the inheritance hierarchy or if using intrusive containers.
1400 //
1401 // ReadDependsIntrusive** intrusivePtr = gAtomicPtr.load(memory_order_read_depends);
1402 // ReadDependsIntrusive* ptr = ((ReadDependsIntrusive*)(((char*)intrusivePtr) - offsetof(ReadDependsIntrusive, next)));
1403 //
1404 // Base* basePtr = gAtomicPtr.load(memory_order_read_depends);
1405 // Dervied* derivedPtr = static_cast<Derived*>(basePtr);
1406 //
1407 // Both of the above castings from the result of the load are valid for this memory order.
1408 //
1409 // You can reinterpret_cast the returned pointer value to a uintptr_t to set bits, clear bits, or xor bits but the pointer must be casted back before doing anything else.
1410 //
1411 // int* ptr = gAtomicPtr.load(memory_order_read_depends);
1412 // ptr = reinterpret_cast<int*>(reinterpret_cast<uintptr_t>(ptr) & ~3);
1413 //
1414 // Do not use any equality or relational operator (==, !=, >, <, >=, <=) results in the computation of offsets before dereferencing.
1415 // As we learned above in the Control Dependencies section, CPUs will not order Load-Load Control Dependencies. Relational and equality operators are often compiled using branches.
1416 // It doesn't have to be compiled to branched, condition instructions could be used. Or some architectures provide comparison instructions such as set less than which do not need
1417 // branches when using the result of the relational operator in arithmetic statements. Then again short circuiting may need to introduct branches since C++ guarantees the
1418 // rest of the expression must not be evaluated.
1419 // The following odd code is forbidden.
1420 //
1421 // int* ptr = gAtomicPtr.load(memory_order_read_depends);
1422 // int* ptr2 = ptr + (ptr >= 0);
1423 // int a = *ptr2;
1424 //
1425 // Only equality comparisons against nullptr are allowed. This is becase the compiler cannot assume that the address of the loaded value is some known address and substitute our loaded value.
1426 // int* ptr = gAtomicPtr.load(memory_order_read_depends);
1427 // if (ptr == nullptr); - VALID
1428 // if (ptr != nullptr); - VALID
1429 //
1430 // Thus the above sentence that states:
1431 // The return pointer of the load must and can only be used to then further load values. And that is it.
1432 // must be respected by the programmer. This memory order is an optimization added for efficient read heavy pointer swapping data structures. IF you are unsure, use memory_order_acquire.
1433 //
1434 // ******** Relaxed && eastl::atomic<T> guarantees ********
1435 //
1436 // We saw various ways that compiler barriers do not help us and that we need something more granular to make sure accesses are not mangled by the compiler to be considered atomic.
1437 // Ensuring these guarantees like preventing dead-store elimination or the splitting of stores into smaller sub stores is where the C/C++11
1438 // standard comes into play to define what it means to operate on an atomic object.
1439 // These basic guarantees are provided via new compiler intrinsics on gcc/clang that provide explicit indication to the compiler.
1440 // Or on msvc by casting the underlying atomic T to a volatile T*, providing stronger compiler guarantees than the standard requires.
1441 // Essentially volatile turns off all possible optimizations on that variable access and ensures all volatile variables cannot be
1442 // reordered across sequence points. Again we are not using volatile here to guarantee atomicity, we are using it in its very intended purpose
1443 // to tell the compiler it cannot assume anything about the contents of that variable. Now let's dive into the base guarantees of eastl::atomic<T>.
1444 //
1445 // The standard defines the following for all operations on an atomic object M.
1446 //
1447 // Write-Write Coherence:
1448 // If an operation A modifies an atomic object M(store), happens before an operation B that modifies M(store), then A shall be earlier than B in the modification order of M.
1449 //
1450 // Read-Read Coherence:
1451 // If a value computation A on an atomic object M(load), happens before a value computation B on M(load), and A takes its value from a side effect X on M(from a previous store to M), then the value
1452 // computed by B shall either be the value stored by X or some later side effect Y on M, where Y follows X in the modification order of M.
1453 //
1454 // Read-Write Coherence:
1455 // If a value computation A on an atomic object M(load), happens before an operation B that modifies M(store), then A shall take its value from a side effect X on M, where X precedes B in the modification
1456 // order of M.
1457 //
1458 // Write-Read Coherence:
1459 // If a side effect X on an atomic object M(store), happens before a value computation B on M(load), then the evaluation of B must take its value from X or from some side effect Y that follows X in the
1460 // modification order of M.
1461 //
1462 // What does all this mean. This is just a pedantic way of saying that the preceding coherence requirements disallow compiler reordering of atomic operations to a single atomic object.
1463 // This means all operations must be emitted by the compiler. Stores cannot be dead-store eliminated even if they are the only stores.
1464 // Loads cannot have common subexpression elimination performed on them even if they are the only loads.
1465 // Loads and Stores to the same atomic object cannot be reordered by the compiler.
1466 // Compiler cannot introduce extra loads or stores to the atomic object.
1467 // Compiler also cannot reload from an atomic object, it must save and store to a stack slot.
1468 // Essentially this provides all the necessary guarantees needed when treating an object as atomic from the compilers point of view.
1469 //
1470 // ******** Same Address LoadLoad Reordering ********
1471 //
1472 // It is expected that same address operations cannot and are not reordered with each other. It is expected that operations to the same address have sequential consistency because
1473 // they are to the same address. If you picture a cpu executing instructions, how is it possible to reorder instructions to the same address and yet keep program behaviour the same.
1474 // Same Address LoadLoad Reordering is one weakening that is possible to do and keep observed program behaviour for a single-threaded program.
1475 // More formally, A and B are two memory instructions onto the same address P, where A is program ordered before B. If A and B are both loads then their order need not be ordered.
1476 // If B is a store then it cannot retire the store before A instruction completes. If A is a store and B is a load, then B must get its value forwarded from the store buffer or observe a later store
1477 // from the cache. Thus Same Address LDST, STST, STLD cannot be reordered but Same Address LDLD can be reordered.
1478 // Intel Itanium and SPARC RMO cpus allow and do Same Address LoadLoad Reordering.
1479 // Let's look at an example.
1480 //
1481 // ---------------------------
1482 // Same Address LoadLoad
1483 // ---------------------------
1484 // Initial State:
1485 // x = 0;
1486 // ---------------------------
1487 // Thread 0 | Thread 1
1488 // ---------------------------
1489 // STORE(x, 1) | r0 = LOAD(x)
1490 // | r1 = LOAD(x)
1491 // ---------------------------
1492 // Observed: r0 = 1 && r0 = 0
1493 // ---------------------------
1494 //
1495 // Notice in the above example it has appeared as if the two loads from the same address have been reordered. If we first observed the new store of 1, then the next load should not observe a value in the past.
1496 // Many programmers, expect same address sequential consistency, all accesses to a single address appear to execute in a sequential order.
1497 // Notice this violates the Read-Read Coherence for all atomic objects defined by the std and thus provided by eastl::atomic<T>.
1498 //
1499 // All operations on eastl::atomic<T> irrelevant of the memory ordering of the operation provides Same Address Sequential Consistency since it must abide by the coherence rules above.
1500 //
1501 // ******** eastl::atomic_thread_fence ********
1502 //
1503 // eastl::atomic_thread_fence(relaxed) : Provides no ordering guarantees
1504 // eastl::atomic_thread_fence(acquire) : Prevents all prior loads from being reordered with all later loads and stores, LDLD && LDST memory barrier
1505 // eastl::atomic_thread_fence(release) : Prevents all prior loads and stores from being reordered with all later stores, STST && LDST memory barrier
1506 // eastl::atomic_thread_fence(acq_rel) : Union of acquire and release, LDLD && STST && LDST memory barrier
1507 // eastl::atomic_thread_fence(seq_cst) : Full memory barrier that provides a single total order
1508 //
1509 // See Reference [9] and Fence-Fence, Atomic-Fence, Fence-Atomic Synchronization, Atomics Order and Consistency in the C++ std.
1510 //
1511 // ******** Atomic && Fence Synchronization ********
1512 //
1513 // ---------------------------
1514 // Fence-Fence Synchronization
1515 // ---------------------------
1516 // A release fence A synchronizes-with an acquire fence B if there exist operations X and Y on the same atomic object M, such that fence A is sequenced-before operation X and X modifies M,
1517 // operation Y is sequenced-before B and Y reads the value written by X.
1518 // In this case all non-atomic and relaxed atomic stores that are sequenced-before fence A will happen-before all non-atomic and relaxed atomic loads after fence B.
1519 //
1520 // ----------------------------
1521 // Atomic-Fence Synchronization
1522 // ----------------------------
1523 // An atomic release operation A on atomic object M synchronizes-with an acquire fence B if there exists some atomic operation X on atomic object M, such that X is sequenced-before B and reads
1524 // the value written by A.
1525 // In this case all non-atomic and relaxed atomic stores that are sequenced-before atomic release operation A will happen-before all non-atomic and relaxed atomic loads after fence B.
1526 //
1527 // ----------------------------
1528 // Fence-Atomic Synchronization
1529 // ----------------------------
1530 // A release fence A synchronizes-with an atomic acquire operation B on an atomic object M if there exists an atomic operation X such that A is sequenced-before X, X modifies M and B reads the
1531 // value written by X.
1532 // In this case all non-atomic and relaxed atomic stores that are sequenced-before fence A will happen-before all non-atomic and relaxed atomic loads after atomic acquire operation B.
1533 //
1534 // This can be used to add synchronization to a series of several relaxed atomic operations, as in the following trivial example.
1535 //
1536 // ----------------------------------------------------------------------------------------
1537 // Initial State:
1538 // x = 0;
1539 // eastl::atomic<int> y = 0;
1540 // z = 0;
1541 // eastl::atomic<int> w = 0;
1542 // ----------------------------------------------------------------------------------------
1543 // Thread 0 | Thread 1
1544 // ----------------------------------------------------------------------------------------
1545 // x = 2 | r0 = y.load(memory_order_relaxed);
1546 // z = 2 | r1 = w.load(memory_order_relaxed);
1547 // atomic_thread_fence(memory_order_release); | atomic_thread_fence(memory_order_acquire);
1548 // y.store(1, memory_order_relaxed); | r2 = x
1549 // w.store(1, memory_order_relaxed); | r3 = z
1550 // ----------------------------------------------------------------------------------------
1551 // Observed: r0 = 1 && r1 = 1 && r2 = 0 && r3 = 0
1552 // ----------------------------------------------------------------------------------------
1553 //
1554 // ******** Atomic vs Standalone Fence ********
1555 //
1556 // A sequentially consistent fence is stronger than a sequentially consistent operation because it is not tied to a specific atomic object.
1557 // An atomic fence must provide synchronization with ANY atomic object whereas the ordering on the atomic object itself must only provide
1558 // that ordering on that SAME atomic object. Thus this can provide cheaper guarantees on architectures with dependency tracking hardware.
1559 // Let's look at a concrete example that will make this all clear.
1560 //
1561 // ----------------------------------------------------------------------------------------
1562 // Initial State:
1563 // eastl::atomic<int> y = 0;
1564 // eastl::atomic<int> z = 0;
1565 // ----------------------------------------------------------------------------------------
1566 // Thread 0 | Thread 1
1567 // ----------------------------------------------------------------------------------------
1568 // z.store(2, memory_order_relaxed); | r0 = y.load(memory_order_relaxed);
1569 // atomic_thread_fence(memory_order_seq_cst); | atomic_thread_fence(memory_order_seq_cst);
1570 // y.store(1, memory_order_relaxed); | r1 = z.load(memory_order_relaxed);
1571 // ----------------------------------------------------------------------------------------
1572 // Observed: r0 = 1 && r1 = 0
1573 // ----------------------------------------------------------------------------------------
1574 //
1575 // Here the two sequentially consistent fences synchronize-with each other thus ensuring that if we observe r0 = 1 then we also observe that r1 = 2.
1576 // In the above example if we observe r0 = 1 it is impossible to observe r1 = 0.
1577 //
1578 // ----------------------------------------------------------------------------------------
1579 // Initial State:
1580 // eastl::atomic<int> x = 0;
1581 // eastl::atomic<int> y = 0;
1582 // eastl::atomic<int> z = 0;
1583 // ----------------------------------------------------------------------------------------
1584 // Thread 0 | Thread 1
1585 // ----------------------------------------------------------------------------------------
1586 // z.store(2, memory_order_relaxed); | r0 = y.load(memory_order_relaxed);
1587 // x.fetch_add(1, memory_order_seq_cst); | x.fetch_add(1, memory_order_seq_cst);
1588 // y.store(1, memory_order_relaxed); | r1 = z.load(memory_order_relaxed);
1589 // ----------------------------------------------------------------------------------------
1590 // Observed: r0 = 1 && r1 = 0
1591 // ----------------------------------------------------------------------------------------
1592 //
1593 // Here the two fetch_add sequentially consistent operations on x synchronize-with each other ensuring that if we observe r0 = 1 then we cannot observer r1 = 0;
1594 // The thing to take note here is that we synchronized on the SAME atomic object, that being the atomic object x.
1595 // Note that replacing the x.fetch_add() in Thread 1 with a sequentially consistent operation on another atomic object or a sequentially consistent fence can lead to
1596 // observing r1 = 0 even if we observe r0 = 1. For example the following code may fail.
1597 //
1598 // ----------------------------------------------------------------------------------------
1599 // Initial State:
1600 // eastl::atomic<int> x = 0;
1601 // eastl::atomic<int> y = 0;
1602 // eastl::atomic<int> z = 0;
1603 // ----------------------------------------------------------------------------------------
1604 // Thread 0 | Thread 1
1605 // ----------------------------------------------------------------------------------------
1606 // z.store(2, memory_order_relaxed); | r0 = y.load(memory_order_relaxed);
1607 // | x.fetch_add(1, memory_order_seq_cst);
1608 // y.fetch_add(1, memory_order_seq_cst); | r1 = z.load(memory_order_relaxed);
1609 // ----------------------------------------------------------------------------------------
1610 // Observed: r0 = 1 && r1 = 0
1611 // ----------------------------------------------------------------------------------------
1612 //
1613 // ----------------------------------------------------------------------------------------
1614 // Initial State:
1615 // eastl::atomic<int> x = 0;
1616 // eastl::atomic<int> y = 0;
1617 // eastl::atomic<int> z = 0;
1618 // ----------------------------------------------------------------------------------------
1619 // Thread 0 | Thread 1
1620 // ----------------------------------------------------------------------------------------
1621 // z.store(2, memory_order_relaxed); | r0 = y.load(memory_order_relaxed);
1622 // x.fetch_add(1, memory_order_seq_cst); | atomic_thread_fence(memory_order_seq_cst);
1623 // y.store(1, memory_order_relaxed); | r1 = z.load(memory_order_relaxed);
1624 // ----------------------------------------------------------------------------------------
1625 // Observed: r0 = 1 && r1 = 0
1626 // ----------------------------------------------------------------------------------------
1627 //
1628 // In this example it is entirely possible that we observe r0 = 1 && r1 = 0 even though we have source code causality and sequentially consistent operations.
1629 // Observability is tied to the atomic object on which the operation was performed and the thread fence doesn't synchronize-with the fetch_add because
1630 // there is no load above the fence that reads the value from the fetch_add.
1631 //
1632 // ******** Sequential Consistency Semantics ********
1633 //
1634 // See section, Order and consistency, in the C++ std and Reference [9].
1635 //
1636 // A load with memory_order_seq_cst performs an acquire operation
1637 // A store with memory_order_seq_cst performs a release operation
1638 // A RMW with memory_order_seq_cst performs both an acquire and a release operation
1639 //
1640 // All memory_order_seq_cst operations exhibit the below single total order in which all threads observe all modifications in the same order
1641 //
1642 // Paraphrasing, there is a single total order on all memory_order_seq_cst operations, S, such that each sequentially consistent operation B that loads a value from
1643 // atomic object M observes either the result of the last sequentially consistent modification A on M, or some modification on M that isn't memory_order_seq_cst.
1644 // For atomic modifications A and B on an atomic object M, B occurs after A in the total order of M if:
1645 // there is a memory_order_seq_cst fence X whereby A is sequenced before X, and X precedes B,
1646 // there is a memory_order_seq_cst fence Y whereby Y is sequenced before B, and A precedes Y,
1647 // there are memory_order_seq_cst fences X and Y such that A is sequenced before X, Y is sequenced before B, and X precedes Y.
1648 //
1649 // Let's look at some examples using memory_order_seq_cst.
1650 //
1651 // ------------------------------------------------------------
1652 // Store-Buffer
1653 // ------------------------------------------------------------
1654 // Initial State:
1655 // x = 0; y = 0;
1656 // ------------------------------------------------------------
1657 // Thread 0 | Thread 1
1658 // ------------------------------------------------------------
1659 // STORE_RELAXED(x, 1) | STORE_RELAXED(y, 1)
1660 // ATOMIC_THREAD_FENCE(SEQ_CST) | ATOMIC_THREAD_FENCE(SEQ_CST)
1661 // r0 = LOAD_RELAXED(y) | r1 = LOAD_RELAXED(x)
1662 // ------------------------------------------------------------
1663 // Observed: r0 = 0 && r1 = 0
1664 // ------------------------------------------------------------
1665 //
1666 // ------------------------------------------------------------
1667 // Store-Buffer
1668 // ------------------------------------------------------------
1669 // Initial State:
1670 // x = 0; y = 0;
1671 // ------------------------------------------------------------
1672 // Thread 0 | Thread 1
1673 // ------------------------------------------------------------
1674 // STORE_SEQ_CST(x, 1) | STORE_SEQ_CST(y, 1)
1675 // r0 = LOAD_SEQ_CST(y) | r1 = LOAD_SEQ_CST(x)
1676 // ------------------------------------------------------------
1677 // Observed: r0 = 0 && r1 = 0
1678 // ------------------------------------------------------------
1679 //
1680 // Both solutions above are correct to ensure that the end results cannot lead to both r0 and r1 returning 0. Notice that the second one requires memory_order_seq_cst on both
1681 // operations to ensure they are in the total order, S, for all memory_order_seq_cst operations. The other example uses the stronger guarantee provided by a sequentially consistent fence.
1682 //
1683 // ------------------------------------------------------------------------------------------------
1684 // Read-To-Write Causality
1685 // ------------------------------------------------------------------------------------------------
1686 // Initial State:
1687 // x = 0; y = 0;
1688 // ------------------------------------------------------------------------------------------------
1689 // Thread 0 | Thread 1 | Thread 2
1690 // ------------------------------------------------------------------------------------------------
1691 // STORE_SEQ_CST(x, 1) | r0 = LOAD_RELAXED(x) | STORE_RELAXED(y, 1)
1692 // | ATOMIC_THREAD_FENCE(SEQ_CST) | ATOMIC_THREAD_FENCE(SEQ_CST)
1693 // | r1 = LOAD_RELAXED(y) | r2 = LOAD_RELAXED(x)
1694 // ------------------------------------------------------------------------------------------------
1695 // Observed: r0 = 1 && r1 = 0 && r2 = 0
1696 // ------------------------------------------------------------------------------------------------
1697 //
1698 // You'll notice this example is an in between example of the Store-Buffer and IRIW examples we have seen earlier. The store in Thread 0 needs to be sequentially consistent so it synchronizes with the
1699 // thread fence in Thread 1. C++20 due to Reference [9], increased the strength of sequentially consistent fences has been increased to allow for the following.
1700 //
1701 // ------------------------------------------------------------------------------------------------
1702 // Read-To-Write Causality - C++20
1703 // ------------------------------------------------------------------------------------------------
1704 // Initial State:
1705 // x = 0; y = 0;
1706 // ------------------------------------------------------------------------------------------------
1707 // Thread 0 | Thread 1 | Thread 2
1708 // ------------------------------------------------------------------------------------------------
1709 // STORE_RELAXED(x, 1) | r0 = LOAD_RELAXED(x) | STORE_RELAXED(y, 1)
1710 // | ATOMIC_THREAD_FENCE(SEQ_CST) | ATOMIC_THREAD_FENCE(SEQ_CST)
1711 // | r1 = LOAD_RELAXED(y) | r2 = LOAD_RELAXED(x)
1712 // ------------------------------------------------------------------------------------------------
1713 // Observed: r0 = 1 && r1 = 0 && r2 = 0
1714 // ------------------------------------------------------------------------------------------------
1715 //
1716 // Notice we were able to turn the store in Thread 0 into a relaxed store and still properly observe either r1 or r2 returning 1.
1717 // Note that all implementations of the C++11 standard for every architecture even now allows the C++20 behaviour.
1718 // The C++20 standard memory model was brought up to recognize that all current implementations are able to implement them stronger.
1719 //
1720 // ******** False Sharing ********
1721 //
1722 // As we know operations work on the granularity of a cacheline. A RMW operation obviously must have some help from the cache to ensure the entire operation
1723 // is seen as one whole unit. Conceptually we can think of this as the cpu's cache taking a lock on the cacheline, the cpu doing the read-modify-write operation on the
1724 // locked cacheline, and then releasing the lock on the cacheline. This means during that time any other cpu needing that cacheline must wait for the lock to be released.
1725 //
1726 // If we have two atomic objects doing RMW operations and they are within the same cacheline, they are unintentionally contending and serializing with each other even
1727 // though they are two completely separate objects. This gives us the common name to this phenomona called false sharing.
1728 // You can cacheline align your structure or the eastl::atomic<T> object to prevent false sharing.
1729 //
1730 // ******** union of eastl::atomic<T> ********
1731 //
1732 // union { eastl::atomic<uint8_t> atomic8; eastl::atomic<uint32_t> atomic32; };
1733 //
1734 // While we know that operations operate at the granularity of a processor's cacheline size and so we may expect that storing and loading
1735 // from different width atomic variables at the same address to not cause weird observable behaviour but it may.
1736 // Store Buffers allow smaller stores to replace parts of larger loads that are forwarded from a store buffer.
1737 // This means if there is 2 bytes of modified data in the store buffer that overlaps with a 4 byte load, the 2 bytes will be forwarded
1738 // from the store buffer. This is even documented behaviour of the x86 store buffer in the x86 architecture manual.
1739 // This behaviour can cause processors to observe values that have never and will never be visible on the bus to other processors.
1740 // The use of a union with eastl::atomic<T> is not wrong but your code must be able to withstand these effects.
1741 //
1742 // Assume everything starts out initially as zero.
1743 //
1744 // -------------------------------------------------------------------------------------------------------
1745 // Thread 0 | Thread 1 | Thread 2
1746 // --------------------------------------------------------------------------------------------------------
1747 // cmpxchg 0 -> 0x11111111 | cmpxchg 0x11111111 -> 0x22222222 | mov byte 0x33; mov 4 bytes into register;
1748 // ---------------------------------------------------------------------------------------------------------
1749 //
1750 // After all operations complete, the value in memory at that location is, 0x22222233.
1751 // It is possible that the 4 byte load in thread 2 actually returns 0x11111133.
1752 // Now 0x11111133 is an observed value that no other cpu could observe because it was never globally visible on the data bus.
1753 //
1754 // If the value in memory is 0x22222233 then the first cmpxchg succeeded, then the second cmpxchg succeeded and finally our
1755 // byte to memory was stored, yet our load returned 0x11111133. This is because store buffer contents can be forwarded to overlapping loads.
1756 // It is possible that the byte store got put in the store buffer. Our load happened after the first cmpxchg with the byte forwarded.
1757 // This behaviour is fine as long as your algorithm is able to cope with this kind of store buffer forwarding effects.
1758 //
1759 // Reference [13] is a great read on more about this topic of mixed-size concurrency.
1760 //
1761 
1762 
1764 
1765 
1766 #include <EASTL/internal/atomic/atomic.h>
1767 #include <EASTL/internal/atomic/atomic_standalone.h>
1768 #include <EASTL/internal/atomic/atomic_flag.h>
1769 #include <EASTL/internal/atomic/atomic_flag_standalone.h>
1770 
1771 
1772 #endif /* EASTL_ATOMIC_H */