Skip to content

Commit 62e5ea6

Browse files
committed
Cleanup: Handle posix the same way as others
1 parent fdafdbc commit 62e5ea6

7 files changed

Lines changed: 113 additions & 88 deletions

File tree

src/rtapi/Submakefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ ifeq ($(BUILD_SYS),uspace)
3434
RTAPI_APP_SRCS := \
3535
rtapi/uspace_rtapi.cc \
3636
rtapi/uspace_rtapi_app.cc \
37-
rtapi/uspace_rtapi_posix.cc \
3837
rtapi/uspace_rtapi_parport.cc \
3938
rtapi/uspace_rtapi_string.c \
4039
rtapi/rtapi_pci.cc
@@ -49,6 +48,15 @@ $(call TOOBJSDEPS, $(RTAPI_APP_SRCS)): EXTRAFLAGS += -DSIM \
4948
TARGETS += ../bin/rtapi_app
5049
endif
5150

51+
USPACE_POSIX_SRCS := rtapi/uspace_posix.cc
52+
USERSRCS += $(USPACE_POSIX_SRCS)
53+
$(call TOOBJSDEPS, $(USPACE_POSIX_SRCS)): EXTRAFLAGS += -pthread -fPIC
54+
../lib/libuspace-posix.so.0: $(call TOOBJS, $(USPACE_POSIX_SRCS))
55+
$(ECHO) Linking $(notdir $@)
56+
$(Q)$(CXX) -shared $(LDFLAGS) -o $@ $^ -Wl,-soname,$(notdir $@)
57+
TARGETS += ../lib/libuspace-posix.so.0
58+
TARGETS += ../lib/libuspace-posix.so
59+
5260
ifeq ($(CONFIG_USPACE_RTAI),y)
5361
USPACE_RTAI_SRCS := rtapi/uspace_rtai.cc
5462
USERSRCS += $(USPACE_RTAI_SRCS)
Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
#include "uspace_rtapi_posix.hh"
1+
#include "config.h"
22
#include "rtapi.h"
3-
3+
#include "uspace_rtapi.hh"
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <string.h>
7+
#include <stdexcept>
78
#ifdef HAVE_SYS_IO_H
89
#include <sys/io.h>
910
#endif
1011

12+
namespace
13+
{
1114
struct PosixTask : rtapi_task
1215
{
1316
PosixTask() : rtapi_task{}, thr{}
@@ -16,12 +19,59 @@ struct PosixTask : rtapi_task
1619
pthread_t thr; /* thread's context */
1720
};
1821

19-
Posix::Posix(int policy) : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) {
20-
pthread_once(&key_once, init_key);
21-
if(do_thread_lock) {
22-
pthread_once(&lock_once, init_lock);
22+
struct Posix : RtapiApp
23+
{
24+
Posix(int policy = SCHED_FIFO) : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) {
25+
if(instance != nullptr){
26+
throw std::invalid_argument("Only one instance allowed!");
27+
}
28+
pthread_once(&key_once, init_key);
29+
if(do_thread_lock) {
30+
pthread_once(&lock_once, init_lock);
31+
}
32+
instance = this;
33+
}
34+
int task_delete(int id);
35+
int task_start(int task_id, unsigned long period_nsec);
36+
int task_pause(int task_id);
37+
int task_resume(int task_id);
38+
int task_self();
39+
long long task_pll_get_reference(void);
40+
int task_pll_set_correction(long value);
41+
void wait();
42+
struct rtapi_task *do_task_new(){
43+
return new PosixTask;
44+
}
45+
unsigned char do_inb(unsigned int port);
46+
void do_outb(unsigned char value, unsigned int port);
47+
int run_threads(int fd, int (*callback)(int fd));
48+
static void *wrapper(void *arg);
49+
bool do_thread_lock;
50+
51+
static pthread_once_t key_once;
52+
static pthread_key_t key;
53+
static void init_key(void) {
54+
pthread_key_create(&key, NULL);
2355
}
24-
}
56+
57+
static pthread_once_t lock_once;
58+
static pthread_mutex_t thread_lock;
59+
static void init_lock(void) {
60+
pthread_mutex_init(&thread_lock, NULL);
61+
}
62+
63+
long long do_get_time(void) {
64+
struct timespec ts;
65+
clock_gettime(CLOCK_MONOTONIC, &ts);
66+
return ts.tv_sec * 1000000000LL + ts.tv_nsec;
67+
}
68+
69+
void do_delay(long ns);
70+
71+
static Posix* instance;
72+
};
73+
74+
Posix* Posix::instance=nullptr;
2575

