-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCcpThread.cpp
More file actions
323 lines (272 loc) · 7.21 KB
/
CcpThread.cpp
File metadata and controls
323 lines (272 loc) · 7.21 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
// Copyright © 2013 CCP ehf.
#include <CCPLog.h>
#include "include/CcpThread.h"
#include "include/CCPMemory.h"
namespace
{
struct CreateThreadData
{
CcpThreadProc_t functionToCall;
void* context;
};
}
#if _WIN32
#include <map>
namespace
{
std::map<CcpThreadHandle_t,CcpThreadId_t> s_handleToId;
DWORD WINAPI ThreadProcHelper( void* context )
{
CreateThreadData* data = reinterpret_cast<CreateThreadData*>( context );
CcpThreadProc_t f = data->functionToCall;
void* ctx = data->context;
CCP_DELETE data;
return f( ctx );
}
}
CcpThreadId_t CcpGetCurrentThreadId()
{
return GetCurrentThreadId();
}
CcpThreadHandle_t CcpCreateThread( CcpThreadProc_t threadProc, void* context, CcpThreadPriority_t priority )
{
CreateThreadData* data = CCP_NEW( "CcpCreateThread/data" ) CreateThreadData;
data->functionToCall = threadProc;
data->context = context;
CcpThreadId_t id;
CcpThreadHandle_t threadHandle = CreateThread( 0, 0, ThreadProcHelper, data, CREATE_SUSPENDED, &id );
s_handleToId[threadHandle] = id;
SetThreadPriority( threadHandle, priority );
ResumeThread( threadHandle );
return threadHandle;
}
CcpThreadId_t CcpGetThreadId( CcpThreadHandle_t handle )
{
return s_handleToId[handle];
}
int CcpJoinThread( CcpThreadHandle_t threadHandle, uint32_t& result )
{
return CcpJoinThreadWithTimeout( threadHandle, INFINITE, result );
}
int CcpJoinThreadWithTimeout( CcpThreadHandle_t threadHandle, uint32_t timeoutInMs, uint32_t& result )
{
DWORD s = WaitForSingleObject( threadHandle, timeoutInMs );
if( s == 0 )
{
GetExitCodeThread( threadHandle, (DWORD*)&result );
::CloseHandle( threadHandle );
return 0;
}
else
{
// TODO: Translate error code
return s;
}
}
bool CcpSetThreadPriority( CcpThreadHandle_t thread, CcpThreadPriority_t priority )
{
return SetThreadPriority( thread, priority ) != 0;
}
void CcpThreadSleep( uint32_t sleepTimeInMs )
{
Sleep( sleepTimeInMs );
}
void CcpKillThread( CcpThreadHandle_t threadHandle )
{
TerminateThread( threadHandle, 0 );
}
void CcpSetThreadPriority( CcpThread& thread, CcpThreadPriority_t priority )
{
if( thread.joinable() )
{
SetThreadPriority( thread.native_handle(), priority );
}
}
bool CcpGetThreadTimes( int64_t& kernelTime, int64_t& userTime )
{
FILETIME dummy;
return GetThreadTimes( GetCurrentThread(), &dummy, &dummy, (LPFILETIME)&kernelTime, (LPFILETIME)&userTime ) != 0;
}
#elif __APPLE__
#include <sys/time.h>
#include "include/CCPAssert.h"
#include <mach/thread_act.h>
namespace
{
void* ThreadProcHelper( void* context )
{
CreateThreadData* data = reinterpret_cast<CreateThreadData*>( context );
CcpThreadProc_t f = data->functionToCall;
void* ctx = data->context;
CCP_DELETE data;
return (void*)(ptrdiff_t)f( ctx );
}
}
CcpThreadId_t CcpGetCurrentThreadId()
{
return pthread_mach_thread_np(pthread_self());
}
CcpThreadHandle_t CcpCreateThread( CcpThreadProc_t threadProc, void* context, CcpThreadPriority_t priority )
{
pthread_attr_t attr;
if( pthread_attr_init( &attr ) != 0 )
{
return nullptr;
}
CreateThreadData* data = CCP_NEW( "CcpCreateThread/data" ) CreateThreadData;
data->functionToCall = threadProc;
data->context = context;
CcpThreadHandle_t threadHandle;
int s = pthread_create( &threadHandle, &attr, ThreadProcHelper, data );
if( priority != CCP_THREAD_PRIORITY_NORMAL )
{
CcpSetThreadPriority( threadHandle, priority );
}
pthread_attr_destroy( &attr );
if( s == 0 )
{
return threadHandle;
}
else
{
return nullptr;
}
}
int CcpJoinThread( CcpThreadHandle_t threadHandle, uint32_t& result )
{
void* threadResult = nullptr;
int s = pthread_join( threadHandle, &threadResult );
if( s == 0 )
{
result = uint32_t( uintptr_t( threadResult ) );
return 0;
}
else
{
// TODO: Translate error code
return s;
}
}
namespace
{
struct JoinTimeoutData
{
CcpThreadHandle_t threadHandle;
bool done;
void* threadResult;
};
void* JoinTimeoutHelper( void* arg )
{
JoinTimeoutData* data = static_cast<JoinTimeoutData*>( arg );
pthread_join( data->threadHandle, &data->threadResult );
data->done = true;
return nullptr;
}
uint32_t GetTicks()
{
timeval tv;
gettimeofday( &tv, nullptr );
return uint32_t( tv.tv_usec / 1000 + tv.tv_sec * 1000 );
}
}
int CcpJoinThreadWithTimeout( CcpThreadHandle_t threadHandle, uint32_t timeoutInMs, uint32_t& result )
{
uint32_t start = GetTicks();
JoinTimeoutData data;
data.threadHandle = threadHandle;
data.done = false;
CcpThreadHandle_t id;
if( pthread_create( &id, nullptr, &JoinTimeoutHelper, &data ) != 0 )
{
return -1;
}
do
{
if( data.done )
{
break;
}
CcpThreadSleep( 10 );
}
while( GetTicks() - start < timeoutInMs );
if( !data.done )
{
pthread_cancel( id );
return 1;
}
pthread_join( id, nullptr );
result = uint32_t( uintptr_t( data.threadResult ) );
return 0;
}
CcpThreadId_t CcpGetThreadId( CcpThreadHandle_t handle )
{
return pthread_mach_thread_np(handle);
}
bool CcpSetThreadPriority( CcpThreadHandle_t thread, CcpThreadPriority_t priority )
{
int policy;
sched_param param;
if( pthread_getschedparam( thread, &policy, ¶m ) )
{
return false;
}
auto minPriority = sched_get_priority_min( policy );
auto maxPriority = sched_get_priority_max( policy );
auto normalPriority = ( minPriority + maxPriority ) / 2;
switch( priority )
{
case CCP_THREAD_PRIORITY_LOWEST:
param.sched_priority = minPriority;
break;
case CCP_THREAD_PRIORITY_BELOW_NORMAL:
param.sched_priority = ( minPriority + normalPriority ) / 2;
break;
case CCP_THREAD_PRIORITY_HIGHEST:
param.sched_priority = maxPriority;
break;
case CCP_THREAD_PRIORITY_ABOVE_NORMAL:
param.sched_priority = ( maxPriority + normalPriority ) / 2;
break;
default:
param.sched_priority = normalPriority;
}
return pthread_setschedparam( thread, policy, ¶m ) == 0;
}
void CcpThreadSleep( uint32_t sleepTimeInMs )
{
timespec ts;
ts.tv_sec = sleepTimeInMs / 1000;
ts.tv_nsec = (sleepTimeInMs % 1000) * 1000000;
nanosleep( &ts, nullptr );
}
void CcpKillThread( CcpThreadHandle_t threadHandle )
{
pthread_cancel( threadHandle );
}
void CcpSetThreadPriority( CcpThread& thread, CcpThreadPriority_t priority )
{
if( !thread.joinable() )
{
return;
}
CcpSetThreadPriority( thread.native_handle(), priority );
}
bool CcpGetThreadTimes( int64_t& kernelTime, int64_t& userTime )
{
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
thread_basic_info_data_t info;
mach_port_t port = pthread_mach_thread_np( pthread_self() );
if( !MACH_PORT_VALID( port ) )
{
return false;
}
int returnCode = thread_info( port, THREAD_BASIC_INFO, (thread_info_t)&info, &count );
if( returnCode != KERN_SUCCESS )
{
return false;
}
userTime = int64_t( info.user_time.seconds ) * 10000000 + int64_t( info.user_time.microseconds ) * 10;
kernelTime = int64_t( info.system_time.seconds ) * 10000000 + int64_t( info.system_time.microseconds ) * 10;
return true;
}
#endif