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+ {
1114struct 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
2676int Posix::task_delete (int id)
2777{
@@ -95,15 +145,14 @@ pthread_once_t Posix::lock_once = PTHREAD_ONCE_INIT;
95145pthread_key_t Posix::key;
96146pthread_mutex_t Posix::thread_lock;
97147
98- extern RtapiApp &App (); // ToDo: Nicer
99-
100148void *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-
186230unsigned char Posix::do_inb (unsigned int port)
187231{
188232#ifdef HAVE_SYS_IO_H
@@ -211,4 +255,16 @@ void Posix::do_delay(long ns) {
211255int 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}
0 commit comments