5#include <linux/sched.h>
9#include <bits/local_lim.h>
13#include <sys/timerfd.h>
22#define handle_error_en(en, msg) \
23 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
26 struct timeval task_time;
27 struct timeval period;
34typedef std::map<std::string, EcThread_Ptr> EcThreadsMap;
36void * periodic_thread ( EcThread_Ptr );
37void * non_periodic_thread ( EcThread_Ptr );
38void * nrt_thread ( EcThread_Ptr );
40extern pthread_barrier_t threads_barrier;
48 void create (
int rt,
int cpu_nr );
52 int is_non_periodic();
54 virtual void th_init (
void * ) = 0;
55 virtual void th_loop (
void * ) = 0;
57 static void * th_helper (
void * );
75 friend void * periodic_thread ( EcThread_Ptr );
76 friend void * non_periodic_thread ( EcThread_Ptr );
80inline EcThread::~EcThread() {
83 std::cout <<
"~" <<
typeid ( this ).name() <<
" " << std::string(name) <<std::endl;
86inline int EcThread::is_non_periodic() {
88 return ( period.period.tv_sec == 0 && period.period.tv_usec == 1 );
91inline void * EcThread::th_helper (
void *kls ) {
93 if ( ( ( EcThread_Ptr ) kls )->is_non_periodic() ) {
94 return non_periodic_thread ( ( EcThread_Ptr ) kls );
96 return periodic_thread ( ( EcThread_Ptr ) kls );
101inline void EcThread::stop() {
106inline void EcThread::join() {
111 int ret= pthread_join ( thread_id, 0 );
113 DPRINTF(
"Unsuccess join on thread, reason: %d\n",ret);
116 }
catch(
const std::exception &e){
117 DPRINTF(
"Join error on thread, reason: %s\n",e.what());
121inline void EcThread::create (
int rt=
true,
int cpu_nr=-1 ) {
125 struct sched_param schedparam;
127 size_t dflt_stacksize;
130 CPU_ZERO ( &cpu_set );
131 CPU_SET ( cpu_nr,&cpu_set );
133 ret = pthread_attr_init(&attr);
134 if (ret != 0) handle_error_en(ret,
"pthread_attr_init");
136 ret = pthread_attr_setinheritsched ( &attr, PTHREAD_EXPLICIT_SCHED );
137 if (ret != 0) handle_error_en(ret,
"pthread_attr_setinheritsched");
139 ret = pthread_attr_setschedpolicy ( &attr, schedpolicy );
140 if (ret != 0) handle_error_en(ret,
"pthread_attr_setschedpolicy");
142 schedparam.sched_priority = priority;
143 ret = pthread_attr_setschedparam ( &attr, &schedparam );
144 if (ret != 0) handle_error_en(ret,
"pthread_attr_setschedparam");
146 pthread_attr_getstacksize ( &attr, &dflt_stacksize );
148 if ( stacksize > 0 ) {
149 ret = pthread_attr_setstacksize ( &attr, stacksize );
150 if (ret != 0) handle_error_en(ret,
"pthread_attr_setstacksize");
154 ret = pthread_attr_setdetachstate ( &attr, PTHREAD_CREATE_JOINABLE );
155 if (ret != 0) handle_error_en(ret,
"pthread_attr_setdetachstate");
158 ret = pthread_attr_setaffinity_np ( &attr,
sizeof ( cpu_set ), &cpu_set );
159 if (ret != 0) handle_error_en(ret,
"pthread_attr_setaffinity_np");
162 ret = pthread_create ( &thread_id, &attr, &th_helper,
this );
164 pthread_attr_destroy ( &attr );
167 DPRINTF (
"%s %d %s", __FILE__, __LINE__, name.c_str() );
168 handle_error_en(ret,
"pthread_create");
181inline void * periodic_thread ( EcThread_Ptr th_hook ) {
187 th_hook->th_init ( 0 );
189 DPRINTF (
"%s %s period {%ld,%ld} tv\n",
190 __FUNCTION__, th_hook->name.c_str(),
191 th_hook->period.period.tv_sec,
192 th_hook->period.period.tv_usec );
194 ret = pthread_setname_np ( pthread_self(), th_hook->name.c_str() );
196 DPRINTF (
"%s : pthread_set_name_np() return code %d\n",
197 th_hook->name.c_str(), ret );
205 if(th_hook->schedpolicy == SCHED_FIFO){
206 ret = pthread_setmode_np ( 0, PTHREAD_WARNSW, 0 );
208 DPRINTF (
"%s : pthread_set_mode_np() return code %d\n",
209 th_hook->name.c_str(), ret );
215 auto start_time = std::chrono::high_resolution_clock::now();
216 auto time = start_time;
217 const auto period = std::chrono::nanoseconds(th_hook->period.period.tv_usec * 1000ULL);
219 DPRINTF (
"%s %s : Start looping ...\n",
220 __FUNCTION__, th_hook->name.c_str() );
223 while ( th_hook->_run_loop ) {
225 float time_elapsed_ms = std::chrono::duration<float, std::milli>(time - start_time).count();
229 th_hook->th_loop ( 0 );
231 time = time + period;
232 const auto now = std::chrono::high_resolution_clock::now();
234 #if defined(PREEMPT_RT) || defined(__COBALT__)
236 if(now > time && th_hook->schedpolicy == SCHED_FIFO){
238 DPRINTF(
"main process overruns: %d\n", overruns);
242 std::this_thread::sleep_until(time);
245 DPRINTF (
"%s %s : exit thread ...\n",
246 __FUNCTION__, th_hook->name.c_str() );
256inline void * non_periodic_thread ( EcThread_Ptr th_hook ) {
260 th_hook->th_init ( 0 );
262 DPRINTF (
"%s %s, period %ld us\n",
263 __FUNCTION__, th_hook->name.c_str(),
264 th_hook->period.period.tv_usec );
266 ret = pthread_setname_np ( pthread_self(), th_hook->name.c_str() );
268 DPRINTF (
"%s : pthread_setname_np() return code %d\n",
269 th_hook->name.c_str(), ret );
276 if(th_hook->schedpolicy == SCHED_FIFO){
277 ret = pthread_setmode_np ( 0, PTHREAD_WARNSW, 0 );
279 DPRINTF (
"%s : pthread_set_mode_np() return code %d\n",
280 th_hook->name.c_str(), ret );
286 DPRINTF (
"%s %s : Start looping ...\n",
287 __FUNCTION__, th_hook->name.c_str() );
290 while ( th_hook->_run_loop ) {
293 th_hook->th_loop ( 0 );
297 DPRINTF (
"%s %s : exit thread ...\n",
298 __FUNCTION__, th_hook->name.c_str() );
Definition ec_thread.h:42
Definition ec_thread.h:25