diff options
Diffstat (limited to 'src/threading.hpp')
-rw-r--r-- | src/threading.hpp | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/src/threading.hpp b/src/threading.hpp index 044b5a4..07c1ab3 100644 --- a/src/threading.hpp +++ b/src/threading.hpp | |||
@@ -1,17 +1,43 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "platform.h" | 3 | #include "platform.h" |
4 | #include "unique.hpp" | ||
4 | 5 | ||
5 | #define THREADAPI_WINDOWS 1 | 6 | #define THREADAPI_WINDOWS 1 |
6 | #define THREADAPI_PTHREAD 2 | 7 | #define THREADAPI_PTHREAD 2 |
7 | 8 | ||
8 | #if (defined(PLATFORM_XBOX) || defined(PLATFORM_WIN32) || defined(PLATFORM_POCKETPC)) | 9 | #if __has_include(<pthread.h>) |
9 | // #pragma message ( "THREADAPI_WINDOWS" ) | 10 | #include <pthread.h> |
10 | #define THREADAPI THREADAPI_WINDOWS | 11 | #define HAVE_PTHREAD 1 |
11 | #else // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) | 12 | //#pragma message("HAVE_PTHREAD") |
12 | // #pragma message ( "THREADAPI_PTHREAD" ) | 13 | #else |
14 | #define HAVE_PTHREAD 0 | ||
15 | #endif // <pthread.h> | ||
16 | |||
17 | #if __has_include(<windows.h>) | ||
18 | #define WIN32_LEAN_AND_MEAN | ||
19 | #include <windows.h> | ||
20 | #define HAVE_WIN32 1 | ||
21 | //#pragma message("HAVE_WIN32") | ||
22 | #elif __has_include(<xtl.h>) | ||
23 | #include <xtl.h> | ||
24 | #define HAVE_WIN32 1 | ||
25 | //#pragma message("HAVE_WIN32") | ||
26 | #else // no <windows.h> nor <xtl.h> | ||
27 | #define HAVE_WIN32 0 | ||
28 | #endif // <windows.h> | ||
29 | |||
30 | #if HAVE_PTHREAD | ||
31 | // unless proven otherwise, if pthread is available, let's assume that's what std::thread is using | ||
13 | #define THREADAPI THREADAPI_PTHREAD | 32 | #define THREADAPI THREADAPI_PTHREAD |
14 | #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) | 33 | //#pragma message ( "THREADAPI_PTHREAD" ) |
34 | #elif HAVE_WIN32 | ||
35 | //#pragma message ( "THREADAPI_WINDOWS" ) | ||
36 | #define THREADAPI THREADAPI_WINDOWS | ||
37 | #include <process.h> | ||
38 | #else // unknown | ||
39 | #error "unsupported threading API" | ||
40 | #endif // unknown | ||
15 | 41 | ||
16 | static constexpr int kThreadPrioDefault{ -999 }; | 42 | static constexpr int kThreadPrioDefault{ -999 }; |
17 | 43 | ||
@@ -19,18 +45,6 @@ static constexpr int kThreadPrioDefault{ -999 }; | |||
19 | // ################################################################################################# | 45 | // ################################################################################################# |
20 | #if THREADAPI == THREADAPI_WINDOWS | 46 | #if THREADAPI == THREADAPI_WINDOWS |
21 | 47 | ||
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 | 48 | ||
35 | /* | 49 | /* |
36 | #define XSTR(x) STR(x) | 50 | #define XSTR(x) STR(x) |
@@ -49,12 +63,6 @@ static constexpr int kThreadPrioMax{ +3 }; | |||
49 | 63 | ||
50 | // PThread (Linux, OS X, ...) | 64 | // PThread (Linux, OS X, ...) |
51 | 65 | ||
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) | 66 | #if defined(PLATFORM_LINUX) && !defined(LINUX_SCHED_RR) |
59 | static constexpr int kThreadPrioMin{ 0 }; | 67 | static constexpr int kThreadPrioMin{ 0 }; |
60 | #else | 68 | #else |
@@ -66,8 +74,15 @@ static constexpr int kThreadPrioMax{ +3 }; | |||
66 | // ################################################################################################# | 74 | // ################################################################################################# |
67 | // ################################################################################################# | 75 | // ################################################################################################# |
68 | 76 | ||
77 | DECLARE_UNIQUE_TYPE(SudoFlag, bool); | ||
78 | DECLARE_UNIQUE_TYPE(NativePrioFlag, bool); | ||
79 | |||
80 | std::pair<int, int> THREAD_NATIVE_PRIOS(); | ||
81 | |||
69 | void THREAD_SETNAME(std::string_view const& name_); | 82 | void THREAD_SETNAME(std::string_view const& name_); |
70 | void THREAD_SET_PRIORITY(int prio_, bool sudo_); | ||
71 | void THREAD_SET_AFFINITY(unsigned int aff_); | ||
72 | 83 | ||
73 | void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, bool sudo_); | 84 | void THREAD_SET_PRIORITY(lua_State* L_, int prio_, NativePrioFlag native_, SudoFlag sudo_); |
85 | |||
86 | void THREAD_SET_AFFINITY(lua_State* L_, unsigned int aff_); | ||
87 | |||
88 | void THREAD_SET_PRIORITY(lua_State* L_, std::thread& thread_, int prio_, NativePrioFlag native_, SudoFlag sudo_); | ||