KEMBAR78
Add DCAS overload of Interlocked.CompareExchange · Issue #31911 · dotnet/runtime · GitHub
Skip to content

Add DCAS overload of Interlocked.CompareExchange #31911

@scalablecory

Description

@scalablecory

A CAS that operates atomically on two pointers at once is useful for writing lock-free algorithms.

This would be an intrinsic for CMPXCHG8B, CMPXCHG16B, CASPAL instructions

Something like:

struct AtomicPair<TFirst, TSecond> where TFirst:class, TSecond:class
{
    public TFirst First;
    public TSecond Second;
}

struct AtomicPair<T> where T:class
{
    public T First;
    public nint Second;
}

struct AtomicPair
{
    public nint First;
    public nint Second;
}

class Interlocked
{
    // upon return, comparand is updated to the value that was in location1.
    public static bool CompareExchange(ref AtomicPair location1, AtomicPair value, ref AtomicPair comparand);
    public static bool CompareExchange<T>(ref AtomicPair<T> location1, AtomicPair<T> value, ref AtomicPair<T> comparand);
    public static bool CompareExchange<TFirst, TSecond>(ref AtomicPair<TFirst, TSecond> location1, AtomicPair<TFirst, TSecond> value, ref AtomicPair<TFirst, TSecond> comparand);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions