Repository navigation
Expand file tree
/
Copy pathThreadConfig.cpp
More file actions
439 lines (390 loc) · 11 KB
/
Copy pathThreadConfig.cpp
File metadata and controls
439 lines (390 loc) · 11 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
#include <cassert>
#include "ThreadConfig.h"
/*!
**
** Copyright (c) 2009 by John W. Ratcliff mailto:jratcliffscarab@gmail.com
**
** If you find this code useful or you are feeling particularily generous I would
** ask that you please go to http://www.amillionpixels.us and make a donation
** to Troy DeMolay.
**
** Skype ID: jratcliff63367
** Yahoo: jratcliff63367
** AOL: jratcliff1961
** email: jratcliffscarab@gmail.com
** Personal website: http://jratcliffscarab.blogspot.com
** Coding Website: http://codesuppository.blogspot.com
** FundRaising Blog: http://amillionpixels.blogspot.com
** Fundraising site: http://www.amillionpixels.us
** New Temple Site: http://newtemple.blogspot.com
**
**
** The MIT license:
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is furnished
** to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all
** copies or substantial portions of the Software.
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if defined(WIN32)
#define _WIN32_WINNT 0x400
#include <windows.h>
#pragma comment(lib,"winmm.lib")
// #ifndef _WIN32_WINNT
// #endif
// #include <windows.h>
//#include <winbase.h>
#endif
#if defined(_XBOX)
#include "NxXBOX.h"
#endif
#if defined(__linux__)
//#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#define __stdcall
#endif
#if defined(__APPLE__) || defined(__linux__)
#include <pthread.h>
#endif
#ifdef NDEBUG
#define VERIFY( x ) (x)
#else
#define VERIFY( x ) assert((x))
#endif
namespace THREAD_CONFIG
{
unsigned int tc_timeGetTime(void)
{
#if defined(__linux__)
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
#else
return timeGetTime();
#endif
}
void tc_sleep(unsigned int ms)
{
#if defined(__linux__)
usleep(ms * 1000);
#else
Sleep(ms);
#endif
}
void tc_spinloop()
{
#ifdef __linux__
asm ( "pause" );
#else
__asm { pause };
#endif
}
void tc_interlockedExchange(void *dest, const int64_t exchange)
{
#ifdef __linux__
// not working
assert(false);
//__sync_lock_test_and_set((int64_t*)dest, exchange);
#else
__asm
{
mov ebx, dword ptr [exchange]
mov ecx, dword ptr [exchange + 4]
mov edi, dest
mov eax, dword ptr [edi]
mov edx, dword ptr [edi + 4]
jmp start
retry:
pause
start:
lock cmpxchg8b [edi]
jnz retry
};
#endif
}
int tc_interlockedCompareExchange(void *dest, int exchange, int compare)
{
#ifdef __linux__
// not working
assert(false);
return 0;
//return __sync_val_compare_and_swap((uintptr_t*)dest, exchange, compare);
//return __sync_bool_compare_and_swap((uintptr_t*)dest, exchange, compare);
#else
char _ret;
//
__asm
{
mov edx, [dest]
mov eax, [compare]
mov ecx, [exchange]
lock cmpxchg [edx], ecx
setz al
mov byte ptr [_ret], al
}
//
return _ret;
#endif
}
int tc_interlockedCompareExchange(void *dest, const int exchange1, const int exchange2, const int compare1, const int compare2)
{
#ifdef __linux__
// not working
assert(false);
return 0;
//uint64_t exchange = ((uint64_t)exchange1 << 32) | (uint64_t)exchange2;
//uint64_t compare = ((uint64_t)compare1 << 32) | (uint64_t)compare2;
//return __sync_bool_compare_and_swap((int64_t*)dest, exchange, compare);
#else
char _ret;
//
__asm
{
mov ebx, [exchange1]
mov ecx, [exchange2]
mov edi, [dest]
mov eax, [compare1]
mov edx, [compare2]
lock cmpxchg8b [edi]
setz al
mov byte ptr [_ret], al
}
//
return _ret;
#endif
}
class MyThreadMutex : public ThreadMutex
{
public:
MyThreadMutex(void)
{
#if defined(WIN32) || defined(_XBOX)
InitializeCriticalSection(&m_Mutex);
#elif defined(__APPLE__) || defined(__linux__)
pthread_mutexattr_t mutexAttr; // Mutex Attribute
VERIFY( pthread_mutexattr_init(&mutexAttr) == 0 );
VERIFY( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0 );
VERIFY( pthread_mutex_init(&m_Mutex, &mutexAttr) == 0 );
VERIFY( pthread_mutexattr_destroy(&mutexAttr) == 0 );
#endif
}
~MyThreadMutex(void)
{
#if defined(WIN32) || defined(_XBOX)
DeleteCriticalSection(&m_Mutex);
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_mutex_destroy(&m_Mutex) == 0 );
#endif
}
void lock(void)
{
#if defined(WIN32) || defined(_XBOX)
EnterCriticalSection(&m_Mutex);
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_mutex_lock(&m_Mutex) == 0 );
#endif
}
bool tryLock(void)
{
#if defined(WIN32) || defined(_XBOX)
bool bRet = false;
//assert(("TryEnterCriticalSection seems to not work on XP???", 0));
bRet = TryEnterCriticalSection(&m_Mutex) ? true : false;
return bRet;
#elif defined(__APPLE__) || defined(__linux__)
int result = pthread_mutex_trylock(&m_Mutex);
return (result == 0);
#endif
}
void unlock(void)
{
#if defined(WIN32) || defined(_XBOX)
LeaveCriticalSection(&m_Mutex);
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_mutex_unlock(&m_Mutex) == 0 );
#endif
}
private:
#if defined(WIN32) || defined(_XBOX)
CRITICAL_SECTION m_Mutex;
#elif defined(__APPLE__) || defined(__linux__)
pthread_mutex_t m_Mutex;
#endif
};
ThreadMutex * tc_createThreadMutex(void)
{
MyThreadMutex *m = new MyThreadMutex;
return static_cast< ThreadMutex *>(m);
}
void tc_releaseThreadMutex(ThreadMutex *tm)
{
MyThreadMutex *m = static_cast< MyThreadMutex *>(tm);
delete m;
}
#if defined(WIN32) || defined(_XBOX)
static unsigned long __stdcall _ThreadWorkerFunc(LPVOID arg);
#elif defined(__APPLE__) || defined(__linux__)
static void* _ThreadWorkerFunc(void* arg);
#endif
class MyThread : public Thread
{
public:
MyThread(ThreadInterface *iface)
{
mInterface = iface;
#if defined(WIN32) || defined(_XBOX)
mThread = CreateThread(0, 0, _ThreadWorkerFunc, this, 0, 0);
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_create(&mThread, NULL, _ThreadWorkerFunc, this) == 0 );
#endif
}
~MyThread(void)
{
#if defined(WIN32) || defined(_XBOX)
if ( mThread )
{
CloseHandle(mThread);
mThread = 0;
}
#endif
}
void onJobExecute(void)
{
mInterface->threadMain();
}
private:
ThreadInterface *mInterface;
#if defined(WIN32) || defined(_XBOX)
HANDLE mThread;
#elif defined(__APPLE__) || defined(__linux__)
pthread_t mThread;
#endif
};
Thread * tc_createThread(ThreadInterface *tinterface)
{
MyThread *m = new MyThread(tinterface);
return static_cast< Thread *>(m);
}
void tc_releaseThread(Thread *t)
{
MyThread *m = static_cast<MyThread *>(t);
delete m;
}
#if defined(WIN32) || defined(_XBOX)
static unsigned long __stdcall _ThreadWorkerFunc(LPVOID arg)
#elif defined(__APPLE__) || defined(__linux__)
static void* _ThreadWorkerFunc(void* arg)
#endif
{
MyThread *worker = (MyThread *) arg;
worker->onJobExecute();
return 0;
}
class MyThreadEvent : public ThreadEvent
{
public:
MyThreadEvent(void)
{
#if defined(WIN32) || defined(_XBOX)
mEvent = ::CreateEventA(NULL,TRUE,TRUE,"ThreadEvent");
#elif defined(__APPLE__) || defined(__linux__)
pthread_mutexattr_t mutexAttr; // Mutex Attribute
VERIFY( pthread_mutexattr_init(&mutexAttr) == 0 );
VERIFY( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0 );
VERIFY( pthread_mutex_init(&mEventMutex, &mutexAttr) == 0 );
VERIFY( pthread_mutexattr_destroy(&mutexAttr) == 0 );
VERIFY( pthread_cond_init(&mEvent, NULL) == 0 );
#endif
}
~MyThreadEvent(void)
{
#if defined(WIN32) || defined(_XBOX)
if ( mEvent )
{
::CloseHandle(mEvent);
}
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_cond_destroy(&mEvent) == 0 );
VERIFY( pthread_mutex_destroy(&mEventMutex) == 0 );
#endif
}
virtual void setEvent(void) // signal the event
{
#if defined(WIN32) || defined(_XBOX)
if ( mEvent )
{
::SetEvent(mEvent);
}
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_mutex_lock(&mEventMutex) == 0 );
VERIFY( pthread_cond_signal(&mEvent) == 0 );
VERIFY( pthread_mutex_unlock(&mEventMutex) == 0 );
#endif
}
void resetEvent(void)
{
#if defined(WIN32) || defined(_XBOX)
if ( mEvent )
{
::ResetEvent(mEvent);
}
#endif
}
virtual void waitForSingleObject(unsigned int ms)
{
#if defined(WIN32) || defined(_XBOX)
if ( mEvent )
{
::WaitForSingleObject(mEvent,ms);
}
#elif defined(__APPLE__) || defined(__linux__)
VERIFY( pthread_mutex_lock(&mEventMutex) == 0 );
if (ms == 0xffffffff)
{
VERIFY( pthread_cond_wait(&mEvent, &mEventMutex) == 0 );
}
else
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += ms * 1000000;
ts.tv_sec += ts.tv_nsec / 1000000000;
ts.tv_nsec %= 1000000000;
int result = pthread_cond_timedwait(&mEvent, &mEventMutex, &ts);
assert(result == 0 || result == ETIMEDOUT);
}
VERIFY( pthread_mutex_unlock(&mEventMutex) == 0 );
#endif
}
private:
#if defined(WIN32) || defined(_XBOX)
HANDLE mEvent;
#elif defined(__APPLE__) || defined(__linux__)
pthread_mutex_t mEventMutex;
pthread_cond_t mEvent;
#endif
};
ThreadEvent * tc_createThreadEvent(void)
{
MyThreadEvent *m = new MyThreadEvent;
return static_cast<ThreadEvent *>(m);
}
void tc_releaseThreadEvent(ThreadEvent *t)
{
MyThreadEvent *m = static_cast< MyThreadEvent *>(t);
delete m;
}
}; // end of namespace