author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Fri, 26 Apr 2024 09:24:26 +0200 | |
changeset 3938 | fc4af5685aa3 |
parent 3782 | 39480d9d3d3f |
child 3947 | 9b5cb90297e4 |
permissions | -rw-r--r-- |
3778 | 1 |
/** |
2 |
* Mac OSX specific code |
|
3 |
**/ |
|
4 |
||
5 |
#include <stdio.h> |
|
6 |
#include <string.h> |
|
7 |
#include <time.h> |
|
8 |
#include <signal.h> |
|
9 |
#include <stdlib.h> |
|
10 |
#include <pthread.h> |
|
11 |
#include <locale.h> |
|
12 |
#include <semaphore.h> |
|
13 |
#include <dispatch/dispatch.h> |
|
14 |
||
3781
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
15 |
static dispatch_semaphore_t Run_PLC; |
3778 | 16 |
|
17 |
long AtomicCompareExchange(long *atomicvar, long compared, long exchange) |
|
18 |
{ |
|
19 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
20 |
} |
|
21 |
||
22 |
long long AtomicCompareExchange64(long long *atomicvar, long long compared, |
|
23 |
long long exchange) |
|
24 |
{ |
|
25 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
26 |
} |
|
27 |
||
28 |
void PLC_GetTime(IEC_TIME * CURRENT_TIME) |
|
29 |
{ |
|
30 |
struct timespec tmp; |
|
31 |
clock_gettime(CLOCK_REALTIME, &tmp); |
|
32 |
CURRENT_TIME->tv_sec = tmp.tv_sec; |
|
33 |
CURRENT_TIME->tv_nsec = tmp.tv_nsec; |
|
34 |
} |
|
35 |
||
36 |
dispatch_queue_t queue; |
|
37 |
dispatch_source_t PLC_timer; |
|
38 |
||
39 |
static inline void PLC_timer_cancel(void *arg) |
|
40 |
{ |
|
41 |
dispatch_release(PLC_timer); |
|
42 |
dispatch_release(queue); |
|
43 |
exit(0); |
|
44 |
} |
|
45 |
||
46 |
static inline void PLC_timer_notify(void *arg) |
|
47 |
{ |
|
48 |
PLC_GetTime(&__CURRENT_TIME); |
|
3781
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
49 |
dispatch_semaphore_signal(Run_PLC); |
3778 | 50 |
} |
51 |
||
52 |
void PLC_SetTimer(unsigned long long next, unsigned long long period) |
|
53 |
{ |
|
54 |
if (next == period && next == 0) { |
|
55 |
dispatch_suspend(PLC_timer); |
|
56 |
} else { |
|
57 |
dispatch_time_t start; |
|
3782
39480d9d3d3f
OSX: use wall time instead of system clock
GP Orcullo <kinsamanka@gmail.com>
parents:
3781
diff
changeset
|
58 |
start = dispatch_walltime(NULL, next); |
3778 | 59 |
dispatch_source_set_timer(PLC_timer, start, period, 0); |
60 |
dispatch_resume(PLC_timer); |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
void catch_signal(int sig) |
|
65 |
{ |
|
66 |
signal(SIGINT, catch_signal); |
|
67 |
printf("Got Signal %d\n", sig); |
|
68 |
dispatch_source_cancel(PLC_timer); |
|
69 |
exit(0); |
|
70 |
} |
|
71 |
||
72 |
static unsigned long __debug_tick; |
|
73 |
||
74 |
pthread_t PLC_thread; |
|
75 |
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
76 |
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
77 |
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
78 |
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
79 |
||
80 |
int PLC_shutdown = 0; |
|
81 |
||
82 |
int ForceSaveRetainReq(void) |
|
83 |
{ |
|
84 |
return PLC_shutdown; |
|
85 |
} |
|
86 |
||
87 |
void PLC_thread_proc(void *arg) |
|
88 |
{ |
|
89 |
while (!PLC_shutdown) { |
|
3781
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
90 |
dispatch_semaphore_wait(Run_PLC, DISPATCH_TIME_FOREVER); |
3778 | 91 |
__run(); |
92 |
} |
|
93 |
pthread_exit(0); |
|
94 |
} |
|
95 |
||
96 |
#define maxval(a,b) ((a>b)?a:b) |
|
97 |
int startPLC(int argc, char **argv) |
|
98 |
{ |
|
99 |
setlocale(LC_NUMERIC, "C"); |
|
100 |
||
101 |
PLC_shutdown = 0; |
|
102 |
||
3781
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
103 |
Run_PLC = dispatch_semaphore_create(0); |
3778 | 104 |
|
105 |
pthread_create(&PLC_thread, NULL, (void *)&PLC_thread_proc, NULL); |
|
106 |
||
107 |
pthread_mutex_init(&debug_wait_mutex, NULL); |
|
108 |
pthread_mutex_init(&debug_mutex, NULL); |
|
109 |
pthread_mutex_init(&python_wait_mutex, NULL); |
|
110 |
pthread_mutex_init(&python_mutex, NULL); |
|
111 |
||
112 |
pthread_mutex_lock(&debug_wait_mutex); |
|
113 |
pthread_mutex_lock(&python_wait_mutex); |
|
114 |
||
115 |
queue = dispatch_queue_create("timerQueue", 0); |
|
116 |
PLC_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); |
|
117 |
||
118 |
dispatch_set_context(PLC_timer, &PLC_timer); |
|
119 |
dispatch_source_set_event_handler_f(PLC_timer, PLC_timer_notify); |
|
120 |
dispatch_source_set_cancel_handler_f(PLC_timer, PLC_timer_cancel); |
|
121 |
||
122 |
if (__init(argc, argv) == 0) { |
|
123 |
PLC_SetTimer(common_ticktime__, common_ticktime__); |
|
124 |
||
125 |
/* install signal handler for manual break */ |
|
126 |
signal(SIGINT, catch_signal); |
|
127 |
} else { |
|
128 |
return 1; |
|
129 |
} |
|
130 |
return 0; |
|
131 |
} |
|
132 |
||
133 |
int TryEnterDebugSection(void) |
|
134 |
{ |
|
135 |
if (pthread_mutex_trylock(&debug_mutex) == 0) { |
|
136 |
/* Only enter if debug active */ |
|
137 |
if (__DEBUG) { |
|
138 |
return 1; |
|
139 |
} |
|
140 |
pthread_mutex_unlock(&debug_mutex); |
|
141 |
} |
|
142 |
return 0; |
|
143 |
} |
|
144 |
||
145 |
void LeaveDebugSection(void) |
|
146 |
{ |
|
147 |
pthread_mutex_unlock(&debug_mutex); |
|
148 |
} |
|
149 |
||
150 |
int stopPLC() |
|
151 |
{ |
|
152 |
/* Stop the PLC */ |
|
153 |
PLC_shutdown = 1; |
|
3781
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
154 |
dispatch_semaphore_signal(Run_PLC); |
3778 | 155 |
PLC_SetTimer(0, 0); |
156 |
pthread_join(PLC_thread, NULL); |
|
3781
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
157 |
dispatch_release(Run_PLC); |
25195da82745
replaced depreciated functions
GP Orcullo <kinsamanka@gmail.com>
parents:
3778
diff
changeset
|
158 |
Run_PLC = NULL; |
3778 | 159 |
dispatch_source_cancel(PLC_timer); |
160 |
__cleanup(); |
|
161 |
pthread_mutex_destroy(&debug_wait_mutex); |
|
162 |
pthread_mutex_destroy(&debug_mutex); |
|
163 |
pthread_mutex_destroy(&python_wait_mutex); |
|
164 |
pthread_mutex_destroy(&python_mutex); |
|
165 |
return 0; |
|
166 |
} |
|
167 |
||
168 |
extern unsigned long __tick; |
|
169 |
||
170 |
int WaitDebugData(unsigned long *tick) |
|
171 |
{ |
|
172 |
int res; |
|
173 |
if (PLC_shutdown) |
|
174 |
return 1; |
|
175 |
/* Wait signal from PLC thread */ |
|
176 |
res = pthread_mutex_lock(&debug_wait_mutex); |
|
177 |
*tick = __debug_tick; |
|
178 |
return res; |
|
179 |
} |
|
180 |
||
181 |
/* Called by PLC thread when debug_publish finished |
|
182 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
183 |
void InitiateDebugTransfer() |
|
184 |
{ |
|
185 |
/* remember tick */ |
|
186 |
__debug_tick = __tick; |
|
187 |
/* signal debugger thread it can read data */ |
|
188 |
pthread_mutex_unlock(&debug_wait_mutex); |
|
189 |
} |
|
190 |
||
191 |
int suspendDebug(int disable) |
|
192 |
{ |
|
193 |
/* Prevent PLC to enter debug code */ |
|
194 |
pthread_mutex_lock(&debug_mutex); |
|
195 |
/*__DEBUG is protected by this mutex */ |
|
196 |
__DEBUG = !disable; |
|
197 |
if (disable) |
|
198 |
pthread_mutex_unlock(&debug_mutex); |
|
199 |
return 0; |
|
200 |
} |
|
201 |
||
202 |
void resumeDebug(void) |
|
203 |
{ |
|
204 |
__DEBUG = 1; |
|
205 |
/* Let PLC enter debug code */ |
|
206 |
pthread_mutex_unlock(&debug_mutex); |
|
207 |
} |
|
208 |
||
209 |
/* from plc_python.c */ |
|
210 |
int WaitPythonCommands(void) |
|
211 |
{ |
|
212 |
/* Wait signal from PLC thread */ |
|
213 |
return pthread_mutex_lock(&python_wait_mutex); |
|
214 |
} |
|
215 |
||
216 |
/* Called by PLC thread on each new python command*/ |
|
217 |
void UnBlockPythonCommands(void) |
|
218 |
{ |
|
219 |
/* signal python thread it can read data */ |
|
220 |
pthread_mutex_unlock(&python_wait_mutex); |
|
221 |
} |
|
222 |
||
223 |
int TryLockPython(void) |
|
224 |
{ |
|
225 |
return pthread_mutex_trylock(&python_mutex) == 0; |
|
226 |
} |
|
227 |
||
228 |
void UnLockPython(void) |
|
229 |
{ |
|
230 |
pthread_mutex_unlock(&python_mutex); |
|
231 |
} |
|
232 |
||
233 |
void LockPython(void) |
|
234 |
{ |
|
235 |
pthread_mutex_lock(&python_mutex); |
|
236 |
} |
|
237 |
||
238 |
struct RT_to_nRT_signal_s { |
|
239 |
pthread_cond_t WakeCond; |
|
240 |
pthread_mutex_t WakeCondLock; |
|
241 |
}; |
|
242 |
||
243 |
typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t; |
|
244 |
||
245 |
#define _LogAndReturnNull(text) \ |
|
246 |
{\ |
|
247 |
char mstr[256] = text " for ";\ |
|
248 |
strncat(mstr, name, 255);\ |
|
249 |
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\ |
|
250 |
return NULL;\ |
|
251 |
} |
|
252 |
||
253 |
void *create_RT_to_nRT_signal(char *name) |
|
254 |
{ |
|
255 |
RT_to_nRT_signal_t *sig = |
|
256 |
(RT_to_nRT_signal_t *) malloc(sizeof(RT_to_nRT_signal_t)); |
|
257 |
||
258 |
if (!sig) |
|
259 |
_LogAndReturnNull("Failed allocating memory for RT_to_nRT signal"); |
|
260 |
||
261 |
pthread_cond_init(&sig->WakeCond, NULL); |
|
262 |
pthread_mutex_init(&sig->WakeCondLock, NULL); |
|
263 |
||
264 |
return (void *)sig; |
|
265 |
} |
|
266 |
||
267 |
void delete_RT_to_nRT_signal(void *handle) |
|
268 |
{ |
|
269 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t *) handle; |
|
270 |
||
271 |
pthread_cond_destroy(&sig->WakeCond); |
|
272 |
pthread_mutex_destroy(&sig->WakeCondLock); |
|
273 |
||
274 |
free(sig); |
|
275 |
} |
|
276 |
||
277 |
int wait_RT_to_nRT_signal(void *handle) |
|
278 |
{ |
|
279 |
int ret; |
|
280 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t *) handle; |
|
281 |
pthread_mutex_lock(&sig->WakeCondLock); |
|
282 |
ret = pthread_cond_wait(&sig->WakeCond, &sig->WakeCondLock); |
|
283 |
pthread_mutex_unlock(&sig->WakeCondLock); |
|
284 |
return ret; |
|
285 |
} |
|
286 |
||
287 |
int unblock_RT_to_nRT_signal(void *handle) |
|
288 |
{ |
|
289 |
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t *) handle; |
|
290 |
return pthread_cond_signal(&sig->WakeCond); |
|
291 |
} |
|
292 |
||
293 |
void nRT_reschedule(void) |
|
294 |
{ |
|
295 |
sched_yield(); |
|
296 |
} |