1 // ============================================================================
7 // Test bug 2772 regression
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"
24 int run(bool doubleLock
);
27 static void * workerThreadWrapper(void *);
29 ACE_Recursive_Thread_Mutex m_mutex
;
30 ACE_Condition_Recursive_Thread_Mutex m_startedCondition
;
31 ACE_Condition_Recursive_Thread_Mutex m_stopCondition
;
36 ThreadTest::ThreadTest() :
37 m_startedCondition(m_mutex
),
38 m_stopCondition(m_mutex
),
39 m_workerRunning(false),
44 ThreadTest::~ThreadTest()
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
;
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
);
65 ACE_ERROR_RETURN ((LM_ERROR
,
66 ACE_TEXT ("%t Could not start worker thread!\n")),
73 ACE_TEXT ("%t Waiting for worker thread to start running...\n")));
74 m_startedCondition
.wait();
77 ACE_TEXT ("%t Worker thread is running...\n")));
80 ACE_TEXT ("%t Broadcasting STOP Condition...\n")));
82 m_stopCondition
.broadcast();
87 ACE_TEXT ("%t Joining worker thread...\n")));
89 ACE_Thread::join(m_workerThreadHandle
);
92 ACE_TEXT ("%t Test finished...\n")));
97 void* ThreadTest::workerThreadWrapper(void *data
)
99 ThreadTest
*thisPtr
= reinterpret_cast<ThreadTest
*>(data
);
100 thisPtr
->workerThread();
104 void ThreadTest::workerThread()
107 m_workerRunning
= true;
108 m_startedCondition
.broadcast();
110 ACE_DEBUG ((LM_DEBUG
,
111 ACE_TEXT ("%t Thread running, waiting for stop condition.\n")));
115 ACE_DEBUG ((LM_DEBUG
,
116 ACE_TEXT ("%t Thread starting double acquire.\n")));
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")));
130 ACE_DEBUG ((LM_DEBUG
,
131 ACE_TEXT ("%t Thread starting double release.\n")));
133 ACE_DEBUG ((LM_DEBUG
,
134 ACE_TEXT ("%t Thread finished double acquire.\n")));
140 run_main (int, ACE_TCHAR
*[])
142 ACE_START_TEST (ACE_TEXT ("Bug_2772_Regression_Test"));
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);