1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/thread/misc.cpp
3 // Purpose: Miscellaneous wxThread test cases
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/thread.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 static size_t gs_counter
= (size_t)-1;
31 static wxCriticalSection gs_critsect
;
32 static wxSemaphore gs_cond
;
34 class MyJoinableThread
: public wxThread
37 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
38 { m_n
= n
; Create(); }
40 // thread execution starts here
41 virtual ExitCode
Entry();
47 wxThread::ExitCode
MyJoinableThread::Entry()
49 unsigned long res
= 1;
50 for ( size_t n
= 1; n
< m_n
; n
++ )
54 // it's a loooong calculation :-)
61 class MyDetachedThread
: public wxThread
64 MyDetachedThread(size_t n
, wxChar ch
)
73 // thread execution starts here
74 virtual ExitCode
Entry();
77 virtual void OnExit();
80 size_t m_n
; // number of characters to write
81 wxChar m_ch
; // character to write
83 bool m_cancelled
; // false if we exit normally
86 wxThread::ExitCode
MyDetachedThread::Entry()
89 wxCriticalSectionLocker
lock(gs_critsect
);
90 if ( gs_counter
== (size_t)-1 )
96 for ( size_t n
= 0; n
< m_n
; n
++ )
114 void MyDetachedThread::OnExit()
116 //wxLogTrace(wxT("thread"), wxT("Thread %ld is in OnExit"), GetId());
118 wxCriticalSectionLocker
lock(gs_critsect
);
119 if ( !--gs_counter
&& !m_cancelled
)
123 class MyWaitingThread
: public wxThread
126 MyWaitingThread( wxMutex
*mutex
, wxCondition
*condition
)
129 m_condition
= condition
;
134 virtual ExitCode
Entry()
136 //wxPrintf(wxT("Thread %lu has started running.\n"), GetId());
139 //wxPrintf(wxT("Thread %lu starts to wait...\n"), GetId());
145 //wxPrintf(wxT("Thread %lu finished to wait, exiting.\n"), GetId());
152 wxCondition
*m_condition
;
156 #include "wx/datetime.h"
158 class MySemaphoreThread
: public wxThread
161 MySemaphoreThread(int i
, wxSemaphore
*sem
)
162 : wxThread(wxTHREAD_JOINABLE
),
169 virtual ExitCode
Entry()
171 //wxPrintf(wxT("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
172 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
176 //wxPrintf(wxT("%s: Thread #%d (%ld) acquired the semaphore.\n"),
177 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
181 //wxPrintf(wxT("%s: Thread #%d (%ld) releasing the semaphore.\n"),
182 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
194 WX_DEFINE_ARRAY_PTR(wxThread
*, ArrayThreads
);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 class MiscThreadTestCase
: public CppUnit::TestCase
203 MiscThreadTestCase();
206 CPPUNIT_TEST_SUITE( MiscThreadTestCase
);
207 CPPUNIT_TEST( TestJoinable
);
208 CPPUNIT_TEST( TestDetached
);
209 CPPUNIT_TEST( TestThreadSuspend
);
210 CPPUNIT_TEST( TestThreadDelete
);
211 CPPUNIT_TEST( TestThreadRun
);
212 CPPUNIT_TEST( TestThreadConditions
);
213 CPPUNIT_TEST( TestSemaphore
);
214 CPPUNIT_TEST_SUITE_END();
218 void TestSemaphore();
220 void TestThreadSuspend();
221 void TestThreadDelete();
222 void TestThreadRun();
223 void TestThreadConditions();
225 DECLARE_NO_COPY_CLASS(MiscThreadTestCase
)
228 // register in the unnamed registry so that these tests are run by default
229 CPPUNIT_TEST_SUITE_REGISTRATION( MiscThreadTestCase
);
231 // also include in its own registry so that these tests can be run alone
232 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscThreadTestCase
, "MiscThreadTestCase" );
234 MiscThreadTestCase::MiscThreadTestCase()
236 int nCPUs
= wxThread::GetCPUCount();
238 wxThread::SetConcurrency(nCPUs
);
241 void MiscThreadTestCase::TestJoinable()
243 // calc 10! in the background
244 MyJoinableThread
thread(10);
245 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread
.Run() );
246 CPPUNIT_ASSERT_EQUAL( 362880, (unsigned long)thread
.Wait() );
249 void MiscThreadTestCase::TestDetached()
251 static const size_t nThreads
= 3;
252 MyDetachedThread
*threads
[nThreads
];
255 for ( n
= 0; n
< nThreads
; n
++ )
257 threads
[n
] = new MyDetachedThread(10, 'A' + n
);
260 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
261 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
263 for ( n
= 0; n
< nThreads
; n
++ )
265 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, threads
[n
]->Run() );
268 // wait until all threads terminate
269 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR
, gs_cond
.Wait() );
272 void MiscThreadTestCase::TestSemaphore()
274 static const int SEM_LIMIT
= 3;
276 wxSemaphore
sem(SEM_LIMIT
, SEM_LIMIT
);
277 ArrayThreads threads
;
279 for ( int i
= 0; i
< 3*SEM_LIMIT
; i
++ )
281 threads
.Add(new MySemaphoreThread(i
, &sem
));
282 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, threads
.Last()->Run() );
285 for ( size_t n
= 0; n
< threads
.GetCount(); n
++ )
287 CPPUNIT_ASSERT_EQUAL( 0, (long)threads
[n
]->Wait() );
292 void MiscThreadTestCase::TestThreadSuspend()
294 MyDetachedThread
*thread
= new MyDetachedThread(15, 'X');
296 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread
->Run() );
298 // this is for this demo only, in a real life program we'd use another
299 // condition variable which would be signaled from wxThread::Entry() to
300 // tell us that the thread really started running - but here just wait a
301 // bit and hope that it will be enough (the problem is, of course, that
302 // the thread might still not run when we call Pause() which will result
306 for ( size_t n
= 0; n
< 3; n
++ )
312 // don't sleep but resume immediately the first time
316 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread
->Resume() );
319 // wait until the thread terminates
320 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR
, gs_cond
.Wait() );
323 void MiscThreadTestCase::TestThreadDelete()
326 // As above, using Sleep() is only for testing here - we must use some
327 // synchronisation object instead to ensure that the thread is still
328 // running when we delete it - deleting a detached thread which already
329 // terminated will lead to a crash!
331 MyDetachedThread
*thread0
= new MyDetachedThread(30, 'W');
332 CPPUNIT_ASSERT_EQUAL( wxTHREAD_MISC_ERROR
, thread0
->Delete() );
333 // delete a thread which didn't start to run yet.
335 MyDetachedThread
*thread1
= new MyDetachedThread(30, 'Y');
336 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread1
->Run() );
338 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread1
->Delete() );
339 // delete a running thread
341 MyDetachedThread
*thread2
= new MyDetachedThread(30, 'Z');
342 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread2
->Run() );
344 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread2
->Pause() );
345 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread2
->Delete() );
346 // delete a sleeping thread
348 MyJoinableThread
thread3(20);
349 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread3
.Run() );
350 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread3
.Delete() );
351 // delete a joinable running thread
353 MyJoinableThread
thread4(2);
354 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread4
.Run() );
356 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread4
.Delete() );
357 // delete a joinable thread which already terminated
360 void MiscThreadTestCase::TestThreadRun()
362 MyJoinableThread
thread1(2);
363 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, thread1
.Run() );
364 thread1
.Wait(); // wait until the thread ends
366 // verify that running twice the same thread fails
367 WX_ASSERT_FAILS_WITH_ASSERT( thread1
.Run() );
370 void MiscThreadTestCase::TestThreadConditions()
373 wxCondition
condition(mutex
);
375 // otherwise its difficult to understand which log messages pertain to
377 //wxLogTrace(wxT("thread"), wxT("Local condition var is %08x, gs_cond = %08x"),
378 // condition.GetId(), gs_cond.GetId());
380 // create and launch threads
381 MyWaitingThread
*threads
[10];
384 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
386 threads
[n
] = new MyWaitingThread( &mutex
, &condition
);
389 for ( n
= 0; n
< WXSIZEOF(threads
); n
++ )
391 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR
, threads
[n
]->Run() );
394 // wait until all threads run
395 // NOTE: main thread is waiting for the other threads to start
397 while ( nRunning
< WXSIZEOF(threads
) )
399 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR
, gs_cond
.Wait() );
403 // note that main thread is already running
409 // now wake one of them up
410 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, condition
.Signal() );
415 // wake all the (remaining) threads up, so that they can exit
416 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, condition
.Broadcast() );
418 // give them time to terminate (dirty!)