Changeset 739db5a in nscp for include/Mutex.h
- Timestamp:
- 09/27/08 19:06:35 (5 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 37e6081
- Parents:
- 9567d4b
- File:
-
- 1 edited
-
include/Mutex.h (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/Mutex.h
r9567d4b r739db5a 25 25 #include <windows.h> 26 26 #include <iostream> 27 #include <error.hpp> 28 29 class mutex_exception { 30 std::wstring what_; 31 public: 32 mutex_exception(std::wstring what) : what_(what) {} 33 std::wstring what() { return what_; } 34 }; 27 35 28 36 /** … … 50 58 */ 51 59 class MutexHandler { 60 public: 52 61 private: 53 62 HANDLE hMutex; 63 DWORD dwWaitResult; 64 bool bCreated; 54 65 public: 55 66 /** … … 57 68 * Creates an unnamed mutex. 58 69 */ 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()); 61 73 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()); 66 79 } 67 80 /** … … 74 87 hMutex = NULL; 75 88 } 89 void close() { 90 if (hMutex) 91 CloseHandle(hMutex); 92 hMutex = NULL; 93 } 94 bool mutexWasCreated() { 95 return bCreated; 96 } 76 97 /** 77 98 * HANDLe cast operator to retrieve the handle from the enclosed mutex object. … … 80 101 operator HANDLE () const { 81 102 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; 82 140 } 83 141 }; … … 125 183 MutexLock(HANDLE hMutex, DWORD timeout = 5000L) : bHasMutex(false), hMutex_(hMutex) { 126 184 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 /* 128 187 bHasMutex = false; 129 188 return; 189 */ 130 190 } 131 191 dwWaitResult = WaitForSingleObject(hMutex_, timeout); … … 135 195 bHasMutex = true; 136 196 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: 141 201 bHasMutex = false; 142 202 break; … … 195 255 void lock(DWORD timeout = 5000L) { 196 256 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; 198 260 bHasMutex = false; 199 261 return; 262 */ 200 263 } 201 264 dwWaitResult = WaitForSingleObject(hMutex_, timeout);
Note: See TracChangeset
for help on using the changeset viewer.








