author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 03 Mar 2023 09:47:39 +0100 | |
branch | wxPython4 |
changeset 3739 | 99263915a91d |
parent 3732 | 929276eea252 |
child 3740 | ac0e6de439b5 |
permissions | -rw-r--r-- |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
1 |
/** |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
2 |
* Linux specific code |
329 | 3 |
**/ |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
4 |
|
205 | 5 |
#include <stdio.h> |
6 |
#include <string.h> |
|
7 |
#include <time.h> |
|
8 |
#include <signal.h> |
|
9 |
#include <stdlib.h> |
|
3725
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
10 |
#include <errno.h> |
329 | 11 |
#include <pthread.h> |
568 | 12 |
#include <locale.h> |
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
13 |
#include <semaphore.h> |
3732
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
14 |
#ifdef REALTIME_LINUX |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
15 |
#include <sys/mman.h> |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
16 |
#endif |
205 | 17 |
|
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
18 |
static unsigned long __debug_tick; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
19 |
|
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
20 |
static pthread_t PLC_thread; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
21 |
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
22 |
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
23 |
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
24 |
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
25 |
|
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
26 |
static int PLC_shutdown = 0; |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
27 |
|
236 | 28 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
205 | 29 |
{ |
30 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
31 |
} |
|
954 | 32 |
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange) |
33 |
{ |
|
34 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
35 |
} |
|
205 | 36 |
|
37 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
38 |
{ |
|
592 | 39 |
struct timespec tmp; |
40 |
clock_gettime(CLOCK_REALTIME, &tmp); |
|
41 |
CURRENT_TIME->tv_sec = tmp.tv_sec; |
|
42 |
CURRENT_TIME->tv_nsec = tmp.tv_nsec; |
|
205 | 43 |
} |
44 |
||
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
45 |
static long long period_ns = 0; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
46 |
struct timespec next_abs_time; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
47 |
|
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
48 |
static void inc_timespec(struct timespec *ts, unsigned long long value_ns) |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
49 |
{ |
3727 | 50 |
long long next_ns = ((long long) ts->tv_sec * 1000000000) + ts->tv_nsec + value_ns; |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
51 |
#ifdef __lldiv_t_defined |
3727 | 52 |
lldiv_t next_div = lldiv(next_ns, 1000000000); |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
53 |
ts->tv_sec = next_div.quot; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
54 |
ts->tv_nsec = next_div.rem; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
55 |
#else |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
56 |
ts->tv_sec = next_ns / 1000000000; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
57 |
ts->tv_nsec = next_ns % 1000000000; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
58 |
#endif |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
59 |
} |
205 | 60 |
|
518
8e61b0066859
Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents:
485
diff
changeset
|
61 |
void PLC_SetTimer(unsigned long long next, unsigned long long period) |
205 | 62 |
{ |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
63 |
/* |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
64 |
printf("SetTimer(%lld,%lld)\n",next, period); |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
65 |
*/ |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
66 |
period_ns = period; |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
67 |
clock_gettime(CLOCK_MONOTONIC, &next_abs_time); |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
68 |
inc_timespec(&next_abs_time, next); |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
69 |
// interrupt clock_nanpsleep |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
70 |
pthread_kill(PLC_thread, SIGUSR1); |
205 | 71 |
} |
3727 | 72 |
|
205 | 73 |
void catch_signal(int sig) |
74 |
{ |
|
75 |
// signal(SIGTERM, catch_signal); |
|
76 |
signal(SIGINT, catch_signal); |
|
77 |
printf("Got Signal %d\n",sig); |
|
78 |
exit(0); |
|
79 |
} |
|
80 |
||
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
81 |
void PLCThreadSignalHandler(int sig) |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
82 |
{ |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
83 |
if (sig == SIGUSR2) |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
84 |
pthread_exit(NULL); |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
85 |
} |
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
86 |
|
2173
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2171
diff
changeset
|
87 |
int ForceSaveRetainReq(void) { |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2171
diff
changeset
|
88 |
return PLC_shutdown; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2171
diff
changeset
|
89 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2171
diff
changeset
|
90 |
|
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
91 |
void PLC_thread_proc(void *arg) |
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
92 |
{ |
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
93 |
while (!PLC_shutdown) { |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
94 |
// Sleep until next PLC run |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
95 |
// TODO check result of clock_nanosleep and wait again or exit eventually |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
96 |
int res = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_abs_time, NULL); |
3727 | 97 |
if(res==EINTR){ |
98 |
continue; |
|
99 |
} |
|
100 |
if(res!=0){ |
|
101 |
printf("PLC thread died with error %d \n", res); |
|
102 |
return; |
|
103 |
} |
|
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
104 |
PLC_GetTime(&__CURRENT_TIME); |
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
105 |
__run(); |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
106 |
inc_timespec(&next_abs_time, period_ns); |
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
107 |
} |
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
108 |
pthread_exit(0); |
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
109 |
} |
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
110 |
|
3732
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
111 |
#define _LogError(text,...) \ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
112 |
{\ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
113 |
char mstr[256];\ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
114 |
snprintf(mstr, 255, text, ##__VA_ARGS__);\ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
115 |
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
116 |
} |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
117 |
#define maxval(a,b) ((a>b)?a:b) |
205 | 118 |
int startPLC(int argc,char **argv) |
119 |
{ |
|
329 | 120 |
|
3732
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
121 |
int ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
122 |
pthread_attr_t *pattr = NULL; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
123 |
|
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
124 |
#ifdef REALTIME_LINUX |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
125 |
struct sched_param param; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
126 |
pthread_attr_t attr; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
127 |
|
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
128 |
/* Lock memory */ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
129 |
ret = mlockall(MCL_CURRENT|MCL_FUTURE); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
130 |
if(ret == -1) { |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
131 |
_LogError("mlockall failed: %m\n"); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
132 |
return ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
133 |
} |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
134 |
|
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
135 |
/* Initialize pthread attributes (default values) */ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
136 |
ret = pthread_attr_init(&attr); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
137 |
if (ret) { |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
138 |
_LogError("init pthread attributes failed\n"); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
139 |
return ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
140 |
} |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
141 |
|
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
142 |
/* Set scheduler policy and priority of pthread */ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
143 |
ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
144 |
if (ret) { |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
145 |
_LogError("pthread setschedpolicy failed\n"); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
146 |
return ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
147 |
} |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
148 |
param.sched_priority = PLC_THREAD_PRIORITY; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
149 |
ret = pthread_attr_setschedparam(&attr, ¶m); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
150 |
if (ret) { |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
151 |
_LogError("pthread setschedparam failed\n"); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
152 |
return ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
153 |
} |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
154 |
|
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
155 |
/* Use scheduling parameters of attr */ |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
156 |
ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
157 |
if (ret) { |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
158 |
_LogError("pthread setinheritsched failed\n"); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
159 |
return ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
160 |
} |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
161 |
|
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
162 |
pattr = &attr; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
163 |
#endif |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
164 |
|
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
165 |
PLC_shutdown = 0; |
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
166 |
|
333 | 167 |
pthread_mutex_init(&debug_wait_mutex, NULL); |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
168 |
pthread_mutex_init(&debug_mutex, NULL); |
333 | 169 |
pthread_mutex_init(&python_wait_mutex, NULL); |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
170 |
pthread_mutex_init(&python_mutex, NULL); |
329 | 171 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
172 |
pthread_mutex_lock(&debug_wait_mutex); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
173 |
pthread_mutex_lock(&python_wait_mutex); |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
174 |
|
3732
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
175 |
if((ret = __init(argc,argv)) == 0 ){ |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
176 |
|
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
177 |
/* Signal to wakeup PLC thread when period changes */ |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
178 |
signal(SIGUSR1, PLCThreadSignalHandler); |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
179 |
/* Signal to end PLC thread */ |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
180 |
signal(SIGUSR2, PLCThreadSignalHandler); |
205 | 181 |
/* install signal handler for manual break */ |
182 |
signal(SIGINT, catch_signal); |
|
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
183 |
|
3727 | 184 |
/* initialize next occurence and period */ |
185 |
period_ns = common_ticktime__; |
|
186 |
clock_gettime(CLOCK_MONOTONIC, &next_abs_time); |
|
187 |
||
3732
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
188 |
ret = pthread_create(&PLC_thread, pattr, (void*) &PLC_thread_proc, NULL); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
189 |
if (ret) { |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
190 |
_LogError("create pthread failed\n"); |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
191 |
return ret; |
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
192 |
} |
205 | 193 |
}else{ |
3732
929276eea252
Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents:
3731
diff
changeset
|
194 |
return ret; |
205 | 195 |
} |
196 |
return 0; |
|
197 |
} |
|
198 |
||
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
199 |
int TryEnterDebugSection(void) |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
200 |
{ |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
201 |
if (pthread_mutex_trylock(&debug_mutex) == 0){ |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
202 |
/* Only enter if debug active */ |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
203 |
if(__DEBUG){ |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
204 |
return 1; |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
205 |
} |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
462
diff
changeset
|
206 |
pthread_mutex_unlock(&debug_mutex); |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
207 |
} |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
208 |
return 0; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
209 |
} |
235 | 210 |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
211 |
void LeaveDebugSection(void) |
235 | 212 |
{ |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
213 |
pthread_mutex_unlock(&debug_mutex); |
235 | 214 |
} |
215 |
||
205 | 216 |
int stopPLC() |
217 |
{ |
|
218 |
/* Stop the PLC */ |
|
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
219 |
PLC_shutdown = 1; |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
220 |
/* Order PLCThread to exit */ |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
221 |
pthread_kill(PLC_thread, SIGUSR2); |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
222 |
pthread_join(PLC_thread, NULL); |
205 | 223 |
__cleanup(); |
329 | 224 |
pthread_mutex_destroy(&debug_wait_mutex); |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
462
diff
changeset
|
225 |
pthread_mutex_destroy(&debug_mutex); |
329 | 226 |
pthread_mutex_destroy(&python_wait_mutex); |
483
bc26c42d2eec
fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents:
462
diff
changeset
|
227 |
pthread_mutex_destroy(&python_mutex); |
386 | 228 |
return 0; |
205 | 229 |
} |
230 |
||
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
386
diff
changeset
|
231 |
extern unsigned long __tick; |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
232 |
|
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
233 |
int WaitDebugData(unsigned long *tick) |
205 | 234 |
{ |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
614
diff
changeset
|
235 |
int res; |
876
179d5c455f29
Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents:
617
diff
changeset
|
236 |
if (PLC_shutdown) return 1; |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
614
diff
changeset
|
237 |
/* Wait signal from PLC thread */ |
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
614
diff
changeset
|
238 |
res = pthread_mutex_lock(&debug_wait_mutex); |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
239 |
*tick = __debug_tick; |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
614
diff
changeset
|
240 |
return res; |
205 | 241 |
} |
329 | 242 |
|
205 | 243 |
/* Called by PLC thread when debug_publish finished |
244 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
245 |
void InitiateDebugTransfer() |
|
246 |
{ |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
247 |
/* remember tick */ |
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
248 |
__debug_tick = __tick; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
249 |
/* signal debugger thread it can read data */ |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
250 |
pthread_mutex_unlock(&debug_wait_mutex); |
205 | 251 |
} |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
252 |
|
614 | 253 |
int suspendDebug(int disable) |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
254 |
{ |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
255 |
/* Prevent PLC to enter debug code */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
256 |
pthread_mutex_lock(&debug_mutex); |
462
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
257 |
/*__DEBUG is protected by this mutex */ |
274e83a5534e
Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents:
446
diff
changeset
|
258 |
__DEBUG = !disable; |
485 | 259 |
if (disable) |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
260 |
pthread_mutex_unlock(&debug_mutex); |
614 | 261 |
return 0; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
262 |
} |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
263 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
264 |
void resumeDebug(void) |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
265 |
{ |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
280
diff
changeset
|
266 |
__DEBUG = 1; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
267 |
/* Let PLC enter debug code */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
268 |
pthread_mutex_unlock(&debug_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
269 |
} |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
270 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
271 |
/* from plc_python.c */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
272 |
int WaitPythonCommands(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
273 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
274 |
/* Wait signal from PLC thread */ |
329 | 275 |
return pthread_mutex_lock(&python_wait_mutex); |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
276 |
} |
329 | 277 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
278 |
/* Called by PLC thread on each new python command*/ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
279 |
void UnBlockPythonCommands(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
280 |
{ |
3334 | 281 |
/* signal python thread it can read data */ |
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
282 |
pthread_mutex_unlock(&python_wait_mutex); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
283 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
284 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
285 |
int TryLockPython(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
286 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
287 |
return pthread_mutex_trylock(&python_mutex) == 0; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
288 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
289 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
290 |
void UnLockPython(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
291 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
292 |
pthread_mutex_unlock(&python_mutex); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
293 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
294 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
295 |
void LockPython(void) |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
296 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
297 |
pthread_mutex_lock(&python_mutex); |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
298 |
} |
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
2173
diff
changeset
|
299 |
|
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
300 |
struct RT_to_nRT_signal_s { |
3725
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
301 |
int used; |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
302 |
pthread_cond_t WakeCond; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
303 |
pthread_mutex_t WakeCondLock; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
304 |
}; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
305 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
306 |
typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
307 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
308 |
#define _LogAndReturnNull(text) \ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
309 |
{\ |
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
310 |
char mstr[256] = text " for ";\ |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
311 |
strncat(mstr, name, 255);\ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
312 |
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
313 |
return NULL;\ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
314 |
} |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
315 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
316 |
void *create_RT_to_nRT_signal(char* name){ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
317 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)malloc(sizeof(RT_to_nRT_signal_t)); |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
318 |
|
3726
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
319 |
if(!sig) |
516779f11803
runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents:
3725
diff
changeset
|
320 |
_LogAndReturnNull("Failed allocating memory for RT_to_nRT signal"); |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
321 |
|
3725
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
322 |
sig->used = 1; |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
323 |
pthread_cond_init(&sig->WakeCond, NULL); |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
324 |
pthread_mutex_init(&sig->WakeCondLock, NULL); |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
325 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
326 |
return (void*)sig; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
327 |
} |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
328 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
329 |
void delete_RT_to_nRT_signal(void* handle){ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
330 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
331 |
|
3725
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
332 |
pthread_mutex_lock(&sig->WakeCondLock); |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
333 |
sig->used = 0; |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
334 |
pthread_cond_signal(&sig->WakeCond); |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
335 |
pthread_mutex_unlock(&sig->WakeCondLock); |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
336 |
} |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
337 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
338 |
int wait_RT_to_nRT_signal(void* handle){ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
339 |
int ret; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
340 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
341 |
pthread_mutex_lock(&sig->WakeCondLock); |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
342 |
ret = pthread_cond_wait(&sig->WakeCond, &sig->WakeCondLock); |
3725
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
343 |
if(!sig->used) ret = -EINVAL; |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
344 |
pthread_mutex_unlock(&sig->WakeCondLock); |
3725
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
345 |
|
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
346 |
if(!sig->used){ |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
347 |
pthread_cond_destroy(&sig->WakeCond); |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
348 |
pthread_mutex_destroy(&sig->WakeCondLock); |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
349 |
free(sig); |
0043e2b9dbec
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents:
3334
diff
changeset
|
350 |
} |
3294
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
351 |
return ret; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
352 |
} |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
353 |
|
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
354 |
int unblock_RT_to_nRT_signal(void* handle){ |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
355 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle; |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
356 |
return pthread_cond_signal(&sig->WakeCond); |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
357 |
} |
e3db472b0dfb
Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2820
diff
changeset
|
358 |
|
3295
0375d801fff7
Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3294
diff
changeset
|
359 |
void nRT_reschedule(void){ |
0375d801fff7
Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3294
diff
changeset
|
360 |
sched_yield(); |
0375d801fff7
Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3294
diff
changeset
|
361 |
} |