Skip to content

[API Proposal]: Volatile read/write for new supported types of Interlocked exchange #107449

@ShadedBlink

Description

@ShadedBlink

Background and motivation

.Net 9 now allows us to exchange more types like byte, short and enums. It would be nice to expand Volatile.Write() and Volatile.Read() with such overloads as well.

API Proposal

public static class Volatile
{
    // Remove constraints from existing methods, making them work for primitives and throw for unsupported types,
    // as was done with Interlocked.{Compare}Exchange
    public static T Read<T>([NotNullIfNotNull(nameof(location))] ref readonly T location)
-        where T : class?
    public static void Write<T>([NotNullIfNotNull(nameof(value))] ref T location, T value) 
-        where T : class?
}

API Usage

var x = SomeEnumType.DefaultValue;
Volatile.Write(ref x, SomeEnumType.NonDefaultValue);

Alternative Designs

No response

Risks

No risks, everything already implemented in net9.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions