EtherCAT Client Library Code 0.0.1
EtherCAT Client library that uses different communication protocols with EtherCAT Master server.
Loading...
Searching...
No Matches
ec_thread.h
1#ifndef __EC_THREAD_H__
2#define __EC_THREAD_H__
3
4#include <pthread.h>
5#include <linux/sched.h>
6#include <unistd.h>
7#include <stdlib.h>
8#include <sys/types.h>
9#include <bits/local_lim.h>
10#include <exception>
11#include <typeinfo>
12#include <iostream>
13#include <sys/timerfd.h>
14#include <map>
15#include <memory>
16
17#include <utils.h>
18
19#include <chrono>
20#include <thread>
21
22#define handle_error_en(en, msg) \
23 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
24
25typedef struct {
26 struct timeval task_time;
27 struct timeval period;
29
30
31class EcThread;
32
33typedef EcThread* EcThread_Ptr;
34typedef std::map<std::string, EcThread_Ptr> EcThreadsMap;
35
36void * periodic_thread ( EcThread_Ptr );
37void * non_periodic_thread ( EcThread_Ptr );
38void * nrt_thread ( EcThread_Ptr );
39
40extern pthread_barrier_t threads_barrier;
41
42class EcThread {
43
44public:
45
46 virtual ~EcThread();
47
48 void create ( int rt, int cpu_nr );
49 void stop ( void );
50 void join ( void );
51
52 int is_non_periodic();
53
54 virtual void th_init ( void * ) = 0;
55 virtual void th_loop ( void * ) = 0;
56
57 static void * th_helper ( void * );
58
59protected:
60
61 int _run_loop;
62
63 std::string name;
64 ec_period_t period;
65
66 pthread_t thread_id;
67 bool joinable=false;
68 // pthread attribute
69 int schedpolicy;
70 int priority;
71 int stacksize;
72
73 int fd_timer;
74
75 friend void * periodic_thread ( EcThread_Ptr );
76 friend void * non_periodic_thread ( EcThread_Ptr );
77
78};
79
80inline EcThread::~EcThread() {
81
82 close(fd_timer);
83 std::cout << "~" << typeid ( this ).name() << " " << std::string(name) <<std::endl;
84}
85
86inline int EcThread::is_non_periodic() {
87
88 return ( period.period.tv_sec == 0 && period.period.tv_usec == 1 );
89}
90
91inline void * EcThread::th_helper ( void *kls ) {
92
93 if ( ( ( EcThread_Ptr ) kls )->is_non_periodic() ) {
94 return non_periodic_thread ( ( EcThread_Ptr ) kls );
95 }
96 return periodic_thread ( ( EcThread_Ptr ) kls );
97
98}
99
100
101inline void EcThread::stop() {
102 // exit loop of function in wrap_thread.cpp
103 _run_loop = 0;
104}
105
106inline void EcThread::join() {
107 //pthread_cancel(thread_id);
108 try{
109 if(joinable){
110 joinable=false;
111 int ret= pthread_join ( thread_id, 0 );
112 if(ret!=0){
113 DPRINTF( "Unsuccess join on thread, reason: %d\n",ret);
114 }
115 }
116 }catch(const std::exception &e){
117 DPRINTF( "Join error on thread, reason: %s\n",e.what());
118 }
119}
120
121inline void EcThread::create ( int rt=true, int cpu_nr=-1 ) {
122
123 int ret;
124 pthread_attr_t attr;
125 struct sched_param schedparam;
126 cpu_set_t cpu_set;
127 size_t dflt_stacksize;
128 _run_loop = 1;
129
130 CPU_ZERO ( &cpu_set );
131 CPU_SET ( cpu_nr,&cpu_set );
132
133 ret = pthread_attr_init(&attr);
134 if (ret != 0) handle_error_en(ret, "pthread_attr_init");
135
136 ret = pthread_attr_setinheritsched ( &attr, PTHREAD_EXPLICIT_SCHED );
137 if (ret != 0) handle_error_en(ret, "pthread_attr_setinheritsched");
138
139 ret = pthread_attr_setschedpolicy ( &attr, schedpolicy );
140 if (ret != 0) handle_error_en(ret, "pthread_attr_setschedpolicy");
141
142 schedparam.sched_priority = priority;
143 ret = pthread_attr_setschedparam ( &attr, &schedparam );
144 if (ret != 0) handle_error_en(ret, "pthread_attr_setschedparam");
145
146 pthread_attr_getstacksize ( &attr, &dflt_stacksize );
147 //DPRINTF ( "default stack size %ld\n", dflt_stacksize );
148 if ( stacksize > 0 ) {
149 ret = pthread_attr_setstacksize ( &attr, stacksize );
150 if (ret != 0) handle_error_en(ret, "pthread_attr_setstacksize");
151
152 }
153
154 ret = pthread_attr_setdetachstate ( &attr, PTHREAD_CREATE_JOINABLE );
155 if (ret != 0) handle_error_en(ret, "pthread_attr_setdetachstate");
156
157 if ( cpu_nr >= 0 ) {
158 ret = pthread_attr_setaffinity_np ( &attr, sizeof ( cpu_set ), &cpu_set );
159 if (ret != 0) handle_error_en(ret, "pthread_attr_setaffinity_np");
160 }
161
162 ret = pthread_create ( &thread_id, &attr, &th_helper, this );
163
164 pthread_attr_destroy ( &attr );
165
166 if ( ret != 0 ) {
167 DPRINTF ( "%s %d %s", __FILE__, __LINE__, name.c_str() );
168 handle_error_en(ret, "pthread_create");
169 }
170 else{
171 joinable=true;
172 }
173
174}
175
176
178//
180
181inline void * periodic_thread ( EcThread_Ptr th_hook ) {
182
183 int ret = 0;
184 int overruns = 0;
185
186 // thread specific initialization
187 th_hook->th_init ( 0 );
188
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 );
193
194 ret = pthread_setname_np ( pthread_self(), th_hook->name.c_str() );
195 if ( ret != 0 ) {
196 DPRINTF ( "%s : pthread_set_name_np() return code %d\n",
197 th_hook->name.c_str(), ret );
198 exit ( 1 );
199 }
200
201
202#ifdef __COBALT__
203 // PTHREAD_WARNSW, when set, cause the signal SIGXCPU to be sent to the
204 // current thread, whenever it involontary switches to secondary mode;
205 if(th_hook->schedpolicy == SCHED_FIFO){
206 ret = pthread_setmode_np ( 0, PTHREAD_WARNSW, 0 );
207 if ( ret != 0 ) {
208 DPRINTF ( "%s : pthread_set_mode_np() return code %d\n",
209 th_hook->name.c_str(), ret );
210 exit ( 1 );
211 }
212 }
213#endif
214
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);
218
219 DPRINTF ( "%s %s : Start looping ...\n",
220 __FUNCTION__, th_hook->name.c_str() );
221
222
223 while ( th_hook->_run_loop ) {
224
225 float time_elapsed_ms = std::chrono::duration<float, std::milli>(time - start_time).count();
226 //DPRINTF("Thread Time elapsed ms: [%f]\n",time_elapsed_ms);
227
228 // thread specific loop
229 th_hook->th_loop ( 0 );
230
231 time = time + period;
232 const auto now = std::chrono::high_resolution_clock::now();
233
234 #if defined(PREEMPT_RT) || defined(__COBALT__)
235 // if less than threshold, print warning (only on rt threads)
236 if(now > time && th_hook->schedpolicy == SCHED_FIFO){
237 ++overruns;
238 DPRINTF("main process overruns: %d\n", overruns);
239 }
240 #endif
241
242 std::this_thread::sleep_until(time);
243 } // end while
244
245 DPRINTF ( "%s %s : exit thread ...\n",
246 __FUNCTION__, th_hook->name.c_str() );
247
248 return 0;
249}
250
251
253//
255
256inline void * non_periodic_thread ( EcThread_Ptr th_hook ) {
257 int ret = 0;
258
259 // thread specific initialization
260 th_hook->th_init ( 0 );
261
262 DPRINTF ( "%s %s, period %ld us\n",
263 __FUNCTION__, th_hook->name.c_str(),
264 th_hook->period.period.tv_usec );
265
266 ret = pthread_setname_np ( pthread_self(), th_hook->name.c_str() );
267 if ( ret != 0 ) {
268 DPRINTF ( "%s : pthread_setname_np() return code %d\n",
269 th_hook->name.c_str(), ret );
270 exit ( 1 );
271 }
272
273#ifdef __COBALT__
274 // PTHREAD_WARNSW, when set, cause the signal SIGXCPU to be sent to the
275 // current thread, whenever it involontary switches to secondary mode;
276 if(th_hook->schedpolicy == SCHED_FIFO){
277 ret = pthread_setmode_np ( 0, PTHREAD_WARNSW, 0 );
278 if ( ret != 0 ) {
279 DPRINTF ( "%s : pthread_set_mode_np() return code %d\n",
280 th_hook->name.c_str(), ret );
281 exit ( 1 );
282 }
283 }
284#endif
285
286 DPRINTF ( "%s %s : Start looping ...\n",
287 __FUNCTION__, th_hook->name.c_str() );
288
289
290 while ( th_hook->_run_loop ) {
291
292 // thread specific loop
293 th_hook->th_loop ( 0 );
294
295 } // end while
296
297 DPRINTF ( "%s %s : exit thread ...\n",
298 __FUNCTION__, th_hook->name.c_str() );
299
300 return 0;
301}
302
303#endif
304
305
Definition ec_thread.h:42
Definition ec_thread.h:25