diff options
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/include/Ice/FactoryTable.h | 1 | ||||
-rw-r--r-- | cpp/include/IceUtil/RWRecMutex.h | 83 | ||||
-rw-r--r-- | cpp/include/IceUtil/StaticMutex.h | 149 | ||||
-rw-r--r-- | cpp/src/IceUtil/RWRecMutex.cpp | 8 | ||||
-rw-r--r-- | cpp/src/IceUtil/StaticMutex.cpp | 135 | ||||
-rw-r--r-- | cpp/test/IceUtil/thread/RWRecMutexTest.cpp | 24 | ||||
-rw-r--r-- | cpp/test/IceUtil/thread/StaticMutexTest.cpp | 8 |
7 files changed, 217 insertions, 191 deletions
diff --git a/cpp/include/Ice/FactoryTable.h b/cpp/include/Ice/FactoryTable.h index 07c5c3d1bbc..9453e4cd8dd 100644 --- a/cpp/include/Ice/FactoryTable.h +++ b/cpp/include/Ice/FactoryTable.h @@ -10,7 +10,6 @@ #ifndef ICE_FACTORYTABLE_H #define ICE_FACTORYTABLE_H -#include <IceUtil/StaticMutex.h> #include <IceUtil/Mutex.h> #include <Ice/UserExceptionFactory.h> #include <Ice/ObjectFactoryF.h> diff --git a/cpp/include/IceUtil/RWRecMutex.h b/cpp/include/IceUtil/RWRecMutex.h index b2433b62b5d..0abe571180c 100644 --- a/cpp/include/IceUtil/RWRecMutex.h +++ b/cpp/include/IceUtil/RWRecMutex.h @@ -16,15 +16,19 @@ namespace IceUtil { + +// +// All classes defined in this file are now deprecated. +// class ICE_UTIL_API DeadlockException : public Exception { public: - DeadlockException(const char*, int); - virtual std::string ice_name() const; - virtual Exception* ice_clone() const; - virtual void ice_throw() const; + ICE_DEPRECATED_API DeadlockException(const char*, int); + ICE_DEPRECATED_API virtual std::string ice_name() const; + ICE_DEPRECATED_API virtual Exception* ice_clone() const; + ICE_DEPRECATED_API virtual void ice_throw() const; private: @@ -36,14 +40,14 @@ class RLockT { public: - RLockT(const T& mutex) : + ICE_DEPRECATED_API RLockT(const T& mutex) : _mutex(mutex) { _mutex.readLock(); _acquired = true; } - ~RLockT() + ICE_DEPRECATED_API ~RLockT() { if (_acquired) { @@ -51,7 +55,7 @@ public: } } - void acquire() const + ICE_DEPRECATED_API void acquire() const { if (_acquired) { @@ -61,7 +65,7 @@ public: _acquired = true; } - bool tryAcquire() const + ICE_DEPRECATED_API bool tryAcquire() const { if (_acquired) { @@ -71,7 +75,7 @@ public: return _acquired; } - bool timedAcquire(const Time& timeout) const + ICE_DEPRECATED_API bool timedAcquire(const Time& timeout) const { if (_acquired) { @@ -79,11 +83,9 @@ public: } _acquired = _mutex.timedReadLock(timeout); return _acquired; - } + } - - - void release() const + ICE_DEPRECATED_API void release() const { if (!_acquired) { @@ -93,24 +95,24 @@ public: _acquired = false; } - bool acquired() const + ICE_DEPRECATED_API bool acquired() const { return _acquired; } - void + ICE_DEPRECATED_API void upgrade() const { _mutex.upgrade(); } - bool + ICE_DEPRECATED_API bool timedUpgrade(const Time& timeout) const { return _mutex.timedUpgrade(timeout); } - void + ICE_DEPRECATED_API void downgrade() const { _mutex.downgrade(); @@ -150,12 +152,12 @@ class TryRLockT : public RLockT<T> { public: - TryRLockT(const T& mutex) : + ICE_DEPRECATED_API TryRLockT(const T& mutex) : RLockT<T>(mutex, true) { } - TryRLockT(const T& mutex, const Time& timeout) : + ICE_DEPRECATED_API TryRLockT(const T& mutex, const Time& timeout) : RLockT<T>(mutex, timeout) { } @@ -166,14 +168,14 @@ class WLockT { public: - WLockT(const T& mutex) : + ICE_DEPRECATED_API WLockT(const T& mutex) : _mutex(mutex) { _mutex.writeLock(); _acquired = true; } - ~WLockT() + ICE_DEPRECATED_API ~WLockT() { if(_acquired) { @@ -181,7 +183,7 @@ public: } } - void acquire() const + ICE_DEPRECATED_API void acquire() const { if (_acquired) { @@ -191,7 +193,7 @@ public: _acquired = true; } - bool tryAcquire() const + ICE_DEPRECATED_API bool tryAcquire() const { if (_acquired) { @@ -201,7 +203,7 @@ public: return _acquired; } - bool timedAcquire(const Time& timeout) const + ICE_DEPRECATED_API bool timedAcquire(const Time& timeout) const { if (_acquired) { @@ -211,7 +213,7 @@ public: return _acquired; } - void release() const + ICE_DEPRECATED_API void release() const { if (!_acquired) { @@ -221,7 +223,7 @@ public: _acquired = false; } - bool acquired() const + ICE_DEPRECATED_API bool acquired() const { return _acquired; } @@ -258,12 +260,12 @@ class TryWLockT : public WLockT<T> { public: - TryWLockT(const T& mutex) : + ICE_DEPRECATED_API TryWLockT(const T& mutex) : WLockT<T>(mutex, true) { } - TryWLockT(const T& mutex, const Time& timeout) : + ICE_DEPRECATED_API TryWLockT(const T& mutex, const Time& timeout) : WLockT<T>(mutex, timeout) { } @@ -279,6 +281,7 @@ public: // upgrade() or timedUpgrade() while holding a read lock promotes // the reader to a writer lock. // + class ICE_UTIL_API RWRecMutex { public: @@ -291,8 +294,8 @@ public: typedef WLockT<RWRecMutex> WLock; typedef TryWLockT<RWRecMutex> TryWLock; - RWRecMutex(); - ~RWRecMutex(); + ICE_DEPRECATED_API RWRecMutex(); + ICE_DEPRECATED_API ~RWRecMutex(); // // Note that readLock/writeLock & unlock in general should not be @@ -302,55 +305,55 @@ public: // // Acquire a read lock. // - void readLock() const; + ICE_DEPRECATED_API void readLock() const; // // Try to acquire a read lock. // - bool tryReadLock() const; + ICE_DEPRECATED_API bool tryReadLock() const; // // Try to acquire a read lock for upto the given timeout. // - bool timedReadLock(const Time&) const; + ICE_DEPRECATED_API bool timedReadLock(const Time&) const; // // Acquire a write lock. // - void writeLock() const; + ICE_DEPRECATED_API void writeLock() const; // // Acquire a write lock. // - bool tryWriteLock() const; + ICE_DEPRECATED_API bool tryWriteLock() const; // // Acquire a write lock for up to the given timeout. // - bool timedWriteLock(const Time&) const; + ICE_DEPRECATED_API bool timedWriteLock(const Time&) const; // // Unlock the reader/writer lock. // - void unlock() const; + ICE_DEPRECATED_API void unlock() const; // // Upgrade the read lock to a writer lock. Note that this method // can only be called if the reader lock is not held recursively. // - void upgrade() const; + ICE_DEPRECATED_API void upgrade() const; // // Upgrade the read lock to a writer lock for up to the given // timeout Note that this method can only be called if the reader // lock is not held recursively. // - bool timedUpgrade(const Time&) const; + ICE_DEPRECATED_API bool timedUpgrade(const Time&) const; // // Downgrade a write lock to a read lock. // - void downgrade() const; + ICE_DEPRECATED_API void downgrade() const; private: diff --git a/cpp/include/IceUtil/StaticMutex.h b/cpp/include/IceUtil/StaticMutex.h index d7e4d3c0d72..938d4801121 100644 --- a/cpp/include/IceUtil/StaticMutex.h +++ b/cpp/include/IceUtil/StaticMutex.h @@ -39,7 +39,7 @@ class Cond; // // -class StaticMutex +class ICE_UTIL_API StaticMutex { public: @@ -54,20 +54,20 @@ public: // directly. Instead use Lock & TryLock. // - void lock() const; + ICE_DEPRECATED_API void lock() const; // // Returns true if the lock was acquired, and false otherwise. // - bool tryLock() const; + ICE_DEPRECATED_API bool tryLock() const; - void unlock() const; + ICE_DEPRECATED_API void unlock() const; #ifdef _WIN32 - mutable CRITICAL_SECTION* _mutex; + ICE_DEPRECATED_API mutable CRITICAL_SECTION* _mutex; #else - mutable pthread_mutex_t _mutex; + ICE_DEPRECATED_API mutable pthread_mutex_t _mutex; #endif @@ -98,8 +98,8 @@ private: void lock(LockState&) const; #ifdef _WIN32 - inline bool initialized() const; - ICE_UTIL_API void initialize() const; + bool initialized() const; + void initialize() const; #endif #ifndef _MSC_VER @@ -114,139 +114,6 @@ private: # define ICE_STATIC_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } #endif -// -// For performance reasons the following functions are inlined. -// - -#ifdef _WIN32 - -inline bool -StaticMutex::initialized() const -{ - // - // Read mutex and then inserts a memory barrier to ensure we can't - // see tmp != 0 before we see the initialized object - // - void* tmp = _mutex; - return InterlockedCompareExchangePointer(reinterpret_cast<void**>(&tmp), 0, 0) != 0; -} - -inline void -StaticMutex::lock() const -{ - if(!initialized()) - { - initialize(); - } - EnterCriticalSection(_mutex); - assert(_mutex->RecursionCount == 1); -} - -inline bool -StaticMutex::tryLock() const -{ - if(!initialized()) - { - initialize(); - } - if(!TryEnterCriticalSection(_mutex)) - { - return false; - } - if(_mutex->RecursionCount > 1) - { - LeaveCriticalSection(_mutex); - throw ThreadLockedException(__FILE__, __LINE__); - } - return true; -} - -inline void -StaticMutex::unlock() const -{ - assert(_mutex != 0); - assert(_mutex->RecursionCount == 1); - LeaveCriticalSection(_mutex); -} - -inline void -StaticMutex::unlock(LockState&) const -{ - assert(_mutex != 0); - assert(_mutex->RecursionCount == 1); - LeaveCriticalSection(_mutex); -} - -inline void -StaticMutex::lock(LockState&) const -{ - if(!initialized()) - { - initialize(); - } - EnterCriticalSection(_mutex); -} - -#else - -inline void -StaticMutex::lock() const -{ - int rc = pthread_mutex_lock(&_mutex); - if(rc != 0) - { - if(rc == EDEADLK) - { - throw ThreadLockedException(__FILE__, __LINE__); - } - else - { - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } - } -} - -inline bool -StaticMutex::tryLock() const -{ - int rc = pthread_mutex_trylock(&_mutex); - if(rc != 0 && rc != EBUSY) - { - if(rc == EDEADLK) - { - throw ThreadLockedException(__FILE__, __LINE__); - } - else - { - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } - } - return (rc == 0); -} - -inline void -StaticMutex::unlock() const -{ - int rc = pthread_mutex_unlock(&_mutex); - if(rc != 0) - { - throw ThreadSyscallException(__FILE__, __LINE__, rc); - } -} - -inline void -StaticMutex::unlock(LockState& state) const -{ - state.mutex = &_mutex; -} - -inline void -StaticMutex::lock(LockState&) const -{ -} - -#endif - } // End namespace IceUtil #endif diff --git a/cpp/src/IceUtil/RWRecMutex.cpp b/cpp/src/IceUtil/RWRecMutex.cpp index 6cede10a3f9..326d331eed4 100644 --- a/cpp/src/IceUtil/RWRecMutex.cpp +++ b/cpp/src/IceUtil/RWRecMutex.cpp @@ -7,6 +7,14 @@ // // ********************************************************************** +// +// We disable deprecation warning here, to allow clean compilation of +// of deprecated methods. +// +#ifdef _MSC_VER +# pragma warning( disable : 4996 ) +#endif + #include <IceUtil/RWRecMutex.h> #include <IceUtil/Exception.h> #include <IceUtil/Time.h> diff --git a/cpp/src/IceUtil/StaticMutex.cpp b/cpp/src/IceUtil/StaticMutex.cpp index 9c5b4d15a3f..f5f307d9d24 100644 --- a/cpp/src/IceUtil/StaticMutex.cpp +++ b/cpp/src/IceUtil/StaticMutex.cpp @@ -7,6 +7,14 @@ // // ********************************************************************** +// +// We disable deprecation warning here, to allow clean compilation of +// of deprecated methods. +// +#ifdef _MSC_VER +# pragma warning( disable : 4996 ) +#endif + #include <IceUtil/StaticMutex.h> #include <IceUtil/ThreadException.h> @@ -72,5 +80,130 @@ void IceUtil::StaticMutex::initialize() const } LeaveCriticalSection(&_criticalSection); } -#endif +bool +IceUtil::StaticMutex::initialized() const +{ + // + // Read mutex and then inserts a memory barrier to ensure we can't + // see tmp != 0 before we see the initialized object + // + void* tmp = _mutex; + return InterlockedCompareExchangePointer(reinterpret_cast<void**>(&tmp), 0, 0) != 0; +} + +void +IceUtil::StaticMutex::lock() const +{ + if(!initialized()) + { + initialize(); + } + EnterCriticalSection(_mutex); + assert(_mutex->RecursionCount == 1); +} + +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; +} + +void +IceUtil::StaticMutex::unlock() const +{ + assert(_mutex != 0); + assert(_mutex->RecursionCount == 1); + LeaveCriticalSection(_mutex); +} + +void +IceUtil::StaticMutex::unlock(LockState&) const +{ + assert(_mutex != 0); + assert(_mutex->RecursionCount == 1); + LeaveCriticalSection(_mutex); +} + +void +IceUtil::StaticMutex::lock(LockState&) const +{ + if(!initialized()) + { + initialize(); + } + EnterCriticalSection(_mutex); +} + +#else + +void +IceUtil::StaticMutex::lock() const +{ + int rc = pthread_mutex_lock(&_mutex); + if(rc != 0) + { + if(rc == EDEADLK) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + else + { + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } + } +} + +bool +IceUtil::StaticMutex::tryLock() const +{ + int rc = pthread_mutex_trylock(&_mutex); + if(rc != 0 && rc != EBUSY) + { + if(rc == EDEADLK) + { + throw ThreadLockedException(__FILE__, __LINE__); + } + else + { + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } + } + return (rc == 0); +} + +void +IceUtil::StaticMutex::unlock() const +{ + int rc = pthread_mutex_unlock(&_mutex); + if(rc != 0) + { + throw ThreadSyscallException(__FILE__, __LINE__, rc); + } +} + +void +IceUtil::StaticMutex::unlock(LockState& state) const +{ + state.mutex = &_mutex; +} + +void +IceUtil::StaticMutex::lock(LockState&) const +{ +} + +#endif diff --git a/cpp/test/IceUtil/thread/RWRecMutexTest.cpp b/cpp/test/IceUtil/thread/RWRecMutexTest.cpp index 167c05bac9a..a74fda1e708 100644 --- a/cpp/test/IceUtil/thread/RWRecMutexTest.cpp +++ b/cpp/test/IceUtil/thread/RWRecMutexTest.cpp @@ -7,6 +7,14 @@ // // ********************************************************************** +// +// We disable deprecation warning here, to allow clean compilation of +// of deprecated methods. +// +#ifdef _MSC_VER +# pragma warning( disable : 4996 ) +#endif + #include <IceUtil/IceUtil.h> #include <RWRecMutexTest.h> @@ -837,15 +845,15 @@ RWRecMutexTest::run() // write thread cannot get the write lock. // t1->waitUpgrade(); -
- RWRecMutexWriteThreadPtr t2 = new RWRecMutexWriteThread(mutex);
- ThreadControl control2 = t2->start();
+ + RWRecMutexWriteThreadPtr t2 = new RWRecMutexWriteThread(mutex); + ThreadControl control2 = t2->start(); t2->waitWrite(); - //
- // Its necessary to sleep for 1 second to ensure that the
- // thread is actually IN the write lock and waiting.
- //
- ThreadControl::sleep(Time::seconds(1));
+ // + // Its necessary to sleep for 1 second to ensure that the + // thread is actually IN the write lock and waiting. + // + ThreadControl::sleep(Time::seconds(1)); // // Unlocking the read mutex lets the upgrade continue. At this diff --git a/cpp/test/IceUtil/thread/StaticMutexTest.cpp b/cpp/test/IceUtil/thread/StaticMutexTest.cpp index 1497540fb65..958c0670cce 100644 --- a/cpp/test/IceUtil/thread/StaticMutexTest.cpp +++ b/cpp/test/IceUtil/thread/StaticMutexTest.cpp @@ -7,6 +7,14 @@ // // ********************************************************************** +// +// We disable deprecation warning here, to allow clean compilation of +// of deprecated methods. +// +#ifdef _MSC_VER +# pragma warning( disable : 4996 ) +#endif + #include <IceUtil/IceUtil.h> #include <StaticMutexTest.h> |