Skip to content
This repository has been 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

Conversation

wilzbach
Copy link
Member

Revival of #724 from the dead.

Changes from #724:

  • rebased
  • fix doc/style nits
  • added changelog entry

This would be a great use case for an experimental stage or an marking a symbol as "unstable" for a few releases.

@dlang-bot
Copy link
Contributor

dlang-bot commented Mar 29, 2018

Thanks for your pull request, @wilzbach!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + druntime#2156"

}

/// ditto
ref immutable(T) assumeUnshared(T)(ref immutable(T) val) @safe @nogc pure nothrow
Copy link
Member Author

Choose a reason for hiding this comment

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

This second overload was controversial in the original PR: #724 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Well as mentioned previously: "One possibility is to not introduce this overload yet, and see how necessary it is in practice."

If we find ourselves often in the situation whereby we write:

static if (is(typeof(x) == immutable)) {
   ... use x ...
} else {
   ... use assumeUnshared(x) ...
}

then we add the overload.

@jercaianu
Copy link
Contributor

@wilzbach
Is this still under work? I really need this
Thanks!

@jacob-carlborg
Copy link
Contributor

@wilzbach ping, what is the status of this?

@thewilsonator thewilsonator added the Needs Rebase needs a `git rebase` performed label Nov 9, 2018
@dlang-bot dlang-bot removed the Needs Rebase needs a `git rebase` performed label May 10, 2019
@wilzbach
Copy link
Member Author

@wilzbach ping, what is the status of this?

Needs approval from @andralex :/

}

/// ditto
ref immutable(T) assumeUnshared(T)(ref immutable(T) val) @safe @nogc pure nothrow
Copy link
Member

Choose a reason for hiding this comment

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

Well as mentioned previously: "One possibility is to not introduce this overload yet, and see how necessary it is in practice."

If we find ourselves often in the situation whereby we write:

static if (is(typeof(x) == immutable)) {
   ... use x ...
} else {
   ... use assumeUnshared(x) ...
}

then we add the overload.

@RazvanN7
Copy link
Contributor

RazvanN7 commented Nov 9, 2021

@andralex I have rebased and removed the immutable overload. Is this good to go now?

PetarKirov
PetarKirov previously approved these changes Nov 9, 2021
Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

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

We should have had this years ago! Well, better late than never :D

@RazvanN7 RazvanN7 added the 72h no objection -> merge The PR will be merged if there are no objections raised. label Nov 9, 2021
* }
* ---
* 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.

@PetarKirov PetarKirov dismissed their stale review November 9, 2021 17:52

Should peel off only the first level of shared

@PetarKirov PetarKirov removed the 72h no objection -> merge The PR will be merged if there are no objections raised. label Nov 9, 2021
@RazvanN7
Copy link
Contributor

@MoonlightSentinel I have added a note about assumeUnshareds transitivity.

@RazvanN7 RazvanN7 added the 72h no objection -> merge The PR will be merged if there are no objections raised. label Jan 24, 2022
@dlang-bot dlang-bot added Needs Rebase needs a `git rebase` performed Needs Work labels Mar 28, 2022
@RazvanN7 RazvanN7 removed the 72h no objection -> merge The PR will be merged if there are no objections raised. label Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Rebase needs a `git rebase` performed Needs Work stalled
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants