summaryrefslogtreecommitdiff
path: root/cppe/test/IceE/thread/MutexTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cppe/test/IceE/thread/MutexTest.cpp')
-rw-r--r--cppe/test/IceE/thread/MutexTest.cpp170
1 files changed, 170 insertions, 0 deletions
diff --git a/cppe/test/IceE/thread/MutexTest.cpp b/cppe/test/IceE/thread/MutexTest.cpp
new file mode 100644
index 00000000000..6e1f2149237
--- /dev/null
+++ b/cppe/test/IceE/thread/MutexTest.cpp
@@ -0,0 +1,170 @@
+// **********************************************************************
+//
+// 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 <MutexTest.h>
+#include <TestCommon.h>
+
+using namespace std;
+using namespace IceE;
+
+static const string mutexTestName("mutex");
+
+class MutexTestThread : public Thread
+{
+public:
+
+ MutexTestThread(Mutex& m) :
+ _mutex(m),
+ _tryLock(false)
+ {
+ }
+
+ virtual void run()
+ {
+ Mutex::TryLock tlock(_mutex);
+ test(!tlock.acquired());
+
+ {
+ Mutex::Lock lock(_tryLockMutex);
+ _tryLock = true;
+ }
+ _tryLockCond.signal();
+
+ Mutex::Lock lock(_mutex);
+ }
+
+ void
+ waitTryLock()
+ {
+ Mutex::Lock lock(_tryLockMutex);
+ while(!_tryLock)
+ {
+ _tryLockCond.wait(lock);
+ }
+ }
+
+private:
+
+ Mutex& _mutex;
+ bool _tryLock;
+ //
+ // Use native Condition variable here, not Monitor.
+ //
+ Cond _tryLockCond;
+ Mutex _tryLockMutex;
+};
+
+typedef Handle<MutexTestThread> MutexTestThreadPtr;
+
+MutexTest::MutexTest() :
+ TestBase(mutexTestName)
+{
+}
+
+void
+MutexTest::run()
+{
+ Mutex mutex;
+ MutexTestThreadPtr t;
+ ThreadControl control;
+
+ {
+ Mutex::Lock lock(mutex);
+
+ // 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
+ }
+
+ Mutex::TryLock lock2(mutex);
+#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());
+
+ // Deadlock testing
+ //
+
+#if !defined(NDEBUG) && !defined(_WIN32)
+ try
+ {
+ Mutex::Lock lock3(mutex);
+ test(false);
+ }
+ catch(const ThreadSyscallException& e)
+ {
+ // Expected
+ test(e.error() == EDEADLK);
+ }
+#endif
+
+ // TEST: Start thread, try to acquire the mutex.
+ t = new MutexTestThread(mutex);
+ 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();
+}