author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Tue, 28 Mar 2017 19:48:51 +0300 | |
changeset 1670 | cd03b8432541 |
parent 1456 | e723c1dd6faa |
child 1717 | a86deec92e08 |
permissions | -rw-r--r-- |
321 | 1 |
/** |
954 | 2 |
* Xenomai Linux specific code |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
3 |
**/ |
321 | 4 |
|
5 |
#include <stdio.h> |
|
615 | 6 |
#include <unistd.h> |
321 | 7 |
#include <string.h> |
8 |
#include <time.h> |
|
9 |
#include <signal.h> |
|
10 |
#include <stdlib.h> |
|
11 |
#include <sys/mman.h> |
|
342 | 12 |
#include <sys/fcntl.h> |
321 | 13 |
|
14 |
#include <native/task.h> |
|
15 |
#include <native/timer.h> |
|
16 |
#include <native/sem.h> |
|
342 | 17 |
#include <native/pipe.h> |
321 | 18 |
|
19 |
unsigned int PLC_state = 0; |
|
615 | 20 |
#define PLC_STATE_TASK_CREATED 1 |
21 |
#define PLC_STATE_DEBUG_FILE_OPENED 2 |
|
22 |
#define PLC_STATE_DEBUG_PIPE_CREATED 4 |
|
23 |
#define PLC_STATE_PYTHON_FILE_OPENED 8 |
|
24 |
#define PLC_STATE_PYTHON_PIPE_CREATED 16 |
|
25 |
#define PLC_STATE_WAITDEBUG_FILE_OPENED 32 |
|
26 |
#define PLC_STATE_WAITDEBUG_PIPE_CREATED 64 |
|
27 |
#define PLC_STATE_WAITPYTHON_FILE_OPENED 128 |
|
28 |
#define PLC_STATE_WAITPYTHON_PIPE_CREATED 256 |
|
29 |
||
30 |
#define WAITDEBUG_PIPE_DEVICE "/dev/rtp0" |
|
31 |
#define WAITDEBUG_PIPE_MINOR 0 |
|
32 |
#define DEBUG_PIPE_DEVICE "/dev/rtp1" |
|
33 |
#define DEBUG_PIPE_MINOR 1 |
|
34 |
#define WAITPYTHON_PIPE_DEVICE "/dev/rtp2" |
|
35 |
#define WAITPYTHON_PIPE_MINOR 2 |
|
36 |
#define PYTHON_PIPE_DEVICE "/dev/rtp3" |
|
37 |
#define PYTHON_PIPE_MINOR 3 |
|
38 |
#define PIPE_SIZE 1 |
|
321 | 39 |
|
40 |
||
41 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
42 |
{ |
|
43 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
44 |
} |
|
954 | 45 |
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange) |
46 |
{ |
|
47 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
48 |
} |
|
321 | 49 |
|
50 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
51 |
{ |
|
52 |
RTIME current_time = rt_timer_read(); |
|
53 |
CURRENT_TIME->tv_sec = current_time / 1000000000; |
|
54 |
CURRENT_TIME->tv_nsec = current_time % 1000000000; |
|
55 |
} |
|
56 |
||
57 |
RT_TASK PLC_task; |
|
342 | 58 |
RT_PIPE WaitDebug_pipe; |
615 | 59 |
RT_PIPE WaitPython_pipe; |
60 |
RT_PIPE Debug_pipe; |
|
61 |
RT_PIPE Python_pipe; |
|
62 |
int WaitDebug_pipe_fd; |
|
63 |
int WaitPython_pipe_fd; |
|
64 |
int Debug_pipe_fd; |
|
65 |
int Python_pipe_fd; |
|
66 |
||
321 | 67 |
int PLC_shutdown = 0; |
68 |
||
615 | 69 |
void PLC_SetTimer(unsigned long long next, unsigned long long period) |
321 | 70 |
{ |
71 |
RTIME current_time = rt_timer_read(); |
|
72 |
rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period)); |
|
73 |
} |
|
74 |
||
75 |
void PLC_task_proc(void *arg) |
|
76 |
{ |
|
1428
e14003eb4d42
Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents:
985
diff
changeset
|
77 |
PLC_SetTimer(common_ticktime__, common_ticktime__); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
78 |
|
695 | 79 |
while (!PLC_shutdown) { |
321 | 80 |
PLC_GetTime(&__CURRENT_TIME); |
81 |
__run(); |
|
82 |
if (PLC_shutdown) break; |
|
83 |
rt_task_wait_period(NULL); |
|
84 |
} |
|
85 |
} |
|
86 |
||
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
345
diff
changeset
|
87 |
static unsigned long __debug_tick; |
321 | 88 |
|
89 |
void PLC_cleanup_all(void) |
|
90 |
{ |
|
91 |
if (PLC_state & PLC_STATE_TASK_CREATED) { |
|
92 |
rt_task_delete(&PLC_task); |
|
93 |
PLC_state &= ~PLC_STATE_TASK_CREATED; |
|
94 |
} |
|
95 |
||
615 | 96 |
if (PLC_state & PLC_STATE_WAITDEBUG_PIPE_CREATED) { |
97 |
rt_pipe_delete(&WaitDebug_pipe); |
|
98 |
PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
99 |
} |
|
100 |
||
101 |
if (PLC_state & PLC_STATE_WAITDEBUG_FILE_OPENED) { |
|
102 |
close(WaitDebug_pipe_fd); |
|
103 |
PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED; |
|
104 |
} |
|
105 |
||
106 |
if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) { |
|
107 |
rt_pipe_delete(&WaitPython_pipe); |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
108 |
PLC_state &= ~PLC_STATE_WAITPYTHON_PIPE_CREATED; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
109 |
} |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
110 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
111 |
if (PLC_state & PLC_STATE_WAITPYTHON_FILE_OPENED) { |
615 | 112 |
close(WaitPython_pipe_fd); |
113 |
PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED; |
|
321 | 114 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
115 |
|
342 | 116 |
if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) { |
615 | 117 |
rt_pipe_delete(&Debug_pipe); |
342 | 118 |
PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED; |
119 |
} |
|
120 |
||
121 |
if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) { |
|
615 | 122 |
close(Debug_pipe_fd); |
342 | 123 |
PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED; |
321 | 124 |
} |
125 |
||
615 | 126 |
if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) { |
127 |
rt_pipe_delete(&Python_pipe); |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
128 |
PLC_state &= ~PLC_STATE_PYTHON_PIPE_CREATED; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
129 |
} |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
130 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
131 |
if (PLC_state & PLC_STATE_PYTHON_FILE_OPENED) { |
615 | 132 |
close(Python_pipe_fd); |
133 |
PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED; |
|
134 |
} |
|
135 |
||
321 | 136 |
} |
137 |
||
138 |
int stopPLC() |
|
139 |
{ |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
140 |
/* Stop the PLC */ |
321 | 141 |
PLC_shutdown = 1; |
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
142 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
143 |
/* Wait until PLC task stops */ |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
144 |
rt_task_join(&PLC_task); |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
145 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
146 |
PLC_cleanup_all(); |
342 | 147 |
__cleanup(); |
321 | 148 |
__debug_tick = -1; |
615 | 149 |
return 0; |
321 | 150 |
} |
151 |
||
152 |
// |
|
153 |
void catch_signal(int sig) |
|
154 |
{ |
|
155 |
stopPLC(); |
|
156 |
// signal(SIGTERM, catch_signal); |
|
157 |
signal(SIGINT, catch_signal); |
|
158 |
printf("Got Signal %d\n",sig); |
|
159 |
exit(0); |
|
160 |
} |
|
161 |
||
162 |
#define max_val(a,b) ((a>b)?a:b) |
|
163 |
int startPLC(int argc,char **argv) |
|
164 |
{ |
|
165 |
signal(SIGINT, catch_signal); |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
166 |
|
615 | 167 |
/* no memory swapping for that process */ |
321 | 168 |
mlockall(MCL_CURRENT | MCL_FUTURE); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
169 |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
170 |
PLC_shutdown = 0; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
171 |
|
615 | 172 |
/*** RT Pipes creation and opening ***/ |
173 |
/* create Debug_pipe */ |
|
174 |
if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE)) |
|
175 |
goto error; |
|
176 |
PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED; |
|
177 |
||
178 |
/* open Debug_pipe*/ |
|
179 |
if((Debug_pipe_fd = open(DEBUG_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
|
180 |
PLC_state |= PLC_STATE_DEBUG_FILE_OPENED; |
|
181 |
||
182 |
/* create Python_pipe */ |
|
183 |
if(rt_pipe_create(&Python_pipe, "Python_pipe", PYTHON_PIPE_MINOR, PIPE_SIZE)) |
|
184 |
goto error; |
|
185 |
PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED; |
|
186 |
||
187 |
/* open Python_pipe*/ |
|
188 |
if((Python_pipe_fd = open(PYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
|
189 |
PLC_state |= PLC_STATE_PYTHON_FILE_OPENED; |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
190 |
|
342 | 191 |
/* create WaitDebug_pipe */ |
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
192 |
if(rt_pipe_create(&WaitDebug_pipe, "WaitDebug_pipe", WAITDEBUG_PIPE_MINOR, PIPE_SIZE)) |
615 | 193 |
goto error; |
194 |
PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
342 | 195 |
|
196 |
/* open WaitDebug_pipe*/ |
|
615 | 197 |
if((WaitDebug_pipe_fd = open(WAITDEBUG_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
198 |
PLC_state |= PLC_STATE_WAITDEBUG_FILE_OPENED; |
|
199 |
||
200 |
/* create WaitPython_pipe */ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
201 |
if(rt_pipe_create(&WaitPython_pipe, "WaitPython_pipe", WAITPYTHON_PIPE_MINOR, PIPE_SIZE)) |
342 | 202 |
goto error; |
615 | 203 |
PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED; |
204 |
||
205 |
/* open WaitPython_pipe*/ |
|
206 |
if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
|
207 |
PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED; |
|
208 |
||
209 |
/*** create PLC task ***/ |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
210 |
if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE)) goto error; |
321 | 211 |
PLC_state |= PLC_STATE_TASK_CREATED; |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
212 |
|
615 | 213 |
if(__init(argc,argv)) goto error; |
214 |
||
215 |
/* start PLC task */ |
|
216 |
if(rt_task_start(&PLC_task, &PLC_task_proc, NULL)) goto error; |
|
321 | 217 |
|
218 |
return 0; |
|
219 |
||
220 |
error: |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
221 |
|
321 | 222 |
PLC_cleanup_all(); |
223 |
return 1; |
|
224 |
} |
|
225 |
||
615 | 226 |
#define DEBUG_FREE 0 |
227 |
#define DEBUG_BUSY 1 |
|
228 |
static long debug_state = DEBUG_FREE; |
|
229 |
||
321 | 230 |
int TryEnterDebugSection(void) |
231 |
{ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
232 |
if(AtomicCompareExchange( |
615 | 233 |
&debug_state, |
234 |
DEBUG_FREE, |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
235 |
DEBUG_BUSY) == DEBUG_FREE){ |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
236 |
if(__DEBUG){ |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
237 |
return 1; |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
238 |
} |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
239 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
240 |
} |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
241 |
return 0; |
615 | 242 |
} |
243 |
||
244 |
#define DEBUG_UNLOCK 1 |
|
321 | 245 |
void LeaveDebugSection(void) |
246 |
{ |
|
615 | 247 |
if(AtomicCompareExchange( &debug_state, |
248 |
DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){ |
|
249 |
char msg = DEBUG_UNLOCK; |
|
250 |
/* signal to NRT for wakeup */ |
|
251 |
rt_pipe_write(&Debug_pipe, &msg, sizeof(msg), P_NORMAL); |
|
252 |
} |
|
321 | 253 |
} |
254 |
||
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
345
diff
changeset
|
255 |
extern unsigned long __tick; |
615 | 256 |
|
257 |
#define DEBUG_PENDING_DATA 1 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
258 |
int WaitDebugData(unsigned long *tick) |
321 | 259 |
{ |
615 | 260 |
char cmd; |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
261 |
int res; |
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
262 |
if (PLC_shutdown) return -1; |
321 | 263 |
/* Wait signal from PLC thread */ |
615 | 264 |
res = read(WaitDebug_pipe_fd, &cmd, sizeof(cmd)); |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
265 |
if (res == sizeof(cmd) && cmd == DEBUG_PENDING_DATA){ |
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
266 |
*tick = __debug_tick; |
615 | 267 |
return 0; |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
268 |
} |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
269 |
return -1; |
321 | 270 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
271 |
|
321 | 272 |
/* Called by PLC thread when debug_publish finished |
273 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
274 |
void InitiateDebugTransfer() |
|
275 |
{ |
|
615 | 276 |
char msg = DEBUG_PENDING_DATA; |
321 | 277 |
/* remember tick */ |
278 |
__debug_tick = __tick; |
|
279 |
/* signal debugger thread it can read data */ |
|
615 | 280 |
rt_pipe_write(&WaitDebug_pipe, &msg, sizeof(msg), P_NORMAL); |
281 |
} |
|
282 |
||
283 |
int suspendDebug(int disable) |
|
284 |
{ |
|
285 |
char cmd = DEBUG_UNLOCK; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
286 |
if (PLC_shutdown) return -1; |
615 | 287 |
while(AtomicCompareExchange( |
288 |
&debug_state, |
|
289 |
DEBUG_FREE, |
|
290 |
DEBUG_BUSY) != DEBUG_FREE && |
|
291 |
cmd == DEBUG_UNLOCK){ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
292 |
if(read(Debug_pipe_fd, &cmd, sizeof(cmd)) != sizeof(cmd)){ |
615 | 293 |
return -1; |
294 |
} |
|
295 |
} |
|
296 |
__DEBUG = !disable; |
|
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
297 |
if (disable) |
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
298 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
615 | 299 |
return 0; |
321 | 300 |
} |
301 |
||
302 |
void resumeDebug(void) |
|
303 |
{ |
|
615 | 304 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
305 |
} |
|
306 |
||
307 |
#define PYTHON_PENDING_COMMAND 1 |
|
308 |
||
309 |
#define PYTHON_FREE 0 |
|
310 |
#define PYTHON_BUSY 1 |
|
311 |
static long python_state = PYTHON_FREE; |
|
312 |
||
321 | 313 |
int WaitPythonCommands(void) |
615 | 314 |
{ |
315 |
char cmd; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
316 |
if (PLC_shutdown) return -1; |
321 | 317 |
/* Wait signal from PLC thread */ |
615 | 318 |
if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){ |
319 |
return 0; |
|
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
320 |
} |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
321 |
return -1; |
321 | 322 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
323 |
|
321 | 324 |
/* Called by PLC thread on each new python command*/ |
325 |
void UnBlockPythonCommands(void) |
|
326 |
{ |
|
615 | 327 |
char msg = PYTHON_PENDING_COMMAND; |
328 |
rt_pipe_write(&WaitPython_pipe, &msg, sizeof(msg), P_NORMAL); |
|
321 | 329 |
} |
330 |
||
331 |
int TryLockPython(void) |
|
332 |
{ |
|
615 | 333 |
return AtomicCompareExchange( |
334 |
&python_state, |
|
335 |
PYTHON_FREE, |
|
336 |
PYTHON_BUSY) == PYTHON_FREE; |
|
337 |
} |
|
338 |
||
339 |
#define UNLOCK_PYTHON 1 |
|
340 |
void LockPython(void) |
|
341 |
{ |
|
342 |
char cmd = UNLOCK_PYTHON; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
343 |
if (PLC_shutdown) return; |
615 | 344 |
while(AtomicCompareExchange( |
345 |
&python_state, |
|
346 |
PYTHON_FREE, |
|
347 |
PYTHON_BUSY) != PYTHON_FREE && |
|
348 |
cmd == UNLOCK_PYTHON){ |
|
349 |
read(Python_pipe_fd, &cmd, sizeof(cmd)); |
|
350 |
} |
|
321 | 351 |
} |
352 |
||
353 |
void UnLockPython(void) |
|
354 |
{ |
|
615 | 355 |
if(AtomicCompareExchange( |
356 |
&python_state, |
|
357 |
PYTHON_BUSY, |
|
358 |
PYTHON_FREE) == PYTHON_BUSY){ |
|
359 |
if(rt_task_self()){/*is that the real time task ?*/ |
|
360 |
char cmd = UNLOCK_PYTHON; |
|
361 |
rt_pipe_write(&Python_pipe, &cmd, sizeof(cmd), P_NORMAL); |
|
362 |
}/* otherwise, no signaling from non real time */ |
|
363 |
} /* as plc does not wait for lock. */ |
|
364 |
} |
|
365 |