diff options
author | Bernard Normier <bernard@zeroc.com> | 2003-09-03 18:16:10 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2003-09-03 18:16:10 +0000 |
commit | 1454e95d7c987919063cb22f1f7f528fb7626ae9 (patch) | |
tree | 2424f28e2416d5429bc33a1c38b8ac712622ba2e /cpp/src/IceUtil/RecMutex.cpp | |
parent | use ICE_HOME (if present) to find translator (diff) | |
download | ice-1454e95d7c987919063cb22f1f7f528fb7626ae9.tar.bz2 ice-1454e95d7c987919063cb22f1f7f528fb7626ae9.tar.xz ice-1454e95d7c987919063cb22f1f7f528fb7626ae9.zip |
Merged from 1.1
Diffstat (limited to 'cpp/src/IceUtil/RecMutex.cpp')
-rw-r--r-- | cpp/src/IceUtil/RecMutex.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/cpp/src/IceUtil/RecMutex.cpp b/cpp/src/IceUtil/RecMutex.cpp index 0addcc0f61f..2221a227480 100644 --- a/cpp/src/IceUtil/RecMutex.cpp +++ b/cpp/src/IceUtil/RecMutex.cpp @@ -19,6 +19,8 @@ using namespace std; #ifdef _WIN32 +# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 + IceUtil::RecMutex::RecMutex() : _count(0) { @@ -78,6 +80,120 @@ IceUtil::RecMutex::lock(LockState& state) const EnterCriticalSection(&_mutex); _count = state.count; } + +# else + +IceUtil::RecMutex::RecMutex() : + _count(0) +{ + _mutex = CreateMutex(0, false, 0); + if(_mutex == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } +} + +IceUtil::RecMutex::~RecMutex() +{ + assert(_count == 0); + BOOL rc = CloseHandle(_mutex); + if(rc == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } +} + +void +IceUtil::RecMutex::lock() const +{ + DWORD rc = WaitForSingleObject(_mutex, INFINITE); + if(rc != WAIT_OBJECT_0) + { + if(rc == WAIT_FAILED) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + else + { + throw ThreadSyscallException(__FILE__, __LINE__, 0); + } + } + + if(++_count > 1) + { + BOOL rc2 = ReleaseMutex(_mutex); + if(rc2 == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + } +} + +bool +IceUtil::RecMutex::tryLock() const +{ + DWORD rc = WaitForSingleObject(_mutex, 0); + if(rc != WAIT_OBJECT_0) + { + return false; + } + if(++_count > 1) + { + BOOL rc2 = ReleaseMutex(_mutex); + if(rc2 == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + } + return true; +} + +void +IceUtil::RecMutex::unlock() const +{ + if(--_count == 0) + { + BOOL rc = ReleaseMutex(_mutex); + if(rc == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + } +} + +void +IceUtil::RecMutex::unlock(LockState& state) const +{ + state.count = _count; + _count = 0; + BOOL rc = ReleaseMutex(_mutex); + if(rc == 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } +} + +void +IceUtil::RecMutex::lock(LockState& state) const +{ + DWORD rc = WaitForSingleObject(_mutex, INFINITE); + if(rc != WAIT_OBJECT_0) + { + if(rc == WAIT_FAILED) + { + throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); + } + else + { + throw ThreadSyscallException(__FILE__, __LINE__, 0); + } + } + + _count = state.count; +} + +# endif + #else IceUtil::RecMutex::RecMutex() : |