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- {
37+ struct PosixApp : RtapiApp {
4138 PosixApp (int policy = SCHED_FIFO) : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) {
4239 pthread_once (&key_once, init_key);
43- if (do_thread_lock) {
40+ if (do_thread_lock) {
4441 pthread_once (&lock_once, init_lock);
4542 }
4643 }
@@ -51,7 +48,8 @@ struct PosixApp : RtapiApp
5148
5249 int task_delete (int id) {
5350 auto task = ::rtapi_get_task<PosixTask>(id);
54- if (!task) return -EINVAL;
51+ if (!task)
52+ return -EINVAL;
5553
5654 pthread_cancel (task->thr );
5755 pthread_join (task->thr , 0 );
@@ -63,7 +61,8 @@ struct PosixApp : RtapiApp
6361
6462 int task_start (int task_id, unsigned long period_nsec) {
6563 auto task = ::rtapi_get_task<PosixTask>(task_id);
66- if (!task) return -EINVAL;
64+ if (!task)
65+ return -EINVAL;
6766
6867 task->period = period_nsec;
6968 struct sched_param param;
@@ -74,45 +73,45 @@ struct PosixApp : RtapiApp
7473 task->pll_correction_limit = period_nsec / 100 ;
7574 task->pll_correction = 0 ;
7675
77- int nprocs = sysconf ( _SC_NPROCESSORS_ONLN );
76+ int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
7877
7978 pthread_attr_t attr;
8079 int ret;
81- if ((ret = pthread_attr_init (&attr)) != 0 )
80+ if ((ret = pthread_attr_init (&attr)) != 0 )
8281 return -ret;
83- if ((ret = pthread_attr_setstacksize (&attr, task->stacksize )) != 0 )
82+ if ((ret = pthread_attr_setstacksize (&attr, task->stacksize )) != 0 )
8483 return -ret;
85- if ((ret = pthread_attr_setschedpolicy (&attr, policy)) != 0 )
84+ if ((ret = pthread_attr_setschedpolicy (&attr, policy)) != 0 )
8685 return -ret;
87- if ((ret = pthread_attr_setschedparam (&attr, ¶m)) != 0 )
86+ if ((ret = pthread_attr_setschedparam (&attr, ¶m)) != 0 )
8887 return -ret;
89- if ((ret = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED)) != 0 )
88+ if ((ret = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED)) != 0 )
9089 return -ret;
91- if (nprocs > 1 ){
90+ if (nprocs > 1 ) {
9291 const static int rt_cpu_number = find_rt_cpu_number ();
9392 rtapi_print_msg (RTAPI_MSG_INFO, " rt_cpu_number = %i\n " , rt_cpu_number);
94- if (rt_cpu_number != -1 ) {
95- #ifdef __FreeBSD__
93+ if (rt_cpu_number != -1 ) {
94+ #ifdef __FreeBSD__
9695 cpuset_t cpuset;
97- #else
96+ #else
9897 cpu_set_t cpuset;
99- #endif
98+ #endif
10099 CPU_ZERO (&cpuset);
101100 CPU_SET (rt_cpu_number, &cpuset);
102- if ((ret = pthread_attr_setaffinity_np (&attr, sizeof (cpuset), &cpuset)) != 0 )
101+ if ((ret = pthread_attr_setaffinity_np (&attr, sizeof (cpuset), &cpuset)) != 0 )
103102 return -ret;
104103 }
105104 }
106- if (do_thread_lock)
105+ if (do_thread_lock)
107106 pthread_mutex_lock (&thread_lock);
108- if ((ret = pthread_create (&task->thr , &attr, &wrapper, reinterpret_cast <void *>(task))) != 0 )
107+ if ((ret = pthread_create (&task->thr , &attr, &wrapper, reinterpret_cast <void *>(task))) != 0 )
109108 return -ret;
110109
111110 return 0 ;
112111 }
113112
114113 static void *wrapper (void *arg) {
115- auto task = reinterpret_cast <PosixTask*>(arg);
114+ auto task = reinterpret_cast <PosixTask *>(arg);
116115
117116 pthread_setspecific (key, arg);
118117 set_namef (" rtapi_app:T#%d" , task->id );
@@ -122,7 +121,7 @@ struct PosixApp : RtapiApp
122121 rtapi_timespec_advance (task->nextstart , now, task->period + task->pll_correction );
123122
124123 /* call the task function with the task argument */
125- (task->taskcode ) (task->arg );
124+ (task->taskcode )(task->arg );
126125
127126 rtapi_print (" ERROR: reached end of wrapper for task %d\n " , task->id );
128127 return NULL ;
@@ -139,39 +138,41 @@ struct PosixApp : RtapiApp
139138 }
140139
141140 long long task_pll_get_reference (void ) {
142- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
143- if (!task) return 0 ;
141+ struct rtapi_task *task = reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
142+ if (!task)
143+ return 0 ;
144144 return task->nextstart .tv_sec * 1000000000LL + task->nextstart .tv_nsec ;
145145 }
146146
147147 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 );
148+ struct rtapi_task *task = reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
149+ if (!task)
150+ return -EINVAL;
151+ if (value > task->pll_correction_limit )
152+ value = task->pll_correction_limit ;
153+ if (value < -(task->pll_correction_limit ))
154+ value = -(task->pll_correction_limit );
152155 task->pll_correction = value;
153156 return 0 ;
154157 }
155158
156159 void wait () {
157- if (do_thread_lock)
160+ if (do_thread_lock)
158161 pthread_mutex_unlock (&thread_lock);
159162 pthread_testcancel ();
160- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
163+ struct rtapi_task *task = reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
161164 rtapi_timespec_advance (task->nextstart , task->nextstart , task->period + task->pll_correction );
162165 struct timespec now;
163166 clock_gettime (CLOCK_MONOTONIC, &now);
164- if (rtapi_timespec_less (task->nextstart , now))
165- {
166- if (policy == SCHED_FIFO)
167+ if (rtapi_timespec_less (task->nextstart , now)) {
168+ if (policy == SCHED_FIFO)
167169 unexpected_realtime_delay (task);
168- }
169- else
170- {
170+ } else {
171171 int res = clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &task->nextstart , nullptr );
172- if (res < 0 ) perror (" clock_nanosleep" );
172+ if (res < 0 )
173+ perror (" clock_nanosleep" );
173174 }
174- if (do_thread_lock)
175+ if (do_thread_lock)
175176 pthread_mutex_lock (&thread_lock);
176177 }
177178
@@ -194,13 +195,16 @@ struct PosixApp : RtapiApp
194195 }
195196
196197 int run_threads (int fd, int (*callback)(int fd)) {
197- while (callback (fd)) { /* nothing */ }
198+ while (callback (fd)) {
199+ /* nothing */
200+ }
198201 return 0 ;
199202 }
200203
201204 int task_self () {
202- struct rtapi_task *task = reinterpret_cast <rtapi_task*>(pthread_getspecific (key));
203- if (!task) return -EINVAL;
205+ struct rtapi_task *task = reinterpret_cast <rtapi_task *>(pthread_getspecific (key));
206+ if (!task)
207+ return -EINVAL;
204208 return task->id ;
205209 }
206210
@@ -235,14 +239,14 @@ pthread_once_t PosixApp::lock_once = PTHREAD_ONCE_INIT;
235239pthread_key_t PosixApp::key;
236240pthread_mutex_t PosixApp::thread_lock;
237241
238- }
242+ } // namespace
239243
240244extern " C" RtapiApp *make (int policy);
241245
242246RtapiApp *make (int policy) {
243- if (policy == SCHED_OTHER){
247+ if (policy == SCHED_OTHER) {
244248 rtapi_print_msg (RTAPI_MSG_ERR, " Note: Using POSIX non-realtime\n " );
245- }else {
249+ } else {
246250 rtapi_print_msg (RTAPI_MSG_ERR, " Note: Using POSIX realtime\n " );
247251 }
248252 return new PosixApp (policy);
0 commit comments