-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.cpp
More file actions
455 lines (345 loc) · 9.9 KB
/
Thread.cpp
File metadata and controls
455 lines (345 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// Thread.cpp: implementation of the CThread class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#ifndef TRACE
#define TRACE
#endif
#ifndef ASSERT
#define ASSERT
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CThread::CThread() :
// Create a semephore
m_hStop ( CreateEvent( NULL, TRUE, FALSE, NULL ) ),
// Create a semephore
m_hStopped ( CreateEvent( NULL, TRUE, FALSE, NULL ) ),
// Create a semephore
m_hPause ( CreateEvent( NULL, TRUE, FALSE, NULL ) ),
// Create a semephore
m_hPaused ( CreateEvent( NULL, TRUE, FALSE, NULL ) ),
// Create a semephore
m_hUnpause ( CreateEvent( NULL, TRUE, FALSE, NULL ) ),
// Create a semephore
m_hInitialized ( CreateEvent( NULL, TRUE, FALSE, NULL ) )
{
m_hThread = NULL;
m_pvoidData = NULL;
m_dwPriority = 15;
m_bException = FALSE;
m_bMfc = FALSE;
m_bMessagePump = FALSE;
}
CThread::~CThread()
{_STT();
// Kill any thread that may be running
StopThread();
// Release the stop events
if ( m_hStop != NULL )
{
CloseHandle( m_hStop );
m_hStop = NULL;
} // end if
if ( m_hStopped != NULL )
{
CloseHandle( m_hStopped );
m_hStopped = NULL;
} // end if
if ( m_hPause != NULL )
{
CloseHandle( m_hPause );
m_hPause = NULL;
} // end if
if ( m_hPaused != NULL )
{
CloseHandle( m_hPaused );
m_hPaused = NULL;
} // end if
if ( m_hUnpause != NULL )
{
CloseHandle( m_hUnpause );
m_hUnpause = NULL;
} // end if
if ( m_hInitialized != NULL )
{
CloseHandle( m_hInitialized );
m_hInitialized = NULL;
} // end if
}
// The number of threads running
DWORD CThread::m_dwThreadCount = 0;
DWORD CThread::m_dwRunningThreadCount = 0;
//////////////////////////////////////////////////////////////
// CommThread () /////////////////////////////////////////////
//
// This static function is the actual body of the thread
// overide DoThread() for custom work
//
//////////////////////////////////////////////////////////////
DWORD WINAPI CThread::Thread ( LPVOID pData )
{_STT();
// Get a pointer to our data
CThread *pThread = (CThread*)pData;
// Sanity Check
if ( pThread == NULL ) return (DWORD)-1;
CoInitialize( NULL );
BOOL bMfc = pThread->m_bMfc;
DWORD dwRet;
HANDLE hStop, hStopped;
LPVOID pUser = pThread->m_pvoidData;
#ifdef MFCSAFETHREADS
bMfc = ( pThread->m_pWinThread != NULL );
#endif
// Get stop event
hStop = pThread->m_hStop;
hStopped = pThread->m_hStopped;
if ( hStop == NULL || hStopped == NULL )
{
// Tell Everyone we are done
return ( pThread->m_dwThreadReturn = (DWORD)-1 );
} // end if
RULIB_TRY
{
// Attempt to initialize the thread
if ( pThread->InitThread( pUser ) )
{
// We did initialize
SetEvent( pThread->m_hInitialized );
// Count one running thread
IncRunningThreadCount();
// Run the thread
do
{
// Do the message pump for this thread
if ( pThread->m_bMessagePump ) MessagePump( pThread );
// Pause if needed
if ( pThread->IsPausing() )
{
// Signal that we are paused
SetEvent( pThread->m_hPaused );
// Wait for unpause
WaitForSingleObject( pThread->m_hUnpause, INFINITE );
// Reset pause event
ResetEvent( pThread->m_hPause );
// Reset unpause event
ResetEvent( pThread->m_hUnpause );
// Clear paused status
ResetEvent( pThread->m_hPaused );
} // end while
} while ( // Check for stop flag
!pThread->IsStopping()
&&
// Don't hog the processor
pThread->ThreadSleep()
&&
// Let's do the important stuff
pThread->DoThread( pUser ) );
// Uncount one running thread
DecRunningThreadCount();
} // end if
// Let the user clean up
pThread->EndThread( pUser );
} // end try
RULIB_CATCH_ALL
{
ASSERT( 0 );
// There was an exception
pThread->m_bException = TRUE;
} // end catch
// Save the users return value
dwRet = pThread->m_dwThreadReturn;
CoUninitialize();
// Tell Everyone we are done
SetEvent( hStopped );
// We can't access any data not on our own function stack now
// This includes class member variables
// Allow some ghosting
// return pThread->GhostThread( pUser, dwRet );
#ifdef MFCSAFETHREADS
// Let MFC in on this
if ( bMfc ) AfxEndThread( dwRet );
#endif
return dwRet;
} // end CommThread()
void CThread::MessagePump( CThread *pThread )
{_STT();
MSG msg;
// If there is a message
while ( PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE ) )
{
// Translate accelerators
// if ( !TranslateAccelerator( msg.hwnd, hAccel, &msg ) )
{
// Translate key strokes
TranslateMessage( &msg );
// Dispatch the message
DispatchMessage( &msg );
} // end if
} // end if
/*
#ifdef MFCSAFETHREADS
if ( pThread != NULL )
{
CWinThread *pWinThread = pThread->m_pWinThread;
if ( pWinThread != NULL )
{
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
while ( bIdle &&
!::PeekMessage( &msg, NULL, NULL, NULL, PM_NOREMOVE ) )
{
// call OnIdle while in bIdle state
if ( !pWinThread->OnIdle( lIdleCount++ ) )
bIdle = FALSE; // assume "no idle" state
} // end while
} // end if
} // end if
#endif
*/
}
BOOL CThread::StartThread( LPVOID pData, BOOL bRestart, BOOL bMessagePump, BOOL bMfc )
{_STT();
// Do we want to restart?
if ( bRestart ) StopThread();
// Quit if we are already running
else if ( IsRunning() != NULL ) return FALSE;
// Reset initialized event
ResetEvent( m_hInitialized );
// Save message pump params
m_bMessagePump = bMessagePump;
m_bMfc = bMfc;
// No exception yet
m_bException = FALSE;
// Save the users data
m_pvoidData = pData;
// Assume thread success
m_dwThreadReturn = 0;
// Reset the semephores
ResetEvent( m_hStop );
ResetEvent( m_hStopped );
ResetEvent( m_hPause );
#ifdef MFCSAFETHREADS
if ( m_bMfc )
{
// MFC crap ( create suspended so we can read information )
m_pWinThread =
AfxBeginThread( CThread::Thread,
(LPVOID)this, THREAD_PRIORITY_NORMAL,
0, CREATE_SUSPENDED );
if ( m_pWinThread == NULL ) return FALSE;
// Save thread id's
m_hThread = m_pWinThread->m_hThread;
m_dwThreadId = m_pWinThread->m_nThreadID;
// Resume the thread
if ( m_pWinThread->ResumeThread() == MAXDWORD )
{ StopThread();
return FALSE;
} // end if
} // end if
else
{
#endif
// Create a thread
m_hThread = CreateThread( (LPSECURITY_ATTRIBUTES)NULL,
0,
CThread::Thread,
(LPVOID)this,
0,
&m_dwThreadId );
#ifdef MFCSAFETHREADS
} // end else
#endif
// Did we get the thread?
if ( m_hThread == NULL ) return FALSE;
// Count one thread
IncThreadCount();
// Apparently, all is well
return TRUE;
}
BOOL CThread::StopThread( BOOL bKill, DWORD dwWait )
{_STT();
// Quit if no thread
if ( m_hThread == NULL ) return FALSE;
// Tell Thread To Stop
SignalStop();
if ( bKill )
{
HANDLE phEvents[ 2 ];
phEvents[ 0 ] = m_hThread;
phEvents[ 1 ] = m_hStopped;
// Record the time
DWORD dwCount = GetTickCount() + dwWait;
// Wait to see if the thread will quit on its own
while( ( WaitForMultipleObjects( 2, phEvents, FALSE, 30 ) == WAIT_TIMEOUT )
&& ( dwCount > GetTickCount() ) )
{ /*+++ MessagePump( this ); */ Sleep( 0 ); }
// Kill the thread off if it is still running
if ( ( WaitForMultipleObjects( 2, phEvents, FALSE, 30 ) == WAIT_TIMEOUT ) )
{
TRACE( _T( "!!! TerminateThread() being called !!!\n" ) );
TerminateThread( m_hThread, FALSE );
#ifdef MFCSAFETHREADS
if ( m_pWinThread != NULL ) CloseHandle( m_hThread );
#endif
} // end if
} // end if
#ifdef MFCSAFETHREADS
if ( m_pWinThread != NULL ) m_pWinThread = NULL;
// Close The Handle To The Thread
else CloseHandle( m_hThread );
#else
// Close The Handle To The Thread
CloseHandle( m_hThread );
#endif
m_hThread = NULL;
// Reset the semephores
ResetEvent( m_hStop );
ResetEvent( m_hStopped );
ResetEvent( m_hPause );
// Uncount one thread
DecThreadCount();
return TRUE;
}
void CThread::SignalStop()
{_STT();
// Quit if no thread
if ( m_hThread == NULL ) return;
// Tell Thread To Stop
SetEvent( m_hStop );
// Post quit message to thread
PostThreadMessage( GetThreadId(), WM_QUIT, 0, 0L );
}
BOOL CThread::WaitThreadInit( DWORD dwTimeout )
{_STT();
HANDLE phEvents[ 4 ];
// Trigger on any of these
phEvents[ 0 ] = m_hStop;
phEvents[ 1 ] = m_hStopped;
phEvents[ 2 ] = m_hPause;
phEvents[ 3 ] = m_hInitialized;
// Wait on an event
return ( WaitForMultipleObjects( 4, phEvents, FALSE, dwTimeout ) != WAIT_TIMEOUT );
}