Very limited shared promotion

Timon Gehr timon.gehr at gmx.ch
Wed Jun 19 14:00:25 UTC 2019


On 19.06.19 10:47, Jonathan M Davis wrote:
> On Monday, June 17, 2019 5:46:44 PM MDT Manu via Digitalmars-d wrote:
>> Is this valid?
>>
>> int x;
>> void fun(scope ref shared(int) x) { ... }
>> fun(x); // implicit promotion to shared in this case
>>
>> This appears to promote a thread-local to shared. The problem with
>> such promotion is that it's not valid that a thread-local AND a shared
>> reference to the same thing can exist at the same time.
>>
>> With scope, we can guarantee that the reference doesn't escape the callee.
>> Since the argument is local to the calling thread, and since the calling
>> thread can not be running other code at the same time as the call is
>> executing, there is no way for any code to execute with a thread-local
>> assumption while the callee makes shared assumptions.
>>
>> I think this might be safe?
> 
> Another thing to consider here is something that's come up before when
> you've made suggestions like this - which is that unlike shared, scope is
> not transitive. So, even if what you're suggesting here would work with int,
> it would not work with types in general.
> 
> - Jonathan M Davis
> 
  Delegate contexts are opaque, so perhaps if the delegate is `pure` and 
doesn't return any `shared` references we can derive implicit promotion 
rules that work with more complex data types.


More information about the Digitalmars-d mailing list