diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/IceUtil/.depend | 1 | ||||
-rw-r--r-- | cpp/src/IceUtil/Makefile | 1 | ||||
-rw-r--r-- | cpp/src/IceUtil/Makefile.mak | 1 | ||||
-rw-r--r-- | cpp/src/IceUtil/Mutex.cpp | 30 | ||||
-rw-r--r-- | cpp/src/IceUtil/RecMutex.cpp | 115 | ||||
-rw-r--r-- | cpp/src/IceUtil/StaticMutex.cpp | 40 |
6 files changed, 53 insertions, 135 deletions
diff --git a/cpp/src/IceUtil/.depend b/cpp/src/IceUtil/.depend index ab30f546191..41b5147f63e 100644 --- a/cpp/src/IceUtil/.depend +++ b/cpp/src/IceUtil/.depend @@ -9,6 +9,7 @@ InputUtil$(OBJEXT): InputUtil.cpp $(includedir)/IceUtil/InputUtil.h $(includedir Options$(OBJEXT): Options.cpp $(includedir)/IceUtil/Options.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/RecMutex.h $(includedir)/IceUtil/Lock.h $(includedir)/IceUtil/ThreadException.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/Time.h $(includedir)/IceUtil/Shared.h $(includedir)/IceUtil/Mutex.h $(includedir)/IceUtil/Handle.h OutputUtil$(OBJEXT): OutputUtil.cpp $(includedir)/IceUtil/OutputUtil.h $(includedir)/IceUtil/Config.h Random$(OBJEXT): Random.cpp $(includedir)/IceUtil/Random.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/StaticMutex.h $(includedir)/IceUtil/Lock.h $(includedir)/IceUtil/ThreadException.h $(includedir)/IceUtil/Time.h +Mutex$(OBJEXT): Mutex.cpp $(includedir)/IceUtil/Mutex.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Lock.h $(includedir)/IceUtil/ThreadException.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/Time.h RWRecMutex$(OBJEXT): RWRecMutex.cpp $(includedir)/IceUtil/RWRecMutex.h $(includedir)/IceUtil/Mutex.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Lock.h $(includedir)/IceUtil/ThreadException.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/Time.h $(includedir)/IceUtil/Cond.h $(includedir)/IceUtil/Thread.h $(includedir)/IceUtil/Shared.h $(includedir)/IceUtil/Handle.h RecMutex$(OBJEXT): RecMutex.cpp $(includedir)/IceUtil/RecMutex.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Lock.h $(includedir)/IceUtil/ThreadException.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/Time.h StaticMutex$(OBJEXT): StaticMutex.cpp $(includedir)/IceUtil/StaticMutex.h $(includedir)/IceUtil/Config.h $(includedir)/IceUtil/Lock.h $(includedir)/IceUtil/ThreadException.h $(includedir)/IceUtil/Exception.h $(includedir)/IceUtil/Time.h diff --git a/cpp/src/IceUtil/Makefile b/cpp/src/IceUtil/Makefile index 033414933b6..abe74995a26 100644 --- a/cpp/src/IceUtil/Makefile +++ b/cpp/src/IceUtil/Makefile @@ -26,6 +26,7 @@ OBJS = ArgVector.o \ Options.o \ OutputUtil.o \ Random.o \ + Mutex.o \ RWRecMutex.o \ RecMutex.o \ StaticMutex.o \ diff --git a/cpp/src/IceUtil/Makefile.mak b/cpp/src/IceUtil/Makefile.mak index 6e646c22767..cb5039170f4 100644 --- a/cpp/src/IceUtil/Makefile.mak +++ b/cpp/src/IceUtil/Makefile.mak @@ -25,6 +25,7 @@ OBJS = ArgVector.obj \ Options.obj \ OutputUtil.obj \ Random.obj \ + Mutex.obj \ RWRecMutex.obj \ RecMutex.obj \ StaticMutex.obj \ diff --git a/cpp/src/IceUtil/Mutex.cpp b/cpp/src/IceUtil/Mutex.cpp new file mode 100644 index 00000000000..32e1036a730 --- /dev/null +++ b/cpp/src/IceUtil/Mutex.cpp @@ -0,0 +1,30 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#include <IceUtil/Mutex.h> +#include <IceUtil/Exception.h> + +#ifdef _WIN32 + +bool +IceUtil::Mutex::tryLock() const +{ + if(!TryEnterCriticalSection(&_mutex)) + { + return false; + } + if(_mutex.RecursionCount > 1) + { + LeaveCriticalSection(&_mutex); + throw ThreadLockedException(__FILE__, __LINE__); + } + return true; +} + +#endif diff --git a/cpp/src/IceUtil/RecMutex.cpp b/cpp/src/IceUtil/RecMutex.cpp index 7a68dd18d4a..2e185ff99a6 100644 --- a/cpp/src/IceUtil/RecMutex.cpp +++ b/cpp/src/IceUtil/RecMutex.cpp @@ -14,8 +14,6 @@ using namespace std; #ifdef _WIN32 -# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 - IceUtil::RecMutex::RecMutex() : _count(0) { @@ -76,119 +74,6 @@ IceUtil::RecMutex::lock(LockState& state) const _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() : diff --git a/cpp/src/IceUtil/StaticMutex.cpp b/cpp/src/IceUtil/StaticMutex.cpp index 53fa1f812b3..8084d79c666 100644 --- a/cpp/src/IceUtil/StaticMutex.cpp +++ b/cpp/src/IceUtil/StaticMutex.cpp @@ -18,11 +18,7 @@ using namespace std; static CRITICAL_SECTION _criticalSection; -# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 typedef list<CRITICAL_SECTION*> MutexList; -# else -typedef list<HANDLE> MutexList; -# endif static MutexList* _mutexList; @@ -53,17 +49,12 @@ Init::Init() Init::~Init() { -# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 for(MutexList::iterator p = _mutexList->begin(); p != _mutexList->end(); ++p) { DeleteCriticalSection(*p); delete *p; } -# else - for_each(_mutexList->begin(), _mutexList->end(), - CloseHandle); -# endif delete _mutexList; DeleteCriticalSection(&_criticalSection); } @@ -82,19 +73,8 @@ void IceUtil::StaticMutex::initialize() const // if(_mutex == 0) { -# if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 CRITICAL_SECTION* newMutex = new CRITICAL_SECTION; InitializeCriticalSection(newMutex); -# else - _recursionCount = 0; - - HANDLE newMutex = CreateMutex(0, false, 0); - if(newMutex == 0) - { - LeaveCriticalSection(&_criticalSection); - throw ThreadSyscallException(__FILE__, __LINE__, GetLastError()); - } -# endif // // _mutex is written after the new initialized CRITICAL_SECTION/Mutex @@ -106,6 +86,26 @@ void IceUtil::StaticMutex::initialize() const } LeaveCriticalSection(&_criticalSection); } + +bool +IceUtil::StaticMutex::tryLock() const +{ + if(!initialized()) + { + initialize(); + } + if(!TryEnterCriticalSection(_mutex)) + { + return false; + } + if(_mutex->RecursionCount > 1) + { + LeaveCriticalSection(_mutex); + throw ThreadLockedException(__FILE__, __LINE__); + } + return true; +} + #endif IceUtil::StaticMutex IceUtil::globalMutex = ICE_STATIC_MUTEX_INITIALIZER; |