forked from gehee/FPVue_rk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchedulingHelper.hpp
57 lines (48 loc) · 1.63 KB
/
SchedulingHelper.hpp
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
//
// Created by consti10 on 09.04.24.
//
#ifndef FPVUE_SCHEDULINGHELPER_H
#define FPVUE_SCHEDULINGHELPER_H
#include <pthread.h>
#include <sys/resource.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <string>
namespace SchedulingHelper {
// Only 'low' in comparison to other realtime tasks
static constexpr int PRIORITY_REALTIME_LOW=30;
static constexpr int PRIORITY_REALTIME_MID=40;
// this thread should run as close to realtime as possible
// https://youtu.be/NrjXEaTSyrw?t=647
// COMMENT: Please don't ever use 99 for your application, there are some kernel
// threads that run at 99 that are really important So ... lets use 90 for now
static void set_thread_params_max_realtime(const std::string& tag,
const int priority = 90) {
pthread_t target = pthread_self();
int policy = SCHED_FIFO;
sched_param param{};
// param.sched_priority = sched_get_priority_max(policy);
param.sched_priority = priority;
auto result = pthread_setschedparam(target, policy, ¶m);
if (result != 0) {
std::stringstream ss;
ss << "Cannot setThreadParamsMaxRealtime " << result;
std::cerr << ss.str() << std::endl;
} else {
std::stringstream ss;
ss << "Changed prio ";
if (!tag.empty()) {
ss << "for " << tag << " ";
}
ss << "to SCHED_FIFO:" << param.sched_priority;
std::cout << ss.str() << std::endl;
}
}
static bool check_root() {
const auto uid = getuid();
const bool root = uid ? false : true;
return root;
}
} // namespace SchedulingHelper
#endif //FPVUE_SCHEDULINGHELPER_H