Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: System.Threading.Relaxed.Read #75874

Open
VSadov opened this issue Sep 20, 2022 · 1 comment
Open

[API Proposal]: System.Threading.Relaxed.Read #75874

VSadov opened this issue Sep 20, 2022 · 1 comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading memory model issues associated with memory model
Milestone

Comments

@VSadov
Copy link
Member

VSadov commented Sep 20, 2022

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.

@VSadov VSadov added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Sep 20, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 20, 2022
@ghost
Copy link

ghost commented Sep 20, 2022

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

Issue Details

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 not be reordered with other memory operations (in program order, not in the order of sideeffects), and used read will not be coalesced with 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 of the same purpose, but will add unnecessary guarantees of the order of the actual read.

API Proposal

namespace System.Threading;

public class Relaxed
{
    [Intrinsic]
    public T Read(ref T location);
}

API Usage

// Fancy the value
var c = new MyFancyCollection<int>();
c.Fancy(42);

// Getting the values out
foreach (var v in c)
    Console.WriteLine(v);

Alternative Designs

Can use Volatile.Read or hand-written uninlinable methods with sole purpose of reading some variable.

Risks

No response

Author: VSadov
Assignees: -
Labels:

api-suggestion, area-System.Threading

Milestone: -

@VSadov VSadov added the memory model issues associated with memory model label Sep 20, 2022
@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label Sep 20, 2022
@mangod9 mangod9 added this to the Future milestone Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading memory model issues associated with memory model
Projects
None yet
Development

No branches or pull requests

2 participants