Skip to content

Commit e30e2ef

Browse files
committed
Cleanup: Clang format
1 parent f12e7d2 commit e30e2ef

File tree

7 files changed

+622
-593
lines changed

7 files changed

+622
-593
lines changed

src/rtapi/uspace_posix.cc

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,18 @@
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, &param)) != 0)
86+
if ((ret = pthread_attr_setschedparam(&attr, &param)) != 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;
235239
pthread_key_t PosixApp::key;
236240
pthread_mutex_t PosixApp::thread_lock;
237241

238-
}
242+
} // namespace
239243

240244
extern "C" RtapiApp *make(int policy);
241245

242246
RtapiApp *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);

src/rtapi/uspace_rtai.cc

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@
2626
#include <sys/io.h>
2727
#endif
2828

29-
namespace
30-
{
29+
namespace {
3130
RtapiApp *app;
3231

3332
struct RtaiTask : rtapi_task {
34-
RtaiTask() : rtapi_task{}, cancel{}, rt_task{}, thr{} {}
33+
RtaiTask() : rtapi_task{}, cancel{}, rt_task{}, thr{} {
34+
}
3535
std::atomic_int cancel;
3636
RT_TASK *rt_task;
3737
pthread_t thr;
3838
};
3939

40-
template<class T=rtapi_task>
41-
T *get_task(int task_id) {
42-
return static_cast<T*>(RtapiApp::get_task(task_id));
40+
template <class T = rtapi_task> T *get_task(int task_id) {
41+
return static_cast<T *>(RtapiApp::get_task(task_id));
4342
}
4443

4544

@@ -54,7 +53,8 @@ struct RtaiApp : RtapiApp {
5453

5554
int task_delete(int id) {
5655
auto task = ::get_task<RtaiTask>(id);
57-
if(!task) return -EINVAL;
56+
if (!task)
57+
return -EINVAL;
5858

5959
task->cancel = 1;
6060
pthread_join(task->thr, nullptr);
@@ -66,7 +66,8 @@ struct RtaiApp : RtapiApp {
6666

6767
int task_start(int task_id, unsigned long period_nsec) {
6868
auto task = ::get_task<RtaiTask>(task_id);
69-
if(!task) return -EINVAL;
69+
if (!task)
70+
return -EINVAL;
7071

7172
task->period = period_nsec;
7273
struct sched_param param;
@@ -79,30 +80,30 @@ struct RtaiApp : RtapiApp {
7980

8081
int ret;
8182
pthread_attr_t attr;
82-
if((ret = pthread_attr_init(&attr)) != 0)
83+
if ((ret = pthread_attr_init(&attr)) != 0)
8384
return -ret;
84-
if((ret = pthread_attr_setstacksize(&attr, task->stacksize)) != 0)
85+
if ((ret = pthread_attr_setstacksize(&attr, task->stacksize)) != 0)
8586
return -ret;
86-
if((ret = pthread_attr_setschedpolicy(&attr, policy)) != 0)
87+
if ((ret = pthread_attr_setschedpolicy(&attr, policy)) != 0)
8788
return -ret;
88-
if((ret = pthread_attr_setschedparam(&attr, &param)) != 0)
89+
if ((ret = pthread_attr_setschedparam(&attr, &param)) != 0)
8990
return -ret;
90-
if((ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) != 0)
91+
if ((ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) != 0)
9192
return -ret;
92-
if((ret = pthread_create(&task->thr, &attr, &wrapper, reinterpret_cast<void*>(task))) != 0)
93+
if ((ret = pthread_create(&task->thr, &attr, &wrapper, reinterpret_cast<void *>(task))) != 0)
9394
return -ret;
9495

9596
return 0;
9697
}
9798

9899
static void *wrapper(void *arg) {
99-
auto task = reinterpret_cast<RtaiTask*>(arg);
100+
auto task = reinterpret_cast<RtaiTask *>(arg);
100101
pthread_setspecific(key, arg);
101-
102-
int nprocs = sysconf( _SC_NPROCESSORS_ONLN );
103-
int cpus_allowed = 1 << (nprocs-1); //Use last CPU as default
102+
103+
int nprocs = sysconf(_SC_NPROCESSORS_ONLN);
104+
int cpus_allowed = 1 << (nprocs - 1); //Use last CPU as default
104105
const static int rt_cpu_number = find_rt_cpu_number();
105-
if(rt_cpu_number != -1) {
106+
if (rt_cpu_number != -1) {
106107
rtapi_print_msg(RTAPI_MSG_INFO, "rt_cpu_number = %i\n", rt_cpu_number);
107108
cpus_allowed = 1 << rt_cpu_number;
108109
}
@@ -113,7 +114,7 @@ struct RtaiApp : RtapiApp {
113114
rt_task_use_fpu(task->rt_task, 1);
114115
rt_make_hard_real_time();
115116
rt_task_make_periodic_relative_ns(task->rt_task, task->period, task->period);
116-
(task->taskcode) (task->arg);
117+
(task->taskcode)(task->arg);
117118

118119
rtapi_print("ERROR: reached end of wrapper for task %d\n", task->id);
119120
rt_make_soft_real_time();
@@ -122,13 +123,15 @@ struct RtaiApp : RtapiApp {
122123

123124
int task_pause(int task_id) {
124125
auto task = ::get_task<RtaiTask>(task_id);
125-
if(!task) return -EINVAL;
126+
if (!task)
127+
return -EINVAL;
126128
return rt_task_suspend(task->rt_task);
127129
}
128130

129131
int task_resume(int task_id) {
130132
auto task = ::get_task<RtaiTask>(task_id);
131-
if(!task) return -EINVAL;
133+
if (!task)
134+
return -EINVAL;
132135
return rt_task_resume(task->rt_task);
133136
}
134137

@@ -146,11 +149,12 @@ struct RtaiApp : RtapiApp {
146149
void wait() {
147150
int task_id = task_self();
148151
auto task = ::get_task<RtaiTask>(task_id);
149-
if(task->cancel) {
152+
if (task->cancel) {
150153
rt_make_soft_real_time();
151154
pthread_exit(nullptr);
152155
}
153-
if(rt_task_wait_period() < 0) unexpected_realtime_delay(task);
156+
if (rt_task_wait_period() < 0)
157+
unexpected_realtime_delay(task);
154158
}
155159

156160
unsigned char do_inb(unsigned int port) {
@@ -172,13 +176,16 @@ struct RtaiApp : RtapiApp {
172176
}
173177

174178
int run_threads(int fd, int (*callback)(int fd)) {
175-
while(callback(fd)) { /* nothing */ }
179+
while (callback(fd)) {
180+
/* nothing */
181+
}
176182
return 0;
177183
}
178184

179185
int task_self() {
180-
struct rtapi_task *task = reinterpret_cast<rtapi_task*>(pthread_getspecific(key));
181-
if(!task) return -EINVAL;
186+
struct rtapi_task *task = reinterpret_cast<rtapi_task *>(pthread_getspecific(key));
187+
if (!task)
188+
return -EINVAL;
182189
return task->id;
183190
}
184191

@@ -207,12 +214,12 @@ struct RtaiApp : RtapiApp {
207214

208215
pthread_once_t RtaiApp::key_once;
209216
pthread_key_t RtaiApp::key;
210-
}
217+
} // namespace
211218

212219
extern "C" RtapiApp *make(int policy);
213220

214221
RtapiApp *make(int policy) {
215-
if(policy != SCHED_FIFO){
222+
if (policy != SCHED_FIFO) {
216223
throw std::invalid_argument("Only SCHED_FIFO allowed");
217224
}
218225
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using LXRT realtime\n");

0 commit comments

Comments
 (0)