[Issue 6607] New: critical_.d and critical.c use double check locking the wrong way

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 5 18:12:30 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6607

           Summary: critical_.d and critical.c use double check locking
                    the wrong way
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: druntime
        AssignedTo: nobody at puremagic.com
        ReportedBy: deadalnix at gmail.com


--- Comment #0 from deadalnix <deadalnix at gmail.com> 2011-09-05 18:12:19 PDT ---
First of all, I'm quite new here and hope I'm not plein wrong. If this is
please forgive me.

I was studying the druntime to know more about what's going on behing the hood
of D2, and I found out that synchronized may have been implemented the wrong
way.

Consider this piece of code from critical_.d :

        if (!dcs.next)
        {
            pthread_mutex_lock(&critical_section.cs);
            if (!dcs.next) // if, in the meantime, another thread didn't set it
            {
                dcs.next = dcs_list;
                dcs_list = dcs;
                pthread_mutex_init(&dcs.cs, &_criticals_attr);
            }
            pthread_mutex_unlock(&critical_section.cs);
        }

The double check locking patern is used the wrong way here. dcs.next may have
been set before the whole dcs struct has been initialized, thus leading to
undefined behaviour. Reordering instruction within the second if isn't
suffiscient as long as compiler, or CPU may reorder instruction and memory
read/write.

The same goes for window code, which is very similar, and c code within
critical.c . I'm not sure which one is used and when). This code duplication
should eventually be reduced/suppressed in the process to avoid having a
problem poping in several place like we can see now.

I would be happy to help to resolve that problem, so don't hesitate to contact
me. Please be indulgent if the problem is irrevevelant because I didn't
understood the way druntime worked. I'm currently learn, and, even if I have a
strong background in devellopement even at low level, druntime is really new to
me and I'm still at the stage where I'm learning it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list