You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2023/07/03 18:09:20 UTC

[celix] branch feature/scheduled_event_on_event_thread updated (28564af5 -> 97a92c42)

This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a change to branch feature/scheduled_event_on_event_thread
in repository https://gitbox.apache.org/repos/asf/celix.git


    from 28564af5 Merge branch 'master' into feature/scheduled_event_on_event_thread
     new fe902d06 Refactor scheduled event to use nextDeadline instead of lastScheduledEventTime
     new 45e4dd11 Fix move semantics of ScheduledEvent
     new 55c803e6 Revert scheduled event usage in pstm, because network io is used
     new 2b2ef9b7 Revert scheduled event usage in remote endpoint poller, because network io is used
     new 97a92c42 Mention Network IO in scheduled event documentation

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../src/pubsub_topology_manager.c                  | 103 ++++++++++++++-------
 .../src/pubsub_topology_manager.h                  |   9 +-
 .../include/endpoint_discovery_poller.h            |   3 +-
 .../src/endpoint_discovery_poller.c                |  67 ++++++++------
 documents/scheduled_events .md                     |   3 +-
 libs/framework/include/celix/ScheduledEvent.h      |  16 +++-
 libs/framework/src/celix_scheduled_event.c         |  24 ++---
 libs/framework/src/celix_scheduled_event.h         |   5 +-
 libs/framework/src/framework.c                     |  30 +++---
 libs/utils/gtest/src/TimeUtilsTestSuite.cc         |  24 +++++
 libs/utils/include/celix_utils.h                   |   8 ++
 libs/utils/src/utils.c                             |  11 +++
 12 files changed, 198 insertions(+), 105 deletions(-)


[celix] 02/05: Fix move semantics of ScheduledEvent

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/scheduled_event_on_event_thread
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 45e4dd11260a7857d071496b15083df1a3ba9666
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon Jul 3 19:55:36 2023 +0200

    Fix move semantics of ScheduledEvent
---
 libs/framework/include/celix/ScheduledEvent.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/libs/framework/include/celix/ScheduledEvent.h b/libs/framework/include/celix/ScheduledEvent.h
index 45202577..ffa6c078 100644
--- a/libs/framework/include/celix/ScheduledEvent.h
+++ b/libs/framework/include/celix/ScheduledEvent.h
@@ -55,8 +55,20 @@ class ScheduledEvent final {
     ScheduledEvent(const ScheduledEvent&) = delete;
     ScheduledEvent& operator=(const ScheduledEvent&) = delete;
 
-    ScheduledEvent(ScheduledEvent&&) noexcept = default;
-    ScheduledEvent& operator=(ScheduledEvent&&) noexcept = default;
+    ScheduledEvent(ScheduledEvent&& rhs) noexcept
+        : ctx{std::move(rhs.ctx)}, eventId{rhs.eventId}, isOneShot{rhs.isOneShot} {
+        rhs.eventId = -1;
+    }
+
+    ScheduledEvent& operator=(ScheduledEvent&& rhs) noexcept {
+        if (this != &rhs) {
+            ctx = std::move(rhs.ctx);
+            eventId = rhs.eventId;
+            isOneShot = rhs.isOneShot;
+            rhs.eventId = -1;
+        }
+        return *this;
+    }
 
     /**
      * @brief Cancels the scheduled event.


[celix] 04/05: Revert scheduled event usage in remote endpoint poller, because network io is used

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/scheduled_event_on_event_thread
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 2b2ef9b70419c53f6780bd719a773f3cc34054f0
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon Jul 3 20:07:51 2023 +0200

    Revert scheduled event usage in remote endpoint poller, because network io is used
---
 .../include/endpoint_discovery_poller.h            |  3 +-
 .../src/endpoint_discovery_poller.c                | 67 ++++++++++++----------
 2 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h b/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h
index 7b309ccf..9cf03291 100644
--- a/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h
+++ b/bundles/remote_services/discovery_common/include/endpoint_discovery_poller.h
@@ -41,11 +41,12 @@ struct endpoint_discovery_poller {
     celix_log_helper_t **loghelper;
 
     celix_thread_mutex_t pollerLock;
+    celix_thread_t pollerThread;
 
     unsigned int poll_interval;
     unsigned int poll_timeout;
 
-    long pollEventId;
+    volatile bool running;
 };
 
 celix_status_t endpointDiscoveryPoller_create(discovery_t *discovery, celix_bundle_context_t *context, const char* defaultPollEndpoints, endpoint_discovery_poller_t **poller);
diff --git a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c
index 50fe245f..5589c846 100644
--- a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c
+++ b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c
@@ -47,7 +47,7 @@
 #define DISCOVERY_POLL_TIMEOUT "DISCOVERY_CFG_POLL_TIMEOUT"
 #define DEFAULT_POLL_TIMEOUT "10" // seconds
 
-static void endpointDiscoveryPoller_performPeriodicPoll(void *data);
+static void *endpointDiscoveryPoller_performPeriodicPoll(void *data);
 celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_t *poller, char *url, array_list_pt currentEndpoints);
 static celix_status_t endpointDiscoveryPoller_getEndpoints(endpoint_discovery_poller_t *poller, char *url, array_list_pt *updatedEndpoints);
 static celix_status_t endpointDiscoveryPoller_endpointDescriptionEquals(const void *endpointPtr, const void *comparePtr, bool *equals);
@@ -93,6 +93,7 @@ celix_status_t endpointDiscoveryPoller_create(discovery_t *discovery, celix_bund
 	(*poller)->poll_interval = atoi(interval);
 	(*poller)->poll_timeout = atoi(timeout);
 	(*poller)->discovery = discovery;
+	(*poller)->running = false;
 	(*poller)->entries = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
 
 	const char* sep = ",";
@@ -110,14 +111,9 @@ celix_status_t endpointDiscoveryPoller_create(discovery_t *discovery, celix_bund
 		return CELIX_BUNDLE_EXCEPTION;
 	}
 
-    double intervalInSeconds = (double)(*poller)->poll_interval;
-    celix_scheduled_event_options_t schedOpts = CELIX_EMPTY_SCHEDULED_EVENT_OPTIONS;
-    schedOpts.name = "Remote Services Endpoint Discovery Poller";
-    schedOpts.callback = endpointDiscoveryPoller_performPeriodicPoll;
-    schedOpts.callbackData = *poller;
-    schedOpts.initialDelayInSeconds = intervalInSeconds;
-    schedOpts.intervalInSeconds = intervalInSeconds;
-    (*poller)->pollEventId = celix_bundleContext_scheduleEvent(context, &schedOpts);
+	(*poller)->running = true;
+
+	status += celixThread_create(&(*poller)->pollerThread, NULL, endpointDiscoveryPoller_performPeriodicPoll, *poller);
 	status += celixThreadMutex_unlock(&(*poller)->pollerLock);
 
 	if(status != CELIX_SUCCESS){
@@ -133,7 +129,9 @@ celix_status_t endpointDiscoveryPoller_create(discovery_t *discovery, celix_bund
 celix_status_t endpointDiscoveryPoller_destroy(endpoint_discovery_poller_t *poller) {
 	celix_status_t status;
 
-    celix_bundleContext_removeScheduledEvent(poller->discovery->context, poller->pollEventId);
+	poller->running = false;
+
+	celixThread_join(poller->pollerThread, NULL);
 
 	hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries);
 	while (hashMapIterator_hasNext(iterator)) {
@@ -292,32 +290,43 @@ celix_status_t endpointDiscoveryPoller_poll(endpoint_discovery_poller_t *poller,
 	return status;
 }
 
-static void endpointDiscoveryPoller_performPeriodicPoll(void *data) {
-    endpoint_discovery_poller_t *poller = (endpoint_discovery_poller_t *) data;
-    celix_status_t status = celixThreadMutex_lock(&poller->pollerLock);
-    if (status != CELIX_SUCCESS) {
-        celix_logHelper_warning(*poller->loghelper, "ENDPOINT_POLLER: failed to obtain lock; retrying...");
-    } else {
-        hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries);
+static void *endpointDiscoveryPoller_performPeriodicPoll(void *data) {
+	endpoint_discovery_poller_t *poller = (endpoint_discovery_poller_t *) data;
+
+	useconds_t interval = (useconds_t) (poller->poll_interval * 1000000L);
+
+	while (poller->running) {
+		usleep(interval);
+		celix_status_t status = celixThreadMutex_lock(&poller->pollerLock);
 
-        while (hashMapIterator_hasNext(iterator)) {
-            hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator);
+		if (status != CELIX_SUCCESS) {
+            celix_logHelper_warning(*poller->loghelper, "ENDPOINT_POLLER: failed to obtain lock; retrying...");
+		} else {
+			hash_map_iterator_pt iterator = hashMapIterator_create(poller->entries);
 
-            char *url = hashMapEntry_getKey(entry);
-            array_list_pt currentEndpoints = hashMapEntry_getValue(entry);
+			while (hashMapIterator_hasNext(iterator)) {
+				hash_map_entry_pt entry = hashMapIterator_nextEntry(iterator);
 
-            endpointDiscoveryPoller_poll(poller, url, currentEndpoints);
-        }
+				char *url = hashMapEntry_getKey(entry);
+				array_list_pt currentEndpoints = hashMapEntry_getValue(entry);
 
-        hashMapIterator_destroy(iterator);
-    }
+				endpointDiscoveryPoller_poll(poller, url, currentEndpoints);
+			}
 
-    status = celixThreadMutex_unlock(&poller->pollerLock);
-    if (status != CELIX_SUCCESS) {
-        celix_logHelper_warning(*poller->loghelper, "ENDPOINT_POLLER: failed to release lock; retrying...");
-    }
+			hashMapIterator_destroy(iterator);
+		}
+
+		status = celixThreadMutex_unlock(&poller->pollerLock);
+		if (status != CELIX_SUCCESS) {
+            celix_logHelper_warning(*poller->loghelper, "ENDPOINT_POLLER: failed to release lock; retrying...");
+		}
+	}
+
+	return NULL;
 }
 
+
+
 struct MemoryStruct {
 	char *memory;
 	size_t size;


[celix] 01/05: Refactor scheduled event to use nextDeadline instead of lastScheduledEventTime

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/scheduled_event_on_event_thread
in repository https://gitbox.apache.org/repos/asf/celix.git

commit fe902d063fc23a76246b9519415511b03f48773e
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon Jul 3 19:55:24 2023 +0200

    Refactor scheduled event to use nextDeadline instead of lastScheduledEventTime
---
 libs/framework/src/celix_scheduled_event.c | 24 +++++++-----------------
 libs/framework/src/celix_scheduled_event.h |  5 ++---
 libs/framework/src/framework.c             | 30 +++++++++++++-----------------
 libs/utils/gtest/src/TimeUtilsTestSuite.cc | 24 ++++++++++++++++++++++++
 libs/utils/include/celix_utils.h           |  8 ++++++++
 libs/utils/src/utils.c                     | 11 +++++++++++
 6 files changed, 65 insertions(+), 37 deletions(-)

diff --git a/libs/framework/src/celix_scheduled_event.c b/libs/framework/src/celix_scheduled_event.c
index ec1d2584..69b651dc 100644
--- a/libs/framework/src/celix_scheduled_event.c
+++ b/libs/framework/src/celix_scheduled_event.c
@@ -72,7 +72,7 @@ struct celix_scheduled_event {
     size_t callCount;           /**< The call count of the scheduled event. */
     bool isMarkedForRemoval;    /**< Whether the scheduled event is marked for removal. */
     bool isRemoved;             /**< Whether the scheduled event is removed. */
-    struct timespec lastScheduledEventTime; /**< The last scheduled event time of the scheduled event. */
+    struct timespec nextDeadline; /**< The next deadline of the scheduled event. */
     bool processForWakeup; /**< Whether the scheduled event should be processed directly due to a wakeupScheduledEvent
                               call. */
 };
