-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Background and motivation
Unlike System.Threading.Volatile.Read, the System.Threading.Relaxed.Read only defeats compiler optimizations.
This is basically an officialVolatileReadWithoutBarrier (see:
| if (VolatileReadWithoutBarrier(ref _current) == entry) |
The read will keep its order with respect to other memory operations (in singlethreaded/program order, not in the order of sideeffects), and used reads will not be coalesced with other reads from the same location.
Such read would be paired with Interlocked.MemoryBarrierProcessWide or could be used to poll a local that could be changed on another thread.
A Volatile.Read could be used for the same purpose, but will add unnecessary guarantees of the order of the actual read.
We can, optionally, add a requirement that the read is atomic.
A better name than "Relaxed" may exist too.
API Proposal
namespace System.Threading;
public class Relaxed
{
public T Read<T>(ref T location) where T: class;
public int Read(ref int location);
public double Read(ref double location);
. . .
}API Usage
Alternative Designs
Can use Volatile.Read or hand-written uninlinable methods with sole purpose of reading some variable.
Risks
No risk, this is completely additive API.