www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18468] New: cannot use `synchronized {}` in safe code

https://issues.dlang.org/show_bug.cgi?id=18468

          Issue ID: 18468
           Summary: cannot use `synchronized {}` in  safe code
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: iamthewilsonator hotmail.com

 safe void main()
{
    synchronized {}
}

onlineapp.d(4): Error: safe function 'main' cannot access __gshared data
'__critsec15'
onlineapp.d(4): Error: safe function 'main' cannot access __gshared data
'__critsec15'
onlineapp.d(4): Error: variable onlineapp.main.__critsec15 __gshared not
allowed in safe functions; use shared

this is due to the compiler rewriting 
synchronized { foo();} // line 15
as
 __gshared ubyte[critsec.sizeof + size_t.sizeof] __critsec15;
_d_criticalenter(__critsec15.ptr);
try { foo(); } finally { _d_criticalexit(__critsec15.ptr); }

instead of 

 __gshared ubyte[critsec.sizeof + size_t.sizeof] __critsec15;
_d_criticalenter(&__critsec15[0]);
try { foo(); } finally { _d_criticalexit(&__critsec15[0]); }

--
Feb 19 2018