2676
int Posix::task_delete(int id)
2777
{
@@ -95,15 +145,14 @@ pthread_once_t Posix::lock_once = PTHREAD_ONCE_INIT;
95145
pthread_key_t Posix::key;
96146
pthread_mutex_t Posix::thread_lock;
97147

98-
extern RtapiApp &App(); //ToDo: Nicer
99-
100148
void *Posix::wrapper(void *arg)
101149
{
102150
struct rtapi_task *task;
103151

104152
/* use the argument to point to the task data */
105153
task = (struct rtapi_task*)arg;
106-
long int period = App().period;
154+
155+
long int period = instance->period;
107156
if(task->period < period) task->period = period;
108157
task->ratio = task->period / period;
109158
task->period = task->ratio * period;
@@ -113,9 +162,8 @@ void *Posix::wrapper(void *arg)
113162
pthread_setspecific(key, arg);
114163
set_namef("rtapi_app:T#%d", task->id);
115164

116-
Posix &papp = reinterpret_cast<Posix&>(App());
117-
if(papp.do_thread_lock)
118-
pthread_mutex_lock(&papp.thread_lock);
165+
if(instance->do_thread_lock)
166+
pthread_mutex_lock(&instance->thread_lock);
119167

120168
struct timespec now;
121169
clock_gettime(RTAPI_CLOCK, &now);
@@ -179,10 +227,6 @@ void Posix::wait() {
179227
pthread_mutex_lock(&thread_lock);
180228
}
181229

182-
struct rtapi_task *Posix::do_task_new() {
183-
return new PosixTask;
184-
}
185-
186230
unsigned char Posix::do_inb(unsigned int port)
187231
{
188232
#ifdef HAVE_SYS_IO_H
@@ -211,4 +255,16 @@ void Posix::do_delay(long ns) {
211255
int Posix::run_threads(int fd, int(*callback)(int fd)) {
212256
while(callback(fd)) { /* nothing */ }
213257
return 0;
258+
}
259+
}
260+
261+
extern "C" RtapiApp *make(int policy);
262+
263+
RtapiApp *make(int policy) {
264+
if(policy == SCHED_OTHER){
265+
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX non-realtime\n");
266+
}else{
267+
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX realtime\n");
268+
}
269+
return new Posix(policy);
214270
}

src/rtapi/uspace_rtai.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ pthread_once_t RtaiApp::key_once;
192192
pthread_key_t RtaiApp::key;
193193
}
194194

195-
extern "C" RtapiApp *make();
195+
extern "C" RtapiApp *make(int policy);
196196

197-
RtapiApp *make() {
197+
RtapiApp *make(int policy) {
198+
(void) policy;
198199
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using LXRT realtime\n");
199-
return app = new RtaiApp;
200+
return app = new RtaiApp();
200201
}

src/rtapi/uspace_rtapi_app.cc

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#include <hal.h>
5858
#include "hal/hal_priv.h"
5959
#include "uspace_common.h"
60-
#include "uspace_rtapi_posix.hh"
6160

6261
RtapiApp &App();
6362

@@ -740,37 +739,36 @@ static int harden_rt()
740739
return 0;
741740
}
742741

742+
static RtapiApp *makeDllApp(string dllName, int policy){
743+
void *dll = nullptr;
744+
dll = dlopen(dllName.c_str(), RTLD_NOW);
745+
if(!dll) fprintf(stderr, "dlopen: %s\n", dlerror());
746+
auto fn = reinterpret_cast<RtapiApp*(*)(int policy)>(dlsym(dll, "make"));
747+
if(!fn) fprintf(stderr, "dlopen: %s\n", dlerror());
748+
auto result = fn ? fn(policy) : nullptr;
749+
if(result) {
750+
return result;
751+
}else{
752+
throw invalid_argument("Could not load DLL!");
753+
}
754+
}
743755

744756
static RtapiApp *makeApp()
745757
{
746758
if(euid != 0 || harden_rt() < 0)
747759
{
748-
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX non-realtime\n");
749-
return new Posix(SCHED_OTHER);
760+
return makeDllApp(EMC2_HOME "/lib/libuspace-posix.so.0", SCHED_OTHER);
750761
}
751762
WithRoot r;
752-
void *dll = nullptr;
753763
if(detect_xenomai_evl()) {
754-
dll = dlopen(EMC2_HOME "/lib/libuspace-xenomai-evl.so.0", RTLD_NOW);
755-
if(!dll) fprintf(stderr, "dlopen: %s\n", dlerror());
764+
return makeDllApp(EMC2_HOME "/lib/libuspace-xenomai-evl.so.0", SCHED_FIFO);
756765
}else if(detect_xenomai()) {
757-
dll = dlopen(EMC2_HOME "/lib/libuspace-xenomai.so.0", RTLD_NOW);
758-
if(!dll) fprintf(stderr, "dlopen: %s\n", dlerror());
766+
return makeDllApp(EMC2_HOME "/lib/libuspace-xenomai.so.0", SCHED_FIFO);
759767
} else if(detect_rtai()) {
760-
dll = dlopen(EMC2_HOME "/lib/libuspace-rtai.so.0", RTLD_NOW);
761-
if(!dll) fprintf(stderr, "dlopen: %s\n", dlerror());
762-
}
763-
if(dll)
764-
{
765-
auto fn = reinterpret_cast<RtapiApp*(*)()>(dlsym(dll, "make"));
766-
if(!fn) fprintf(stderr, "dlopen: %s\n", dlerror());
767-
auto result = fn ? fn() : nullptr;
768-
if(result) {
769-
return result;
770-
}
768+
return makeDllApp(EMC2_HOME "/lib/libuspace-rtai.so.0", SCHED_FIFO);
769+
} else {
770+
return makeDllApp(EMC2_HOME "/lib/libuspace-posix.so.0", SCHED_FIFO);
771771
}
772-
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX realtime\n");
773-
return new Posix(SCHED_FIFO);
774772
}
775773
RtapiApp &App()
776774
{

src/rtapi/uspace_rtapi_posix.hh

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/rtapi/uspace_xenomai.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ pthread_once_t XenomaiApp::key_once;
198198
pthread_key_t XenomaiApp::key;
199199
}
200200

201-
extern "C" RtapiApp *make();
201+
extern "C" RtapiApp *make(int policy);
202202

203-
RtapiApp *make() {
203+
RtapiApp *make(int policy) {
204+
(void) policy;
204205
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using XENOMAI (posix-skin) realtime\n");
205-
return new XenomaiApp;
206+
return new XenomaiApp();
206207
}

src/rtapi/uspace_xenomai_evl.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ pthread_once_t XenomaiApp::key_once;
218218
pthread_key_t XenomaiApp::key;
219219
}
220220

221-
extern "C" RtapiApp *make();
221+
extern "C" RtapiApp *make(int policy);
222222

223-
RtapiApp *make() {
223+
RtapiApp *make(int policy) {
224+
(void) policy;
224225
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using XENOMAI4 EVL realtime\n");
225-
return new XenomaiApp;
226+
return new XenomaiApp();
226227
}

0 commit comments

Comments
 (0)