Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

assumeUnshared: convert shared lvalue to a non-shared one #2156

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions changelog/core-atomic-assumeUnshared.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
`core.atomic.assumeUnshared` has been added

$(REF assumeUnshared, core,atomic) allows to convert a shared lvalue to a non-shared lvalue

---
shared static int i;

// i is never used outside of synchronized {} blocks...

synchronized
{
++i; // ERROR: cannot directly modify shared lvalue

atomicOp!"+="(i, 1); // possible overhead

// Directly modify i
assumeUnshared(i) += 1;
// or:
++assumeUnshared(i);
// or:
i.assumeUnshared += 1;
}
---
124 changes: 124 additions & 0 deletions src/core/atomic.d
Original file line number Diff line number Diff line change
@@ -263,6 +263,55 @@ in (atomicPtrIsProperlyAligned(here), "Argument `here` is not properly aligned")
return cast(shared)core.internal.atomic.atomicExchange!ms(cast(T*)here, cast(V)exchangeWith);
}

/**
* Converts a shared lvalue to a non-shared lvalue.
*
* This functions allows to treat a shared lvalue as if it was thread-local.
* It is useful to avoid overhead of atomic operations when access to shared data
* is known to be within one thread (i.e. always under a lock).
* ---
* shared static int i;
*
* // i is never used outside of synchronized {} blocks...
*
* synchronized
* {
* ++i; // ERROR: cannot directly modify shared lvalue
*
* atomicOp!"+="(i, 1); // possible overhead
*
* // Directly modify i
* assumeUnshared(i) += 1;
* // or:
* ++assumeUnshared(i);
* // or:
* i.assumeUnshared += 1;
* }
* ---
* Usage of this function is restricted to allowing limited lvalue access to shared instances of
* primitive and POD types (e.g. direct use of operators), thus it is not defined for classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about pointers, i.e. a struct pointer is pretty similar to a class reference.

Maybe assumeUnshared should simply strip the outer shared instead of removing it entirely:

struct S
{
    // members...
    void foo();

    void bar() shared;
}

unittest
{
    shared S* s = new shared S();
    // assumeUnshared(s).foo() // invalid
    assumeUnshared(s).bar() // valid
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. IIRC there was a HeadUnshared template for exactly that reason.

Copy link
Contributor

@RazvanN7 RazvanN7 Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be easier to just not accept pointers (similar to what we do with classes). It seems that the intended usage is for primitive and POD types. The fundamental use case for assumeUnshared is to be able to modify a variable bypassing atomics - this is a straightforward case. If you have a pointer (or a class reference, for that matter) suddenly assumeUnshared(v) becomes ambiguous: does it apply transitively or not? People might argue that if you use assumeUnshared on a pointer then it should transitively apply to all sub-members.

Given all of this, I suggest we modify this PR to not accept pointers and if the need arises we can extend the functionality to reference types also. It's better to start overly restrictive and then relax.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intended usage for this methods is to replace atomics - hence it should not be transitive. The documentation is also pretty clear that it works only with the supplied lvalue which implies the pointer variable - not the target.

Rejecting pointers is fine for now but it should be enhanced to accept both pointers and classes as suggested above (the latter might need a voldemort type to allow assignments while rejecting class member access).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MoonlightSentinel The intended usage is to treat a shared as if it was thread-local. Been nearly 8 years (wow) now since I wrote that comment, but it stands, although it probably could stand to lose or reword the lvalue part. To my knowledge, shared(T*) is still shared(shared(T)*), is it not? If so, assumeUnshared(s).bar() should not be valid, as it should return a reference to S*, not to a shared(S)*.

If you have a class, or a struct, with shared methods, that implies the class/struct implements necessary exclusion and/or synchronization machinery. You wouldn't be using assumeUnshared with instances of such classes, as that's obviated by their very design. You would, however, use it with a pointer to a regular struct or a POD, to replace the pointer value (or e.g. free and nullify it), when doing so under a lock in implementation of a shared method of a struct/class containing that pointer, or to call a non-shared method when you've already established exclusive access.

Also, there's no practical use for a mixed-bag class offering shared and unshared interface simultaneously, so that's kind of a contrived example.

Wrapping references to non-shared classes into shared aggregates should, IMHO, be disallowed altogether by the language (tall order, but one is entitled to some wishful thinking). Classes were designed with their own synchronization in mind, and should therefore be implemented accordingly, which is why they were excluded from this function entirely.
shared(Klass) is a global - there's no other use for it.

assumeUnshared, as I envisioned it at the time, was a hatch to escape transitivity, that is, a facility to lean upon when implementing those very shared methods in classes and structs, an instrument that allows to avoid having casts literally everywhere. At least, that's how I saw it before it got lost to time. What you're suggesting, OTOH, should be done with ugly, explicit, greppable casts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what do we do here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Merge it! This goose has been on the spit for eight years, for crying out loud :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MoonlightSentinel The intended usage is to treat a shared as if it was thread-local. Been nearly 8 years (wow) now since I wrote that comment, but it stands, although it probably could stand to lose or reword the lvalue part. To my knowledge, shared(T*) is still shared(shared(T)), is it not? If so, assumeUnshared(s).bar() should not be valid, as it should return a reference to S, not to a shared(S)*.

That would the expected behaviour iff assumeShared removes shared from all nested layers of the supplied type. Then the documentation be updated to reflect the behaviour w.r.t indirections and arguably classes should be accepted as well.

assumeUnshared, as I envisioned it at the time, was a hatch to escape transitivity, that is, a facility to lean upon when implementing those very shared methods in classes and structs, an instrument that allows to avoid having casts literally everywhere. At least, that's how I saw it before it got lost to time. What you're suggesting, OTOH, should be done with ugly, explicit, greppable casts.

Guess that's a matter of personal preference. Assuming all data reached by any indirections is unshared seems more problematic to me than only removing the first layer of shared. But to each their own.

TL,DR: I would prefer to have consistent behaviour for struct pointers and class references. But at the very least the documentation should be changed to reflect whether assumeUnshared is transitive or not.

*
* Note: this function does not perform any ordering.
*
* Note: `assumeUnshared` is a special-purpose primitive and should be used with care. When accessing
* shared variables both inside and outside of synchronized blocks, atomic operations should be
* used instead.
*
* Note: the result of assumeUnshared is an object for which `shared` has been stripped transitively.
* Therefore, any field that is accessed from the assumed unshared object will also be unshared.
*
* Params:
* val = the shared lvalue.
*
* Returns:
* The non-shared lvalue.
*/
ref T assumeUnshared(T)(ref shared T val) @system @nogc pure nothrow
if (!is(T == class) && !is(T == interface))
{
return *cast(T*) &val;
}

