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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/*
* THREADING.CPP Copyright (c) 2007-08, Asko Kauppi
* Copyright (C) 2009-24, Benoit Germain
*
* Lua Lanes OS threading specific code.
*
* References:
* <http://www.cse.wustl.edu/~schmidt/win32-cv-1.html>
*/
/*
===============================================================================
Copyright (C) 2007-10 Asko Kauppi <akauppi@gmail.com>
Copyright (C) 2009-24, Benoit Germain <bnt.germain@gmail.com>
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.
===============================================================================
*/
#include "_pch.hpp"
#if defined(__linux__)
#ifndef _GNU_SOURCE // definition by the makefile can cause a redefinition error
#define _GNU_SOURCE // must be defined before any include
#endif // _GNU_SOURCE
#ifdef __ANDROID__
#include <android/log.h>
#define LOG_TAG "LuaLanes"
#endif // __ANDROID__
#endif // __linux__
#include "threading.hpp"
#if !defined(PLATFORM_XBOX) && !defined(PLATFORM_WIN32) && !defined(PLATFORM_POCKETPC)
#include <sys/time.h>
#endif // non-WIN32 timing
#if defined(PLATFORM_LINUX) || defined(PLATFORM_CYGWIN)
#include <sys/types.h>
#include <unistd.h>
#endif
#ifdef PLATFORM_OSX
#include "threading_osx.h"
#endif
/* Linux with older glibc (such as Debian) don't have pthread_setname_np, but have prctl
*/
#if defined PLATFORM_LINUX
#if defined __GNU_LIBRARY__ && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 12
#define LINUX_USE_PTHREAD_SETNAME_NP 1
#else // glibc without pthread_setname_np
#include <sys/prctl.h>
#define LINUX_USE_PTHREAD_SETNAME_NP 0
#endif // glibc without pthread_setname_np
#endif // PLATFORM_LINUX
#ifdef _MSC_VER
// ".. selected for automatic inline expansion" (/O2 option)
#pragma warning(disable : 4711)
// ".. type cast from function pointer ... to data pointer"
#pragma warning(disable : 4054)
#endif
/*
* FAIL is for unexpected API return values - essentially programming
* error in _this_ code.
*/
#if defined(PLATFORM_XBOX) || defined(PLATFORM_WIN32) || defined(PLATFORM_POCKETPC)
static void FAIL(char const* funcname_, DWORD const rc_)
{
#if defined(PLATFORM_XBOX)
fprintf(stderr, "%s() failed! (%d)\n", funcname_, rc_);
#else // PLATFORM_XBOX
char buf[256];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, rc_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, nullptr);
fprintf(stderr, "%s() failed! [GetLastError() -> %lu] '%s'", funcname_, rc_, buf);
#endif // PLATFORM_XBOX
#ifdef _MSC_VER
__debugbreak(); // give a chance to the debugger!
#endif // _MSC_VER
abort();
}
#endif // win32 build
/*---=== Threading ===---*/
// #################################################################################################
// #################################################################################################
#if THREADAPI == THREADAPI_WINDOWS
static int const gs_prio_remap[] = {
THREAD_PRIORITY_IDLE,
THREAD_PRIORITY_LOWEST,
THREAD_PRIORITY_BELOW_NORMAL,
THREAD_PRIORITY_NORMAL,
THREAD_PRIORITY_ABOVE_NORMAL,
THREAD_PRIORITY_HIGHEST,
THREAD_PRIORITY_TIME_CRITICAL
};
// #################################################################################################
void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_)
{
// prio range [-3,+3] was checked by the caller
if (!SetThreadPriority(GetCurrentThread(), gs_prio_remap[prio_ + 3])) {
FAIL("THREAD_SET_PRIORITY", GetLastError());
}
}
// #################################################################################################
void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, [[maybe_unused]] bool sudo_)
{
// prio range [-3,+3] was checked by the caller
// for some reason when building for mingw, native_handle() is an unsigned long long, but HANDLE is a void*
// -> need a strong cast to make g++ happy
if (!SetThreadPriority((HANDLE)thread_.native_handle(), gs_prio_remap[prio_ + 3])) {
FAIL("THREAD_SET_PRIORITY", GetLastError());
}
}
// #################################################################################################
void THREAD_SET_AFFINITY(unsigned int aff_)
{
if (!SetThreadAffinityMask(GetCurrentThread(), aff_)) {
FAIL("THREAD_SET_AFFINITY", GetLastError());
}
}
// #################################################################################################
#if !defined __GNUC__
// see http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
#define MS_VC_EXCEPTION 0x406D1388
#pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // Must be 0x1000.
LPCSTR szName; // Pointer to name (in user addr space).
DWORD dwThreadID; // Thread ID (-1=caller thread).
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
#endif // !__GNUC__
void THREAD_SETNAME(std::string_view const& name_)
{
#if !defined __GNUC__
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name_.data();
info.dwThreadID = GetCurrentThreadId();
info.dwFlags = 0;
__try {
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*) &info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
#endif // !__GNUC__
}
// #################################################################################################
// #################################################################################################
#else // THREADAPI == THREADAPI_PTHREAD
// #################################################################################################
// #################################################################################################
// PThread (Linux, OS X, ...)
//
// On OS X, user processes seem to be able to change priorities.
// On Linux, SCHED_RR and su privileges are required.. !-(
//
#include <errno.h>
#include <sched.h>
#if (defined PLATFORM_MINGW) && defined pthread_attr_setschedpolicy
#if pthread_attr_setschedpolicy(A, S) == ENOTSUP
// from the mingw-w64 team:
// Well, we support pthread_setschedparam by which you can specify
// threading-policy. Nevertheless, yes we lack this function. In
// general its implementation is pretty much trivial, as on Win32 target
// just SCHED_OTHER can be supported.
#undef pthread_attr_setschedpolicy
[[nodiscard]]
static int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy)
{
if (policy != SCHED_OTHER) {
return ENOTSUP;
}
return 0;
}
#endif // pthread_attr_setschedpolicy()
#endif // defined(__MINGW32__) || defined(__MINGW64__)
static void _PT_FAIL(int rc, const char* name, const char* file, int line)
{
const char* why = (rc == EINVAL) ? "EINVAL"
: (rc == EBUSY) ? "EBUSY"
: (rc == EPERM) ? "EPERM"
: (rc == ENOMEM) ? "ENOMEM"
: (rc == ESRCH) ? "ESRCH"
: (rc == ENOTSUP) ? "ENOTSUP"
: "<UNKNOWN>";
fprintf(stderr, "%s %d: %s failed, %d %s\n", file, line, name, rc, why);
abort();
}
#define PT_CALL(call) \
{ \
int rc = call; \
if (rc != 0) \
_PT_FAIL(rc, #call, __FILE__, __LINE__); \
}
// array of 7 thread priority values, hand-tuned by platform so that we offer a uniform [-3,+3] public priority range
static int const gs_prio_remap[] = {
// NB: PThreads priority handling is about as twisty as one can get it
// (and then some). DON*T TRUST ANYTHING YOU READ ON THE NET!!!
//---
// "Select the scheduling policy for the thread: one of SCHED_OTHER
// (regular, non-real-time scheduling), SCHED_RR (real-time,
// round-robin) or SCHED_FIFO (real-time, first-in first-out)."
//
// "Using the RR policy ensures that all threads having the same
// priority level will be scheduled equally, regardless of their activity."
//
// "For SCHED_FIFO and SCHED_RR, the only required member of the
// sched_param structure is the priority sched_priority. For SCHED_OTHER,
// the affected scheduling parameters are implementation-defined."
//
// "The priority of a thread is specified as a delta which is added to
// the priority of the process."
//
// ".. priority is an integer value, in the range from 1 to 127.
// 1 is the least-favored priority, 127 is the most-favored."
//
// "Priority level 0 cannot be used: it is reserved for the system."
//
// "When you use specify a priority of -99 in a call to
// pthread_setschedparam(), the priority of the target thread is
// lowered to the lowest possible value."
//
// ...
// ** CONCLUSION **
//
// PThread priorities are _hugely_ system specific, and we need at
// least OS specific settings. Hopefully, Linuxes and OS X versions
// are uniform enough, among each other...
//
#if defined PLATFORM_OSX
// AK 10-Apr-07 (OS X PowerPC 10.4.9):
//
// With SCHED_RR, 26 seems to be the "normal" priority, where setting
// it does not seem to affect the order of threads processed.
//
// With SCHED_OTHER, the range 25..32 is normal (maybe the same 26,
// but the difference is not so clear with OTHER).
//
// 'sched_get_priority_min()' and '..max()' give 15, 47 as the
// priority limits. This could imply, user mode applications won't
// be able to use values outside of that range.
//
#define _PRIO_MODE SCHED_OTHER
// OS X 10.4.9 (PowerPC) gives ENOTSUP for process scope
// #define _PRIO_SCOPE PTHREAD_SCOPE_PROCESS
#define _PRIO_HI 32 // seems to work (_carefully_ picked!)
#define _PRIO_0 26 // detected
#define _PRIO_LO 1 // seems to work (tested)
#elif defined PLATFORM_LINUX
// (based on Ubuntu Linux 2.6.15 kernel)
//
// SCHED_OTHER is the default policy, but does not allow for priorities.
// SCHED_RR allows priorities, all of which (1..99) are higher than
// a thread with SCHED_OTHER policy.
//
// <http://kerneltrap.org/node/6080>
// <http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library>
// <http://www.net.in.tum.de/~gregor/docs/pthread-scheduling.html>
//
// Manuals suggest checking #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING,
// but even Ubuntu does not seem to define it.
//
#define _PRIO_MODE SCHED_RR
// NTLP 2.5: only system scope allowed (being the basic reason why
// root privileges are required..)
// #define _PRIO_SCOPE PTHREAD_SCOPE_PROCESS
#define _PRIO_HI 99
#define _PRIO_0 50
#define _PRIO_LO 1
#elif defined(PLATFORM_BSD)
//
// <http://www.net.in.tum.de/~gregor/docs/pthread-scheduling.html>
//
// "When control over the thread scheduling is desired, then FreeBSD
// with the libpthread implementation is by far the best choice .."
//
#define _PRIO_MODE SCHED_OTHER
#define _PRIO_SCOPE PTHREAD_SCOPE_PROCESS
#define _PRIO_HI 31
#define _PRIO_0 15
#define _PRIO_LO 1
#elif defined(PLATFORM_CYGWIN) || defined(PLATFORM_MINGW)
//
// TBD: Find right values for Cygwin
//
#define _PRIO_MODE SCHED_OTHER
#define _PRIO_SCOPE PTHREAD_SCOPE_PROCESS
#define _PRIO_HI 31
#define _PRIO_0 15
#define _PRIO_LO 1
#else
#error "Unknown OS: not implemented!"
#endif
#if defined _PRIO_0
#define _PRIO_AN (_PRIO_0 + ((_PRIO_HI - _PRIO_0) / 2))
#define _PRIO_BN (_PRIO_LO + ((_PRIO_0 - _PRIO_LO) / 2))
_PRIO_LO,
_PRIO_LO,
_PRIO_BN,
_PRIO_0,
_PRIO_AN,
_PRIO_HI,
_PRIO_HI
#endif // _PRIO_0
};
void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_)
{
#ifdef PLATFORM_LINUX
if (!sudo_) // only root-privileged process can change priorities
return;
#endif // PLATFORM_LINUX
struct sched_param sp;
// prio range [-3,+3] was checked by the caller
sp.sched_priority = gs_prio_remap[prio_ + 3];
PT_CALL(pthread_setschedparam(pthread_self(), _PRIO_MODE, &sp));
}
// #################################################################################################
void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, [[maybe_unused]] bool sudo_)
{
#ifdef PLATFORM_LINUX
if (!sudo_) // only root-privileged process can change priorities
return;
#endif // PLATFORM_LINUX
struct sched_param sp;
// prio range [-3,+3] was checked by the caller
sp.sched_priority = gs_prio_remap[prio_ + 3];
PT_CALL(pthread_setschedparam(static_cast<pthread_t>(thread_.native_handle()), _PRIO_MODE, &sp));
}
// #################################################################################################
#ifdef __PROSPERO__
void THREAD_SET_AFFINITY(unsigned int aff_)
{
scePthreadSetaffinity(scePthreadSelf(), aff_);
}
#else // __PROSPERO__
void THREAD_SET_AFFINITY(unsigned int aff_)
{
int bit = 0;
#ifdef __NetBSD__
cpuset_t* cpuset = cpuset_create();
if (cpuset == nullptr)
_PT_FAIL(errno, "cpuset_create", __FILE__, __LINE__ - 2);
#define CPU_SET(b, s) cpuset_set(b, *(s))
#else
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
#endif
while (aff_ != 0) {
if (aff_ & 1) {
CPU_SET(bit, &cpuset);
}
++bit;
aff_ >>= 1;
}
#ifdef __ANDROID__
PT_CALL(sched_setaffinity(pthread_self(), sizeof(cpu_set_t), &cpuset));
#elif defined(__NetBSD__)
PT_CALL(pthread_setaffinity_np(pthread_self(), cpuset_size(cpuset), cpuset));
cpuset_destroy(cpuset);
#else
PT_CALL(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset));
#endif
}
#endif // __PROSPERO__
// #################################################################################################
#ifdef __PROSPERO__
void THREAD_SETNAME(std::string_view const& name_)
{
scePthreadRename(scePthreadSelf(), name_.data());
}
#else // __PROSPERO__
void THREAD_SETNAME(std::string_view const& name_)
{
// exact API to set the thread name is platform-dependant
// if you need to fix the build, or if you know how to fill a hole, tell me (bnt.germain@gmail.com) so that I can submit the fix in github.
#if defined PLATFORM_BSD && !defined __NetBSD__
pthread_set_name_np(pthread_self(), name_.data());
#elif defined PLATFORM_BSD && defined __NetBSD__
pthread_setname_np(pthread_self(), "%s", (void*) name_.data());
#elif defined PLATFORM_LINUX
#if LINUX_USE_PTHREAD_SETNAME_NP
pthread_setname_np(pthread_self(), name_.data());
#else // LINUX_USE_PTHREAD_SETNAME_NP
prctl(PR_SET_NAME, name_.data(), 0, 0, 0);
#endif // LINUX_USE_PTHREAD_SETNAME_NP
#elif defined PLATFORM_QNX || defined PLATFORM_CYGWIN
pthread_setname_np(pthread_self(), name_.data());
#elif defined PLATFORM_OSX
pthread_setname_np(name_.data());
#else
fprintf(stderr, "THREAD_SETNAME: unsupported platform\n");
abort();
#endif
}
#endif // __PROSPERO__
#endif // THREADAPI == THREADAPI_PTHREAD
// #################################################################################################
// #################################################################################################
|