This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 419
assumeUnshared: convert shared lvalue to a non-shared one #2156
Open
wilzbach
wants to merge
4
commits into
dlang:master
Choose a base branch
from
wilzbach:pr/724
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 outershared
instead of removing it entirely: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) suddenlyassumeUnshared(v)
becomes ambiguous: does it apply transitively or not? People might argue that if you useassumeUnshared
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.
cc @PetarKirov @MoonlightSentinel
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 stillshared(shared(T)*)
, is it not? If so,assumeUnshared(s).bar()
should not be valid, as it should return a reference toS*
, not to ashared(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 usingassumeUnshared
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 ashared
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 intoshared
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 veryshared
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.
That would the expected behaviour iff
assumeShared
removesshared
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.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.