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

Changeset 57

Show
Ignore:
Timestamp:
12/07/08 06:33:12 (16 years ago)
Author:
walter
Message:

fix to scan tls

Files:

Legend:

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

    r43 r57  
    8585    private 
    8686    { 
    8787        import core.stdc.stdint : uintptr_t; // for _beginthreadex decl below 
    8888        import core.sys.windows.windows; 
    8989 
    9090        const DWORD TLS_OUT_OF_INDEXES  = 0xFFFFFFFF; 
    9191 
    9292        extern (Windows) alias uint function(void*) btex_fptr; 
    9393        extern (C) uintptr_t _beginthreadex(void*, uint, btex_fptr, void*, uint, uint*); 
    9494 
     95    /* The memory between the addresses of _tlsstart and _tlsend 
     96     * lies all the implicit thread local storage variables for this 
     97     * thread for this module. 
     98     * Both are declared in \dm\src\win32\tlsseg.asm 
     99     */ 
     100    extern (C) 
     101    { 
     102        extern __thread int _tlsstart; 
     103        extern __thread int _tlsend; 
     104    } 
    95105 
    96106        // 
    97107        // entry point for Windows threads 
    98108        // 
    99109        extern (Windows) uint thread_entryPoint( void* arg ) 
    100110        { 
    101111            Thread  obj = cast(Thread) arg; 
    102112            assert( obj ); 
    103113            scope( exit ) Thread.remove( obj ); 
    104114 
    105115            assert( obj.m_curr is &obj.m_main ); 
    106116            obj.m_main.bstack = getStackBottom(); 
    107117            obj.m_main.tstack = obj.m_main.bstack; 
    108118            Thread.add( &obj.m_main ); 
    109119            Thread.setThis( obj ); 
    110120 
     121        /* Save the bounds of the TLS data block 
     122         */ 
     123        void* pstart = cast(void*)&_tlsstart; 
     124        void* pend = cast(void*)&_tlsend; 
     125        obj.m_tls = pstart[0 .. pend - pstart]; 
     126 
    111127            // NOTE: No GC allocations may occur until the stack pointers have 
    112128            //       been set and Thread.getThis returns a valid reference to 
    113129            //       this thread object (this latter condition is not strictly 
    114130            //       necessary on Windows but it should be followed for the 
    115131            //       sake of consistency). 
    116132 
    117133            // TODO: Consider putting an auto exception object here (using 
    118134            //       alloca) forOutOfMemoryError plus something to track 
    119135            //       whether an exception is in-flight? 
    120136 
     
    156172        import core.sys.posix.signal; 
    157173        import core.sys.posix.time; 
    158174        import core.stdc.errno; 
    159175 
    160176        extern (C) int getErrno(); 
    161177 
    162178        version( GNU ) 
    163179        { 
    164180            import gcc.builtins; 
    165181        } 
     182 
     183    /* The memory between the addresses of _tlsstart and _tlsend 
     184     * lies all the implicit thread local storage variables for this 
     185     * thread for this module. 
     186     * These two are magically allocated by the compiler so that 
     187     * they will bracket the .tdata and .tbss segments. 
     188     * See elfobj.c in the dmd compiler source for details. 
     189     */ 
     190    extern (C) 
     191    { 
     192        __thread int _tlsstart; 
     193        __thread int _tlsend; 
     194    } 
    166195 
    167196 
    168197        // 
    169198        // entry point for POSIX threads 
    170199        // 
    171200        extern (C) void* thread_entryPoint( void* arg ) 
    172201        { 
    173202            Thread  obj = cast(Thread) arg; 
    174203            assert( obj ); 
    175204            scope( exit ) 
     
    232261                obj.m_main.bstack = getBasePtr(); 
    233262            } 
    234263            else version( StackGrowsDown ) 
    235264                obj.m_main.bstack = &obj + 1; 
    236265            else 
    237266                obj.m_main.bstack = &obj; 
    238267            obj.m_main.tstack = obj.m_main.bstack; 
    239268            assert( obj.m_curr == &obj.m_main ); 
    240269            Thread.add( &obj.m_main ); 
    241270            Thread.setThis( obj ); 
     271 
     272        /* Save the bounds of the TLS data block 
     273         */ 
     274        void* pstart = cast(void*)&_tlsstart; 
     275        void* pend = cast(void*)&_tlsend; 
     276        obj.m_tls = pstart[0 .. pend - pstart]; 
     277 
    242278 
    243279            // NOTE: No GC allocations may occur until the stack pointers have 
    244280            //       been set and Thread.getThis returns a valid reference to 
    245281            //       this thread object (this latter condition is not strictly 
    246282            //       necessary on Windows but it should be followed for the 
    247283            //       sake of consistency). 
    248284 
    249285            // TODO: Consider putting an auto exception object here (using 
    250286            //       alloca) forOutOfMemoryError plus something to track 
    251287            //       whether an exception is in-flight? 
     
    11131149 
    11141150private: 
    11151151    // 
    11161152    // Initializes a thread object which has no associated executable function. 
    11171153    // This is used for the main thread initialized in thread_init(). 
    11181154    // 
    11191155    this() 
    11201156    { 
    11211157        m_call = Call.NO; 
    11221158        m_curr = &m_main; 
     1159 
     1160    void* pstart = cast(void*)&_tlsstart; 
     1161    void* pend = cast(void*)&_tlsend; 
     1162    m_tls = pstart[0 .. pend - pstart]; 
    11231163    } 
    11241164 
    11251165 
    11261166    // 
    11271167    // Thread entry point.  Invokes the function or delegate passed on 
    11281168    // construction (if any). 
    11291169    // 
    11301170    final void run() 
    11311171    { 
    11321172        switch( m_call ) 
     
    12731313                        tstack; 
    12741314        Context*        within; 
    12751315        Context*        next, 
    12761316                        prev; 
    12771317    } 
    12781318 
    12791319 
    12801320    Context             m_main; 
    12811321    Context*            m_curr; 
    12821322    bool                m_lock; 
     1323    void[]              m_tls;  // spans implicit thread local storage 
    12831324 
    12841325    version( Windows ) 
    12851326    { 
    12861327        uint[8]         m_reg; // edi,esi,ebp,esp,ebx,edx,ecx,eax 
    12871328    } 
    12881329 
    12891330 
    12901331private: 
    12911332    /////////////////////////////////////////////////////////////////////////// 
    12921333    // GC Scanning Support 
     
    19602001            //       so don't do the "+1" for StackGrowsDown. 
    19612002            if( c.tstack && c.tstack < c.bstack ) 
    19622003                scan( c.tstack, c.bstack ); 
    19632004        } 
    19642005        else 
    19652006        { 
    19662007            if( c.bstack && c.bstack < c.tstack ) 
    19672008                scan( c.bstack, c.tstack + 1 ); 
    19682009        } 
    19692010    } 
    1970     version( Windows ) 
    1971     { 
    1972         for( Thread t = Thread.sm_tbeg; t; t = t.next ) 
    1973         { 
     2011 
     2012    for( Thread t = Thread.sm_tbeg; t; t = t.next ) 
     2013    { 
     2014    /* Scan thread local storage. 
     2015     * The BUG here is that the tls for other modules 
     2016     * is not scanned. 
     2017     */ 
     2018    scan( t.m_tls.ptr, t.m_tls.ptr + t.m_tls.length ); 
     2019 
     2020    version (Windows) 
    19742021            scan( &t.m_reg[0], &t.m_reg[0] + t.m_reg.length ); 
    1975         } 
    19762022    } 
    19772023} 
    19782024 
    19792025 
    19802026/////////////////////////////////////////////////////////////////////////////// 
    19812027// Thread Local 
    19822028/////////////////////////////////////////////////////////////////////////////// 
    19832029 
    19842030 
    19852031/**