]> git.saurik.com Git - wxWidgets.git/blob - tests/thread/misc.cpp
move code testing wxThread classes from the console sample to a new CppUnit test
[wxWidgets.git] / tests / thread / misc.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/thread/misc.cpp
3 // Purpose: Miscellaneous wxThread test cases
4 // Author: Francesco Montorsi (extracted from console sample)
5 // Created: 2010-05-10
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #endif // WX_PRECOMP
22
23 #include "wx/thread.h"
24 #include "wx/tls.h"
25
26 // ----------------------------------------------------------------------------
27 // globals
28 // ----------------------------------------------------------------------------
29
30 static size_t gs_counter = (size_t)-1;
31 static wxCriticalSection gs_critsect;
32 static wxSemaphore gs_cond;
33
34 class MyJoinableThread : public wxThread
35 {
36 public:
37 MyJoinableThread(size_t n) : wxThread(wxTHREAD_JOINABLE)
38 { m_n = n; Create(); }
39
40 // thread execution starts here
41 virtual ExitCode Entry();
42
43 private:
44 size_t m_n;
45 };
46
47 wxThread::ExitCode MyJoinableThread::Entry()
48 {
49 unsigned long res = 1;
50 for ( size_t n = 1; n < m_n; n++ )
51 {
52 res *= n;
53
54 // it's a loooong calculation :-)
55 wxMilliSleep(100);
56 }
57
58 return (ExitCode)res;
59 }
60
61 class MyDetachedThread : public wxThread
62 {
63 public:
64 MyDetachedThread(size_t n, wxChar ch)
65 {
66 m_n = n;
67 m_ch = ch;
68 m_cancelled = false;
69
70 Create();
71 }
72
73 // thread execution starts here
74 virtual ExitCode Entry();
75
76 // and stops here
77 virtual void OnExit();
78
79 private:
80 size_t m_n; // number of characters to write
81 wxChar m_ch; // character to write
82
83 bool m_cancelled; // false if we exit normally
84 };
85
86 wxThread::ExitCode MyDetachedThread::Entry()
87 {
88 {
89 wxCriticalSectionLocker lock(gs_critsect);
90 if ( gs_counter == (size_t)-1 )
91 gs_counter = 1;
92 else
93 gs_counter++;
94 }
95
96 for ( size_t n = 0; n < m_n; n++ )
97 {
98 if ( TestDestroy() )
99 {
100 m_cancelled = true;
101
102 break;
103 }
104
105 //wxPutchar(m_ch);
106 //fflush(stdout);
107
108 wxThread::Sleep(100);
109 }
110
111 return 0;
112 }
113
114 void MyDetachedThread::OnExit()
115 {
116 //wxLogTrace(wxT("thread"), wxT("Thread %ld is in OnExit"), GetId());
117
118 wxCriticalSectionLocker lock(gs_critsect);
119 if ( !--gs_counter && !m_cancelled )
120 gs_cond.Post();
121 }
122
123 class MyWaitingThread : public wxThread
124 {
125 public:
126 MyWaitingThread( wxMutex *mutex, wxCondition *condition )
127 {
128 m_mutex = mutex;
129 m_condition = condition;
130
131 Create();
132 }
133
134 virtual ExitCode Entry()
135 {
136 //wxPrintf(wxT("Thread %lu has started running.\n"), GetId());
137 gs_cond.Post();
138
139 //wxPrintf(wxT("Thread %lu starts to wait...\n"), GetId());
140
141 m_mutex->Lock();
142 m_condition->Wait();
143 m_mutex->Unlock();
144
145 //wxPrintf(wxT("Thread %lu finished to wait, exiting.\n"), GetId());
146
147 return 0;
148 }
149
150 private:
151 wxMutex *m_mutex;
152 wxCondition *m_condition;
153 };
154
155 // semaphore tests
156 #include "wx/datetime.h"
157
158 class MySemaphoreThread : public wxThread
159 {
160 public:
161 MySemaphoreThread(int i, wxSemaphore *sem)
162 : wxThread(wxTHREAD_JOINABLE),
163 m_sem(sem),
164 m_i(i)
165 {
166 Create();
167 }
168
169 virtual ExitCode Entry()
170 {
171 //wxPrintf(wxT("%s: Thread #%d (%ld) starting to wait for semaphore...\n"),
172 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
173
174 m_sem->Wait();
175
176 //wxPrintf(wxT("%s: Thread #%d (%ld) acquired the semaphore.\n"),
177 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
178
179 Sleep(1000);
180
181 //wxPrintf(wxT("%s: Thread #%d (%ld) releasing the semaphore.\n"),
182 // wxDateTime::Now().FormatTime().c_str(), m_i, (long)GetId());
183
184 m_sem->Post();
185
186 return 0;
187 }
188
189 private:
190 wxSemaphore *m_sem;
191 int m_i;
192 };
193
194 WX_DEFINE_ARRAY_PTR(wxThread *, ArrayThreads);
195
196 // ----------------------------------------------------------------------------
197 // test class
198 // ----------------------------------------------------------------------------
199
200 class MiscThreadTestCase : public CppUnit::TestCase
201 {
202 public:
203 MiscThreadTestCase();
204
205 private:
206 CPPUNIT_TEST_SUITE( MiscThreadTestCase );
207 CPPUNIT_TEST( TestJoinable );
208 CPPUNIT_TEST( TestDetached );
209 CPPUNIT_TEST( TestThreadSuspend );
210 CPPUNIT_TEST( TestThreadDelete );
211 CPPUNIT_TEST( TestThreadConditions );
212 CPPUNIT_TEST( TestSemaphore );
213 CPPUNIT_TEST_SUITE_END();
214
215 void TestJoinable();
216 void TestDetached();
217 void TestSemaphore();
218
219 void TestThreadSuspend();
220 void TestThreadDelete();
221 void TestThreadConditions();
222
223 DECLARE_NO_COPY_CLASS(MiscThreadTestCase)
224 };
225
226 // register in the unnamed registry so that these tests are run by default
227 CPPUNIT_TEST_SUITE_REGISTRATION( MiscThreadTestCase );
228
229 // also include in it's own registry so that these tests can be run alone
230 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscThreadTestCase, "MiscThreadTestCase" );
231
232 MiscThreadTestCase::MiscThreadTestCase()
233 {
234 int nCPUs = wxThread::GetCPUCount();
235 if ( nCPUs != -1 )
236 wxThread::SetConcurrency(nCPUs);
237 }
238
239 void MiscThreadTestCase::TestJoinable()
240 {
241 // calc 10! in the background
242 MyJoinableThread thread(10);
243 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread.Run() );
244 CPPUNIT_ASSERT_EQUAL( 362880, (unsigned long)thread.Wait() );
245 }
246
247 void MiscThreadTestCase::TestDetached()
248 {
249 static const size_t nThreads = 3;
250 MyDetachedThread *threads[nThreads];
251
252 size_t n;
253 for ( n = 0; n < nThreads; n++ )
254 {
255 threads[n] = new MyDetachedThread(10, 'A' + n);
256 }
257
258 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY);
259 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY);
260
261 for ( n = 0; n < nThreads; n++ )
262 {
263 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, threads[n]->Run() );
264 }
265
266 // wait until all threads terminate
267 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR, gs_cond.Wait() );
268 }
269
270 void MiscThreadTestCase::TestSemaphore()
271 {
272 static const int SEM_LIMIT = 3;
273
274 wxSemaphore sem(SEM_LIMIT, SEM_LIMIT);
275 ArrayThreads threads;
276
277 for ( int i = 0; i < 3*SEM_LIMIT; i++ )
278 {
279 threads.Add(new MySemaphoreThread(i, &sem));
280 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, threads.Last()->Run() );
281 }
282
283 for ( size_t n = 0; n < threads.GetCount(); n++ )
284 {
285 CPPUNIT_ASSERT_EQUAL( 0, (long)threads[n]->Wait() );
286 delete threads[n];
287 }
288 }
289
290 void MiscThreadTestCase::TestThreadSuspend()
291 {
292 MyDetachedThread *thread = new MyDetachedThread(15, 'X');
293
294 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread->Run() );
295
296 // this is for this demo only, in a real life program we'd use another
297 // condition variable which would be signaled from wxThread::Entry() to
298 // tell us that the thread really started running - but here just wait a
299 // bit and hope that it will be enough (the problem is, of course, that
300 // the thread might still not run when we call Pause() which will result
301 // in an error)
302 wxThread::Sleep(300);
303
304 for ( size_t n = 0; n < 3; n++ )
305 {
306 thread->Pause();
307
308 if ( n > 0 )
309 {
310 // don't sleep but resume immediately the first time
311 wxThread::Sleep(300);
312 }
313
314 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread->Resume() );
315 }
316
317 // wait until the thread terminates
318 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR, gs_cond.Wait() );
319 }
320
321 void MiscThreadTestCase::TestThreadDelete()
322 {
323 // As above, using Sleep() is only for testing here - we must use some
324 // synchronisation object instead to ensure that the thread is still
325 // running when we delete it - deleting a detached thread which already
326 // terminated will lead to a crash!
327
328 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W');
329 CPPUNIT_ASSERT_EQUAL( wxTHREAD_MISC_ERROR, thread0->Delete() );
330 // delete a thread which didn't start to run yet.
331
332 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y');
333 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1->Run() );
334 wxThread::Sleep(300);
335 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1->Delete() );
336 // delete a running thread
337
338 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z');
339 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread2->Run() );
340 wxThread::Sleep(300);
341 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread2->Pause() );
342 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread2->Delete() );
343 // delete a sleeping thread
344
345 MyJoinableThread thread3(20);
346 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Run() );
347 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Delete() );
348 // delete a joinable thread
349
350 MyJoinableThread thread4(2);
351 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread4.Run() );
352 wxThread::Sleep(300);
353 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread4.Delete() );
354 // delete a joinable thread which already terminated
355 }
356
357 void MiscThreadTestCase::TestThreadConditions()
358 {
359 wxMutex mutex;
360 wxCondition condition(mutex);
361
362 // otherwise its difficult to understand which log messages pertain to
363 // which condition
364 //wxLogTrace(wxT("thread"), wxT("Local condition var is %08x, gs_cond = %08x"),
365 // condition.GetId(), gs_cond.GetId());
366
367 // create and launch threads
368 MyWaitingThread *threads[10];
369
370 size_t n;
371 for ( n = 0; n < WXSIZEOF(threads); n++ )
372 {
373 threads[n] = new MyWaitingThread( &mutex, &condition );
374 }
375
376 for ( n = 0; n < WXSIZEOF(threads); n++ )
377 {
378 CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, threads[n]->Run() );
379 }
380
381 // wait until all threads run
382 // NOTE: main thread is waiting for the other threads to start
383 size_t nRunning = 0;
384 while ( nRunning < WXSIZEOF(threads) )
385 {
386 CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR, gs_cond.Wait() );
387
388 nRunning++;
389
390 // note that main thread is already running
391 }
392
393 wxThread::Sleep(500);
394
395 #if 1
396 // now wake one of them up
397 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, condition.Signal() );
398 #endif
399
400 wxThread::Sleep(200);
401
402 // wake all the (remaining) threads up, so that they can exit
403 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, condition.Broadcast() );
404
405 // give them time to terminate (dirty!)
406 wxThread::Sleep(500);
407 }