[MERGED] libosmocore[master]: core: Add timespec helper macros and make timer_compat.h public

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Tue Feb 27 15:44:29 UTC 2018


Harald Welte has submitted this change and it was merged.

Change subject: core: Add timespec helper macros and make timer_compat.h public
......................................................................


core: Add timespec helper macros and make timer_compat.h public

If a monotonic clock must be used, then the clock_gettime API is used
which uses timespec structures. Linux systems by default don't provide
helpers to calculate time using timespecs, so let's add them here.

Let's also make this header public so these helpers can be used in other
projects using libosmocore (expected user: libosmo-netif).

Change-Id: I45fc993b9bb0a343763238bf463c8640f47b00f1
---
M include/Makefile.am
M include/osmocom/core/timer_compat.h
2 files changed, 45 insertions(+), 1 deletion(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/include/Makefile.am b/include/Makefile.am
index f95d90c..8b05f80 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -47,6 +47,7 @@
                        osmocom/core/strrb.h \
                        osmocom/core/talloc.h \
                        osmocom/core/timer.h \
+                       osmocom/core/timer_compat.h \
                        osmocom/core/utils.h \
                        osmocom/core/write_queue.h \
                        osmocom/crypt/auth.h \
@@ -148,7 +149,6 @@
 endif
 
 noinst_HEADERS = \
-	osmocom/core/timer_compat.h \
 	osmocom/gsm/kasumi.h osmocom/gsm/gea.h
 
 osmocom/core/bit%gen.h: osmocom/core/bitXXgen.h.tpl
diff --git a/include/osmocom/core/timer_compat.h b/include/osmocom/core/timer_compat.h
index 77d4ce0..8fdd0a0 100644
--- a/include/osmocom/core/timer_compat.h
+++ b/include/osmocom/core/timer_compat.h
@@ -71,5 +71,49 @@
   } while (0)
 #endif
 
+/* Convenience macros for operations on timespecs.
+   NOTE: `timercmp' does not work for >= or <=.  */
+
+#ifndef timespecisset
+# define timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
+#endif
+
+#ifndef timespecclear
+# define timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
+#endif
+
+#ifndef timespeccmp
+# define timespeccmp(a, b, CMP) 				              \
+  (((a)->tv_sec == (b)->tv_sec) ? 					      \
+   ((a)->tv_nsec CMP (b)->tv_nsec) : 					      \
+   ((a)->tv_sec CMP (b)->tv_sec))
+#endif
+
+#ifndef timespecadd
+# define timespecadd(a, b, result)					      \
+  do {									      \
+    (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;			      \
+    (result)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec;			      \
+    if ((result)->tv_nsec >= 1000000000)			              \
+      {									      \
+	++(result)->tv_sec;						      \
+	(result)->tv_nsec -= 1000000000;				      \
+      }									      \
+  } while (0)
+#endif
+
+#ifndef timespecsub
+# define timespecsub(a, b, result)					      \
+  do {									      \
+    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;			      \
+    (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec;			      \
+    if ((result)->tv_nsec < 0) {					      \
+      --(result)->tv_sec;						      \
+      (result)->tv_nsec += 1000000000;					      \
+    }									      \
+  } while (0)
+#endif
+
+
 
 /*! @} */

-- 
To view, visit https://gerrit.osmocom.org/6948
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I45fc993b9bb0a343763238bf463c8640f47b00f1
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list