diff mbox series

[v3,1/3] timeout: add l_timeout_remaining

Message ID 20241031202627.1548592-1-denkenz@gmail.com (mailing list archive)
State New
Headers show
Series [v3,1/3] timeout: add l_timeout_remaining | expand

Checks

Context Check Description
tedd_an/pre-ci_am fail error: patch failed: ell/ell.sym:529 error: ell/ell.sym: patch does not apply error: patch failed: ell/timeout.c:23 error: ell/timeout.c: patch does not apply error: patch failed: ell/timeout.h:34 error: ell/timeout.h: patch does not apply hint: Use 'git am --show-current-patch' to see the failed patch

Commit Message

Denis Kenzior Oct. 31, 2024, 8:26 p.m. UTC
From: James Prestwood <prestwoj@gmail.com>

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 | 29 +++++++++++++++++++++++++++++
 ell/timeout.h |  3 ++-
 3 files changed, 32 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/ell/ell.sym b/ell/ell.sym
index 00123136a527..af7395f3f1ae 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -529,6 +529,7 @@  global:
 	l_timeout_create_ms;
 	l_timeout_modify;
 	l_timeout_modify_ms;
+	l_timeout_remaining;
 	l_timeout_remove;
 	l_timeout_set_callback;
 	/* tls */
diff --git a/ell/timeout.c b/ell/timeout.c
index 4fc21a0854c1..541692ecff93 100644
--- a/ell/timeout.c
+++ b/ell/timeout.c
@@ -23,6 +23,7 @@ 
 #include "timeout.h"
 #include "main-private.h"
 #include "private.h"
+#include "time-private.h"
 
 /**
  * SECTION:timeout
@@ -298,3 +299,31 @@  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_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 = _time_from_timespec(&current.it_value);
+
+	return true;
+}
diff --git a/ell/timeout.h b/ell/timeout.h
index 2db78d8fcd41..c0d463c1d021 100644
--- a/ell/timeout.h
+++ b/ell/timeout.h
@@ -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_remaining(struct l_timeout *timeout,
+				uint64_t *remaining);
 #ifdef __cplusplus
 }
 #endif