Changeset 43

Show
Ignore:
Timestamp:
11/04/08 19:35:42 (4 years ago)
Author:
sean
Message:

* Fixed the documentation for sleep().
* Changed the implementation of sleep() on Windows such that a yield will not be forced upon the user if they pass an interval that evaluates to zero. For more information regarding Sleep(0), see the MSDN docs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/D1.0/src/common/core/thread.d

    r36 r43  
    112112            //       been set and Thread.getThis returns a valid reference to 
    113113            //       this thread object (this latter condition is not strictly 
    114             //       necessary on Win32 but it should be followed for the sak
    115             //       of consistency). 
     114            //       necessary on Windows but it should be followed for th
     115            //       sake of consistency). 
    116116 
    117117            // TODO: Consider putting an auto exception object here (using 
     
    244244            //       been set and Thread.getThis returns a valid reference to 
    245245            //       this thread object (this latter condition is not strictly 
    246             //       necessary on Win32 but it should be followed for the sak
    247             //       of consistency). 
     246            //       necessary on Windows but it should be followed for th
     247            //       sake of consistency). 
    248248 
    249249            // TODO: Consider putting an auto exception object here (using 
     
    480480        } 
    481481 
    482         version( Win32
     482        version( Windows
    483483        { 
    484484            m_addr = m_addr.init; 
     
    516516    body 
    517517    { 
    518         version( Win32 ) {} else 
     518        version( Windows ) {} else 
    519519        version( Posix ) 
    520520        { 
     
    536536        synchronized( slock ) 
    537537        { 
    538             version( Win32
     538            version( Windows
    539539            { 
    540540                m_hndl = cast(HANDLE) _beginthreadex( null, m_sz, &thread_entryPoint, cast(void*) this, 0, &m_addr ); 
     
    574574    final Object join( bool rethrow = true ) 
    575575    { 
    576         version( Win32
     576        version( Windows
    577577        { 
    578578            if( WaitForSingleObject( m_hndl, INFINITE ) != WAIT_OBJECT_0 ) 
     
    691691        } 
    692692 
    693         version( Win32
     693        version( Windows
    694694        { 
    695695            uint ecode = 0; 
     
    738738    final int priority() 
    739739    { 
    740         version( Win32
     740        version( Windows
    741741        { 
    742742            return GetThreadPriority( m_hndl ); 
     
    762762    final void priority( int val ) 
    763763    { 
    764         version( Win32
     764        version( Windows
    765765        { 
    766766            if( !SetThreadPriority( m_hndl, val ) ) 
     
    806806     * ------------------------------------------------------------------------ 
    807807     * 
    808      * Thread.sleep( 500 );        // sleep for 50 milliseconds 
     808     * Thread.sleep( 500_000 );    // sleep for 50 milliseconds 
    809809     * Thread.sleep( 50_000_000 ); // sleep for 5 seconds 
    810810     * 
     
    818818    body 
    819819    { 
    820         version( Win32
     820        version( Windows
    821821        { 
    822822            enum : uint 
     
    826826            } 
    827827 
    828             period = period < TICKS_PER_MILLI ? 
    829                         1 : 
    830                         period / TICKS_PER_MILLI; 
     828            // NOTE: In instances where all other threads in the process have a 
     829            //       lower priority than the current thread, the current thread 
     830            //       will not yield with a sleep time of zero.  However, unlike 
     831            //       yield(), the user is not asking for a yield to occur but 
     832            //       only for execution to suspend for the requested interval. 
     833            //       Therefore, expected performance may not be met if a yield 
     834            //       is forced upon the user. 
     835            period /= TICKS_PER_MILLI; 
    831836            while( period > MAX_SLEEP_MILLIS ) 
    832837            { 
     
    883888    static void yield() 
    884889    { 
    885         version( Win32
     890        version( Windows
    886891        { 
    887892            // NOTE: Sleep(1) is necessary because Sleep(0) does not give 
     
    914919        //       completed.  See thread_suspendAll for more information 
    915920        //       on why this might occur. 
    916         version( Win32
     921        version( Windows
    917922        { 
    918923            return cast(Thread) TlsGetValue( sm_this ); 
     
    10841089    static this() 
    10851090    { 
    1086         version( Win32
     1091        version( Windows
    10871092        { 
    10881093            PRIORITY_MIN = -15; 
     
    11541159    // Standard types 
    11551160    // 
    1156     version( Win32
     1161    version( Windows
    11571162    { 
    11581163        alias uint TLSKey; 
     
    11781183    // Standard thread data 
    11791184    // 
    1180     version( Win32
     1185    version( Windows
    11811186    { 
    11821187        HANDLE          m_hndl; 
     
    12101215    static void setThis( Thread t ) 
    12111216    { 
    1212         version( Win32
     1217        version( Windows
    12131218        { 
    12141219            TlsSetValue( sm_this, cast(void*) t ); 
     
    12771282    bool                m_lock; 
    12781283 
    1279     version( Win32
     1284    version( Windows
    12801285    { 
    12811286        uint[8]         m_reg; // edi,esi,ebp,esp,ebx,edx,ecx,eax 
     
    14381443        assert( t ); 
    14391444        assert( t.next || t.prev ); 
    1440         version( Win32
     1445        version( Windows
    14411446        { 
    14421447            // NOTE: This doesn't work for Posix as m_isRunning must be set to 
     
    14961501    //       functions to detect the condition and return immediately. 
    14971502 
    1498     version( Win32
     1503    version( Windows
    14991504    { 
    15001505        Thread.sm_this = TlsAlloc(); 
     
    15581563extern (C) void thread_attachThis() 
    15591564{ 
    1560     version( Win32
     1565    version( Windows
    15611566    { 
    15621567        Thread          thisThread  = new Thread(); 
     
    16981703    void suspend( Thread t ) 
    16991704    { 
    1700         version( Win32
     1705        version( Windows
    17011706        { 
    17021707            if( t.m_addr != GetCurrentThreadId() && SuspendThread( t.m_hndl ) == 0xFFFFFFFF ) 
     
    18421847    void resume( Thread t ) 
    18431848    { 
    1844         version( Win32
     1849        version( Windows
    18451850        { 
    18461851            if( t.m_addr != GetCurrentThreadId() && ResumeThread( t.m_hndl ) == 0xFFFFFFFF ) 
     
    19631968        } 
    19641969    } 
    1965     version( Win32
     1970    version( Windows
    19661971    { 
    19671972        for( Thread t = Thread.sm_tbeg; t; t = t.next ) 
     
    22432248        else 
    22442249        { 
    2245             version( Win32
     2250            version( Windows
    22462251                version = AsmX86_Win32; 
    22472252            else version( Posix ) 
     
    27622767    static Fiber getThis() 
    27632768    { 
    2764         version( Win32
     2769        version( Windows
    27652770        { 
    27662771            return cast(Fiber) TlsGetValue( sm_this ); 
     
    27802785    static this() 
    27812786    { 
    2782         version( Win32
     2787        version( Windows
    27832788        { 
    27842789            sm_this = TlsAlloc(); 
     
    31493154    static void setThis( Fiber f ) 
    31503155    { 
    3151         version( Win32
     3156        version( Windows
    31523157        { 
    31533158            TlsSetValue( sm_this, cast(void*) f ); 
  • trunk/src/common/core/thread.d

    r36 r43  
    112112            //       been set and Thread.getThis returns a valid reference to 
    113113            //       this thread object (this latter condition is not strictly 
    114             //       necessary on Win32 but it should be followed for the sak
    115             //       of consistency). 
     114            //       necessary on Windows but it should be followed for th
     115            //       sake of consistency). 
    116116 
    117117            // TODO: Consider putting an auto exception object here (using 
     
    244244            //       been set and Thread.getThis returns a valid reference to 
    245245            //       this thread object (this latter condition is not strictly 
    246             //       necessary on Win32 but it should be followed for the sak
    247             //       of consistency). 
     246            //       necessary on Windows but it should be followed for th
     247            //       sake of consistency). 
    248248 
    249249            // TODO: Consider putting an auto exception object here (using 
     
    480480        } 
    481481 
    482         version( Win32
     482        version( Windows
    483483        { 
    484484            m_addr = m_addr.init; 
     
    516516    body 
    517517    { 
    518         version( Win32 ) {} else 
     518        version( Windows ) {} else 
    519519        version( Posix ) 
    520520        { 
     
    536536        synchronized( slock ) 
    537537        { 
    538             version( Win32
     538            version( Windows
    539539            { 
    540540                m_hndl = cast(HANDLE) _beginthreadex( null, m_sz, &thread_entryPoint, cast(void*) this, 0, &m_addr ); 
     
    574574    final Object join( bool rethrow = true ) 
    575575    { 
    576         version( Win32
     576        version( Windows
    577577        { 
    578578            if( WaitForSingleObject( m_hndl, INFINITE ) != WAIT_OBJECT_0 ) 
     
    691691        } 
    692692 
    693         version( Win32
     693        version( Windows
    694694        { 
    695695            uint ecode = 0; 
     
    738738    final int priority() 
    739739    { 
    740         version( Win32
     740        version( Windows
    741741        { 
    742742            return GetThreadPriority( m_hndl ); 
     
    762762    final void priority( int val ) 
    763763    { 
    764         version( Win32
     764        version( Windows
    765765        { 
    766766            if( !SetThreadPriority( m_hndl, val ) ) 
     
    806806     * ------------------------------------------------------------------------ 
    807807     * 
    808      * Thread.sleep( 500 );        // sleep for 50 milliseconds 
     808     * Thread.sleep( 500_000 );    // sleep for 50 milliseconds 
    809809     * Thread.sleep( 50_000_000 ); // sleep for 5 seconds 
    810810     * 
     
    818818    body 
    819819    { 
    820         version( Win32
     820        version( Windows
    821821        { 
    822822            enum : uint 
     
    826826            } 
    827827 
    828             period = period < TICKS_PER_MILLI ? 
    829                         1 : 
    830                         period / TICKS_PER_MILLI; 
     828            // NOTE: In instances where all other threads in the process have a 
     829            //       lower priority than the current thread, the current thread 
     830            //       will not yield with a sleep time of zero.  However, unlike 
     831            //       yield(), the user is not asking for a yield to occur but 
     832            //       only for execution to suspend for the requested interval. 
     833            //       Therefore, expected performance may not be met if a yield 
     834            //       is forced upon the user. 
     835            period /= TICKS_PER_MILLI; 
    831836            while( period > MAX_SLEEP_MILLIS ) 
    832837            { 
     
    883888    static void yield() 
    884889    { 
    885         version( Win32
     890        version( Windows
    886891        { 
    887892            // NOTE: Sleep(1) is necessary because Sleep(0) does not give 
     
    914919        //       completed.  See thread_suspendAll for more information 
    915920        //       on why this might occur. 
    916         version( Win32
     921        version( Windows
    917922        { 
    918923            return cast(Thread) TlsGetValue( sm_this ); 
     
    10841089    static this() 
    10851090    { 
    1086         version( Win32
     1091        version( Windows
    10871092        { 
    10881093            PRIORITY_MIN = -15; 
     
    11541159    // Standard types 
    11551160    // 
    1156     version( Win32
     1161    version( Windows
    11571162    { 
    11581163        alias uint TLSKey; 
     
    11781183    // Standard thread data 
    11791184    // 
    1180     version( Win32
     1185    version( Windows
    11811186    { 
    11821187        HANDLE          m_hndl; 
     
    12101215    static void setThis( Thread t ) 
    12111216    { 
    1212         version( Win32
     1217        version( Windows
    12131218        { 
    12141219            TlsSetValue( sm_this, cast(void*) t ); 
     
    12771282    bool                m_lock; 
    12781283 
    1279     version( Win32
     1284    version( Windows
    12801285    { 
    12811286        uint[8]         m_reg; // edi,esi,ebp,esp,ebx,edx,ecx,eax 
     
    14381443        assert( t ); 
    14391444        assert( t.next || t.prev ); 
    1440         version( Win32
     1445        version( Windows
    14411446        { 
    14421447            // NOTE: This doesn't work for Posix as m_isRunning must be set to 
     
    14961501    //       functions to detect the condition and return immediately. 
    14971502 
    1498     version( Win32
     1503    version( Windows
    14991504    { 
    15001505        Thread.sm_this = TlsAlloc(); 
     
    15581563extern (C) void thread_attachThis() 
    15591564{ 
    1560     version( Win32
     1565    version( Windows
    15611566    { 
    15621567        Thread          thisThread  = new Thread(); 
     
    16981703    void suspend( Thread t ) 
    16991704    { 
    1700         version( Win32
     1705        version( Windows
    17011706        { 
    17021707            if( t.m_addr != GetCurrentThreadId() && SuspendThread( t.m_hndl ) == 0xFFFFFFFF ) 
     
    18421847    void resume( Thread t ) 
    18431848    { 
    1844         version( Win32
     1849        version( Windows
    18451850        { 
    18461851            if( t.m_addr != GetCurrentThreadId() && ResumeThread( t.m_hndl ) == 0xFFFFFFFF ) 
     
    19631968        } 
    19641969    } 
    1965     version( Win32
     1970    version( Windows
    19661971    { 
    19671972        for( Thread t = Thread.sm_tbeg; t; t = t.next ) 
     
    22432248        else 
    22442249        { 
    2245             version( Win32
     2250            version( Windows
    22462251                version = AsmX86_Win32; 
    22472252            else version( Posix ) 
     
    27622767    static Fiber getThis() 
    27632768    { 
    2764         version( Win32
     2769        version( Windows
    27652770        { 
    27662771            return cast(Fiber) TlsGetValue( sm_this ); 
     
    27802785    static this() 
    27812786    { 
    2782         version( Win32
     2787        version( Windows
    27832788        { 
    27842789            sm_this = TlsAlloc(); 
     
    31493154    static void setThis( Fiber f ) 
    31503155    { 
    3151         version( Win32
     3156        version( Windows
    31523157        { 
    31533158            TlsSetValue( sm_this, cast(void*) f );