Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutable does not promote to shared #17880

Open
dlangBugzillaToGithub opened this issue Oct 1, 2018 · 1 comment
Open

mutable does not promote to shared #17880

dlangBugzillaToGithub opened this issue Oct 1, 2018 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Manu reported this on 2018-10-01T04:13:20Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=19279

Description

struct Bob
{
  void setThing() shared;
}

As I understand, `shared` attribution intends to guarantee that I dun
synchronisation internally.
This method is declared shared, so if I have shared instances, I can
call it... because it must handle thread-safety internally.

void f(ref shared Bob a, ref Bob b)
{
  a.setThing(); // I have a shared object, can call shared method

  b.setThing(); // ERROR
}

The method is shared, which suggests that it must handle thread-safety. My instance `b` is NOT shared, that is, it is thread-local.
A method that handles thread-safety doesn't not work when it's only accessed from a single thread.

mutable -> shared should work the same as mutable -> const... because surely that's safe?
@dlangBugzillaToGithub
Copy link
Author

turkeyman commented on 2018-10-01T05:32:29Z

Conversation: https://github.com/dlang/dmd/pull/8782

Reveals that `scope` is also necessary to guarantee that the promoted reference does not escape.
Promotion is safe so long as no promoted-reference outlives the call where the instance was promoted.

struct Bob
{
  void setThing() shared scope;
}

void f(ref shared Bob a, ref Bob b)
{
  a.setThing(); // I have a shared object, can call shared method

  b.setThing(); // this should work with `scope`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant