aboutsummaryrefslogtreecommitdiff
path: root/src/threading.hpp
blob: 912c28f21fdb3b4b6079341c52cca8a6ba47e9cc (plain)
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
#pragma once

#include "platform.h"

#define THREADAPI_WINDOWS 1
#define THREADAPI_PTHREAD 2

#if __has_include(<pthread.h>)
#include <pthread.h>
#define HAVE_PTHREAD 1
//#pragma message("HAVE_PTHREAD")
#else
#define HAVE_PTHREAD 0
#endif // <pthread.h>

#if __has_include(<windows.h>)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define HAVE_WIN32 1
//#pragma message("HAVE_WIN32")
#elif __has_include(<xtl.h>)
#include <xtl.h>
#define HAVE_WIN32 1
//#pragma message("HAVE_WIN32")
#else // no <windows.h> nor <xtl.h>
#define HAVE_WIN32 0
#endif // <windows.h>

#if HAVE_PTHREAD
// unless proven otherwise, if pthread is available, let's assume that's what std::thread is using
#define THREADAPI THREADAPI_PTHREAD
//#pragma message ( "THREADAPI_PTHREAD" )
#elif HAVE_WIN32
//#pragma message ( "THREADAPI_WINDOWS" )
#define THREADAPI THREADAPI_WINDOWS
#include <process.h>
#else // unknown
#error "unsupported threading API"
#endif // unknown

static constexpr int kThreadPrioDefault{ -999 };

// #################################################################################################
// #################################################################################################
#if THREADAPI == THREADAPI_WINDOWS


/*
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message( "The value of _WIN32_WINNT: " XSTR(_WIN32_WINNT))
*/

static constexpr int kThreadPrioMin{ -3 };
static constexpr int kThreadPrioMax{ +3 };

// #################################################################################################
// #################################################################################################
#else // THREADAPI == THREADAPI_PTHREAD
// #################################################################################################
// #################################################################################################

// PThread (Linux, OS X, ...)

#if defined(PLATFORM_LINUX) && !defined(LINUX_SCHED_RR)
static constexpr int kThreadPrioMin{ 0 };
#else
static constexpr int kThreadPrioMin{ -3 };
#endif
static constexpr int kThreadPrioMax{ +3 };

#endif // THREADAPI == THREADAPI_PTHREAD
// #################################################################################################
// #################################################################################################

void THREAD_SETNAME(std::string_view const& name_);
void THREAD_SET_PRIORITY(int prio_, bool sudo_);
void THREAD_SET_AFFINITY(unsigned int aff_);

void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, bool sudo_);