-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathThread.cpp
277 lines (237 loc) · 6.11 KB
/
Thread.cpp
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
/*****************************************************************************
The Dark Mod GPL Source Code
This file is part of the The Dark Mod Source Code, originally based
on the Doom 3 GPL Source Code as published in 2011.
The Dark Mod Source Code is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version. For details, see LICENSE.TXT.
Project: The Dark Mod (http://www.thedarkmod.com/)
******************************************************************************/
#pragma hdrstop
#include "precompiled.h"
/*
================================================================================================
Contains the vartious ThreadingClass implementations.
================================================================================================
*/
/*
================================================================================================
idSysThread
================================================================================================
*/
/*
========================
idSysThread::idSysThread
========================
*/
idSysThread::idSysThread() :
threadHandle( 0 ),
isWorker( false ),
isRunning( false ),
isTerminating( false ),
moreWorkToDo( false ),
signalWorkerDone( true ) {
}
/*
========================
idSysThread::~idSysThread
========================
*/
idSysThread::~idSysThread() {
StopThread( true );
if ( threadHandle ) {
Sys_DestroyThread( threadHandle );
}
}
/*
========================
idSysThread::StartThread
========================
*/
bool idSysThread::StartThread( const char * name_, core_t core, xthreadPriority priority, int stackSize ) {
if ( isRunning ) {
return false;
}
name = name_;
isTerminating = false;
if ( threadHandle ) {
Sys_DestroyThread( threadHandle );
}
threadHandle = Sys_CreateThread( (xthread_t)ThreadProc, this, priority, name, core, stackSize, false );
isRunning = true;
return true;
}
/*
========================
idSysThread::StartWorkerThread
========================
*/
bool idSysThread::StartWorkerThread( const char * name_, core_t core, xthreadPriority priority, int stackSize ) {
if ( isRunning ) {
return false;
}
isWorker = true;
bool result = StartThread( name_, core, priority, stackSize );
signalWorkerDone.Wait( idSysSignal::WAIT_INFINITE );
return result;
}
/*
========================
idSysThread::StopThread
========================
*/
void idSysThread::StopThread( bool wait ) {
if ( !isRunning ) {
return;
}
if ( isWorker ) {
signalMutex.Lock();
moreWorkToDo = true;
signalWorkerDone.Clear();
isTerminating = true;
signalMoreWorkToDo.Raise();
signalMutex.Unlock();
} else {
isTerminating = true;
}
if ( wait ) {
WaitForThread();
}
}
/*
========================
idSysThread::WaitForThread
========================
*/
void idSysThread::WaitForThread() {
if ( isWorker ) {
signalWorkerDone.Wait( idSysSignal::WAIT_INFINITE );
} else if ( isRunning ) {
Sys_DestroyThread( threadHandle );
threadHandle = 0;
}
}
/*
========================
idSysThread::SignalWork
========================
*/
void idSysThread::SignalWork() {
if ( isWorker ) {
signalMutex.Lock();
moreWorkToDo = true;
signalWorkerDone.Clear();
signalMoreWorkToDo.Raise();
signalMutex.Unlock();
}
}
/*
========================
idSysThread::IsWorkDone
========================
*/
bool idSysThread::IsWorkDone() {
if ( isWorker ) {
// a timeout of 0 will return immediately with true if signaled
if ( signalWorkerDone.Wait( 0 ) ) {
return true;
}
}
return false;
}
/*
========================
idSysThread::ThreadProc
========================
*/
int idSysThread::ThreadProc( idSysThread * thread ) {
// stgatilov #4550: set FPU props (FTZ + DAZ, etc.)
sys->ThreadStartup();
int retVal = 0;
try {
if ( thread->isWorker ) {
for( ; ; ) {
thread->signalMutex.Lock();
if ( thread->moreWorkToDo ) {
thread->moreWorkToDo = false;
thread->signalMoreWorkToDo.Clear();
thread->signalMutex.Unlock();
} else {
thread->signalWorkerDone.Raise();
thread->signalMutex.Unlock();
TRACE_CPU_SCOPE_COLOR( "ThreadProc::Idle", TRACE_COLOR_IDLE )
thread->signalMoreWorkToDo.Wait( idSysSignal::WAIT_INFINITE );
continue;
}
if ( thread->isTerminating ) {
break;
}
// stgatilov #4550: update FPU props (e.g. NaN exceptions)
sys->ThreadHeartbeat( thread->GetName() );
retVal = thread->Run();
}
thread->signalWorkerDone.Raise();
} else {
retVal = thread->Run();
}
} catch ( idException & ex ) {
idLib::Warning( "Fatal error in thread %s: %s", thread->GetName(), ex.GetError() );
// We don't handle threads terminating unexpectedly very well, so just terminate the whole process
std::exit( 0 );
}
thread->isRunning = false;
return retVal;
}
/*
========================
idSysThread::Run
========================
*/
int idSysThread::Run() {
// The Run() is not pure virtual because on destruction of a derived class
// the virtual function pointer will be set to NULL before the idSysThread
// destructor actually stops the thread.
return 0;
}
/*
================================================================================================
test
================================================================================================
*/
/*
================================================
idMyThread test class.
================================================
*/
class idMyThread : public idSysThread {
public:
virtual int Run() override {
// run threaded code here
return 0;
}
// specify thread data here
};
/*
========================
TestThread
========================
*/
void TestThread() {
idMyThread thread;
thread.StartThread( "myThread", CORE_ANY );
}
/*
========================
TestWorkers
========================
*/
void TestWorkers() {
idSysWorkerThreadGroup<idMyThread> workers( "myWorkers", 4 );
for ( ; ; ) {
for ( int i = 0; i < workers.GetNumThreads(); i++ ) {
// workers.GetThread( i )-> // setup work for this thread
}
workers.SignalWorkAndWait();
}
}