-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuspace_posix.cc
More file actions
249 lines (213 loc) · 7.37 KB
/
uspace_posix.cc
File metadata and controls
249 lines (213 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (C) 2006-2014 Jeff Epler <jepler@unpythonic.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "rtapi.h"
#include "uspace_rtapi_app.hh"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#ifdef HAVE_SYS_IO_H
#include <sys/io.h>
#endif
namespace
{
struct PosixTask : rtapi_task
{
PosixTask() : rtapi_task{}, thr{}
{}
pthread_t thr; /* thread's context */
};
struct PosixApp : RtapiApp
{
PosixApp(int policy = SCHED_FIFO) : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) {
pthread_once(&key_once, init_key);
if(do_thread_lock) {
pthread_once(&lock_once, init_lock);
}
}
struct rtapi_task *do_task_new() {
return new PosixTask;
}
int task_delete(int id) {
auto task = ::rtapi_get_task<PosixTask>(id);
if(!task) return -EINVAL;
pthread_cancel(task->thr);
pthread_join(task->thr, 0);
task->magic = 0;
task_array[id] = 0;
delete task;
return 0;
}
int task_start(int task_id, unsigned long period_nsec) {
auto task = ::rtapi_get_task<PosixTask>(task_id);
if(!task) return -EINVAL;
task->period = period_nsec;
struct sched_param param;
memset(¶m, 0, sizeof(param));
param.sched_priority = task->prio;
// limit PLL correction values to +/-1% of cycle time
task->pll_correction_limit = period_nsec / 100;
task->pll_correction = 0;
int nprocs = sysconf( _SC_NPROCESSORS_ONLN );
pthread_attr_t attr;
int ret;
if((ret = pthread_attr_init(&attr)) != 0)
return -ret;
if((ret = pthread_attr_setstacksize(&attr, task->stacksize)) != 0)
return -ret;
if((ret = pthread_attr_setschedpolicy(&attr, policy)) != 0)
return -ret;
if((ret = pthread_attr_setschedparam(&attr, ¶m)) != 0)
return -ret;
if((ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)) != 0)
return -ret;
if(nprocs > 1){
const static int rt_cpu_number = find_rt_cpu_number();
rtapi_print_msg(RTAPI_MSG_INFO, "rt_cpu_number = %i\n", rt_cpu_number);
if(rt_cpu_number != -1) {
#ifdef __FreeBSD__
cpuset_t cpuset;
#else
cpu_set_t cpuset;
#endif
CPU_ZERO(&cpuset);
CPU_SET(rt_cpu_number, &cpuset);
if((ret = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset)) != 0)
return -ret;
}
}
if(do_thread_lock)
pthread_mutex_lock(&thread_lock);
if((ret = pthread_create(&task->thr, &attr, &wrapper, reinterpret_cast<void*>(task))) != 0)
return -ret;
return 0;
}
static void *wrapper(void *arg) {
auto task = reinterpret_cast<PosixTask*>(arg);
pthread_setspecific(key, arg);
set_namef("rtapi_app:T#%d", task->id);
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
rtapi_timespec_advance(task->nextstart, now, task->period + task->pll_correction);
/* call the task function with the task argument */
(task->taskcode) (task->arg);
rtapi_print("ERROR: reached end of wrapper for task %d\n", task->id);
return NULL;
}
int task_pause(int task_id) {
(void)task_id;
return -ENOSYS;
}
int task_resume(int task_id) {
(void)task_id;
return -ENOSYS;
}
long long task_pll_get_reference(void) {
struct rtapi_task *task = reinterpret_cast<rtapi_task*>(pthread_getspecific(key));
if(!task) return 0;
return task->nextstart.tv_sec * 1000000000LL + task->nextstart.tv_nsec;
}
int task_pll_set_correction(long value) {
struct rtapi_task *task = reinterpret_cast<rtapi_task*>(pthread_getspecific(key));
if(!task) return -EINVAL;
if (value > task->pll_correction_limit) value = task->pll_correction_limit;
if (value < -(task->pll_correction_limit)) value = -(task->pll_correction_limit);
task->pll_correction = value;
return 0;
}
void wait() {
if(do_thread_lock)
pthread_mutex_unlock(&thread_lock);
pthread_testcancel();
struct rtapi_task *task = reinterpret_cast<rtapi_task*>(pthread_getspecific(key));
rtapi_timespec_advance(task->nextstart, task->nextstart, task->period + task->pll_correction);
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if(rtapi_timespec_less(task->nextstart, now))
{
if(policy == SCHED_FIFO)
unexpected_realtime_delay(task);
}
else
{
int res = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &task->nextstart, nullptr);
if(res < 0) perror("clock_nanosleep");
}
if(do_thread_lock)
pthread_mutex_lock(&thread_lock);
}
unsigned char do_inb(unsigned int port) {
#ifdef HAVE_SYS_IO_H
return inb(port);
#else
(void)port;
return 0;
#endif
}
void do_outb(unsigned char val, unsigned int port) {
#ifdef HAVE_SYS_IO_H
return outb(val, port);
#else
(void)val;
(void)port;
#endif
}
int run_threads(int fd, int (*callback)(int fd)) {
while(callback(fd)) { /* nothing */ }
return 0;
}
int task_self() {
struct rtapi_task *task = reinterpret_cast<rtapi_task*>(pthread_getspecific(key));
if(!task) return -EINVAL;
return task->id;
}
bool do_thread_lock;
static pthread_once_t key_once;
static pthread_key_t key;
static void init_key(void) {
pthread_key_create(&key, NULL);
}
static pthread_once_t lock_once;
static pthread_mutex_t thread_lock;
static void init_lock(void) {
pthread_mutex_init(&thread_lock, NULL);
}
long long do_get_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000LL + ts.tv_nsec;
}
void do_delay(long ns) {
struct timespec ts = {0, ns};
clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr);
}
};
pthread_once_t PosixApp::key_once = PTHREAD_ONCE_INIT;
pthread_once_t PosixApp::lock_once = PTHREAD_ONCE_INIT;
pthread_key_t PosixApp::key;
pthread_mutex_t PosixApp::thread_lock;
}
extern "C" RtapiApp *make(int policy);
RtapiApp *make(int policy) {
if(policy == SCHED_OTHER){
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX non-realtime\n");
}else{
rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX realtime\n");
}
return new PosixApp(policy);
}