[sr-dev] git:master:d04edc01: core: ut - added ksr_clock_gettime()

Daniel-Constantin Mierla miconda at gmail.com
Wed May 19 08:23:30 CEST 2021


Module: kamailio
Branch: master
Commit: d04edc014e6077e9ffc1f3b8e73f24ab71f45728
URL: https://github.com/kamailio/kamailio/commit/d04edc014e6077e9ffc1f3b8e73f24ab71f45728

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2021-05-19T08:23:16+02:00

core: ut - added ksr_clock_gettime()

- portable version for clock_gettime()

---

Modified: src/core/ut.c
Modified: src/core/ut.h

---

Diff:  https://github.com/kamailio/kamailio/commit/d04edc014e6077e9ffc1f3b8e73f24ab71f45728.diff
Patch: https://github.com/kamailio/kamailio/commit/d04edc014e6077e9ffc1f3b8e73f24ab71f45728.patch

---

diff --git a/src/core/ut.c b/src/core/ut.c
index 08edd2f24e..65143b3e9d 100644
--- a/src/core/ut.c
+++ b/src/core/ut.c
@@ -32,6 +32,11 @@
 #include <sys/utsname.h> /* uname() */
 #include <libgen.h>
 
+#ifdef __OS_darwin
+/* portable clock_gettime() */
+#include <mach/clock.h>
+#include <mach/mach.h>
+#endif
 
 #include "ut.h"
 #include "mem/mem.h"
@@ -150,6 +155,27 @@ time_t utc2local(time_t in)
 }
 
 
+/**
+ * portable implementation for clock_gettime(CLOCK_REALTIME, ts)
+ */
+int ksr_clock_gettime(struct timespec *ts)
+{
+#ifdef __OS_darwin
+	clock_serv_t cclock;
+	mach_timespec_t mts;
+
+	/* OS X does not have clock_gettime, use clock_get_time */
+	host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
+	clock_get_time(cclock, &mts);
+	mach_port_deallocate(mach_task_self(), cclock);
+	ts->tv_sec = mts.tv_sec;
+	ts->tv_nsec = mts.tv_nsec;
+	return 0;
+#else
+	return clock_gettime(CLOCK_REALTIME, ts);
+#endif
+}
+
 /*
  * Return str as zero terminated string allocated
  * using pkg_malloc
diff --git a/src/core/ut.h b/src/core/ut.h
index 61ff696e13..f2173d3dcf 100644
--- a/src/core/ut.h
+++ b/src/core/ut.h
@@ -1065,6 +1065,9 @@ time_t local2utc(time_t in);
 /* Convert time_t value in UTC to to value relative to local time zone */
 time_t utc2local(time_t in);
 
+/* Portable clock_gettime() */
+int ksr_clock_gettime(struct timespec *ts);
+
 /*
  * Return str as zero terminated string allocated
  * using pkg_malloc




More information about the sr-dev mailing list