Changeset 5443

Show
Ignore:
Timestamp:
08/13/06 09:17:42
Author:
robert
Message:

From Paul Martz, changed win32 Mutex implementation across to using EnterCriticalSection/LeaveCriticalSection?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenThreads/trunk/win32_src/Win32Mutex.cpp

    r5313 r5443  
    2828using namespace OpenThreads; 
    2929 
     30Win32MutexPrivateData::Win32MutexPrivateData() 
     31{ 
     32#ifdef USE_CRITICAL_SECTION 
     33    InitializeCriticalSection( &_cs ); 
     34#else 
     35    mutex  = 0; 
     36#endif 
     37} 
    3038Win32MutexPrivateData::~Win32MutexPrivateData() 
    3139{ 
    32 
    33  
    34  
     40#ifdef USE_CRITICAL_SECTION 
     41    DeleteCriticalSection( &_cs ); 
     42#endif 
     43
     44 
     45 
     46#ifndef USE_CRITICAL_SECTION 
    3547 
    3648template <int instance> 
     
    5567    if (__log_nsec <= 20) { 
    5668        SwitchToThread(); //Sleep(0); // adegli replaced it Sleep by SwitchToThread 
    57        } else { 
     69    } else { 
    5870        Sleep(1 << (__log_nsec - 20)); 
    59        
     71   
    6072} 
    6173 
    6274 
    6375#if defined(_MSC_VER) && _MSC_VER <= 1300 
    64        template WIN32MutexSpin <0>; 
     76    template WIN32MutexSpin <0>; 
    6577#endif 
     78 
     79#endif // USE_CRITICAL_SECTION 
    6680 
    6781//---------------------------------------------------------------------------- 
     
    7387Mutex::Mutex() { 
    7488    Win32MutexPrivateData *pd = new Win32MutexPrivateData(); 
    75     pd->mutex  = 0; 
    7689    _prvData = static_cast<void *>(pd); 
    7790} 
     
    97110        static_cast<Win32MutexPrivateData*>(_prvData); 
    98111 
     112#ifdef USE_CRITICAL_SECTION 
     113 
     114    // Block until we can take this lock. 
     115    EnterCriticalSection( &(pd->_cs) ); 
     116 
     117    return 0; 
     118 
     119#else 
     120 
    99121    volatile unsigned long* lock = &pd->mutex; 
    100        // InterlockedExchange returns old value 
    101        // if old_value  == 0 mutex wasn't locked , now it is 
    102        if( !InterlockedExchange((long*)lock, 1L)) { 
     122    // InterlockedExchange returns old value 
     123    // if old_value  == 0 mutex wasn't locked , now it is 
     124    if( !InterlockedExchange((long*)lock, 1L)) { 
    103125       return 0; 
    104126    } 
     
    136158      _S_nsec_sleep(__log_nsec); 
    137159    } 
    138         return -1; 
     160    return -1; 
     161 
     162#endif // USE_CRITICAL_SECTION 
    139163} 
    140164 
     
    148172    Win32MutexPrivateData *pd = 
    149173        static_cast<Win32MutexPrivateData*>(_prvData); 
     174 
     175#ifdef USE_CRITICAL_SECTION 
     176 
     177    // Release this lock. CRITICAL_SECTION is nested, thus 
     178    //   unlock() calls must be paired with lock() calls. 
     179    LeaveCriticalSection( &(pd->_cs) ); 
     180 
     181    return 0; 
     182 
     183#else 
    150184 
    151185    volatile unsigned long* lock = &pd->mutex; 
     
    153187    // This is not sufficient on many multiprocessors, since 
    154188    // writes to protected variables and the lock may be reordered. 
    155         return 0; 
     189    return 0; 
     190 
     191#endif // USE_CRITICAL_SECTION 
    156192} 
    157193 
     
    166202        static_cast<Win32MutexPrivateData*>(_prvData); 
    167203 
     204#ifdef USE_CRITICAL_SECTION 
     205 
     206    // Take the lock if we can; regardless don't block. 
     207    // 'result' is FALSE if we took the lock or already held 
     208    //   it amd TRUE if another thread already owns the lock. 
     209    BOOL result = TryEnterCriticalSection( &(pd->_cs) ); 
     210 
     211    return( (result==TRUE) ? 0 : 1 ); 
     212 
     213#else 
     214 
    168215    volatile unsigned long* lock = &pd->mutex; 
    169216 
    170        if( !InterlockedExchange((long*)lock, 1L)) { 
     217    if( !InterlockedExchange((long*)lock, 1L)) { 
    171218      return 1; // TRUE 
    172219    } 
    173220 
    174         return 0; // FALSE 
    175  
    176 
     221    return 0; // FALSE 
     222 
     223#endif // USE_CRITICAL_SECTION 
     224
     225 
  • OpenThreads/trunk/win32_src/Win32MutexPrivateData.h

    r5313 r5443  
    3838private: 
    3939 
    40     Win32MutexPrivateData() {}
     40    Win32MutexPrivateData()
    4141 
    4242    ~Win32MutexPrivateData(); 
    43  
     43#define USE_CRITICAL_SECTION 
     44#ifdef USE_CRITICAL_SECTION 
     45    CRITICAL_SECTION _cs; 
     46#else 
    4447    volatile unsigned long mutex; 
     48#endif 
    4549 
    4650};