Skip to content

Commit

Permalink
feat(el): EventSources from an EventLoop visible in the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr committed Apr 20, 2022
1 parent 37aa239 commit 41f0d7f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
33 changes: 8 additions & 25 deletions arch/eventloop_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ UA_EventLoopPOSIX_start(UA_EventLoopPOSIX *el) {
#endif

UA_StatusCode res = UA_STATUSCODE_GOOD;
UA_EventSource *es = el->eventSources;
UA_EventSource *es = el->eventLoop.eventSources;
while(es) {
UA_UNLOCK(&el->elMutex);
res |= es->start(es);
Expand All @@ -148,7 +148,7 @@ UA_EventLoopPOSIX_start(UA_EventLoopPOSIX *el) {

static void
checkClosed(UA_EventLoopPOSIX *el) {
UA_EventSource *es = el->eventSources;
UA_EventSource *es = el->eventLoop.eventSources;
while(es) {
if(es->state != UA_EVENTSOURCESTATE_STOPPED)
return;
Expand Down Expand Up @@ -183,7 +183,7 @@ UA_EventLoopPOSIX_stop(UA_EventLoopPOSIX *el) {
"Stopping the EventLoop");

/* Shutdown all event sources. This closes open connections. */
UA_EventSource *es = el->eventSources;
UA_EventSource *es = el->eventLoop.eventSources;
while(es) {
if(es->state == UA_EVENTSOURCESTATE_STARTING ||
es->state == UA_EVENTSOURCESTATE_STARTED)
Expand Down Expand Up @@ -279,8 +279,8 @@ UA_EventLoopPOSIX_registerEventSource(UA_EventLoopPOSIX *el,

/* Add to linked list */
UA_LOCK(&el->elMutex);
es->next = el->eventSources;
el->eventSources = es;
es->next = el->eventLoop.eventSources;
el->eventLoop.eventSources = es;
UA_UNLOCK(&el->elMutex);

es->eventLoop = &el->eventLoop;
Expand All @@ -305,7 +305,7 @@ UA_EventLoopPOSIX_deregisterEventSource(UA_EventLoopPOSIX *el,

/* Remove from the linked list */
UA_LOCK(&el->elMutex);
UA_EventSource **s = &el->eventSources;
UA_EventSource **s = &el->eventLoop.eventSources;
while(*s) {
if(*s == es) {
*s = es->next;
Expand All @@ -321,20 +321,6 @@ UA_EventLoopPOSIX_deregisterEventSource(UA_EventLoopPOSIX *el,
return UA_STATUSCODE_GOOD;
}

static UA_EventSource *
UA_EventLoopPOSIX_findEventSource(UA_EventLoopPOSIX *el,
const UA_String name) {
UA_LOCK(&el->elMutex);
UA_EventSource *s = el->eventSources;
while(s) {
if(UA_String_equal(&name, &s->name))
break;
s = s->next;
}
UA_UNLOCK(&el->elMutex);
return s;
}

/***************/
/* Time Domain */
/***************/
Expand Down Expand Up @@ -375,8 +361,8 @@ UA_EventLoopPOSIX_free(UA_EventLoopPOSIX *el) {
}

/* Deregister and delete all the EventSources */
while(el->eventSources) {
UA_EventSource *es = el->eventSources;
while(el->eventLoop.eventSources) {
UA_EventSource *es = el->eventLoop.eventSources;
UA_UNLOCK(&el->elMutex);
UA_EventLoopPOSIX_deregisterEventSource(el, es);
UA_LOCK(&el->elMutex);
Expand Down Expand Up @@ -444,9 +430,6 @@ UA_EventLoop_new_POSIX(const UA_Logger *logger) {
el->eventLoop.deregisterEventSource =
(UA_StatusCode (*)(UA_EventLoop*, UA_EventSource*))
UA_EventLoopPOSIX_deregisterEventSource;
el->eventLoop.findEventSource =
(UA_EventSource* (*)(UA_EventLoop*, const UA_String))
UA_EventLoopPOSIX_findEventSource;

return &el->eventLoop;
}
3 changes: 0 additions & 3 deletions arch/eventloop_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ typedef struct {
/* Linked List of Delayed Callbacks */
UA_DelayedCallback *delayedCallbacks;

/* Pointers to registered EventSources */
UA_EventSource *eventSources;

/* Flag determining whether the eventloop is currently within the
* "run" method */
UA_Boolean executing;
Expand Down
16 changes: 9 additions & 7 deletions include/open62541/plugin/eventloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,15 @@ struct UA_EventLoop {

void (*addDelayedCallback)(UA_EventLoop *el, UA_DelayedCallback *dc);

/* Manage EventSources
* ~~~~~~~~~~~~~~~~~~~ */
/* EventSources
* ~~~~~~~~~~~~
* EventSources are stored in a singly-linked list for direct access. But
* only the below methods shall be used for adding and removing - this
* impacts the lifecycle of the EventSource. For example it may be
* auto-started if the EventLoop is already running. */

/* Linked list of EventSources */
UA_EventSource *eventSources;

/* Register the ES. Immediately starts the ES if the EventLoop is already
* started. Otherwise the ES is started together with the EventLoop. */
Expand All @@ -158,11 +165,6 @@ struct UA_EventLoop {
/* Stops the EventSource before deregistrering it */
UA_StatusCode
(*deregisterEventSource)(UA_EventLoop *el, UA_EventSource *es);

/* Look up the EventSource by name. Returns the first EventSource of that
* name (duplicates should be avoided). */
UA_EventSource *
(*findEventSource)(UA_EventLoop *el, const UA_String name);
};

/**
Expand Down

0 comments on commit 41f0d7f

Please sign in to comment.