1 #ifndef _GRL_REALTIME_HPP_ 2 #define _GRL_REALTIME_HPP_ 6 #include <mach/mach_time.h> 7 #include <mach/mach_init.h> 8 #include <mach/thread_policy.h> 13 #include <sys/param.h> 16 static inline uint64_t
17 nanos_to_abs(uint64_t ns, uint32_t numer, uint32_t denom)
19 return (uint64_t)(ns * (((double)denom) / ((double)numer)));
53 inline int set_realtime(
int period,
int computation,
int constraint,
bool preemptible =
false) {
55 struct mach_timebase_info mti;
56 struct thread_time_constraint_policy ttcpolicy;
59 kret = mach_timebase_info(&mti);
61 if (kret != KERN_SUCCESS) {
62 warnx(
"Could not get timebase info %d", kret);
66 thread_port_t threadport = pthread_mach_thread_np(pthread_self());
68 ttcpolicy.period = nanos_to_abs(period, mti.numer, mti.denom);
69 ttcpolicy.computation = nanos_to_abs(computation, mti.numer, mti.denom);
70 ttcpolicy.constraint = nanos_to_abs(constraint, mti.numer, mti.denom);
71 ttcpolicy.preemptible = preemptible;
73 if ((kret=thread_policy_set(threadport,
74 THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&ttcpolicy,
75 THREAD_TIME_CONSTRAINT_POLICY_COUNT)) != KERN_SUCCESS) {
76 fprintf(stderr,
"set_realtime() failed.\n");
77 warnx(
"Failed to set_realtime %d", kret);