Very limited shared promotion

Timon Gehr timon.gehr at gmx.ch
Tue Jun 18 02:03:09 UTC 2019


On 18.06.19 02:49, Manu wrote:
> On Tue, Jun 18, 2019 at 9:46 AM Manu <turkeyman at gmail.com> 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?
> 
> I guess this is the problem case; as is often the problem case:
> 
> int x;
> void fun(scope ref shared(int) x, ref int y) { ... }
> fun(x, x); // aliasing
> 

Walter is thinking about making it unsafe to pass two mutable pointers 
to the same object into a function by `ref`. For simple cases like your 
example, where x is a local variable, I think a slightly adapted version 
of the algorithm he has in mind would work too.


More information about the Digitalmars-d mailing list