-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKThread.cpp
376 lines (329 loc) · 10 KB
/
KThread.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
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
/*
*
* Copyright (C) 2007-2014 Jürg Müller, CH-5524
*
* This program 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 version 3 of the License.
*
* This program 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 program. If not, see http://www.gnu.org/licenses/ .
*/
#if defined(__PYTHON__)
#include <Python.h>
#endif
#include <stdio.h>
#if defined(__WINDOWS__)
#include <windows.h>
#endif
#if !defined(__NO_MULTITHREADS__)
#if defined(__LINUX__)
#include <signal.h>
#include <unistd.h>
#endif
#endif
#include "NTypes.h"
#include "NUtils.h"
#include "KThread.h"
using namespace NUtils;
#if defined(__WINDOWS__) || defined(__WINE__)
TNativeInt gMainThreadId = GetCurrentThreadId();
TNativeInt gProcessId = GetCurrentProcessId();
#elif defined(__LINUX__)
TNativeInt gMainThreadId = (TNativeInt) pthread_self();
TNativeInt gProcessId = getpid(); // ist auch für Windows definiert (process.h)
#endif
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
const int Priorities[] = {
THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL,
THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL};
TThreadPriorityEnum KThread::GetPriority(void)
{
int P;
int i;
TThreadPriorityEnum Result;
P = GetThreadPriority(mHandle);
CheckThreadErrorB(P != THREAD_PRIORITY_ERROR_RETURN);
Result = tpNormal;
for (i = 0; i <= (int) High(Priorities); i++)
if (Priorities[i] == P) Result = TThreadPriorityEnum(i);
return Result;
}
DWORD WINAPI ThreadProc(LPVOID aThread)
{
int Result = 0;
KThread * This = (KThread *) aThread;
try
{
if (!This->Terminated())
This->Execute();
}
catch(...)
{
#if defined(__CONSOLE__)
printf("exception caught in \"ThreadProc\"\n");
#endif
}
This->mFinished = true;
#if defined(__DEBUG__) && defined(__CONSOLE__)
printf("stop thread 0x%x\n", (int)((KThread *)(aThread))->GetID());
#endif
ExitThread(Result);
return Result;
}
void ShowMessage(const char * aMsgBuf)
{
#if defined(__CONSOLE__)
printf("ART Error %s\n", aMsgBuf);
#else
#if defined(__WINDOWS__)
// Wenn das Resultat 0 ist, dann steht nicht genügend Memory vom System zur
// Verfügung. Dies ist beim Programmende der Fall, wenn das Softwarepaket als DLL
// implementiert ist.
MessageBoxA(NULL, aMsgBuf, "ART Error", MB_OK);
#endif
#endif
}
#endif
#if defined(__LINUX__)
void * KThread::ThreadProc(void * aThread)
{
void * Result = NULL;
KThread * This = (KThread *) aThread;
try
{
// Die virtuelle Methode "Execute" wird zu früh aufgerufen. Der Thread wird
// ausgeführt, der Pointer zur Methode der übergeordneten Klasse wird jedoch
// erst später gesetzt.
// "mInitialSuspendDone" wird mit der Methode "Resume" auf true gesetzt.
while (!This->mInitialSuspendDone && !This->Terminated())
NUtils::SleepMs(1);
if (!This->Terminated())
{
This->Execute();
}
}
catch(...)
{
#if defined(__CONSOLE__)
printf("exception caught in \"ThreadProc\"\n");
#endif
}
This->mFinished = true;
#if defined(__DEBUG__) && defined(__CONSOLE__)
printf("stop thread 0x%x\n", (unsigned) (long)((KThread *)(aThread))->GetID());
#endif
// Directly call pthread_exit since EndThread will detach the thread causing
// the pthread_join in KThread.WaitFor to fail. Also, make sure the EndThreadProc
// is called just like EndThread would do. EndThreadProc should ! return
// && call pthread_exit itself.
pthread_exit(&Result);
return Result;
}
#endif
#endif
KThread::KThread()
{
mInitialSuspendDone = false;
mTerminated = false;
mFinished = false;
#if !defined(__NO_MULTITHREADS__)
mThreadID = 0;
#if defined(__WINDOWS__)
mHandle = CreateThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, &mThreadID);
if (!mHandle)
{
char Buffer[64];
sprintf(Buffer, "CreateThread error: %d", (int) GetLastError());
::ShowMessage(Buffer);
}
#endif
#if defined(__LINUX__)
int ErrCode;
CheckThreadError(pthread_attr_init(&mThreadAttr));
ErrCode = pthread_create(&mThreadID, NULL, ThreadProc, this);
CheckThreadError(ErrCode);
#endif
#if defined(__DEBUG__) && defined(__CONSOLE__)
printf("create thread 0x%x\n", (unsigned)(long) GetID());
#endif
#endif
}
KThread::~KThread()
{
#if !defined(__NO_MULTITHREADS__)
if (&mThreadID && !mFinished)
{
Terminate();
WaitFor();
}
#if defined(__WINDOWS__)
if (mHandle)
CloseHandle(mHandle);
#endif
#if defined(__LINUX__)
// This final check is to ensure that even if the thread was never waited on
// its resources will be freed.
if (&mThreadID)
pthread_detach(mThreadID);
pthread_attr_destroy(&mThreadAttr);
#endif
#endif
}
void KThread::CheckThreadError(int aErrCode)
{
#if !defined(__NO_MULTITHREADS__)
if (aErrCode)
{
#if defined(__CONSOLE__)
printf("Thread error %d\n", aErrCode);
#endif
}
#endif
}
void KThread::CheckThreadErrorB(bool aSuccess)
{
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
if (!aSuccess)
CheckThreadError(GetLastError());
#endif
#endif
}
bool KThread::Finished()
{
if (!this)
return true;
return mFinished;
}
void KThread::Resume()
{
if (mTerminated)
return;
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
int SuspendCount;
SuspendCount = ResumeThread(mHandle);
CheckThreadErrorB(SuspendCount >= 0);
mInitialSuspendDone = true;
#endif
#if defined(__LINUX__)
/* About Suspend and Resume. POSIX does not support suspending/resuming a thread.
Suspending a thread is considerd dangerous since it is not guaranteed where the
thread would be suspend. It might be holding a lock, mutex or it might be inside
a critical section. In order to simulate it in Linux we've used signals. To
suspend, a thread SIGSTOP is sent and to resume, SIGCONT is sent. Note that this
is Linux only i.e. according to POSIX if a thread receives SIGSTOP then the
entire process is stopped. However Linux doesn't entirely exhibit the POSIX-mandated
behaviour. If and when it fully complies with the POSIX standard then suspend
and resume won't work. */
if (!mInitialSuspendDone)
{
mInitialSuspendDone = true;
} else
CheckThreadError(pthread_kill(mThreadID, SIGCONT));
#endif
NUtils::SleepMs(100);
#endif
}
void KThread::SetPriority(TThreadPriorityEnum aValue)
{
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__)
CheckThreadErrorB(SetThreadPriority(mHandle, Priorities[aValue]) != 0);
#endif
#if defined(__LINUX__)
switch (aValue)
{
case tpHigher:
mThreadParam.sched_priority = 50;
break;
case tpHighest:
mThreadParam.sched_priority = 50;
break;
case tpTimeCritical:
mThreadParam.sched_priority = 50;
break;
default:
mThreadParam.sched_priority = 0;
break;
}
CheckThreadError(pthread_attr_setschedpolicy(&mThreadAttr, SCHED_RR));
CheckThreadError(pthread_attr_setschedparam(&mThreadAttr, &mThreadParam));
#endif
#endif
}
void KThread::Terminate()
{
#if !defined(__NO_MULTITHREADS__)
if (this) mTerminated = true;
#endif
}
bool KThread::Terminated()
{
#if !defined(__NO_MULTITHREADS__)
return this ? mTerminated : true;
#else
return false;
#endif
}
#if !defined(__NO_MULTITHREADS__)
#if defined(__WINDOWS__) || defined(__WINE__)
unsigned long KThread::WaitFor(void)
{
HANDLE H;
// unsigned long WaitResult;
// MSG Msg;
unsigned long Result;
if (!mInitialSuspendDone)
return false;
H = mHandle;
/* if (false && (TNativeInt) GetCurrentThreadId() == gMainThreadId)
{
WaitResult = 0;
do
{
// This prevents a potential deadlock if the background thread
// does a SendMessage to the foreground thread
if (WaitResult == WAIT_OBJECT_0 + 1)
PeekMessage(&Msg, 0, 0, 0, PM_NOREMOVE);
SleepMs(1);
WaitResult = MsgWaitForMultipleObjects(1, &H, false, 0, QS_SENDMESSAGE);
if (WaitResult == WAIT_FAILED)
{
char Buffer[64];
sprintf(Buffer, "Thread wait error: %d", GetLastError());
ShowMessage(Buffer);
}
} while (WaitResult == WAIT_OBJECT_0 && !mFinished);
} else */
WaitForSingleObject(H, INFINITE);
CheckThreadError(GetExitCodeThread(H, &Result));
return Result;
}
#elif defined(__LINUX__)
pthread_t KThread::WaitFor(void)
{
void * X;
pthread_t ID;
pthread_t Result = 0;
ID = mThreadID;
if ((void *)pthread_self() == (void *)gMainThreadId)
while (!mFinished)
{
NUtils::SleepMs(1);
}
mThreadID = 0;
X = &Result;
CheckThreadError(pthread_join(ID, &X));
return Result;
}
#endif
#endif