@@ -119,7 +119,7 @@ celix_scheduled_event_t* celix_scheduledEvent_create(celix_framework_t* fw,
     event->useCount = 1;
     event->callCount = 0;
     event->isRemoved = false;
-    event->lastScheduledEventTime = celixThreadCondition_getTime();
+    event->nextDeadline = celixThreadCondition_getDelayedTime(event->initialDelayInSeconds);
     event->processForWakeup = false;
 
     celixThreadMutex_create(&event->mutex, NULL);
@@ -177,25 +177,15 @@ long celix_scheduledEvent_getBundleId(const celix_scheduled_event_t* event) { re
 
 bool celix_scheduledEvent_deadlineReached(celix_scheduled_event_t* event,
                                           const struct timespec* currentTime,
-                                          double* nextProcessTimeInSeconds) {
+                                          struct timespec* nextDeadline) {
     celixThreadMutex_lock(&event->mutex);
-    double elapsed = celix_difftime(&event->lastScheduledEventTime, currentTime);
-    double deadline = event->callCount == 0 ? event->initialDelayInSeconds : event->intervalInSeconds;
-    deadline -= CELIX_SCHEDULED_EVENT_INTERVAL_ALLOW_ERROR_IN_SECONDS;
-    bool deadlineReached = elapsed >= deadline;
+    double timeLeft = celix_difftime(currentTime, &event->nextDeadline);
+    bool deadlineReached = timeLeft - CELIX_SCHEDULED_EVENT_INTERVAL_ALLOW_ERROR_IN_SECONDS <= 0;
     if (event->processForWakeup) {
         deadlineReached = true;
     }
-
-    if (deadlineReached && nextProcessTimeInSeconds) {
-        *nextProcessTimeInSeconds =
-            event->intervalInSeconds == 0 /*one shot*/ ? CELIX_FRAMEWORK_DEFAULT_MAX_TIMEDWAIT_EVENT_HANDLER_IN_SECONDS
-                                                       : event->intervalInSeconds;
-    } else if (nextProcessTimeInSeconds) {
-        *nextProcessTimeInSeconds = event->callCount == 0 ? event->initialDelayInSeconds : event->intervalInSeconds;
-    }
     celixThreadMutex_unlock(&event->mutex);
-    return deadlineReached;
+    return deadlineReached || event->processForWakeup;
 }
 
 void celix_scheduledEvent_process(celix_scheduled_event_t* event, const struct timespec* currentTime) {
@@ -211,7 +201,7 @@ void celix_scheduledEvent_process(celix_scheduled_event_t* event, const struct t
     struct timespec end = celix_gettime(CLOCK_MONOTONIC);
 
     celixThreadMutex_lock(&event->mutex);
-    event->lastScheduledEventTime = *currentTime;
+    event->nextDeadline = celix_delayedTimespec(currentTime, event->intervalInSeconds);
     event->callCount += 1;
     event->processForWakeup = false;
     celixThreadCondition_broadcast(&event->cond); // for changed callCount
diff --git a/libs/framework/src/celix_scheduled_event.h b/libs/framework/src/celix_scheduled_event.h
index 0dca61b7..77ff37e6 100644
--- a/libs/framework/src/celix_scheduled_event.h
+++ b/libs/framework/src/celix_scheduled_event.h
@@ -114,13 +114,12 @@ long celix_scheduledEvent_getBundleId(const celix_scheduled_event_t* event);
  * @brief Returns whether the event deadline is reached and the event should be processed.
  * @param[in] event The event to check.
  * @param[in] currentTime The current time.
- * @param[out] nextProcessTimeInSeconds The time in seconds until the next event should be processed.
- *                                      if the deadline is reached, this is the next interval.
+ * @param[out] nextDeadline The next deadline. Can be NULL.
  * @return true if the event deadline is reached and the event should be processed.
  */
 bool celix_scheduledEvent_deadlineReached(celix_scheduled_event_t* event,
                                           const struct timespec* currentTime,
-                                          double* nextProcessTimeInSeconds);
+                                          struct timespec* nextDeadline);
 
 /**
  * @brief Process the event by calling the event callback.
diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c
index 4c5a4fdb..63f2740d 100644
--- a/libs/framework/src/framework.c
+++ b/libs/framework/src/framework.c
@@ -1407,17 +1407,17 @@ static inline void fw_handleEvents(celix_framework_t* framework) {
 /**
  * @brief Process all scheduled events.
  */
-static double celix_framework_processScheduledEvents(celix_framework_t* fw) {
+static struct timespec celix_framework_processScheduledEvents(celix_framework_t* fw) {
     struct timespec ts = celixThreadCondition_getTime();
 
-    double nextClosestScheduledEvent;
+    struct timespec closestDeadline;
     celix_scheduled_event_t* callEvent;
     celix_scheduled_event_t* removeEvent;
     do {
-        nextClosestScheduledEvent = -1; //negative means no event next event
+        closestDeadline.tv_sec = 0;
+        closestDeadline.tv_nsec = 0;
         callEvent = NULL;
         removeEvent = NULL;
-        double nextEvent;
         celixThreadMutex_lock(&fw->dispatcher.mutex);
         CELIX_LONG_HASH_MAP_ITERATE(fw->dispatcher.scheduledEvents, entry) {
             celix_scheduled_event_t* visit = entry.value.ptrValue;
@@ -1427,9 +1427,10 @@ static double celix_framework_processScheduledEvents(celix_framework_t* fw) {
                 break;
             }
 
-            bool call = celix_scheduledEvent_deadlineReached(visit, &ts, &nextEvent);
-            if (nextClosestScheduledEvent < 0 || nextEvent < nextClosestScheduledEvent) {
-                nextClosestScheduledEvent = nextEvent;
+            struct timespec nextDeadline;
+            bool call = celix_scheduledEvent_deadlineReached(visit, &ts, &nextDeadline);
+            if (celix_compareTime(&nextDeadline, &closestDeadline) < 0) {
+                closestDeadline = nextDeadline;
             }
             if (call) {
                 callEvent = visit;
@@ -1458,7 +1459,7 @@ static double celix_framework_processScheduledEvents(celix_framework_t* fw) {
         }
     } while (callEvent || removeEvent);
 
-    return nextClosestScheduledEvent;
+    return closestDeadline;
 }
 
 void celix_framework_cleanupScheduledEvents(celix_framework_t* fw, long bndId) {
@@ -1513,15 +1514,10 @@ static bool requiresScheduledEventsProcessing(celix_framework_t* framework) {
     return eventProcessingRequired;
 }
 
-static void celix_framework_waitForNextEvent(celix_framework_t* fw, double nextScheduledEvent) {
-    if (nextScheduledEvent < 0 || nextScheduledEvent > CELIX_FRAMEWORK_DEFAULT_MAX_TIMEDWAIT_EVENT_HANDLER_IN_SECONDS) {
-        nextScheduledEvent = CELIX_FRAMEWORK_DEFAULT_MAX_TIMEDWAIT_EVENT_HANDLER_IN_SECONDS;
-    }
-    struct timespec absTimeout = celixThreadCondition_getDelayedTime(nextScheduledEvent);
-
+static void celix_framework_waitForNextEvent(celix_framework_t* fw, struct timespec nextDeadline) {
     celixThreadMutex_lock(&fw->dispatcher.mutex);
     if (celix_framework_eventQueueSize(fw) == 0 && !requiresScheduledEventsProcessing(fw) && fw->dispatcher.active) {
-        celixThreadCondition_waitUntil(&fw->dispatcher.cond, &fw->dispatcher.mutex, &absTimeout);
+        celixThreadCondition_waitUntil(&fw->dispatcher.cond, &fw->dispatcher.mutex, &nextDeadline);
         // note failing through to fw_eventDispatcher even if timeout is not reached, the fw_eventDispatcher
         // will call this again after processing the events and scheduled events.
     }
@@ -1537,8 +1533,8 @@ static void *fw_eventDispatcher(void *fw) {
 
     while (active) {
         fw_handleEvents(framework);
-        double nextScheduledEvent = celix_framework_processScheduledEvents(framework);
-        celix_framework_waitForNextEvent(framework, nextScheduledEvent);
+        struct timespec nextDeadline = celix_framework_processScheduledEvents(framework);
+        celix_framework_waitForNextEvent(framework, nextDeadline);
 
         celixThreadMutex_lock(&framework->dispatcher.mutex);
         active = framework->dispatcher.active;
diff --git a/libs/utils/gtest/src/TimeUtilsTestSuite.cc b/libs/utils/gtest/src/TimeUtilsTestSuite.cc
index 2aff69a4..d175d7d6 100644
--- a/libs/utils/gtest/src/TimeUtilsTestSuite.cc
+++ b/libs/utils/gtest/src/TimeUtilsTestSuite.cc
@@ -110,3 +110,27 @@ TEST_F(TimeUtilsTestSuite, DelayedTimespecTest) {
     ASSERT_EQ(delayedTime.tv_sec, expectedTime.tv_sec);
     ASSERT_EQ(delayedTime.tv_nsec, expectedTime.tv_nsec);
 }
+
+TEST_F(TimeUtilsTestSuite, CompareTimeTest) {
+    struct timespec time1 = {0, 500000000};
+    struct timespec time2 = {0, 500000000};
+    ASSERT_EQ(celix_compareTime(&time1, &time2), 0);
+
+    time1 = {0, 500000000};
+    time2 = {0, 600000000};
+    ASSERT_EQ(celix_compareTime(&time1, &time2), -1); //time1 is before time2
+
+    time1 = {0, 600000000};
+    time2 = {0, 500000000};
+    ASSERT_EQ(celix_compareTime(&time1, &time2), 1); //time1 is after time2
+
+    time1 = {1, 500000000};
+    time2 = {0, 500000000};
+    ASSERT_EQ(celix_compareTime(&time1, &time2), 1); //time1 is after time2
+
+    time1 = {0, 500000000};
+    time2 = {1, 500000000};
+    ASSERT_EQ(celix_compareTime(&time1, &time2), -1); //time1 is before time2
+}
+
+
diff --git a/libs/utils/include/celix_utils.h b/libs/utils/include/celix_utils.h
index eab3484a..707637eb 100644
--- a/libs/utils/include/celix_utils.h
+++ b/libs/utils/include/celix_utils.h
@@ -172,6 +172,14 @@ CELIX_UTILS_EXPORT struct timespec celix_delayedTimespec(const struct timespec*
  */
 CELIX_UTILS_EXPORT double celix_elapsedtime(clockid_t clockId, struct timespec startTime);
 
+/**
+ * @brief Compare two time arguments.
+ * @param[in] a The first timespec.
+ * @param[in] b The second timespec.
+ * @return 0 if equal, -1 if a is before b and 1 if a is after b.
+ */
+CELIX_UTILS_EXPORT int celix_compareTime(const struct timespec* a, const struct timespec* b);
+
 /**
  * @brief Creates a hash from a string
  */
diff --git a/libs/utils/src/utils.c b/libs/utils/src/utils.c
index 24371d43..f80b3bd1 100644
--- a/libs/utils/src/utils.c
+++ b/libs/utils/src/utils.c
@@ -295,6 +295,17 @@ double celix_elapsedtime(clockid_t clockId, struct timespec startTime) {
     return celix_difftime(&startTime, &now);
 }
 
+int celix_compareTime(const struct timespec *a, const struct timespec *b) {
+    if (a->tv_sec == b->tv_sec && a->tv_nsec == b->tv_nsec) {
+        return 0;
+    }
+    double diff = celix_difftime(a, b);
+    if (diff < 0) {
+        return 1;
+    }
+    return -1;
+}
+
 char* celix_utils_strdup(const char *str) {
     if (str != NULL) {
         return strndup(str, CELIX_UTILS_MAX_STRLEN);


[celix] 05/05: Mention Network IO in scheduled event documentation

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/scheduled_event_on_event_thread
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 97a92c4218eb536e392ca72a421b44f3d3fac4ef
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon Jul 3 20:09:08 2023 +0200

    Mention Network IO in scheduled event documentation
---
 documents/scheduled_events .md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/documents/scheduled_events .md b/documents/scheduled_events .md
index 8d8fac32..988c7340 100644
--- a/documents/scheduled_events .md	
+++ b/documents/scheduled_events .md	
@@ -28,7 +28,8 @@ Scheduled events will be called repeatedly using a provided interval or once if
 only an initial delay is provided. For the interval time, a monotonic clock is used.
 
 The event callback should be relatively fast, and the scheduled event interval should be relatively high; otherwise, 
-the framework event queue will be blocked, and the framework will not function properly.
+the framework event queue will be blocked, and the framework will not function properly. 
+Network IO should not be done in the event callback, but instead, a separate thread should be used for this.
 
 ## Scheduling an Event
 


[celix] 03/05: Revert scheduled event usage in pstm, because network io is used

Posted by pn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/scheduled_event_on_event_thread
in repository https://gitbox.apache.org/repos/asf/celix.git

commit 55c803e6f49b0451f20c8d7d163caf35959fe119
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon Jul 3 20:03:22 2023 +0200

    Revert scheduled event usage in pstm, because network io is used
---
 .../src/pubsub_topology_manager.c                  | 103 ++++++++++++++-------
 .../src/pubsub_topology_manager.h                  |   9 +-
 2 files changed, 77 insertions(+), 35 deletions(-)

diff --git a/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.c b/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.c
index adb259f7..fc055db0 100644
--- a/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.c
+++ b/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.c
@@ -43,7 +43,7 @@
 #define UUID_STR_LEN    37
 #endif
 
-static void pstm_psaHandlingEvent(void* data);
+static void *pstm_psaHandlingThread(void *data);
 
 celix_status_t pubsub_topologyManager_create(celix_bundle_context_t *context, celix_log_helper_t *logHelper, pubsub_topology_manager_t **out) {
     celix_status_t status = CELIX_SUCCESS;
@@ -64,6 +64,9 @@ celix_status_t pubsub_topologyManager_create(celix_bundle_context_t *context, ce
     status |= celixThreadMutex_create(&manager->topicReceivers.mutex, NULL);
     status |= celixThreadMutex_create(&manager->topicSenders.mutex, NULL);
     status |= celixThreadMutex_create(&manager->psaMetrics.mutex, NULL);
+    status |= celixThreadMutex_create(&manager->psaHandling.mutex, NULL);
+
+    status |= celixThreadCondition_init(&manager->psaHandling.cond, NULL);
 
     manager->discoveredEndpoints.map = hashMap_create(utils_stringHash, NULL, utils_stringEquals, NULL);
     manager->announceEndpointListeners.list = celix_arrayList_create();
@@ -74,30 +77,26 @@ celix_status_t pubsub_topologyManager_create(celix_bundle_context_t *context, ce
 
     manager->loghelper = logHelper;
     manager->verbose = celix_bundleContext_getPropertyAsBool(context, PUBSUB_TOPOLOGY_MANAGER_VERBOSE_KEY, PUBSUB_TOPOLOGY_MANAGER_DEFAULT_VERBOSE);
-    long handlingThreadSleepTime = celix_bundleContext_getPropertyAsLong(context, PUBSUB_TOPOLOGY_MANAGER_HANDLING_THREAD_SLEEPTIME_SECONDS_KEY, PSTM_PSA_HANDLING_SLEEPTIME_IN_SECONDS);
+    unsigned handlingThreadSleepTime = celix_bundleContext_getPropertyAsLong(context, PUBSUB_TOPOLOGY_MANAGER_HANDLING_THREAD_SLEEPTIME_SECONDS_KEY, PSTM_PSA_HANDLING_SLEEPTIME_IN_SECONDS);
     if ( handlingThreadSleepTime >= 0 ) {
         manager->handlingThreadSleepTime = handlingThreadSleepTime * 1000L;
     }
     manager->handlingThreadSleepTime = celix_bundleContext_getPropertyAsLong(context, PUBSUB_TOPOLOGY_MANAGER_HANDLING_THREAD_SLEEPTIME_MS_KEY,  manager->handlingThreadSleepTime);
-
-    double handlingThreadSleepTimeInSeconds = ((double)manager->handlingThreadSleepTime) / 1000.0;
-    celix_scheduled_event_options_t schedOpts = CELIX_EMPTY_SCHEDULED_EVENT_OPTIONS;
-    schedOpts.name = "PubSub TopologyManager PSA Handling";
-    schedOpts.initialDelayInSeconds = handlingThreadSleepTimeInSeconds;
-    schedOpts.intervalInSeconds = handlingThreadSleepTimeInSeconds;
-    schedOpts.callbackData = manager;
-    schedOpts.callback = pstm_psaHandlingEvent;
-    manager->scheduledEventId = celix_bundleContext_scheduleEvent(context, &schedOpts);
+    manager->psaHandling.running = true;
+    celixThread_create(&manager->psaHandling.thread, NULL, pstm_psaHandlingThread, manager);
+    celixThread_setName(&manager->psaHandling.thread, "PubSub TopologyManager");
 
     return status;
 }
 
-void pubsub_topologyManager_destroy(pubsub_topology_manager_t *manager) {
-    if (manager == NULL) {
-        return;
-    }
+celix_status_t pubsub_topologyManager_destroy(pubsub_topology_manager_t *manager) {
+    celix_status_t status = CELIX_SUCCESS;
 
-    celix_bundleContext_removeScheduledEvent(manager->context, manager->scheduledEventId);
+    celixThreadMutex_lock(&manager->psaHandling.mutex);
+    manager->psaHandling.running = false;
+    celixThreadCondition_broadcast(&manager->psaHandling.cond);
+    celixThreadMutex_unlock(&manager->psaHandling.mutex);
+    celixThread_join(manager->psaHandling.thread, NULL);
 
     celixThreadMutex_lock(&manager->pubsubadmins.mutex);
     hashMap_destroy(manager->pubsubadmins.map, false, false);
@@ -172,6 +171,8 @@ void pubsub_topologyManager_destroy(pubsub_topology_manager_t *manager) {
     hashMap_destroy(manager->psaMetrics.map, false, false);
 
     free(manager);
+
+    return status;
 }
 
 void pubsub_topologyManager_psaAdded(void *handle, void *svc, const celix_properties_t *props __attribute__((unused))) {
@@ -217,8 +218,11 @@ void pubsub_topologyManager_psaAdded(void *handle, void *svc, const celix_proper
                       "A new PSA is added after at least one active publisher/provided. \
                 It is preferred that all PSA are started before publiser/subscriber are started!\n\
                 Current topic/sender count is %i", needsRematchCount);
-        celix_bundleContext_wakeupScheduledEvent(manager->context, manager->scheduledEventId);
+        celixThreadMutex_lock(&manager->psaHandling.mutex);
+        celixThreadCondition_broadcast(&manager->psaHandling.cond);
+        celixThreadMutex_unlock(&manager->psaHandling.mutex);
     }
+
 }
 
 void pubsub_topologyManager_psaRemoved(void *handle, void *svc __attribute__((unused)), const celix_properties_t *props) {
@@ -299,7 +303,9 @@ void pubsub_topologyManager_psaRemoved(void *handle, void *svc __attribute__((un
     }
     celix_arrayList_destroy(revokedEndpoints);
 
-    celix_bundleContext_wakeupScheduledEvent(manager->context, manager->scheduledEventId);
+    celixThreadMutex_lock(&manager->psaHandling.mutex);
+    celixThreadCondition_broadcast(&manager->psaHandling.cond);
+    celixThreadMutex_unlock(&manager->psaHandling.mutex);
 }
 
 void pubsub_topologyManager_subscriberAdded(void *handle, void *svc __attribute__((unused)), const celix_properties_t *props, const celix_bundle_t *bnd) {
@@ -342,12 +348,16 @@ void pubsub_topologyManager_subscriberAdded(void *handle, void *svc __attribute_
         entry->subscriberProperties = celix_properties_copy(props);
         hashMap_put(manager->topicReceivers.map, entry->scopeAndTopicKey, entry);
         celix_logHelper_trace(manager->loghelper, "Created new topic receiver entry %s", entry->scopeAndTopicKey);
-
-        //new entry, signal psaHandling event to setup topic receiver
-        celix_bundleContext_wakeupScheduledEvent(manager->context, manager->scheduledEventId);
     }
+    //signal psa handling thread
+    bool triggerCondition = (entry->usageCount == 1);
     celixThreadMutex_unlock(&manager->topicReceivers.mutex);
 
+    if (triggerCondition) {
+        celixThreadMutex_lock(&manager->psaHandling.mutex);
+        celixThreadCondition_broadcast(&manager->psaHandling.cond);
+        celixThreadMutex_unlock(&manager->psaHandling.mutex);
+    }
 }
 
 void pubsub_topologyManager_subscriberRemoved(void *handle, void *svc __attribute__((unused)), const celix_properties_t *props, const celix_bundle_t *bnd) {
@@ -382,6 +392,7 @@ void pubsub_topologyManager_pubsubAnnounceEndpointListenerAdded(void *handle, vo
 
     celix_logHelper_trace(manager->loghelper, "Added EndpointListener");
 
+
     //1) retroactively call announceEndpoint for already existing endpoints (manager->announcedEndpoints)
     //2) Add listener to manager->announceEndpointListeners
 
@@ -440,6 +451,7 @@ void pubsub_topologyManager_publisherTrackerAdded(void *handle, const celix_serv
     //2) update the usage count. if not found
     //3) signal psaHandling thread to find a psa and setup TopicSender
 
+
     char *topicFromFilter = NULL;
     char *scopeFromFilter = NULL;
     pubsub_getPubSubInfoFromFilter(info->filter->filterStr, &scopeFromFilter, &topicFromFilter);
@@ -480,12 +492,17 @@ void pubsub_topologyManager_publisherTrackerAdded(void *handle, const celix_serv
         entry->bndId = info->bundleId;
         hashMap_put(manager->topicSenders.map, entry->scopeAndTopicKey, entry);
         celix_logHelper_trace(manager->loghelper, "Created new topic sender entry %s", entry->scopeAndTopicKey);
-
-        //new entry, so wake up psa handling thread
-        celix_bundleContext_wakeupScheduledEvent(manager->context, manager->scheduledEventId);
     }
+    //new entry -> wakeup psaHandling thread
+    bool triggerCondition = (entry->usageCount == 1);
+
     celixThreadMutex_unlock(&manager->topicSenders.mutex);
 
+    if (triggerCondition) {
+        celixThreadMutex_lock(&manager->psaHandling.mutex);
+        celixThreadCondition_broadcast(&manager->psaHandling.cond);
+        celixThreadMutex_unlock(&manager->psaHandling.mutex);
+    }
 }
 
 void pubsub_topologyManager_publisherTrackerRemoved(void *handle, const celix_service_tracker_info_t *info) {
@@ -535,6 +552,7 @@ celix_status_t pubsub_topologyManager_addDiscoveredEndpoint(void *handle, const
     // 1) See if endpoint is already discovered, if so increase usage count.
     // 1) If not, find matching psa using the matchEndpoint
     // 2) if found call addEndpoint of the matching psa
+    bool triggerCondition = false;
 
     if (manager->verbose) {
         celix_logHelper_trace(manager->loghelper,
@@ -562,10 +580,17 @@ celix_status_t pubsub_topologyManager_addDiscoveredEndpoint(void *handle, const
         celix_logHelper_trace(manager->loghelper, "Created new discovered endpoint entry %s", uuid);
 
         //waking up psa handling thread to select psa
-        celix_bundleContext_wakeupScheduledEvent(manager->context, manager->scheduledEventId);
+        triggerCondition = true;
+
     }
     celixThreadMutex_unlock(&manager->discoveredEndpoints.mutex);
 
+    if (triggerCondition) {
+        celixThreadMutex_lock(&manager->psaHandling.mutex);
+        celixThreadCondition_broadcast(&manager->psaHandling.cond);
+        celixThreadMutex_unlock(&manager->psaHandling.mutex);
+    }
+
     return status;
 }
 
@@ -1102,18 +1127,30 @@ static void pstm_setupTopicReceivers(pubsub_topology_manager_t *manager) {
     celix_arrayList_destroy(setupEntries);
 }
 
-static void pstm_psaHandlingEvent(void* data) {
+static void *pstm_psaHandlingThread(void *data) {
     pubsub_topology_manager_t *manager = data;
 
-    //first teardown -> also if rematch is needed
-    pstm_teardownTopicSenders(manager);
-    pstm_teardownTopicReceivers(manager);
+    celixThreadMutex_lock(&manager->psaHandling.mutex);
+    bool running = manager->psaHandling.running;
+    celixThreadMutex_unlock(&manager->psaHandling.mutex);
+
+    while (running) {
+        //first teardown -> also if rematch is needed
+        pstm_teardownTopicSenders(manager);
+        pstm_teardownTopicReceivers(manager);
 
-    //then see if any topic sender/receiver are needed
-    pstm_setupTopicSenders(manager);
-    pstm_setupTopicReceivers(manager);
+        //then see if any topic sender/receiver are needed
+        pstm_setupTopicSenders(manager);
+        pstm_setupTopicReceivers(manager);
 
-    pstm_findPsaForEndpoints(manager); //trying to find psa and possible set for endpoints with no psa
+        pstm_findPsaForEndpoints(manager); //trying to find psa and possible set for endpoints with no psa
+
+        celixThreadMutex_lock(&manager->psaHandling.mutex);
+        celixThreadCondition_timedwaitRelative(&manager->psaHandling.cond, &manager->psaHandling.mutex, manager->handlingThreadSleepTime  / 1000, (manager->handlingThreadSleepTime  % 1000) * 1000000);
+        running = manager->psaHandling.running;
+        celixThreadMutex_unlock(&manager->psaHandling.mutex);
+    }
+    return NULL;
 }
 
 void pubsub_topologyManager_addMetricsService(void * handle, void *svc, const celix_properties_t *props) {
diff --git a/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.h b/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.h
index e573fa57..93df5144 100644
--- a/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.h
+++ b/bundles/pubsub/pubsub_topology_manager/src/pubsub_topology_manager.h
@@ -67,7 +67,12 @@ typedef struct pubsub_topology_manager {
         hash_map_t *map; //key = svcId, value = pubsub_admin_metrics_service_t*
     } psaMetrics;
 
-    long scheduledEventId;
+    struct {
+        celix_thread_t thread;
+        celix_thread_mutex_t mutex; //protect running and condition
+        celix_thread_cond_t cond;
+        bool running;
+    } psaHandling;
 
     celix_log_helper_t *loghelper;
 
@@ -106,7 +111,7 @@ typedef struct pstm_topic_receiver_or_sender_entry {
 } pstm_topic_receiver_or_sender_entry_t;
 
 celix_status_t pubsub_topologyManager_create(celix_bundle_context_t *context, celix_log_helper_t *logHelper, pubsub_topology_manager_t **manager);
-void pubsub_topologyManager_destroy(pubsub_topology_manager_t *manager);
+celix_status_t pubsub_topologyManager_destroy(pubsub_topology_manager_t *manager);
 
 void pubsub_topologyManager_psaAdded(void *handle, void *svc, const celix_properties_t *props);
 void pubsub_topologyManager_psaRemoved(void *handle, void *svc, const celix_properties_t *props);