Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / Bug_2772_Regression_Test.cpp
blob14c948de6d7a466039a04ee21ae174cb67b5e8d1
1 // ============================================================================
2 //
3 // = LIBRARY
4 // tests
5 //
6 // = DESCRIPTION
7 // Test bug 2772 regression
8 //
9 // = AUTHOR
10 // Johnny Willemsen
12 // ============================================================================
14 #include "test_config.h"
15 #include "ace/Recursive_Thread_Mutex.h"
16 #include "ace/Condition_Recursive_Thread_Mutex.h"
17 #include "ace/Thread.h"
19 class ThreadTest
21 public:
22 ThreadTest();
23 ~ThreadTest();
24 int run(bool doubleLock);
26 private:
27 static void * workerThreadWrapper(void *);
28 void workerThread();
29 ACE_Recursive_Thread_Mutex m_mutex;
30 ACE_Condition_Recursive_Thread_Mutex m_startedCondition;
31 ACE_Condition_Recursive_Thread_Mutex m_stopCondition;
32 bool m_workerRunning;
33 bool m_doubleLock;
36 ThreadTest::ThreadTest() :
37 m_startedCondition(m_mutex),
38 m_stopCondition(m_mutex),
39 m_workerRunning(false),
40 m_doubleLock(false)
44 ThreadTest::~ThreadTest()
48 int
49 ThreadTest::run(bool doubleLock)
51 ACE_hthread_t m_workerThreadHandle;
52 ACE_thread_t m_workerThreadId;
53 m_workerRunning = false;
54 m_doubleLock = doubleLock;
56 m_mutex.acquire();
58 // Start worker thread
59 int rval = ACE_Thread::spawn((ACE_THR_FUNC) workerThreadWrapper, this,
60 THR_JOINABLE | THR_NEW_LWP, &m_workerThreadId, &m_workerThreadHandle,
61 ACE_DEFAULT_THREAD_PRIORITY);
63 if (rval == -1)
65 ACE_ERROR_RETURN ((LM_ERROR,
66 ACE_TEXT ("%t Could not start worker thread!\n")),
67 1);
70 if (!m_workerRunning)
72 ACE_DEBUG ((LM_DEBUG,
73 ACE_TEXT ("%t Waiting for worker thread to start running...\n")));
74 m_startedCondition.wait();
76 ACE_DEBUG ((LM_DEBUG,
77 ACE_TEXT ("%t Worker thread is running...\n")));
79 ACE_DEBUG ((LM_DEBUG,
80 ACE_TEXT ("%t Broadcasting STOP Condition...\n")));
82 m_stopCondition.broadcast();
84 m_mutex.release();
86 ACE_DEBUG ((LM_DEBUG,
87 ACE_TEXT ("%t Joining worker thread...\n")));
89 ACE_Thread::join(m_workerThreadHandle);
91 ACE_DEBUG ((LM_DEBUG,
92 ACE_TEXT ("%t Test finished...\n")));
94 return 0;
97 void* ThreadTest::workerThreadWrapper(void *data)
99 ThreadTest *thisPtr = reinterpret_cast<ThreadTest *>(data);
100 thisPtr->workerThread();
101 return 0;
104 void ThreadTest::workerThread()
106 m_mutex.acquire();
107 m_workerRunning = true;
108 m_startedCondition.broadcast();
110 ACE_DEBUG ((LM_DEBUG,
111 ACE_TEXT ("%t Thread running, waiting for stop condition.\n")));
113 if (m_doubleLock)
115 ACE_DEBUG ((LM_DEBUG,
116 ACE_TEXT ("%t Thread starting double acquire.\n")));
117 m_mutex.acquire();
118 ACE_DEBUG ((LM_DEBUG,
119 ACE_TEXT ("%t Thread finished double acquire.\n")));
123 // Wait for the STOP condition to occur
124 m_stopCondition.wait();
125 ACE_DEBUG ((LM_DEBUG,
126 ACE_TEXT ("%t Thread received stop condition, exiting.\n")));
128 if (m_doubleLock)
130 ACE_DEBUG ((LM_DEBUG,
131 ACE_TEXT ("%t Thread starting double release.\n")));
132 m_mutex.release();
133 ACE_DEBUG ((LM_DEBUG,
134 ACE_TEXT ("%t Thread finished double acquire.\n")));
136 m_mutex.release();
140 run_main (int, ACE_TCHAR *[])
142 ACE_START_TEST (ACE_TEXT ("Bug_2772_Regression_Test"));
144 int status = 0;
146 ThreadTest test;
148 // This test passes
149 ACE_DEBUG ((LM_DEBUG, "TEST 1 - Single Lock\n"));
150 ACE_DEBUG ((LM_DEBUG, "--------------------\n"));
152 status += test.run(false);
154 // This test hangs; m_stopCondition.wait() in the worker thread
155 // doesn't unlock the mutex twice and thus a deadlock occurs
156 ACE_DEBUG ((LM_DEBUG, "TEST 2 - Double Lock\n"));
157 ACE_DEBUG ((LM_DEBUG, "--------------------\n"));
158 status += test.run(true);
160 ACE_END_TEST;
161 return status;