25 #include <semaphore.h> 28 #include <sys/types.h> 32 #include <sys/resource.h> 44 static bool init_rt =
false;
45 static pthread_key_t is_rt_key;
52 std::cout <<
"***WARNING*** You are using the POSIX compatibility layer, RTXI is NOT running in realtime!!!\n";
54 if (mlockall(MCL_CURRENT | MCL_FUTURE))
56 std::cout <<
"RT::OS(POSIX)::initiate : failed to lock memory.\n";
65 pthread_key_create(&is_rt_key,0);
73 pthread_key_delete(is_rt_key);
78 void *(*entry)(
void *);
84 static void *bounce(
void *bounce_info)
89 void *(*entry)(
void *) = info->
entry;
90 void *arg = info->
arg;
94 t->
thread = pthread_self();
96 pthread_setspecific(is_rt_key,
reinterpret_cast<const void *
>(t));
114 sem_init(&info.
sem,0,0);
116 retval = pthread_create(&t->
thread,NULL,&::bounce,&info);
120 ERROR_MSG(
"RT::OS::createTask : pthread_create failed\n");
122 sem_destroy(&info.
sem);
132 pthread_join(t->
thread,0);
138 if (init_rt && pthread_getspecific(is_rt_key))
147 gettimeofday(&tv,NULL);
149 return 1000000000ll*tv.tv_sec+1000ll*tv.tv_usec;
173 sleep_time / 1000000000ll,
174 sleep_time % 1000000000ll,
177 while (nanosleep(&ts,&ts) < 0 && errno == EINTR);
187 ERROR_MSG(
"RT::OS::getCpuUsage : This function should only be run in user space. Aborting.");
192 long cpu_time_elapsed;
193 long proc_time_elapsed;
199 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &proc_time);
200 clock_gettime(CLOCK_REALTIME, &clock_time);
205 if (cpu_time_elapsed <= 0)
return 0.0;
206 proc_time_elapsed = 1e9*(proc_time.tv_sec -
last_proc_time.tv_sec) +
208 cpu_percent = 100.0*(proc_time_elapsed) / cpu_time_elapsed;