Skip to contentSkip to conversation

timeout: add l_timeout_get_remaining

PATCH In review James Prestwood 2 participants 2 messages Aug 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 timeout: add l_timeout_get_remaining

Gets the remaining microseconds left on a timer. Microseconds were chosen in order to be easily compatible with l_time APIs.

---
 ell/ell.sym   |  1 +
 ell/timeout.c | 30 ++++++++++++++++++++++++++++++
 ell/timeout.h |  3 ++-
 3 files changed, 33 insertions(+), 1 deletion(-)
ell/ell.sym +1 -0
index c7dc9e6..759a84c 100644
@@ -524,6 +524,7 @@ global:
 	/* timeout */
 	l_timeout_create;
 	l_timeout_create_ms;
+	l_timeout_get_remaining;
 	l_timeout_modify;
 	l_timeout_modify_ms;
 	l_timeout_remove;
ell/timeout.c +30 -0
index 4fc21a0..220b2df 100644
@@ -23,6 +23,7 @@
 #include "timeout.h"
 #include "main-private.h"
 #include "private.h"
+#include "time.h"
 
 /**
  * SECTION:timeout
@@ -298,3 +299,32 @@ LIB_EXPORT void l_timeout_set_callback(struct l_timeout *timeout,
 	timeout->user_data = user_data;
 	timeout->destroy = destroy;
 }
+
+/**
+ * l_timeout_get_remaining:
+ *
+ * Get the remaining time for a timeout in microseconds
+ *
+ * @timeout: timeout object
+ * @remaining: microseconds remaining on timer
+ *
+ * Returns: True if successfully got remaining time
+ *          False if failure to get remaining time
+ **/
+LIB_EXPORT bool l_timeout_get_remaining(struct l_timeout *timeout,
+						uint64_t *remaining)
+{
+	struct itimerspec current;
+
+	if (unlikely(!timeout))
+		return false;
+
+	if (timerfd_gettime(timeout->fd, &current) < 0)
+		return false;
+
+	if (remaining)
+		*remaining = current.it_value.tv_sec * L_USEC_PER_SEC +
+				current.it_value.tv_nsec / L_NSEC_PER_USEC;
+
+	return true;
+}
ell/timeout.h +2 -1
index 2db78d8..b9735a4 100644
@@ -34,7 +34,8 @@ void l_timeout_remove(struct l_timeout *timeout);
 void l_timeout_set_callback(struct l_timeout *timeout,
 				l_timeout_notify_cb_t callback, void *user_data,
 				l_timeout_destroy_cb_t destroy);
-
+bool l_timeout_get_remaining(struct l_timeout *timeout,
+				uint64_t *remaining);
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.34.1
#1 lore raw

Hi James,

ell/timeout.c LIB_EXPORT void l_timeout_set_callback(struct l_timeout *timeout, · line 315

Quoted 2 lines
ell/timeout.c +2 -0
+LIB_EXPORT bool l_timeout_get_remaining(struct l_timeout *timeout,
+						uint64_t *remaining)

Lets call this l_timeout_remaining()

ell/timeout.c LIB_EXPORT void l_timeout_set_callback(struct l_timeout *timeout, · line 327

Quoted 12 lines
ell/timeout.c +12 -0
+{
+	struct itimerspec current;
+
+	if (unlikely(!timeout))
+		return false;
+
+	if (timerfd_gettime(timeout->fd, &current) < 0)
+		return false;
+
+	if (remaining)
+		*remaining = current.it_value.tv_sec * L_USEC_PER_SEC +
+				current.it_value.tv_nsec / L_NSEC_PER_USEC;

Can we use _time_from_timespec? Or maybe add that to time-private.h?

ell/timeout.c LIB_EXPORT void l_timeout_set_callback(struct l_timeout *timeout, · line 330

Quoted 3 lines
ell/timeout.c +3 -0
+
+	return true;
+}

Regards,
-Denis