Skip to content

Commit 2f4d4a7

Browse files
committed
Cleanup: Separate some classes
1 parent 341d510 commit 2f4d4a7

4 files changed

Lines changed: 169 additions & 164 deletions

File tree

src/rtapi/Submakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ $(patsubst ./rtapi/%,../include/%,$(RTAPIINCS)): ../include/%.h: ./rtapi/%.h
3232
ifeq ($(BUILD_SYS),uspace)
3333

3434
RTAPI_APP_SRCS := \
35+
rtapi/uspace_rtapi.cc \
3536
rtapi/uspace_rtapi_app.cc \
3637
rtapi/uspace_rtapi_parport.cc \
3738
rtapi/uspace_rtapi_string.c \

src/rtapi/uspace_rtapi.cc

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include "rtapi.h"
2+
#include "uspace_rtapi.hh"
3+
4+
std::atomic_int WithRoot::level;
5+
uid_t euid, ruid;
6+
7+
WithRoot::WithRoot() {
8+
if(!level++) {
9+
#ifdef __linux__
10+
setfsuid(euid);
11+
#endif
12+
}
13+
}
14+
15+
WithRoot::~WithRoot() {
16+
if(!--level) {
17+
#ifdef __linux__
18+
setfsuid(ruid);
19+
#endif
20+
}
21+
}
22+
23+
rtapi_task::rtapi_task()
24+
: magic{}, id{}, owner{}, uses_fp{}, stacksize{}, prio{},
25+
period{}, nextstart{},
26+
ratio{}, pll_correction{}, pll_correction_limit{},
27+
arg{}, taskcode{}
28+
29+
{}
30+
31+
/* Priority functions. Uspace uses POSIX task priorities. */
32+
33+
int RtapiApp::prio_highest() const
34+
{
35+
return sched_get_priority_max(policy);
36+
}
37+
38+
int RtapiApp::prio_lowest() const
39+
{
40+
return sched_get_priority_min(policy);
41+
}
42+
43+
int RtapiApp::prio_higher_delta() const {
44+
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
45+
return 1;
46+
}
47+
return -1;
48+
}
49+
50+
int RtapiApp::prio_bound(int prio) const {
51+
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
52+
if (prio >= rtapi_prio_highest())
53+
return rtapi_prio_highest();
54+
if (prio < rtapi_prio_lowest())
55+
return rtapi_prio_lowest();
56+
} else {
57+
if (prio <= rtapi_prio_highest())
58+
return rtapi_prio_highest();
59+
if (prio > rtapi_prio_lowest())
60+
return rtapi_prio_lowest();
61+
}
62+
return prio;
63+
}
64+
65+
bool RtapiApp::prio_check(int prio) const {
66+
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
67+
return (prio <= rtapi_prio_highest()) && (prio >= rtapi_prio_lowest());
68+
} else {
69+
return (prio <= rtapi_prio_lowest()) && (prio >= rtapi_prio_highest());
70+
}
71+
}
72+
73+
int RtapiApp::prio_next_higher(int prio) const
74+
{
75+
prio = prio_bound(prio);
76+
if(prio != rtapi_prio_highest())
77+
return prio + prio_higher_delta();
78+
return prio;
79+
}
80+
81+
int RtapiApp::prio_next_lower(int prio) const
82+
{
83+
prio = prio_bound(prio);
84+
if(prio != rtapi_prio_lowest())
85+
return prio - prio_higher_delta();
86+
return prio;
87+
}
88+
89+
int RtapiApp::allocate_task_id()
90+
{
91+
for(int n=0; n<MAX_TASKS; n++)
92+
{
93+
rtapi_task **taskptr = &(task_array[n]);
94+
if(__sync_bool_compare_and_swap(taskptr, (rtapi_task*)0, TASK_MAGIC_INIT))
95+
return n;
96+
}
97+
return -ENOSPC;
98+
}
99+
100+
int RtapiApp::task_new(void (*taskcode) (void*), void *arg,
101+
int prio, int owner, unsigned long int stacksize, int /*uses_fp*/) {
102+
/* check requested priority */
103+
if (!prio_check(prio))
104+
{
105+
rtapi_print_msg(RTAPI_MSG_ERR,"rtapi:task_new prio is not in bound lowest %i prio %i highest %i\n",
106+
rtapi_prio_lowest(), prio, rtapi_prio_highest());
107+
return -EINVAL;
108+
}
109+
110+
/* label as a valid task structure */
111+
int n = allocate_task_id();
112+
if(n < 0) return n;
113+
114+
struct rtapi_task *task = do_task_new();
115+
if(stacksize < (1024*1024)) stacksize = (1024*1024);
116+
task->id = n;
117+
task->owner = owner;
118+
/* uses_fp is deprecated and ignored; always save FPU state */
119+
task->uses_fp = 1;
120+
task->arg = arg;
121+
task->stacksize = stacksize;
122+
task->taskcode = taskcode;
123+
task->prio = prio;
124+
task->magic = TASK_MAGIC;
125+
task_array[n] = task;
126+
127+
/* and return handle to the caller */
128+
129+
return n;
130+
}
131+
132+
rtapi_task *RtapiApp::get_task(int task_id) {
133+
if(task_id < 0 || task_id >= MAX_TASKS) return NULL;
134+
/* validate task handle */
135+
rtapi_task *task = task_array[task_id];
136+
if(!task || task == TASK_MAGIC_INIT || task->magic != TASK_MAGIC)
137+
return NULL;
138+
139+
return task;
140+
}
141+
142+
void RtapiApp::unexpected_realtime_delay(rtapi_task *task, int /*nperiod*/) {
143+
static int printed = 0;
144+
if(!printed)
145+
{
146+
rtapi_print_msg(RTAPI_MSG_ERR,
147+
"Unexpected realtime delay on task %d with period %ld\n"
148+
"This Message will only display once per session.\n"
149+
"Run the Latency Test and resolve before continuing.\n",
150+
task->id, task->period);
151+
printed = 1;
152+
}
153+
}
154+
155+
long RtapiApp::clock_set_period(long nsecs)
156+
{
157+
if(nsecs == 0) return period;
158+
if(period != 0) {
159+
rtapi_print_msg(RTAPI_MSG_ERR, "attempt to set period twice\n");
160+
return -EINVAL;
161+
}
162+
period = nsecs;
163+
return period;
164+
}

