summaryrefslogtreecommitdiff
path: root/cpp/test/IceUtil/thread/RecMutexTest.cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2001-12-27 18:38:22 +0000
committerMatthew Newhook <matthew@zeroc.com>2001-12-27 18:38:22 +0000
commitca2b7f71b4133faf486a677b3744904c0f759471 (patch)
tree2ea5e06f611c338f8160a0c3bd36d069118926fe /cpp/test/IceUtil/thread/RecMutexTest.cpp
parentfile run.py was initially added on branch IceThread. (diff)
downloadice-ca2b7f71b4133faf486a677b3744904c0f759471.tar.bz2
ice-ca2b7f71b4133faf486a677b3744904c0f759471.tar.xz
ice-ca2b7f71b4133faf486a677b3744904c0f759471.zip
IceThread merge.
Diffstat (limited to 'cpp/test/IceUtil/thread/RecMutexTest.cpp')
-rw-r--r--cpp/test/IceUtil/thread/RecMutexTest.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/cpp/test/IceUtil/thread/RecMutexTest.cpp b/cpp/test/IceUtil/thread/RecMutexTest.cpp
new file mode 100644
index 00000000000..a66c1efe1bf
--- /dev/null
+++ b/cpp/test/IceUtil/thread/RecMutexTest.cpp
@@ -0,0 +1,106 @@
+// **********************************************************************
+//
+// Copyright (c) 2001
+// MutableRealms, Inc.
+// Huntsville, AL, USA
+//
+// All Rights Reserved
+//
+// **********************************************************************
+
+#include <IceUtil/IceUtil.h>
+
+#include <RecMutexTest.h>
+#include <TestCommon.h>
+
+using namespace std;
+using namespace IceUtil;
+
+static const std::string recMutexTestName("recursive mutex");
+
+class RecMutexTestThread : public Thread
+{
+public:
+
+ RecMutexTestThread(RecMutex& m) :
+ _mutex(m),
+ _trylock(false)
+ {
+ }
+
+ virtual void run()
+ {
+ try
+ {
+ RecMutex::TryLock lock(_mutex);
+ test(false);
+ }
+ catch(const LockedException&)
+ {
+ // Expected
+ }
+
+ _trylock = true;
+ _trylockCond.signal();
+
+ RecMutex::Lock lock(_mutex);
+ }
+
+ void
+ waitTrylock()
+ {
+ Mutex::Lock lock(_trylockMutex);
+ while (!_trylock)
+ {
+ _trylockCond.wait(lock);
+ }
+ }
+
+private:
+
+ RecMutex& _mutex;
+ bool _trylock;
+ //
+ // Use native Condition variable here, not Monitor.
+ //
+ Cond _trylockCond;
+ Mutex _trylockMutex;
+};
+
+typedef Handle<RecMutexTestThread> RecMutexTestThreadPtr;
+
+RecMutexTest::RecMutexTest() :
+ TestBase(recMutexTestName)
+{
+}
+
+void
+RecMutexTest::run()
+{
+ RecMutex mutex;
+ RecMutexTestThreadPtr t;
+ ThreadControl control;
+
+ {
+ RecMutex::Lock lock(mutex);
+
+ // TEST: lock twice
+ RecMutex::Lock lock2(mutex);
+
+ // TEST: TryLock
+ RecMutex::TryLock lock3(mutex);
+
+ // TEST: Start thread, try to acquire the mutex.
+ t = new RecMutexTestThread(mutex);
+ control = t->start();
+
+ // TEST: Wait until the trylock has been tested.
+ t->waitTrylock();
+ }
+
+ //
+ // TEST: Once the recursive mutex has been released, the thread
+ // should acquire the mutex and then terminate.
+ //
+ control.join();
+}