/**
* Performs either compare-and-set or compare-and-swap (or exchange).
*
@@ -1276,4 +1325,79 @@ version (CoreUnittest)
shared NoIndirections n;
static assert(is(typeof(atomicLoad(n)) == NoIndirections));
}

pure nothrow @nogc @system unittest
{
int base = 0;
shared int atom = 0;

// only accept shared lvalues
static assert(!is(typeof(assumeUnshared(base))));

++assumeUnshared(atom);
assert(atomicLoad!(MemoryOrder.raw)(atom) == 1);
}

pure nothrow @nogc @system unittest
{
shared const int catom = 0;
shared immutable int iatom = 0;
// allow const
static assert(is(typeof(assumeUnshared(catom))));
// preserve const
static assert(!is(typeof(++assumeUnshared(catom))));
static assert(!is(typeof(++assumeUnshared(iatom))));
}

pure nothrow @nogc @system unittest
{
class Klass {}

Klass c1;
shared Klass c2;

// don't accept class instances
static assert(!is(typeof(assumeUnshared(c1))));
static assert(!is(typeof(assumeUnshared(c2))));
}

pure nothrow @nogc @system unittest
{
interface Interface {}
Interface i1;
shared Interface i2;

// don't accept interfaces
static assert(!is(typeof(assumeUnshared(i1))));
static assert(!is(typeof(assumeUnshared(i2))));
}

pure nothrow @nogc @system unittest
{
// test assumeShared with inout
shared struct S
{
int atom = 0;

@property ref get() inout
{
return atom.assumeUnshared;
}
}

shared S sm;
shared const S sc;
shared immutable S si;

static assert(is(typeof(sm.get) == int));
static assert(is(typeof(sc.get) == const(int)));
static assert(is(typeof(si.get) == immutable(int)));

static assert( is(typeof(++sm.get)));
static assert(!is(typeof(++sc.get)));
static assert(!is(typeof(++si.get)));

sm.get += 10;
assert(atomicLoad!(MemoryOrder.raw)(sm.atom) == 10);
}
}