Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 484

Show
Ignore:
Timestamp:
01/08/11 06:32:57 (14 years ago)
Author:
sean
Message:

Added a comment to core.thread explaining the rationale behind the latest change.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/core/thread.d

    r483 r484  
    16701670    // 
    16711671    static void add( Thread t ) 
    16721672    in 
    16731673    { 
    16741674        assert( t ); 
    16751675        assert( !t.next && !t.prev ); 
    16761676        assert( t.isRunning ); 
    16771677    } 
    16781678    body 
    16791679    { 
     1680        // NOTE: This loop is necessary to avoid a race between newly created 
     1681        //       threads and the GC.  If a collection starts between the time 
     1682        //       Thread.start is called and the new thread calls Thread.add, 
     1683        //       the thread could manipulate global state while the collection 
     1684        //       is running, and by being added to the thread list it could be 
     1685        //       resumed by the GC when it was never suspended, which would 
     1686        //       result in an exception thrown by the GC code.  An alternative 
     1687        //       would be to have Thread.start call Thread.add for the new 
     1688        //       thread, but this introduces its own problems, since the 
     1689        //       thread object isn't entirely ready to be operated on by the 
     1690        //       GC.  This could be fixed by tracking thread startup status, 
     1691        //       but it's far easier to simply have Thread.add wait for any 
     1692        //       running collection to stop before altering the thread list. 
     1693         
    16801694        while( true ) 
    16811695        { 
    16821696            synchronized( slock ) 
    16831697            { 
    16841698                if( !suspendDepth ) 
    16851699                { 
    16861700                    if( sm_tbeg ) 
    16871701                    { 
    16881702                        t.next = sm_tbeg; 
    16891703                        sm_tbeg.prev = t;