forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventloop_posix.c
436 lines (365 loc) · 14.2 KB
/
eventloop_posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2021 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
* Copyright 2021 (c) Fraunhofer IOSB (Author: Jan Hermes)
*/
#include "eventloop_posix.h"
#include "open62541/plugin/eventloop.h"
/*********/
/* Timer */
/*********/
static void
timerExecutionTrampoline(void *executionApplication,
UA_ApplicationCallback cb,
void *callbackApplication,
void *data) {
cb(callbackApplication, data);
}
static UA_DateTime
UA_EventLoopPOSIX_nextCyclicTime(UA_EventLoop *public_el) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)public_el;
return UA_Timer_nextRepeatedTime(&el->timer);
}
static UA_StatusCode
UA_EventLoopPOSIX_addTimedCallback(UA_EventLoop *public_el,
UA_Callback callback,
void *application, void *data,
UA_DateTime date,
UA_UInt64 *callbackId) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)public_el;
return UA_Timer_addTimedCallback(&el->timer, callback, application,
data, date, callbackId);
}
static UA_StatusCode
UA_EventLoopPOSIX_addCyclicCallback(UA_EventLoop *public_el,
UA_Callback cb,
void *application, void *data,
UA_Double interval_ms,
UA_DateTime *baseTime,
UA_TimerPolicy timerPolicy,
UA_UInt64 *callbackId) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)public_el;
return UA_Timer_addRepeatedCallback(&el->timer, cb, application,
data, interval_ms, baseTime,
timerPolicy, callbackId);
}
static UA_StatusCode
UA_EventLoopPOSIX_modifyCyclicCallback(UA_EventLoop *public_el,
UA_UInt64 callbackId,
UA_Double interval_ms,
UA_DateTime *baseTime,
UA_TimerPolicy timerPolicy) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)public_el;
return UA_Timer_changeRepeatedCallback(&el->timer, callbackId,
interval_ms, baseTime,
timerPolicy);
}
static void
UA_EventLoopPOSIX_removeCyclicCallback(UA_EventLoop *public_el,
UA_UInt64 callbackId) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)public_el;
UA_Timer_removeCallback(&el->timer, callbackId);
}
static void
UA_EventLoopPOSIX_addDelayedCallback(UA_EventLoop *public_el,
UA_DelayedCallback *dc) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)public_el;
UA_LOCK(&el->elMutex);
dc->next = el->delayedCallbacks;
el->delayedCallbacks = dc;
UA_UNLOCK(&el->elMutex);
}
/* Process and then free registered delayed callbacks */
static void
processDelayed(UA_EventLoopPOSIX *el) {
UA_LOG_DEBUG(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Process delayed callbacks");
UA_LOCK_ASSERT(&el->elMutex, 1);
while(el->delayedCallbacks) {
UA_DelayedCallback *dc = el->delayedCallbacks;
el->delayedCallbacks = dc->next;
/* Delayed Callbacks might have no cb pointer if all
* we want to do is free the memory */
if(dc->callback) {
UA_UNLOCK(&el->elMutex);
dc->callback(dc->application, dc->context);
UA_LOCK(&el->elMutex);
}
UA_free(dc);
}
}
/***********************/
/* EventLoop Lifecycle */
/***********************/
static UA_StatusCode
UA_EventLoopPOSIX_start(UA_EventLoopPOSIX *el) {
UA_LOCK(&el->elMutex);
if(el->eventLoop.state != UA_EVENTLOOPSTATE_FRESH &&
el->eventLoop.state != UA_EVENTLOOPSTATE_STOPPED) {
UA_UNLOCK(&el->elMutex);
return UA_STATUSCODE_BADINTERNALERROR;
}
UA_LOG_INFO(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Starting the EventLoop");
#ifdef UA_HAVE_EPOLL
el->epollfd = epoll_create1(0);
if(el->epollfd == -1) {
UA_LOG_SOCKET_ERRNO_WRAP(
UA_LOG_WARNING(el->eventLoop.logger, UA_LOGCATEGORY_NETWORK,
"Eventloop\t| Could not create the epoll socket (%s)",
errno_str));
return UA_STATUSCODE_BADINTERNALERROR;
}
#endif
UA_StatusCode res = UA_STATUSCODE_GOOD;
UA_EventSource *es = el->eventLoop.eventSources;
while(es) {
UA_UNLOCK(&el->elMutex);
res |= es->start(es);
UA_LOCK(&el->elMutex);
es = es->next;
}
/* Dirty-write the state that is const "from the outside" */
*(UA_EventLoopState*)(uintptr_t)&el->eventLoop.state =
UA_EVENTLOOPSTATE_STARTED;
UA_UNLOCK(&el->elMutex);
return res;
}
static void
checkClosed(UA_EventLoopPOSIX *el) {
UA_EventSource *es = el->eventLoop.eventSources;
while(es) {
if(es->state != UA_EVENTSOURCESTATE_STOPPED)
return;
es = es->next;
}
/* Dirty-write the state that is const "from the outside" */
*(UA_EventLoopState*)(uintptr_t)&el->eventLoop.state =
UA_EVENTLOOPSTATE_STOPPED;
/* Close the epoll/IOCP socket once all EventSources have shut down */
#ifdef UA_HAVE_EPOLL
close(el->epollfd);
#endif
UA_LOG_INFO(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"The EventLoop has stopped");
}
static void
UA_EventLoopPOSIX_stop(UA_EventLoopPOSIX *el) {
UA_LOCK(&el->elMutex);
if(el->eventLoop.state != UA_EVENTLOOPSTATE_STARTED) {
UA_LOG_WARNING(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"The EventLoop is not running, cannot be stopped");
UA_UNLOCK(&el->elMutex);
return;
}
UA_LOG_INFO(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Stopping the EventLoop");
/* Shutdown all event sources. This closes open connections. */
UA_EventSource *es = el->eventLoop.eventSources;
while(es) {
if(es->state == UA_EVENTSOURCESTATE_STARTING ||
es->state == UA_EVENTSOURCESTATE_STARTED) {
UA_UNLOCK(&el->elMutex);
es->stop(es);
UA_LOCK(&el->elMutex);
}
es = es->next;
}
*(UA_EventLoopState*)(uintptr_t)&el->eventLoop.state =
UA_EVENTLOOPSTATE_STOPPING;
checkClosed(el);
UA_UNLOCK(&el->elMutex);
}
static UA_StatusCode
UA_EventLoopPOSIX_run(UA_EventLoopPOSIX *el, UA_UInt32 timeout) {
UA_LOCK(&el->elMutex);
if(el->executing) {
UA_LOG_ERROR(el->eventLoop.logger,
UA_LOGCATEGORY_EVENTLOOP,
"Cannot run EventLoop from the run method itself");
UA_UNLOCK(&el->elMutex);
return UA_STATUSCODE_BADINTERNALERROR;
}
el->executing = true;
if(el->eventLoop.state == UA_EVENTLOOPSTATE_FRESH ||
el->eventLoop.state == UA_EVENTLOOPSTATE_STOPPED) {
UA_LOG_WARNING(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Cannot iterate a stopped EventLoop");
el->executing = false;
UA_UNLOCK(&el->elMutex);
return UA_STATUSCODE_BADINTERNALERROR;
}
UA_LOG_TRACE(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Iterate the EventLoop");
/* Process cyclic callbacks */
UA_DateTime dateBefore =
el->eventLoop.dateTime_nowMonotonic(&el->eventLoop);
UA_UNLOCK(&el->elMutex);
UA_DateTime dateNext =
UA_Timer_process(&el->timer, dateBefore,
timerExecutionTrampoline, NULL);
UA_LOCK(&el->elMutex);
processDelayed(el); /* Process delayed callbacks. Remove closed sockets
* already here instead of calling select for them. */
/* Compute the remaining time */
UA_DateTime maxDate = dateBefore + (timeout * UA_DATETIME_MSEC);
if(dateNext > maxDate)
dateNext = maxDate;
UA_DateTime listenTimeout =
dateNext - el->eventLoop.dateTime_nowMonotonic(&el->eventLoop);
if(listenTimeout < 0)
listenTimeout = 0;
/* Listen on the active file-descriptors (sockets) from the
* ConnectionManagers */
UA_StatusCode rv = UA_EventLoopPOSIX_pollFDs(el, listenTimeout);
/* Check if the last EventSource was successfully stopped */
if(el->eventLoop.state == UA_EVENTLOOPSTATE_STOPPING)
checkClosed(el);
el->executing = false;
UA_UNLOCK(&el->elMutex);
return rv;
}
/*****************************/
/* Registering Event Sources */
/*****************************/
static UA_StatusCode
UA_EventLoopPOSIX_registerEventSource(UA_EventLoopPOSIX *el,
UA_EventSource *es) {
/* Already registered? */
if(es->state != UA_EVENTSOURCESTATE_FRESH) {
UA_LOG_ERROR(el->eventLoop.logger, UA_LOGCATEGORY_NETWORK,
"Cannot register the EventSource \"%.*s\": "
"already registered",
(int)es->name.length, (char*)es->name.data);
return UA_STATUSCODE_BADINTERNALERROR;
}
/* Add to linked list */
UA_LOCK(&el->elMutex);
es->next = el->eventLoop.eventSources;
el->eventLoop.eventSources = es;
UA_UNLOCK(&el->elMutex);
es->eventLoop = &el->eventLoop;
es->state = UA_EVENTSOURCESTATE_STOPPED;
/* Start if the entire EventLoop is started */
if(el->eventLoop.state == UA_EVENTLOOPSTATE_STARTED)
return es->start(es);
return UA_STATUSCODE_GOOD;
}
static UA_StatusCode
UA_EventLoopPOSIX_deregisterEventSource(UA_EventLoopPOSIX *el,
UA_EventSource *es) {
if(es->state != UA_EVENTSOURCESTATE_STOPPED) {
UA_LOG_WARNING(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Cannot deregister the EventSource %.*s: "
"Has to be stopped first",
(int)es->name.length, es->name.data);
return UA_STATUSCODE_BADINTERNALERROR;
}
/* Remove from the linked list */
UA_LOCK(&el->elMutex);
UA_EventSource **s = &el->eventLoop.eventSources;
while(*s) {
if(*s == es) {
*s = es->next;
break;
}
s = &(*s)->next;
}
UA_UNLOCK(&el->elMutex);
/* Set the state to non-registered */
es->state = UA_EVENTSOURCESTATE_FRESH;
return UA_STATUSCODE_GOOD;
}
/***************/
/* Time Domain */
/***************/
/* No special synchronization with an external source, just use the globally
* defined functions. */
static UA_DateTime
UA_EventLoopPOSIX_DateTime_now(UA_EventLoop *el) {
return UA_DateTime_now();
}
static UA_DateTime
UA_EventLoopPOSIX_DateTime_nowMonotonic(UA_EventLoop *el) {
return UA_DateTime_nowMonotonic();
}
static UA_Int64
UA_EventLoopPOSIX_DateTime_localTimeUtcOffset(UA_EventLoop *el) {
return UA_DateTime_localTimeUtcOffset();
}
/*************************/
/* Initialize and Delete */
/*************************/
static UA_StatusCode
UA_EventLoopPOSIX_free(UA_EventLoopPOSIX *el) {
UA_LOCK(&el->elMutex);
/* Check if the EventLoop can be deleted */
if(el->eventLoop.state != UA_EVENTLOOPSTATE_STOPPED &&
el->eventLoop.state != UA_EVENTLOOPSTATE_FRESH) {
UA_LOG_WARNING(el->eventLoop.logger, UA_LOGCATEGORY_EVENTLOOP,
"Cannot delete a running EventLoop");
UA_UNLOCK(&el->elMutex);
return UA_STATUSCODE_BADINTERNALERROR;
}
/* Deregister and delete all the EventSources */
while(el->eventLoop.eventSources) {
UA_EventSource *es = el->eventLoop.eventSources;
UA_UNLOCK(&el->elMutex);
UA_EventLoopPOSIX_deregisterEventSource(el, es);
UA_LOCK(&el->elMutex);
es->free(es);
}
/* Remove the repeated timed callbacks */
UA_Timer_clear(&el->timer);
/* Process remaining delayed callbacks */
processDelayed(el);
#ifdef _WIN32
/* Stop the Windows networking subsystem */
WSACleanup();
#endif
/* Clean up */
UA_UNLOCK(&el->elMutex);
UA_LOCK_DESTROY(&el->elMutex);
UA_free(el);
return UA_STATUSCODE_GOOD;
}
UA_EventLoop *
UA_EventLoop_new_POSIX(const UA_Logger *logger) {
UA_EventLoopPOSIX *el = (UA_EventLoopPOSIX*)
UA_calloc(1, sizeof(UA_EventLoopPOSIX));
if(!el)
return NULL;
UA_LOCK_INIT(&el->elMutex);
UA_Timer_init(&el->timer);
#ifdef _WIN32
/* Start the WSA networking subsystem on Windows */
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
/* Set the public EventLoop content */
el->eventLoop.logger = logger;
el->eventLoop.start = (UA_StatusCode (*)(UA_EventLoop*))UA_EventLoopPOSIX_start;
el->eventLoop.stop = (void (*)(UA_EventLoop*))UA_EventLoopPOSIX_stop;
el->eventLoop.run = (UA_StatusCode (*)(UA_EventLoop*, UA_UInt32))UA_EventLoopPOSIX_run;
el->eventLoop.free = (UA_StatusCode (*)(UA_EventLoop*))UA_EventLoopPOSIX_free;
el->eventLoop.dateTime_now = UA_EventLoopPOSIX_DateTime_now;
el->eventLoop.dateTime_nowMonotonic =
UA_EventLoopPOSIX_DateTime_nowMonotonic;
el->eventLoop.dateTime_localTimeUtcOffset =
UA_EventLoopPOSIX_DateTime_localTimeUtcOffset;
el->eventLoop.nextCyclicTime = UA_EventLoopPOSIX_nextCyclicTime;
el->eventLoop.addCyclicCallback = UA_EventLoopPOSIX_addCyclicCallback;
el->eventLoop.modifyCyclicCallback = UA_EventLoopPOSIX_modifyCyclicCallback;
el->eventLoop.removeCyclicCallback = UA_EventLoopPOSIX_removeCyclicCallback;
el->eventLoop.addTimedCallback = UA_EventLoopPOSIX_addTimedCallback;
el->eventLoop.addDelayedCallback = UA_EventLoopPOSIX_addDelayedCallback;
el->eventLoop.registerEventSource =
(UA_StatusCode (*)(UA_EventLoop*, UA_EventSource*))
UA_EventLoopPOSIX_registerEventSource;
el->eventLoop.deregisterEventSource =
(UA_StatusCode (*)(UA_EventLoop*, UA_EventSource*))
UA_EventLoopPOSIX_deregisterEventSource;
return &el->eventLoop;
}