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 85f48ab168b99263350b827467915738d754b20f Author: Pepijn Noltes <[email protected]> AuthorDate: Thu Jun 15 18:51:37 2023 +0200 Change clock to realtime for celix_cond_waitUntil usage --- libs/framework/gtest/src/ScheduledEventTestSuite.cc | 4 ++-- libs/framework/src/celix_scheduled_event.c | 10 +++++----- libs/utils/gtest/src/ThreadsTestSuite.cc | 4 ++-- libs/utils/include/celix_threads.h | 2 ++ libs/utils/src/utils.c | 8 +++++--- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/libs/framework/gtest/src/ScheduledEventTestSuite.cc b/libs/framework/gtest/src/ScheduledEventTestSuite.cc index 3a833e98..8940c2ff 100644 --- a/libs/framework/gtest/src/ScheduledEventTestSuite.cc +++ b/libs/framework/gtest/src/ScheduledEventTestSuite.cc @@ -559,14 +559,14 @@ TEST_F(ScheduledEventTestSuite, WaitForScheduledEvent) { //And the event is fired EXPECT_EQ(1, count.load()); - //When waiting to short for the event + //When waiting too short for the event status = celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(), eventId, 0.0001); //Then the return status is timeout EXPECT_EQ(CELIX_TIMEOUT, status); //When waiting for the event with a timeout longer than the interval - status = celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(), eventId, 1); + status = celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(), eventId, 0.0012); //Then the return status is success EXPECT_EQ(CELIX_SUCCESS, status); diff --git a/libs/framework/src/celix_scheduled_event.c b/libs/framework/src/celix_scheduled_event.c index 7537a67e..118858d9 100644 --- a/libs/framework/src/celix_scheduled_event.c +++ b/libs/framework/src/celix_scheduled_event.c @@ -240,12 +240,12 @@ celix_status_t celix_scheduledEvent_waitForAtLeastCallCount(celix_scheduled_even double waitTimeInSeconds) { celix_status_t status = CELIX_SUCCESS; if (waitTimeInSeconds > 0) { - struct timespec start = celix_gettime(CLOCK_MONOTONIC); + struct timespec start = celix_gettime(CLOCK_REALTIME); struct timespec absTimeoutTime = celix_addDelayInSecondsToTime(&start, waitTimeInSeconds); celixThreadMutex_lock(&event->mutex); while (event->callCount < targetCallCount) { celixThreadCondition_waitUntil(&event->cond, &event->mutex, &absTimeoutTime); - struct timespec now = celix_gettime(CLOCK_MONOTONIC); + struct timespec now = celix_gettime(CLOCK_REALTIME); if (celix_difftime(&start, &now) > waitTimeInSeconds) { status = CELIX_TIMEOUT; break; @@ -257,7 +257,7 @@ celix_status_t celix_scheduledEvent_waitForAtLeastCallCount(celix_scheduled_even } void celix_scheduledEvent_waitForRemoved(celix_scheduled_event_t* event) { - struct timespec start = celix_gettime(CLOCK_MONOTONIC); + struct timespec start = celix_gettime(CLOCK_REALTIME); struct timespec absLogTimeout = celix_addDelayInSecondsToTime(&start, CELIX_SCHEDULED_EVENT_ERROR_LOG_TIMEOUT_IN_SECONDS); celixThreadMutex_lock(&event->mutex); while (!event->isRemoved) { @@ -269,7 +269,7 @@ void celix_scheduledEvent_waitForRemoved(celix_scheduled_event_t* event) { event->eventName, event->scheduledEventId, event->bndId); - start = celix_gettime(CLOCK_MONOTONIC); + start = celix_gettime(CLOCK_REALTIME); absLogTimeout = celix_addDelayInSecondsToTime(&start, CELIX_SCHEDULED_EVENT_ERROR_LOG_TIMEOUT_IN_SECONDS); } } @@ -280,7 +280,7 @@ celix_status_t celix_scheduledEvent_wait(celix_scheduled_event_t* event, double celix_status_t status = CELIX_SUCCESS; celixThreadMutex_lock(&event->mutex); size_t targetCallCount = event->callCount + 1; - struct timespec start = celix_gettime(CLOCK_MONOTONIC); + struct timespec start = celix_gettime(CLOCK_REALTIME); struct timespec absTimeoutTime = celix_addDelayInSecondsToTime(&start, timeoutInSeconds); while (event->callCount < targetCallCount) { celix_status_t waitStatus = celixThreadCondition_waitUntil(&event->cond, &event->mutex, &absTimeoutTime); diff --git a/libs/utils/gtest/src/ThreadsTestSuite.cc b/libs/utils/gtest/src/ThreadsTestSuite.cc index d23e5260..3d160037 100644 --- a/libs/utils/gtest/src/ThreadsTestSuite.cc +++ b/libs/utils/gtest/src/ThreadsTestSuite.cc @@ -303,13 +303,13 @@ TEST_F(ThreadsTestSuite, CondTimedWaitTest) { ASSERT_EQ(status, CELIX_ILLEGAL_ARGUMENT); //Test with valid abstime - auto start = celix_gettime(CLOCK_MONOTONIC); + auto start = celix_gettime(CLOCK_REALTIME); auto targetEnd = celix_addDelayInSecondsToTime(&start, 0.001); pthread_mutex_lock(&mutex); status = celixThreadCondition_waitUntil(&cond, &mutex, &targetEnd); ASSERT_EQ(status, ETIMEDOUT); pthread_mutex_unlock(&mutex); - auto end = celix_gettime(CLOCK_MONOTONIC); + auto end = celix_gettime(CLOCK_REALTIME); EXPECT_NEAR(celix_difftime(&end, &start), 0.001, 0.01); } diff --git a/libs/utils/include/celix_threads.h b/libs/utils/include/celix_threads.h index 8d8f873a..c6d84543 100644 --- a/libs/utils/include/celix_threads.h +++ b/libs/utils/include/celix_threads.h @@ -157,6 +157,8 @@ CELIX_UTILS_EXPORT celix_status_t celixThreadCondition_waitFor(celix_thread_cond * - CELIX_ILLEGAL_ARGUMENT if the abstime is negative. * - ENOTRECOVERABLE if the state protected by the mutex is not recoverable. * - ETIMEDOUT If the abstime has passed. + * + * Values for abstime can be obtained by adding a delay to the current time obtained using gettimeofday(2) * * @param[in] cond The condition to wait for. * @param[in] mutex The (locked) mutex to use. diff --git a/libs/utils/src/utils.c b/libs/utils/src/utils.c index 1d1269af..499d8ada 100644 --- a/libs/utils/src/utils.c +++ b/libs/utils/src/utils.c @@ -22,6 +22,7 @@ #include <string.h> #include <assert.h> #include <stdarg.h> +#include <math.h> #include "utils.h" #include "celix_utils.h" @@ -267,14 +268,15 @@ struct timespec celix_gettime(clockid_t clockId) { struct timespec celix_addDelayInSecondsToTime(const struct timespec* time, double delayInSeconds) { struct timespec delayedTime; - memset(&delayedTime, 0, sizeof(delayedTime)); - memset(&delayedTime, 0, sizeof(delayedTime)); if (time != NULL) { delayedTime = *time; + } else { + delayedTime.tv_nsec = 0; + delayedTime.tv_sec = 0; } long seconds = (long)delayInSeconds; - long nanoseconds = (delayInSeconds - seconds) * CELIX_NS_IN_SEC; + long nanoseconds = (long)((delayInSeconds - floor(delayInSeconds)) * CELIX_NS_IN_SEC); delayedTime.tv_sec += seconds; delayedTime.tv_nsec += nanoseconds;
