Changeset 739db5a in nscp for include/Mutex.h


Ignore:
Timestamp:
09/27/08 19:06:35 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
37e6081
Parents:
9567d4b
Message:

First attempt at merging stable changes over to here (probably needs more work, but compiles and starts...)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • include/Mutex.h

    r9567d4b r739db5a  
    2525#include <windows.h> 
    2626#include <iostream> 
     27#include <error.hpp> 
     28 
     29class mutex_exception { 
     30  std::wstring what_; 
     31public: 
     32  mutex_exception(std::wstring what) : what_(what) {} 
     33  std::wstring what() { return what_; } 
     34}; 
    2735 
    2836/** 
     
    5058 */ 
    5159class MutexHandler { 
     60public: 
    5261private: 
    5362  HANDLE hMutex; 
     63  DWORD dwWaitResult; 
     64  bool bCreated; 
    5465public: 
    5566  /** 
     
    5768   * Creates an unnamed mutex. 
    5869   */ 
    59   MutexHandler() : hMutex(NULL) { 
    60     hMutex = CreateMutex(NULL, FALSE, NULL); 
     70  MutexHandler(std::wstring name = _T("")) : hMutex(NULL), dwWaitResult(0), bCreated(false) { 
     71    //std::wcout << _T("Creating mutex: ") << name << std::endl; 
     72    hMutex = CreateMutex(NULL, FALSE, name.empty()?NULL:name.c_str()); 
    6173    if (hMutex == NULL && GetLastError() == ERROR_ALREADY_EXISTS ) 
    62       hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, NULL); 
    63     if (hMutex == NULL) { 
    64       std::wcout << _T("*** *** *** Error in mutex creation: ") << GetLastError() << std::endl; 
    65     } 
     74      hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, name.empty()?NULL:name.c_str()); 
     75    else  
     76      bCreated = true; 
     77    if (hMutex == NULL) 
     78      throw mutex_exception(_T("Failed to create mutex: ") + error::lookup::last_error()); 
    6679  } 
    6780  /** 
     
    7487    hMutex = NULL; 
    7588  } 
     89  void close() { 
     90    if (hMutex) 
     91      CloseHandle(hMutex); 
     92    hMutex = NULL; 
     93  } 
     94  bool mutexWasCreated() { 
     95    return bCreated; 
     96  } 
    7697  /** 
    7798   * HANDLe cast operator to retrieve the handle from the enclosed mutex object. 
     
    80101  operator HANDLE () const { 
    81102    return hMutex; 
     103  } 
     104  /** 
     105  * Release the mutex 
     106  */ 
     107  void releaseLock() { 
     108    if (hMutex == NULL) 
     109      throw mutex_exception(_T("Failed to release mutex lock (mutex handle is null)")); 
     110    if (!ReleaseMutex(hMutex)) 
     111      throw mutex_exception(_T("Failed to relase the mutex: ") + error::lookup::last_error()); 
     112  } 
     113  /** 
     114  * Waits for the mutex object. 
     115  * @timeout The timeout before abandoning wait 
     116  */ 
     117  bool accuireLock(DWORD timeout = 5000L) { 
     118    if (hMutex == NULL) 
     119      throw mutex_exception(_T("Failed to get mutex lock (mutex handle is null)")); 
     120    dwWaitResult = WaitForSingleObject(hMutex, timeout); 
     121    switch (dwWaitResult) { 
     122      // The thread got mutex ownership. 
     123    case WAIT_OBJECT_0: 
     124      return true; 
     125    case WAIT_TIMEOUT:  
     126      return false; 
     127    case WAIT_ABANDONED:  
     128      return true; 
     129    default: 
     130      throw mutex_exception(_T("Unknown returncode from the mutex: ") + strEx::itos(dwWaitResult)); 
     131       
     132    } 
     133  } 
     134  /** 
     135  * Get the result of the wait operation. 
     136  * @return Result of the wait operation 
     137  */ 
     138  DWORD getWaitResult() const { 
     139    return dwWaitResult; 
    82140  } 
    83141}; 
     
    125183  MutexLock(HANDLE hMutex, DWORD timeout = 5000L) : bHasMutex(false), hMutex_(hMutex) { 
    126184    if (hMutex_ == NULL) { 
    127       std::wcout << _T("*** *** *** Error in mutex lock: ") << std::endl; 
     185      throw mutex_exception(_T("Failed to get mutex lock (mutex handle is null)")); 
     186      /* 
    128187      bHasMutex = false; 
    129188      return; 
     189      */ 
    130190    } 
    131191    dwWaitResult = WaitForSingleObject(hMutex_, timeout); 
     
    135195      bHasMutex = true; 
    136196      break; 
    137         case WAIT_TIMEOUT:  
    138       bHasMutex = false; 
    139       break; 
    140         case WAIT_ABANDONED:  
     197        case WAIT_TIMEOUT: 
     198      bHasMutex = false; 
     199      break; 
     200        case WAIT_ABANDONED: 
    141201      bHasMutex = false; 
    142202      break; 
     
    195255  void lock(DWORD timeout = 5000L) { 
    196256    if (hMutex_ == NULL) { 
    197       std::wcout << _T("*** *** *** Error in mutex lock: ") << std::endl; 
     257      throw mutex_exception(_T("Failed to get mutex lock (mutex handle is null)")); 
     258      /* 
     259      std::wcout << _T("Error in mutex lock: ") << std::endl; 
    198260      bHasMutex = false; 
    199261      return; 
     262      */ 
    200263    } 
    201264    dwWaitResult = WaitForSingleObject(hMutex_, timeout); 
Note: See TracChangeset for help on using the changeset viewer.