src/rtapi/uspace_rtapi.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@ int find_rt_cpu_number();
115115

116116
extern struct rtapi_task *task_array[MAX_TASKS];
117117

118+
extern uid_t euid, ruid; //ToDo: Improve
119+
118120
#define WITH_ROOT WithRoot root
119121
#endif

src/rtapi/uspace_rtapi_app.cc

Lines changed: 2 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,7 @@
5656
#include "rtapi.h"
5757
#include <hal.h>
5858
#include "hal/hal_priv.h"
59-
#include "uspace_rtapi.hh"
60-
61-
std::atomic_int WithRoot::level;
62-
static uid_t euid, ruid;
63-
64-
#include "rtapi/uspace_common.h"
65-
66-
WithRoot::WithRoot() {
67-
if(!level++) {
68-
#ifdef __linux__
69-
setfsuid(euid);
70-
#endif
71-
}
72-
}
73-
74-
WithRoot::~WithRoot() {
75-
if(!--level) {
76-
#ifdef __linux__
77-
setfsuid(ruid);
78-
#endif
79-
}
80-
}
59+
#include "uspace_common.h"
8160

8261
namespace
8362
{
@@ -625,14 +604,6 @@ struct rtapi_module {
625604
#define MAX_MODULES 64
626605
#define MODULE_OFFSET 32768
627606

628-
rtapi_task::rtapi_task()
629-
: magic{}, id{}, owner{}, uses_fp{}, stacksize{}, prio{},
630-
period{}, nextstart{},
631-
ratio{}, pll_correction{}, pll_correction_limit{},
632-
arg{}, taskcode{}
633-
634-
{}
635-
636607
namespace
637608
{
638609
struct PosixTask : rtapi_task
@@ -887,130 +858,6 @@ RtapiApp &App()
887858
/* data for all tasks */
888859
struct rtapi_task *task_array[MAX_TASKS];
889860

890-
/* Priority functions. Uspace uses POSIX task priorities. */
891-
892-
int RtapiApp::prio_highest() const
893-
{
894-
return sched_get_priority_max(policy);
895-
}
896-
897-
int RtapiApp::prio_lowest() const
898-
{
899-
return sched_get_priority_min(policy);
900-
}
901-
902-
int RtapiApp::prio_higher_delta() const {
903-
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
904-
return 1;
905-
}
906-
return -1;
907-
}
908-
909-
int RtapiApp::prio_bound(int prio) const {
910-
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
911-
if (prio >= rtapi_prio_highest())
912-
return rtapi_prio_highest();
913-
if (prio < rtapi_prio_lowest())
914-
return rtapi_prio_lowest();
915-
} else {
916-
if (prio <= rtapi_prio_highest())
917-
return rtapi_prio_highest();
918-
if (prio > rtapi_prio_lowest())
919-
return rtapi_prio_lowest();
920-
}
921-
return prio;
922-
}
923-
924-
bool RtapiApp::prio_check(int prio) const {
925-
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
926-
return (prio <= rtapi_prio_highest()) && (prio >= rtapi_prio_lowest());
927-
} else {
928-
return (prio <= rtapi_prio_lowest()) && (prio >= rtapi_prio_highest());
929-
}
930-
}
931-
932-
int RtapiApp::prio_next_higher(int prio) const
933-
{
934-
prio = prio_bound(prio);
935-
if(prio != rtapi_prio_highest())
936-
return prio + prio_higher_delta();
937-
return prio;
938-
}
939-
940-
int RtapiApp::prio_next_lower(int prio) const
941-
{
942-
prio = prio_bound(prio);
943-
if(prio != rtapi_prio_lowest())
944-
return prio - prio_higher_delta();
945-
return prio;
946-
}
947-
948-
int RtapiApp::allocate_task_id()
949-
{
950-
for(int n=0; n<MAX_TASKS; n++)
951-
{
952-
rtapi_task **taskptr = &(task_array[n]);
953-
if(__sync_bool_compare_and_swap(taskptr, (rtapi_task*)0, TASK_MAGIC_INIT))
954-
return n;
955-
}
956-
return -ENOSPC;
957-
}
958-
959-
int RtapiApp::task_new(void (*taskcode) (void*), void *arg,
960-
int prio, int owner, unsigned long int stacksize, int /*uses_fp*/) {
961-
/* check requested priority */
962-
if (!prio_check(prio))
963-
{
964-
rtapi_print_msg(RTAPI_MSG_ERR,"rtapi:task_new prio is not in bound lowest %i prio %i highest %i\n",
965-
rtapi_prio_lowest(), prio, rtapi_prio_highest());
966-
return -EINVAL;
967-
}
968-
969-
/* label as a valid task structure */
970-
int n = allocate_task_id();
971-
if(n < 0) return n;
972-
973-
struct rtapi_task *task = do_task_new();
974-
if(stacksize < (1024*1024)) stacksize = (1024*1024);
975-
task->id = n;
976-
task->owner = owner;
977-
/* uses_fp is deprecated and ignored; always save FPU state */
978-
task->uses_fp = 1;
979-
task->arg = arg;
980-
task->stacksize = stacksize;
981-
task->taskcode = taskcode;
982-
task->prio = prio;
983-
task->magic = TASK_MAGIC;
984-
task_array[n] = task;
985-
986-
/* and return handle to the caller */
987-
988-
return n;
989-
}
990-
991-
rtapi_task *RtapiApp::get_task(int task_id) {
992-
if(task_id < 0 || task_id >= MAX_TASKS) return NULL;
993-
/* validate task handle */
994-
rtapi_task *task = task_array[task_id];
995-
if(!task || task == TASK_MAGIC_INIT || task->magic != TASK_MAGIC)
996-
return NULL;
997-
998-
return task;
999-
}
1000-
1001-
void RtapiApp::unexpected_realtime_delay(rtapi_task *task, int /*nperiod*/) {
1002-
static int printed = 0;
1003-
if(!printed)
1004-
{
1005-
rtapi_print_msg(RTAPI_MSG_ERR,
1006-
"Unexpected realtime delay on task %d with period %ld\n"
1007-
"This Message will only display once per session.\n"
1008-
"Run the Latency Test and resolve before continuing.\n",
1009-
task->id, task->period);
1010-
printed = 1;
1011-
}
1012-
}
1013-
1014861
int Posix::task_delete(int id)
1015862
{
1016863
auto task = ::rtapi_get_task<PosixTask>(id);
@@ -1286,16 +1133,7 @@ long rtapi_clock_set_period(long nsecs)
12861133
return App().clock_set_period(nsecs);
12871134
}
12881135

1289-
long RtapiApp::clock_set_period(long nsecs)
1290-
{
1291-
if(nsecs == 0) return period;
1292-
if(period != 0) {
1293-
rtapi_print_msg(RTAPI_MSG_ERR, "attempt to set period twice\n");
1294-
return -EINVAL;
1295-
}
1296-
period = nsecs;
1297-
return period;
1298-
}
1136+
12991137

13001138

13011139
int rtapi_task_new(void (*taskcode) (void*), void *arg,

0 commit comments

Comments
 (0)