author | 'Laurent Bessard <laurent.bessard@lolitech.fr>' |
Fri, 16 Oct 2009 10:50:16 +0200 | |
changeset 420 | c093ec48f2fd |
parent 397 | 6a7ff66a811d |
child 446 | 1edde533db19 |
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> |
|
329 | 10 |
#include <pthread.h> |
205 | 11 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
12 |
/* provided by POUS.C */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
13 |
extern int common_ticktime__; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
14 |
|
236 | 15 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
205 | 16 |
{ |
17 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
18 |
} |
|
19 |
||
20 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
21 |
{ |
|
22 |
clock_gettime(CLOCK_REALTIME, CURRENT_TIME); |
|
23 |
} |
|
24 |
||
25 |
void PLC_timer_notify(sigval_t val) |
|
26 |
{ |
|
27 |
PLC_GetTime(&__CURRENT_TIME); |
|
28 |
__run(); |
|
29 |
} |
|
30 |
||
31 |
timer_t PLC_timer; |
|
32 |
||
33 |
void PLC_SetTimer(long long next, long long period) |
|
34 |
{ |
|
35 |
struct itimerspec timerValues; |
|
36 |
/* |
|
37 |
printf("SetTimer(%lld,%lld)\n",next, period); |
|
38 |
*/ |
|
39 |
memset (&timerValues, 0, sizeof (struct itimerspec)); |
|
40 |
{ |
|
41 |
#ifdef __lldiv_t_defined |
|
42 |
lldiv_t nxt_div = lldiv(next, 1000000000); |
|
43 |
lldiv_t period_div = lldiv(period, 1000000000); |
|
44 |
timerValues.it_value.tv_sec = nxt_div.quot; |
|
45 |
timerValues.it_value.tv_nsec = nxt_div.rem; |
|
46 |
timerValues.it_interval.tv_sec = period_div.quot; |
|
47 |
timerValues.it_interval.tv_nsec = period_div.rem; |
|
48 |
#else |
|
49 |
timerValues.it_value.tv_sec = next / 1000000000; |
|
50 |
timerValues.it_value.tv_nsec = next % 1000000000; |
|
51 |
timerValues.it_interval.tv_sec = period / 1000000000; |
|
52 |
timerValues.it_interval.tv_nsec = period % 1000000000; |
|
53 |
#endif |
|
329 | 54 |
} |
205 | 55 |
timer_settime (PLC_timer, 0, &timerValues, NULL); |
56 |
} |
|
57 |
// |
|
58 |
void catch_signal(int sig) |
|
59 |
{ |
|
60 |
// signal(SIGTERM, catch_signal); |
|
61 |
signal(SIGINT, catch_signal); |
|
62 |
printf("Got Signal %d\n",sig); |
|
63 |
exit(0); |
|
64 |
} |
|
65 |
||
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
66 |
|
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
|
67 |
static unsigned long __debug_tick; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
68 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
69 |
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
70 |
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER; |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
71 |
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
72 |
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
73 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
74 |
#define maxval(a,b) ((a>b)?a:b) |
205 | 75 |
int startPLC(int argc,char **argv) |
76 |
{ |
|
77 |
struct sigevent sigev; |
|
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
|
78 |
/* 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:
386
diff
changeset
|
79 |
Ttick = common_ticktime__?common_ticktime__:1000000; |
329 | 80 |
|
205 | 81 |
memset (&sigev, 0, sizeof (struct sigevent)); |
82 |
sigev.sigev_value.sival_int = 0; |
|
83 |
sigev.sigev_notify = SIGEV_THREAD; |
|
84 |
sigev.sigev_notify_attributes = NULL; |
|
85 |
sigev.sigev_notify_function = PLC_timer_notify; |
|
86 |
||
333 | 87 |
pthread_mutex_init(&debug_wait_mutex, NULL); |
88 |
pthread_mutex_init(&python_wait_mutex, NULL); |
|
329 | 89 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
90 |
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
|
91 |
pthread_mutex_lock(&python_wait_mutex); |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
92 |
|
205 | 93 |
timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
94 |
if( __init(argc,argv) == 0 ){ |
|
95 |
PLC_SetTimer(Ttick,Ttick); |
|
329 | 96 |
|
205 | 97 |
/* install signal handler for manual break */ |
98 |
// signal(SIGTERM, catch_signal); |
|
99 |
signal(SIGINT, catch_signal); |
|
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
|
100 |
|
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
|
101 |
pthread_mutex_trylock(&debug_mutex); |
205 | 102 |
}else{ |
103 |
return 1; |
|
104 |
} |
|
105 |
return 0; |
|
106 |
} |
|
107 |
||
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
108 |
int TryEnterDebugSection(void) |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
109 |
{ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
110 |
return pthread_mutex_trylock(&debug_mutex) == 0; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
111 |
} |
235 | 112 |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
113 |
void LeaveDebugSection(void) |
235 | 114 |
{ |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
115 |
pthread_mutex_unlock(&debug_mutex); |
235 | 116 |
} |
117 |
||
205 | 118 |
int stopPLC() |
119 |
{ |
|
120 |
/* Stop the PLC */ |
|
121 |
PLC_SetTimer(0,0); |
|
122 |
timer_delete (PLC_timer); |
|
123 |
__cleanup(); |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
124 |
__debug_tick = -1; |
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
|
125 |
pthread_mutex_unlock(&debug_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
|
126 |
pthread_mutex_unlock(&debug_wait_mutex); |
329 | 127 |
pthread_mutex_destroy(&debug_wait_mutex); |
128 |
pthread_mutex_unlock(&python_wait_mutex); |
|
129 |
pthread_mutex_destroy(&python_wait_mutex); |
|
386 | 130 |
return 0; |
205 | 131 |
} |
132 |
||
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
|
133 |
extern unsigned long __tick; |
205 | 134 |
/* from plc_debugger.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:
386
diff
changeset
|
135 |
unsigned long WaitDebugData() |
205 | 136 |
{ |
137 |
/* Wait signal from PLC thread */ |
|
329 | 138 |
if(pthread_mutex_lock(&debug_wait_mutex)) return -1; |
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
139 |
return __debug_tick; |
205 | 140 |
} |
329 | 141 |
|
205 | 142 |
/* Called by PLC thread when debug_publish finished |
143 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
144 |
void InitiateDebugTransfer() |
|
145 |
{ |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
146 |
/* remember tick */ |
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
147 |
__debug_tick = __tick; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
148 |
/* 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
|
149 |
pthread_mutex_unlock(&debug_wait_mutex); |
205 | 150 |
} |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
151 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
152 |
void suspendDebug(void) |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
153 |
{ |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
280
diff
changeset
|
154 |
__DEBUG = 0; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
155 |
/* Prevent PLC to enter debug code */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
156 |
pthread_mutex_lock(&debug_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
157 |
} |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
158 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
159 |
void resumeDebug(void) |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
160 |
{ |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
280
diff
changeset
|
161 |
__DEBUG = 1; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
162 |
/* Let PLC enter debug code */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
163 |
pthread_mutex_unlock(&debug_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
164 |
} |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
165 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
166 |
/* 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
|
167 |
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
|
168 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
169 |
/* Wait signal from PLC thread */ |
329 | 170 |
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
|
171 |
} |
329 | 172 |
|
280
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
173 |
/* 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
|
174 |
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
|
175 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
176 |
/* signal debugger thread it can read data */ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
177 |
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
|
178 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
179 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
180 |
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
|
181 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
182 |
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
|
183 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
184 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
185 |
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
|
186 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
187 |
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
|
188 |
} |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
189 |
|
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
190 |
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
|
191 |
{ |
f2ef79f3dba0
Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents:
245
diff
changeset
|
192 |
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
|
193 |
} |