aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/platform.h12
-rw-r--r--src/threading.cpp15
-rw-r--r--src/threading.hpp55
3 files changed, 51 insertions, 31 deletions
diff --git a/src/platform.h b/src/platform.h
index b92f7e0..ce9ece2 100644
--- a/src/platform.h
+++ b/src/platform.h
@@ -1,26 +1,32 @@
1#pragma once 1#pragma once
2 2
3#if (defined _WIN32_WCE) 3#if (defined __MINGW32__) || (defined __MINGW64__) // detect mingw before windows, because mingw defines _WIN32
4#define PLATFORM_MINGW
5//#pragma message("PLATFORM_MINGW")
6#elif (defined _WIN32_WCE)
4#define PLATFORM_POCKETPC 7#define PLATFORM_POCKETPC
8//#pragma message("PLATFORM_POCKETPC")
5#elif defined(_XBOX) 9#elif defined(_XBOX)
6#define PLATFORM_XBOX 10#define PLATFORM_XBOX
11//#pragma message("PLATFORM_XBOX")
7#elif (defined _WIN32) 12#elif (defined _WIN32)
8#define PLATFORM_WIN32 13#define PLATFORM_WIN32
14//#pragma message("PLATFORM_WIN32")
9#if !defined(NOMINMAX) 15#if !defined(NOMINMAX)
10#define NOMINMAX 16#define NOMINMAX
11#endif // NOMINMAX 17#endif // NOMINMAX
12#elif (defined __linux__) 18#elif (defined __linux__)
13#define PLATFORM_LINUX 19#define PLATFORM_LINUX
20//#pragma message("PLATFORM_LINUX")
14#elif (defined __APPLE__) && (defined __MACH__) 21#elif (defined __APPLE__) && (defined __MACH__)
15#define PLATFORM_OSX 22#define PLATFORM_OSX
23//#pragma message("PLATFORM_OSX")
16#elif (defined __NetBSD__) || (defined __FreeBSD__) || (defined BSD) 24#elif (defined __NetBSD__) || (defined __FreeBSD__) || (defined BSD)
17#define PLATFORM_BSD 25#define PLATFORM_BSD
18#elif (defined __QNX__) 26#elif (defined __QNX__)
19#define PLATFORM_QNX 27#define PLATFORM_QNX
20#elif (defined __CYGWIN__) 28#elif (defined __CYGWIN__)
21#define PLATFORM_CYGWIN 29#define PLATFORM_CYGWIN
22#elif (defined __MINGW32__) || (defined __MINGW64__)
23#define PLATFORM_MINGW
24#else 30#else
25#error "Unknown platform!" 31#error "Unknown platform!"
26#endif 32#endif
diff --git a/src/threading.cpp b/src/threading.cpp
index bedbcf8..3e594ff 100644
--- a/src/threading.cpp
+++ b/src/threading.cpp
@@ -86,7 +86,7 @@ THE SOFTWARE.
86 * FAIL is for unexpected API return values - essentially programming 86 * FAIL is for unexpected API return values - essentially programming
87 * error in _this_ code. 87 * error in _this_ code.
88 */ 88 */
89#if defined(PLATFORM_XBOX) || defined(PLATFORM_WIN32) || defined(PLATFORM_POCKETPC) 89#if HAVE_WIN32
90static void FAIL(char const* funcname_, DWORD const rc_) 90static void FAIL(char const* funcname_, DWORD const rc_)
91{ 91{
92#if defined(PLATFORM_XBOX) 92#if defined(PLATFORM_XBOX)
@@ -101,7 +101,7 @@ static void FAIL(char const* funcname_, DWORD const rc_)
101#endif // _MSC_VER 101#endif // _MSC_VER
102 abort(); 102 abort();
103} 103}
104#endif // win32 build 104#endif // HAVE_WIN32
105 105
106/*---=== Threading ===---*/ 106/*---=== Threading ===---*/
107 107
@@ -136,7 +136,7 @@ void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, [[maybe_unused]] bool
136 // prio range [-3,+3] was checked by the caller 136 // prio range [-3,+3] was checked by the caller
137 // for some reason when building for mingw, native_handle() is an unsigned long long, but HANDLE is a void* 137 // for some reason when building for mingw, native_handle() is an unsigned long long, but HANDLE is a void*
138 // -> need a strong cast to make g++ happy 138 // -> need a strong cast to make g++ happy
139 if (!SetThreadPriority((HANDLE)thread_.native_handle(), gs_prio_remap[prio_ + 3])) { 139 if (!SetThreadPriority(thread_.native_handle(), gs_prio_remap[prio_ + 3])) {
140 FAIL("THREAD_SET_PRIORITY", GetLastError()); 140 FAIL("THREAD_SET_PRIORITY", GetLastError());
141 } 141 }
142} 142}
@@ -382,7 +382,7 @@ void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, [[maybe_unused]] bool
382 struct sched_param sp; 382 struct sched_param sp;
383 // prio range [-3,+3] was checked by the caller 383 // prio range [-3,+3] was checked by the caller
384 sp.sched_priority = gs_prio_remap[prio_ + 3]; 384 sp.sched_priority = gs_prio_remap[prio_ + 3];
385 PT_CALL(pthread_setschedparam(static_cast<pthread_t>(thread_.native_handle()), _PRIO_MODE, &sp)); 385 PT_CALL(pthread_setschedparam(thread_.native_handle(), _PRIO_MODE, &sp));
386} 386}
387 387
388// ################################################################################################# 388// #################################################################################################
@@ -398,6 +398,12 @@ void THREAD_SET_AFFINITY(unsigned int aff_)
398 398
399void THREAD_SET_AFFINITY(unsigned int aff_) 399void THREAD_SET_AFFINITY(unsigned int aff_)
400{ 400{
401#if HAVE_WIN32 // "hybrid": Win32 API is available, and pthread too
402 // since pthread_setaffinity_np can be missing (for example mingw), use win32 api instead
403 if (!SetThreadAffinityMask(GetCurrentThread(), aff_)) {
404 FAIL("THREAD_SET_AFFINITY", GetLastError());
405 }
406#else // pure pthread
401 int bit = 0; 407 int bit = 0;
402#ifdef __NetBSD__ 408#ifdef __NetBSD__
403 cpuset_t* cpuset = cpuset_create(); 409 cpuset_t* cpuset = cpuset_create();
@@ -423,6 +429,7 @@ void THREAD_SET_AFFINITY(unsigned int aff_)
423#else 429#else
424 PT_CALL(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset)); 430 PT_CALL(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset));
425#endif 431#endif
432#endif // PLATFORM_MINGW
426} 433}
427 434
428#endif // __PROSPERO__ 435#endif // __PROSPERO__
diff --git a/src/threading.hpp b/src/threading.hpp
index 044b5a4..912c28f 100644
--- a/src/threading.hpp
+++ b/src/threading.hpp
@@ -5,13 +5,38 @@
5#define THREADAPI_WINDOWS 1 5#define THREADAPI_WINDOWS 1
6#define THREADAPI_PTHREAD 2 6#define THREADAPI_PTHREAD 2
7 7
8#if (defined(PLATFORM_XBOX) || defined(PLATFORM_WIN32) || defined(PLATFORM_POCKETPC)) 8#if __has_include(<pthread.h>)
9// #pragma message ( "THREADAPI_WINDOWS" ) 9#include <pthread.h>
10#define THREADAPI THREADAPI_WINDOWS 10#define HAVE_PTHREAD 1
11#else // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 11//#pragma message("HAVE_PTHREAD")
12// #pragma message ( "THREADAPI_PTHREAD" ) 12#else
13#define HAVE_PTHREAD 0
14#endif // <pthread.h>
15
16#if __has_include(<windows.h>)
17#define WIN32_LEAN_AND_MEAN
18#include <windows.h>
19#define HAVE_WIN32 1
20//#pragma message("HAVE_WIN32")
21#elif __has_include(<xtl.h>)
22#include <xtl.h>
23#define HAVE_WIN32 1
24//#pragma message("HAVE_WIN32")
25#else // no <windows.h> nor <xtl.h>
26#define HAVE_WIN32 0
27#endif // <windows.h>
28
29#if HAVE_PTHREAD
30// unless proven otherwise, if pthread is available, let's assume that's what std::thread is using
13#define THREADAPI THREADAPI_PTHREAD 31#define THREADAPI THREADAPI_PTHREAD
14#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) 32//#pragma message ( "THREADAPI_PTHREAD" )
33#elif HAVE_WIN32
34//#pragma message ( "THREADAPI_WINDOWS" )
35#define THREADAPI THREADAPI_WINDOWS
36#include <process.h>
37#else // unknown
38#error "unsupported threading API"
39#endif // unknown
15 40
16static constexpr int kThreadPrioDefault{ -999 }; 41static constexpr int kThreadPrioDefault{ -999 };
17 42
@@ -19,18 +44,6 @@ static constexpr int kThreadPrioDefault{ -999 };
19// ################################################################################################# 44// #################################################################################################
20#if THREADAPI == THREADAPI_WINDOWS 45#if THREADAPI == THREADAPI_WINDOWS
21 46
22#if defined(PLATFORM_XBOX)
23#include <xtl.h>
24#else // !PLATFORM_XBOX
25#define WIN32_LEAN_AND_MEAN
26// CONDITION_VARIABLE needs version 0x0600+
27// _WIN32_WINNT value is already defined by MinGW, but not by MSVC
28#ifndef _WIN32_WINNT
29#define _WIN32_WINNT 0x0600
30#endif // _WIN32_WINNT
31#include <windows.h>
32#endif // !PLATFORM_XBOX
33#include <process.h>
34 47
35/* 48/*
36#define XSTR(x) STR(x) 49#define XSTR(x) STR(x)
@@ -49,12 +62,6 @@ static constexpr int kThreadPrioMax{ +3 };
49 62
50// PThread (Linux, OS X, ...) 63// PThread (Linux, OS X, ...)
51 64
52// looks like some MinGW installations don't support PTW32_INCLUDE_WINDOWS_H, so let's include it ourselves, just in case
53#if defined(PLATFORM_WIN32)
54#include <windows.h>
55#endif // PLATFORM_WIN32
56#include <pthread.h>
57
58#if defined(PLATFORM_LINUX) && !defined(LINUX_SCHED_RR) 65#if defined(PLATFORM_LINUX) && !defined(LINUX_SCHED_RR)
59static constexpr int kThreadPrioMin{ 0 }; 66static constexpr int kThreadPrioMin{ 0 };
60#else 67#else