31 #include <sys/resource.h> 33 #include <sys/types.h> 51 std::cout <<
"***WARNING*** You are using the POSIX compatibility layer, " 52 "RTXI is NOT running in realtime!!!\n";
53 std::string strbuf(256,
'\0');
54 int retval = mlockall(MCL_CURRENT | MCL_FUTURE);
55 strerror_r(errno, strbuf.data(), strbuf.size());
57 ERROR_MSG(
"RT::OS(POSIX)::initiate : failed to lock memory : {}", strbuf);
62 task->
thread_id = std::any(pthread_self());
79 ERROR_MSG(
"RT::OS::createTask : Task cannot be created from rt context");
82 auto wrapper = [](
RT::OS::Task* tsk, void (*fn)(
void*),
void* args)
84 std::string strbuf(256,
'\0');
86 strerror_r(errno, strbuf.data(), strbuf.size());
88 ERROR_MSG(
"RT::OS::createTask : RT::OS::initiate() : {}", strbuf);
93 std::thread thread_obj(wrapper, task, func, arg);
95 if (thread_obj.joinable()) {
96 task->rt_thread = std::move(thread_obj);
107 ERROR_MSG(
"RT::OS::createTask : Task cannot be deleted from rt context");
123 return std::chrono::steady_clock::now().time_since_epoch().count();
144 if (task->
next_t < current_time) {
148 const int64_t wakeup_time = task->
next_t;
154 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts,
nullptr);
159 if (pthread_setname_np(thread.native_handle(), name.c_str()) != 0) {
160 ERROR_MSG(
"RT::OS::renameOSThread : unable to set name to thread");
176 double cpu_percent = 0.0;
177 int64_t cpu_time_elapsed = 0;
178 int64_t proc_time_elapsed = 0;
180 timespec clock_time = {};
181 timespec proc_time = {};
184 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &proc_time);
185 clock_gettime(CLOCK_REALTIME, &clock_time);
191 if (cpu_time_elapsed <= 0) {
197 cpu_percent = 100.0 * (
static_cast<double>(proc_time_elapsed))
198 /
static_cast<double>(cpu_time_elapsed);
void ERROR_MSG(const std::string &errmsg, Args... args)
int createTask(Task *task, void(*func)(void *), void *arg)
int setPeriod(Task *task, int64_t period)
const int64_t SECONDS_TO_NANOSECONDS
void shutdown(RT::OS::Task *task)
void sleepTimestep(Task *task)
void renameOSThread(std::thread &thread, const std::string &name)
int initiate(RT::OS::Task *task)
void deleteTask(Task *task)
const int64_t DEFAULT_PERIOD
thread_local int64_t DEFAULT_PERIOD_VALUE
thread_local int64_t * RT_PERIOD
thread_local bool realtime_key