10#if (defined(__unix__) || defined(unix)) && !defined(USG)
16#include <sys/resource.h>
18#elif defined(__MACH__)
19#include <mach/mach_time.h>
20#include <mach/thread_act.h>
25#include <sys/rtprio.h>
32DISPENSO_THREAD_LOCAL
ThreadPriority g_threadPriority = ThreadPriority::kNormal;
36 return g_threadPriority;
40bool setCurrentThreadPriority(ThreadPriority prio) {
41 mach_port_t threadport = pthread_mach_thread_np(pthread_self());
42 if (prio == ThreadPriority::kRealtime) {
43 mach_timebase_info_data_t info;
44 mach_timebase_info(&info);
45 double msToAbsTime = ((double)info.denom / (
double)info.numer) * 1000000.0;
46 thread_time_constraint_policy_data_t time_constraints;
47 time_constraints.period = 0;
48 time_constraints.computation =
static_cast<uint32_t
>(1.0 * msToAbsTime);
49 time_constraints.constraint =
static_cast<uint32_t
>(10.0 * msToAbsTime);
50 time_constraints.preemptible = 0;
52 if (thread_policy_set(
54 THREAD_TIME_CONSTRAINT_POLICY,
55 (thread_policy_t)&time_constraints,
56 THREAD_TIME_CONSTRAINT_POLICY_COUNT) != KERN_SUCCESS) {
62 struct thread_precedence_policy ttcpolicy;
65 case ThreadPriority::kLow:
66 ttcpolicy.importance = 20;
68 case ThreadPriority::kNormal:
69 ttcpolicy.importance = 37;
71 case ThreadPriority::kHigh:
72 case ThreadPriority::kRealtime:
73 ttcpolicy.importance = 63;
77 if (thread_policy_set(
79 THREAD_PRECEDENCE_POLICY,
80 (thread_policy_t)&ttcpolicy,
81 THREAD_PRECEDENCE_POLICY_COUNT) != KERN_SUCCESS) {
85 g_threadPriority = prio;
90bool setCurrentThreadPriority(ThreadPriority prio) {
91 if (prio == ThreadPriority::kRealtime) {
92 if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) {
97 if (prio == ThreadPriority::kHigh) {
99 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
102 bool success =
false;
104 case ThreadPriority::kLow:
105 success = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
107 case ThreadPriority::kNormal:
108 success = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
110 case ThreadPriority::kHigh:
111 case ThreadPriority::kRealtime:
112 success = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
120 g_threadPriority = prio;
123#elif defined(__linux__)
124bool setCurrentThreadPriority(ThreadPriority prio) {
125 if (prio == ThreadPriority::kRealtime) {
126 struct sched_param param;
127 param.sched_priority = 99;
128 if (pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m)) {
134 case ThreadPriority::kLow:
138 case ThreadPriority::kNormal:
142 case ThreadPriority::kHigh:
143 case ThreadPriority::kRealtime: {
145 getrlimit(RLIMIT_NICE, &rlim);
146 if (rlim.rlim_max <= 20) {
149 rlim.rlim_cur = rlim.rlim_max;
150 setrlimit(RLIMIT_NICE, &rlim);
152 (void)!nice(
static_cast<int>(20 - rlim.rlim_max));
158 g_threadPriority = prio;
161#elif defined(__FreeBSD__)
163bool setCurrentThreadPriority(ThreadPriority prio) {
166 if (prio == ThreadPriority::kRealtime) {
167 rtp.type = RTP_PRIO_REALTIME;
169 if (rtprio_thread(RTP_SET, 0, &rtp)) {
173 rtp.type = RTP_PRIO_NORMAL;
175 case ThreadPriority::kLow:
178 case ThreadPriority::kNormal:
181 case ThreadPriority::kHigh:
182 case ThreadPriority::kRealtime:
186 if (rtprio_thread(RTP_SET, 0, &rtp)) {
190 g_threadPriority = prio;
194bool setCurrentThreadPriority(ThreadPriority prio) {