-
-
Notifications
You must be signed in to change notification settings - Fork 423
assumeUnshared: convert shared lvalue to a non-shared one #2156
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request, @wilzbach! Bugzilla referencesYour 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 locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + druntime#2156" |
src/core/atomic.d
Outdated
} | ||
|
||
/// ditto | ||
ref immutable(T) assumeUnshared(T)(ref immutable(T) val) @safe @nogc pure nothrow |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
@wilzbach |
@wilzbach ping, what is the status of this? |
src/core/atomic.d
Outdated
} | ||
|
||
/// ditto | ||
ref immutable(T) assumeUnshared(T)(ref immutable(T) val) @safe @nogc pure nothrow |
There was a problem hiding this comment.
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.
@andralex I have rebased and removed the immutable overload. Is this good to go now? |
There was a problem hiding this 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
* } | ||
* --- | ||
* 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. |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
Should peel off only the first level of shared
@MoonlightSentinel I have added a note about |
Revival of #724 from the dead.
Changes from #724:
This would be a great use case for an experimental stage or an marking a symbol as "unstable" for a few releases.