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