2626#include < sys/io.h>
2727#endif
2828
29- namespace
30- {
31- struct PosixTask : rtapi_task
32- {
33- PosixTask () : rtapi_task{}, thr{}
34- {}
35-
36- pthread_t thr; /* thread's context */
29+ namespace {
30+ struct PosixTask : rtapi_task {
31+ PosixTask () : rtapi_task{}, thr{} {
32+ }
33+
34+ pthread_t thr; /* thread's context */
3735};
3836
39- struct PosixApp : RtapiApp
40- {
41- PosixApp ( int policy = SCHED_FIFO) : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) {
37+ struct PosixApp : RtapiApp {
38+ PosixApp ( int policy = SCHED_FIFO)
39+ : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) {
4240 pthread_once (&key_once, init_key);
43- if (do_thread_lock) {
41+ if (do_thread_lock) {
4442 pthread_once (&lock_once, init_lock);
4543 }
4644 }
@@ -51,7 +49,8 @@ struct PosixApp : RtapiApp
5149
5250 int task_delete (int id) {
5351 auto task = ::rtapi_get_task<PosixTask>(id);
54- if (!task) return -EINVAL;
52+ if (!task)
53+ return -EINVAL;
5554
5655 pthread_cancel (task->thr );
5756 pthread_join (task->thr , 0 );
@@ -63,7 +62,8 @@ struct PosixApp : RtapiApp
6362
6463 int task_start (int task_id, unsigned long period_nsec) {
6564 auto task = ::rtapi_get_task<PosixTask>(task_id);
66- if (!task) return -EINVAL;
65+ if (!task)
66+ return -EINVAL;
6767
6868 task->period = period_nsec;
6969 struct sched_param param;
@@ -74,55 +74,59 @@ struct PosixApp : RtapiApp
7474 task->pll_correction_limit = period_nsec / 100 ;
7575 task->pll_correction = 0 ;
7676
77- int nprocs = sysconf ( _SC_NPROCESSORS_ONLN );
77+ int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
7878
7979 pthread_attr_t attr;
8080 int ret;
81- if ((ret = pthread_attr_init (&attr)) != 0 )
81+ if ((ret = pthread_attr_init (&attr)) != 0 )
8282 return -ret;
83- if ((ret = pthread_attr_setstacksize (&attr, task->stacksize )) != 0 )
83+ if ((ret = pthread_attr_setstacksize (&attr, task->stacksize )) != 0 )
8484 return -ret;
85- if ((ret = pthread_attr_setschedpolicy (&attr, policy)) != 0 )
85+ if ((ret = pthread_attr_setschedpolicy (&attr, policy)) != 0 )
8686 return -ret;
87- if ((ret = pthread_attr_setschedparam (&attr, ¶m)) != 0 )
87+ if ((ret = pthread_attr_setschedparam (&attr, ¶m)) != 0 )
8888 return -ret;
89- if ((ret = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED)) != 0 )
89+ if ((ret = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED)) != 0 )
9090 return -ret;
91- if (nprocs > 1 ){
91+ if (nprocs > 1 ) {
9292 const static int rt_cpu_number = find_rt_cpu_number ();
93- rtapi_print_msg (RTAPI_MSG_INFO, " rt_cpu_number = %i\n " , rt_cpu_number);
94- if (rt_cpu_number != -1 ) {
95- #ifdef __FreeBSD__
93+ rtapi_print_msg (
94+ RTAPI_MSG_INFO, " rt_cpu_number = %i\n " , rt_cpu_number
95+ );
96+ if (rt_cpu_number != -1 ) {
97+ #ifdef __FreeBSD__
9698 cpuset_t cpuset;
97- #else
99+ #else
98100 cpu_set_t cpuset;
99- #endif
101+ #endif
100102 CPU_ZERO (&cpuset);
101103 CPU_SET (rt_cpu_number, &cpuset);
102- if ((ret = pthread_attr_setaffinity_np (&attr, sizeof (cpuset), &cpuset)) != 0 )
104+ if ((ret = pthread_attr_setaffinity_np (&attr, sizeof (cpuset), &cpuset)) != 0 )
103105 return -ret;
104106 }
105107 }
106- if (do_thread_lock)
108+ if (do_thread_lock)
107109 pthread_mutex_lock (&thread_lock);
108- if ((ret = pthread_create (&task->thr , &attr, &wrapper, reinterpret_cast <void *>(task))) != 0 )
110+ if ((ret = pthread_create (&task->thr , &attr, &wrapper, reinterpret_cast <void *>(task))) != 0 )
109111 return -ret;
110112
111113 return 0 ;
112114 }
113115
114116 static void *wrapper (void *arg) {
115- auto task = reinterpret_cast <PosixTask*>(arg);
117+ auto task = reinterpret_cast <PosixTask *>(arg);
116118
117119 pthread_setspecific (key, arg);
118120 set_namef (" rtapi_app:T#%d" , task->id );
119121
120122 struct timespec now;
121123 clock_gettime (CLOCK_MONOTONIC, &now);
122- rtapi_timespec_advance (task->nextstart , now, task->period + task->pll_correction );
124+ rtapi_timespec_advance (
125+ task->nextstart , now, task->period + task->pll_correction
126+ );
123127
124128 /* call the task function with the task argument */
125- (task->taskcode ) (task->arg );
129+ (task->taskcode )(task->arg );
126130
127131 rtapi_print (" ERROR: reached end of wrapper for task %d\n " , task->id );
128132 return NULL ;
@@ -139,39 +143,50 @@ struct PosixApp : RtapiApp
139143 }
140144
141145 long long task_pll_get_reference (void ) {
142- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
143- if (!task) return 0 ;
146+ struct rtapi_task *task =
147+ reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
148+ if (!task)
149+ return 0 ;
144150 return task->nextstart .tv_sec * 1000000000LL + task->nextstart .tv_nsec ;
145151 }
146152
147153 int task_pll_set_correction (long value) {
148- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
149- if (!task) return -EINVAL;
150- if (value > task->pll_correction_limit ) value = task->pll_correction_limit ;
151- if (value < -(task->pll_correction_limit )) value = -(task->pll_correction_limit );
154+ struct rtapi_task *task =
155+ reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
156+ if (!task)
157+ return -EINVAL;
158+ if (value > task->pll_correction_limit )
159+ value = task->pll_correction_limit ;
160+ if (value < -(task->pll_correction_limit ))
161+ value = -(task->pll_correction_limit );
152162 task->pll_correction = value;
153163 return 0 ;
154164 }
155165
156166 void wait () {
157- if (do_thread_lock)
167+ if (do_thread_lock)
158168 pthread_mutex_unlock (&thread_lock);
159169 pthread_testcancel ();
160- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
161- rtapi_timespec_advance (task->nextstart , task->nextstart , task->period + task->pll_correction );
170+ struct rtapi_task *task =
171+ reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
172+ rtapi_timespec_advance (
173+ task->nextstart ,
174+ task->nextstart ,
175+ task->period + task->pll_correction
176+ );
162177 struct timespec now;
163178 clock_gettime (CLOCK_MONOTONIC, &now);
164- if (rtapi_timespec_less (task->nextstart , now))
165- {
166- if (policy == SCHED_FIFO)
179+ if (rtapi_timespec_less (task->nextstart , now)) {
180+ if (policy == SCHED_FIFO)
167181 unexpected_realtime_delay (task);
182+ } else {
183+ int res = clock_nanosleep (
184+ CLOCK_MONOTONIC, TIMER_ABSTIME, &task->nextstart , nullptr
185+ );
186+ if (res < 0 )
187+ perror (" clock_nanosleep" );
168188 }
169- else
170- {
171- int res = clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &task->nextstart , nullptr );
172- if (res < 0 ) perror (" clock_nanosleep" );
173- }
174- if (do_thread_lock)
189+ if (do_thread_lock)
175190 pthread_mutex_lock (&thread_lock);
176191 }
177192
@@ -194,13 +209,17 @@ struct PosixApp : RtapiApp
194209 }
195210
196211 int run_threads (int fd, int (*callback)(int fd)) {
197- while (callback (fd)) { /* nothing */ }
212+ while (callback (fd)) {
213+ /* nothing */
214+ }
198215 return 0 ;
199216 }
200217
201218 int task_self () {
202- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
203- if (!task) return -EINVAL;
219+ struct rtapi_task *task =
220+ reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
221+ if (!task)
222+ return -EINVAL;
204223 return task->id ;
205224 }
206225
@@ -235,14 +254,14 @@ pthread_once_t PosixApp::lock_once = PTHREAD_ONCE_INIT;
235254pthread_key_t PosixApp::key;
236255pthread_mutex_t PosixApp::thread_lock;
237256
238- }
257+ } // namespace
239258
240259extern " C" RtapiApp *make (int policy);
241260
242261RtapiApp *make (int policy) {
243- if (policy == SCHED_OTHER){
262+ if (policy == SCHED_OTHER) {
244263 rtapi_print_msg (RTAPI_MSG_ERR, " Note: Using POSIX non-realtime\n " );
245- }else {
264+ } else {
246265 rtapi_print_msg (RTAPI_MSG_ERR, " Note: Using POSIX realtime\n " );
247266 }
248267 return new PosixApp (policy);
0 commit comments