-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththread_util.cpp
executable file
·49 lines (44 loc) · 1.38 KB
/
thread_util.cpp
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
//
// Created by Nolan Woods on 10/13/2018.
//
#include <iostream>
#include <cstring>
#include "thread_util.h"
#ifdef _WIN32
#elif __APPLE__
#include "TargetConditionals.h"
#elif __linux__
#include <pthread.h>
#include <sched.h>
#endif
void increment_priority(std::thread& thread, int amount) {
#ifdef _WIN32
//TODO implement windows functionality
//SetThreadPriority()
#elif __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_MAC
// TODO implement OSX functionality
#else
# error "Unknown Apple platform"
#endif
#elif __linux__
int policy;
sched_param sch_params;
int errno;
if(errno = pthread_getschedparam(thread.native_handle(), &policy, &sch_params)) {
std::cerr << "Failed to get thread scheduling: " << std::strerror(errno) << std::endl;
return;
}
sch_params.sched_priority += amount;
int max = sched_get_priority_max(policy);
int min = sched_get_priority_min(policy);
if (max <= sch_params.sched_priority && min >= sch_params.sched_priority) {
std::cerr << "Thread priority of " << sch_params.sched_priority << " out of range: " << min << " to " << max << std::endl;
return;
}
if(errno = pthread_setschedparam(thread.native_handle(), policy, &sch_params)) {
std::cerr << "Failed to set thread scheduling: " << std::strerror(errno) << std::endl;
}
#endif
}