27 #ifndef RL_UTIL_RTAI_THREAD
28 #define RL_UTIL_RTAI_THREAD
32 #include <rtai_lxrt.h>
33 #include <system_error>
68 friend class ::std::hash<thread::id>;
100 template<
class CharT,
class Traits>
101 friend ::std::basic_ostream<CharT, Traits>&
operator<<(::std::basic_ostream<CharT, Traits>& out,
thread::id id)
105 return out <<
"thread::id of a non-executing thread";
109 return out <<
id.M_thread;
129 template<
typename Callable,
typename... Args>
130 explicit thread(Callable&& f, Args&&... args)
135 ::std::forward<Callable>(f),
136 ::std::forward<Args>(args)...
154 if (this->
M_id !=
id())
161 throw ::std::system_error(::std::error_code(e, ::std::generic_category()));
181 if (this->
M_id !=
id())
188 throw ::std::system_error(::std::error_code(e, ::std::generic_category()));
196 return !(this->
M_id == id());
223 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
227 template<
typename Rep,
typename Period>
230 ::std::chrono::nanoseconds period_ns = ::std::chrono::duration_cast< ::std::chrono::nanoseconds>(period);
232 int e = ::rt_task_make_periodic(this->
M_id.
M_task, ::rt_get_time(), ::nano2count(period_ns.count()));
236 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
240 template<
typename Duration,
typename Rep,
typename Period>
241 void set_periodic(const ::std::chrono::time_point<chrono::system_clock, Duration>& idate, const ::std::chrono::duration<Rep, Period>& period)
243 ::std::chrono::time_point<chrono::system_clock, ::std::chrono::nanoseconds> idate_ns = ::std::chrono::time_point_cast< ::std::chrono::nanoseconds>(idate);
244 ::std::chrono::nanoseconds period_ns = ::std::chrono::duration_cast< ::std::chrono::nanoseconds>(period);
246 int e = ::rt_task_make_periodic(this->
M_id.
M_task, ::nano2count(idate_ns.time_since_epoch().count()), ::nano2count(period_ns.count()));
250 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
254 template<
typename Clock,
typename Duration,
typename Rep,
typename Period>
255 void set_periodic(const ::std::chrono::time_point<Clock, Duration>& idate, const ::std::chrono::duration<Rep, Period>& period)
257 const typename Clock::time_point c_entry = Clock::now();
259 const ::std::chrono::nanoseconds delta = idate - c_entry;
266 int e = ::rt_change_prio(this->
M_id.
M_task, prio);
270 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
280 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
313 template<
typename Callable>
317 func(::
std::forward<Callable>(f))
338 ::rt_make_hard_real_time();
340 ::rt_make_soft_real_time();
351 template<
typename Callable>
354 return ::std::make_shared<Impl<Callable>>(::std::forward<Callable>(f));
359 if (!__gthread_active_p())
362 throw ::std::system_error(::std::make_error_code(::std::errc::operation_not_permitted),
"Enable multithreading to use std::thread");
364 throw ::std::system_error(::std::error_code(::std::errc::operation_not_permitted, ::std::generic_category()));
368 b->M_id = this->
M_id;
375 throw ::std::system_error(::std::error_code(e, ::std::generic_category()));
384 namespace this_thread
388 return thread::id(::pthread_self(), ::rt_buddy());
393 return ::rt_get_prio(::rt_buddy());
403 return RT_SCHED_LOWEST_PRIORITY;
408 int e = ::rt_change_prio(::rt_buddy(), priority);
412 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
416 template<
typename Rep,
typename Period>
417 inline void sleep_for(const ::std::chrono::duration<Rep, Period>& rtime)
419 ::std::chrono::nanoseconds ns = ::std::chrono::duration_cast< ::std::chrono::nanoseconds>(rtime);
420 int e = ::rt_sleep(::nano2count(ns.count()));
424 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
428 template<
typename Duration>
429 inline void sleep_until(const ::std::chrono::time_point<chrono::system_clock, Duration>& atime)
431 ::std::chrono::time_point<chrono::system_clock, ::std::chrono::nanoseconds> ns = ::std::chrono::time_point_cast< ::std::chrono::nanoseconds>(atime);
432 int e = ::rt_sleep_until(::nano2count(ns.time_since_epoch().count()));
436 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
440 template<
typename Clock,
typename Duration>
441 inline void sleep_until(const ::std::chrono::time_point<Clock, Duration>& atime)
448 ::rt_set_oneshot_mode();
459 int e = ::rt_task_wait_period();
463 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
469 int e = ::rt_task_yield();
473 throw ::std::system_error(::std::error_code(-e, ::std::generic_category()));
486 size_t operator()(const ::rl::util::rtai::thread::id&
id)
const
488 return hash< ::rl::util::rtai::thread::native_handle_type>()(
id.M_thread);
498 #endif // RL_UTIL_RTAI_THREAD