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 b5bea53aa53766264cbb942657d956737ccfee5c
Author: Pepijn Noltes <[email protected]>
AuthorDate: Sat Jun 17 13:50:51 2023 +0200

    Add celix_threadCondition_getTime for cond timeout usage
---
 libs/utils/include/celix_threads.h | 23 +++++++++++++++++++++++
 libs/utils/src/celix_threads.c     | 31 ++++++++++++++++++++-----------
 2 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/libs/utils/include/celix_threads.h 
b/libs/utils/include/celix_threads.h
index c6d84543..72dbcfa2 100644
--- a/libs/utils/include/celix_threads.h
+++ b/libs/utils/include/celix_threads.h
@@ -149,6 +149,29 @@ CELIX_UTILS_EXPORT celix_status_t 
celixThreadCondition_waitFor(celix_thread_cond
                                                                
celix_thread_mutex_t* mutex, 
                                                                double 
delayInSeconds);
 
+/**
+ * @brief Returns the current time.
+ *
+ * The returned timespec can be used in celixThreadCondition_waitUntil and
+ * will use a different clock depending on the OS (e.g. CLOCK_MONOTONIC or 
CLOCK_REALTIME).
+ * The returned time is not meant to be used in logging the current time.
+ *
+ * @return The current time.
+ */
+CELIX_UTILS_EXPORT struct timespec celix_threadCondition_getTime();
+
+/**
+ * @brief Returns the current time plus the given delayInSeconds.
+ *
+ * The returned timespec can be used in celixThreadCondition_waitUntil and
+ * will use a different clock depending on the OS (e.g. CLOCK_MONOTONIC or 
CLOCK_REALTIME).
+ * The returned time is not meant to be used in logging the current time.
+ *
+ * @param delayInSeconds The delay in seconds to add to the current time.
+ * @return The current time plus the given delayInSeconds.
+ */
+CELIX_UTILS_EXPORT struct timespec celix_threadCondition_getDelayedTime(double 
delayInSeconds);
+
 /**
  * @brief Wait for the condition to be signaled or until the given absolute 
time is reached.
  * 
diff --git a/libs/utils/src/celix_threads.c b/libs/utils/src/celix_threads.c
index f06775d9..bd540818 100644
--- a/libs/utils/src/celix_threads.c
+++ b/libs/utils/src/celix_threads.c
@@ -182,17 +182,7 @@ celix_status_t 
celixThreadCondition_timedwaitRelative(celix_thread_cond_t *cond,
 }
 #else
 celix_status_t celixThreadCondition_timedwaitRelative(celix_thread_cond_t 
*cond, celix_thread_mutex_t *mutex, long seconds, long nanoseconds) {
-    struct timespec time;
-    seconds = seconds >= 0 ? seconds : 0;
-    time = celix_gettime(CLOCK_MONOTONIC);
-    time.tv_sec += seconds;
-    if (nanoseconds > 0) {
-        time.tv_nsec += nanoseconds;
-        while (time.tv_nsec > CELIX_NS_IN_SEC) {
-            time.tv_sec++;
-            time.tv_nsec -= CELIX_NS_IN_SEC;
-        }
-    }
+    struct timespec time = celix_threadCondition_getTime();
     return pthread_cond_timedwait(cond, mutex, &time);
 }
 #endif
@@ -208,6 +198,25 @@ CELIX_UTILS_EXPORT celix_status_t 
celixThreadCondition_waitFor(celix_thread_cond
     return celixThreadCondition_waitUntil(cond, mutex, &abstime);
 }
 
+struct timespec celix_threadCondition_getTime() {
+    return celix_threadCondition_getDelayedTime(0);
+}
+
+struct timespec celix_threadCondition_getDelayedTime(double delayInSeconds) {
+#if __APPLE__
+    struct timeval tv;
+    struct timespec now;
+    gettimeofday(&tv, NULL);
+    TIMEVAL_TO_TIMESPEC(&tv, &now);
+#else
+    struct timespec new = celix_gettime(CLOCK_MONOTONIC);
+#endif
+    if (delayInSeconds == 0) {
+        return now;
+    }
+    return celix_delayedTimespec(&now, delayInSeconds);
+}
+
 CELIX_UTILS_EXPORT celix_status_t 
celixThreadCondition_waitUntil(celix_thread_cond_t* cond, 
                                                                  
celix_thread_mutex_t* mutex, 
                                                                  const struct 
timespec* abstime) {

Reply via email to