diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2005-07-05 11:09:55 +0000 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2005-07-05 11:09:55 +0000 |
commit | 9b8cc712d4a41d71840416776bc94ee8485bb9b3 (patch) | |
tree | 7d467fdd6a66bc2b5878d82070d45adbd5c20414 /cppe/test/IceE/thread/StaticMutexTest.cpp | |
parent | cleaning the cache method out of ReferenceFactory (diff) | |
download | ice-9b8cc712d4a41d71840416776bc94ee8485bb9b3.tar.bz2 ice-9b8cc712d4a41d71840416776bc94ee8485bb9b3.tar.xz ice-9b8cc712d4a41d71840416776bc94ee8485bb9b3.zip |
Changed Ice to IceE EVERYWHERE!!!
Diffstat (limited to 'cppe/test/IceE/thread/StaticMutexTest.cpp')
-rw-r--r-- | cppe/test/IceE/thread/StaticMutexTest.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/cppe/test/IceE/thread/StaticMutexTest.cpp b/cppe/test/IceE/thread/StaticMutexTest.cpp new file mode 100644 index 00000000000..3ae782937c8 --- /dev/null +++ b/cppe/test/IceE/thread/StaticMutexTest.cpp @@ -0,0 +1,158 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2005 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 <IceE/IceE.h> + +#include <StaticMutexTest.h> +#include <TestCommon.h> + +using namespace std; +using namespace IceE; + +static const string mutexTestName("static mutex"); + +static StaticMutex staticMutex = ICEE_STATIC_MUTEX_INITIALIZER; + +class StaticMutexTestThread : public Thread +{ +public: + + StaticMutexTestThread() : + _tryLock(false) + { + } + + virtual void run() + { + StaticMutex::TryLock tlock(staticMutex); + test(!tlock.acquired()); + + { + Mutex::Lock lock(_tryLockMutex); + _tryLock = true; + } + _tryLockCond.signal(); + + StaticMutex::Lock lock(staticMutex); + } + + void + waitTryLock() + { + Mutex::Lock lock(_tryLockMutex); + while(!_tryLock) + { + _tryLockCond.wait(lock); + } + } + +private: + + bool _tryLock; + // + // Use native Condition variable here, not Monitor. + // + Cond _tryLockCond; + Mutex _tryLockMutex; +}; + +typedef Handle<StaticMutexTestThread> StaticMutexTestThreadPtr; + +StaticMutexTest::StaticMutexTest() : + TestBase(mutexTestName) +{ +} + +void +StaticMutexTest::run() +{ + StaticMutexTestThreadPtr t; + ThreadControl control; + + { + StaticMutex::Lock lock(staticMutex); + + // LockT testing: + // + + test(lock.acquired()); + + try + { + lock.acquire(); + test(false); + } + catch(const ThreadLockedException&) + { + // Expected + } + + try + { + lock.tryAcquire(); + test(false); + } + catch(const ThreadLockedException&) + { + // Expected + } + + test(lock.acquired()); + lock.release(); + test(!lock.acquired()); + + try + { + lock.release(); + test(false); + } + catch(const ThreadLockedException&) + { + // Expected + } + + StaticMutex::TryLock lock2(staticMutex); + // + // Under WinCE tryAcquire() does not do recursion checks. + // +#ifndef _WIN32_WCE +#ifdef __FreeBSD__ + try + { + test(lock.tryAcquire() == false); + } + catch(const IceE::ThreadSyscallException& ex) + { + // + // pthread_mutex_trylock returns EDEADLK in FreeBSD's new threading implementation. + // + test(ex.error() == EDEADLK); + } +#else + test(lock.tryAcquire() == false); +#endif + lock2.release(); + test(lock.tryAcquire() == true); + test(lock.acquired()); +#endif + + // TEST: Start thread, try to acquire the mutex. + t = new StaticMutexTestThread; + control = t->start(); + + // TEST: Wait until the tryLock has been tested. + t->waitTryLock(); + } + + // + // TEST: Once the mutex has been released, the thread should + // acquire the mutex and then terminate. + // + control.join(); +} |