forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.cpp
More file actions
81 lines (67 loc) · 1.87 KB
/
Thread.cpp
File metadata and controls
81 lines (67 loc) · 1.87 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
// Copyright 2019 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "util/Thread.h"
#include "util/Logging.h"
#ifdef _WIN32
#else
#include <unistd.h>
#endif
#if defined(__APPLE__)
#include <pthread.h>
#endif
namespace stellar
{
#if defined(_WIN32)
void
runCurrentThreadWithLowPriority()
{
HANDLE curThread = ::GetCurrentThread();
BOOL ret = ::SetThreadPriority(curThread, THREAD_PRIORITY_BELOW_NORMAL);
if (!ret)
{
LOG(DEBUG) << "Unable to set priority for thread: " << ret;
}
}
#elif defined(__linux__)
void
runCurrentThreadWithLowPriority()
{
constexpr auto const LOW_PRIORITY_NICE = 5;
auto newNice = nice(LOW_PRIORITY_NICE);
if (newNice != LOW_PRIORITY_NICE)
{
LOG(DEBUG) << "Unable to run worker thread with low priority. "
"Normal priority will be used.";
}
}
#elif defined(__APPLE__)
void
runCurrentThreadWithLowPriority()
{
// Default MacOS priority is 31 in a user-mode band from 0..63, niceing (or
// other priority-adjustment) usually subtracts from there. Range is +/- 16,
// with lower meaning lower (i.e. UTILITY class is 20). The standard
// pthreads API works for adjusting a single thread's priority.
constexpr auto const LOW_PRIORITY_NICE = 5;
struct sched_param sp;
int policy;
int ret = pthread_getschedparam(pthread_self(), &policy, &sp);
if (ret != 0)
{
LOG(DEBUG) << "Unable to get priority for thread: " << ret;
}
sp.sched_priority -= LOW_PRIORITY_NICE;
ret = pthread_setschedparam(pthread_self(), policy, &sp);
if (ret != 0)
{
LOG(DEBUG) << "Unable to set priority for thread: " << ret;
}
}
#else
void
runCurrentThreadWithLowPriority()
{
}
#endif
}