summaryrefslogtreecommitdiff
path: root/cpp/test/IceUtil/thread/StaticMutexTest.cpp
diff options
context:
space:
mode:
authorMichi Henning <michi@zeroc.com>2003-10-20 23:18:23 +0000
committerMichi Henning <michi@zeroc.com>2003-10-20 23:18:23 +0000
commit8941640745d51e543fadc0524368d5f861691152 (patch)
tree384020f2b665d7f1a1c20b5ea435e0bf74faf476 /cpp/test/IceUtil/thread/StaticMutexTest.cpp
parentmerging changes from garbage collector (diff)
downloadice-8941640745d51e543fadc0524368d5f861691152.tar.bz2
ice-8941640745d51e543fadc0524368d5f861691152.tar.xz
ice-8941640745d51e543fadc0524368d5f861691152.zip
merging changes for garbage collector
Diffstat (limited to 'cpp/test/IceUtil/thread/StaticMutexTest.cpp')
-rw-r--r--cpp/test/IceUtil/thread/StaticMutexTest.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/cpp/test/IceUtil/thread/StaticMutexTest.cpp b/cpp/test/IceUtil/thread/StaticMutexTest.cpp
new file mode 100644
index 00000000000..34635a35ead
--- /dev/null
+++ b/cpp/test/IceUtil/thread/StaticMutexTest.cpp
@@ -0,0 +1,144 @@
+// **********************************************************************
+//
+// Copyright (c) 2003
+// ZeroC, Inc.
+// Billerica, MA, USA
+//
+// All Rights Reserved.
+//
+// Ice is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation.
+//
+// **********************************************************************
+
+#include <IceUtil/IceUtil.h>
+
+#include <StaticMutexTest.h>
+#include <TestCommon.h>
+
+using namespace std;
+using namespace IceUtil;
+
+static const string mutexTestName("static mutex");
+
+static StaticMutex staticMutex = ICE_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);
+ test(lock.tryAcquire() == false);
+ lock2.release();
+ test(lock.tryAcquire() == true);
+ test(lock.acquired());
+
+ // 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();
+}