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]: Interlocked.[Compare]Exchange for bool #100914

Open
colejohnson66 opened this issue Apr 11, 2024 · 4 comments
Open

[API Proposal]: Interlocked.[Compare]Exchange for bool #100914

colejohnson66 opened this issue Apr 11, 2024 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading
Milestone

Comments

@colejohnson66
Copy link

colejohnson66 commented Apr 11, 2024

Background and motivation

Many times, I've found myself wanting to use Interlocked.Exchange on a single boolean flag. However, this is not possible, and I'm forced to use an int with a zero or one.

An API discrepancy exists where Volatile.Read and Volatile.Write with bool exist, but none for Interlocked.

Following #64658, bool overloads should be added.

Theoretically, these can be implemented as wrappers around existing Interlocked.[Compare]Exchange through Unsafe.As.

API Proposal

namespace System.Threading;

public static class Interlocked
{
    public static bool CompareExchange(ref bool location, bool value, bool comparand);
    public static bool Exchange(ref bool location, bool value);
}

API Usage

Old code:

if (Interlocked.Exchange(ref _interlock, 1) == 1)
    return; // some other thread got here before us
// do stuff
Volatile.Write(ref _interlock, 0);

New code:

if (Interlocked.Exchange(ref _interlock, true))
    return; // some other thread got here before us
// do stuff
Volatile.Write(ref _interlock, false); // already existing API since .NET 4.5

Alternative Designs

No response

Risks

None.

@colejohnson66 colejohnson66 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 11, 2024
Copy link
Contributor

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

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 11, 2024
@jkotas
Copy link
Member

jkotas commented Apr 11, 2024

The bool overload was discussed and rejected in #64658 (comment)

@colejohnson66
Copy link
Author

colejohnson66 commented Apr 12, 2024

Unless I'm missing something, I don't see a rejection anywhere. Just that users using Unsafe.As<byte,bool> (and vice versa) themselves is dangerous. Any non-zero backed boolean is True, but comparison is done bitwise. Specifically, Stephen Toub said that he "[doesn't] particularly care whether we add a bool-based overload, [...]"

@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label May 2, 2024
@mangod9 mangod9 added this to the Future milestone May 2, 2024
@colejohnson66
Copy link
Author

Duplicate of #65184?

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
Projects
None yet
Development

No branches or pull requests

3 participants