summaryrefslogtreecommitdiff
path: root/cpp/test/IceUtil/thread/AliveTest.cpp
diff options
context:
space:
mode:
authorJoe George <joe@zeroc.com>2015-03-03 17:30:50 -0500
committerJoe George <joe@zeroc.com>2015-05-12 11:41:55 -0400
commitd35bb9f5c19e34aee31f83d445695a8186ef675e (patch)
treed5324eaf44f5f9776495537c51653f50a66a7237 /cpp/test/IceUtil/thread/AliveTest.cpp
downloadice-d35bb9f5c19e34aee31f83d445695a8186ef675e.tar.bz2
ice-d35bb9f5c19e34aee31f83d445695a8186ef675e.tar.xz
ice-d35bb9f5c19e34aee31f83d445695a8186ef675e.zip
Ice 3.4.2 Source Distributionv3.4.2
Diffstat (limited to 'cpp/test/IceUtil/thread/AliveTest.cpp')
-rw-r--r--cpp/test/IceUtil/thread/AliveTest.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/cpp/test/IceUtil/thread/AliveTest.cpp b/cpp/test/IceUtil/thread/AliveTest.cpp
new file mode 100644
index 00000000000..f36eebe8cc3
--- /dev/null
+++ b/cpp/test/IceUtil/thread/AliveTest.cpp
@@ -0,0 +1,99 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2011 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 <IceUtil/IceUtil.h>
+#include <AliveTest.h>
+#include <TestCommon.h>
+
+using namespace std;
+using namespace IceUtil;
+
+static const string createTestName("thread alive");
+
+class CondVar : public IceUtil::Monitor<IceUtil::RecMutex>
+{
+public:
+
+ CondVar() :
+ _done(false)
+ {
+ }
+
+ void waitForSignal()
+ {
+ IceUtil::Monitor<IceUtil::RecMutex>::Lock lock(*this);
+ while(!_done)
+ {
+ wait();
+ }
+ }
+
+ void signal()
+ {
+ IceUtil::Monitor<IceUtil::RecMutex>::Lock lock(*this);
+ _done = true;
+ notify();
+ }
+
+private:
+
+ bool _done;
+};
+
+class AliveTestThread : public Thread
+{
+public:
+
+ AliveTestThread(CondVar& childCreated, CondVar& parentReady) :
+ _childCreated(childCreated), _parentReady(parentReady)
+ {
+ }
+
+ virtual void run()
+ {
+ try
+ {
+ _childCreated.signal();
+ _parentReady.waitForSignal();
+ }
+ catch(IceUtil::ThreadLockedException &)
+ {
+ }
+ }
+
+private:
+
+ CondVar& _childCreated;
+ CondVar& _parentReady;
+};
+
+typedef Handle<AliveTestThread> AliveTestThreadPtr;
+
+AliveTest::AliveTest() :
+ TestBase(createTestName)
+{
+}
+
+void
+AliveTest::run()
+{
+ //
+ // Check that calling isAlive() returns the correct result for alive and
+ // and dead threads.
+ //
+ CondVar childCreated;
+ CondVar parentReady;
+ AliveTestThreadPtr t = new AliveTestThread(childCreated, parentReady);
+ IceUtil::ThreadControl c = t->start();
+ childCreated.waitForSignal();
+ test(t->isAlive());
+ parentReady.signal();
+ c.join();
+ test(!t->isAlive());
+}