From: Vsevolod Stakhov Date: Thu, 5 Mar 2015 18:16:28 +0000 (+0000) Subject: Add portable get_ticks function. X-Git-Tag: 0.9.0~554 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f2d8ad8d000772fff31a5062513550dce48db149;p=rspamd.git Add portable get_ticks function. --- diff --git a/src/libutil/util.c b/src/libutil/util.c index b2435dc8d..2a64b9c10 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -45,6 +45,10 @@ #include #endif +#ifdef __APPLE__ +#include +#endif + /* Check log messages intensity once per minute */ #define CHECK_TIME 60 /* More than 2 log messages per second */ @@ -2233,6 +2237,23 @@ rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen) return res; } +gdouble +rspamd_get_ticks (void) +{ + gdouble res; + +#ifdef __APPLE__ + res = mach_absolute_time () / 1000000000.; +#else + struct timespec ts; + clock_gettime (CLOCK_MONOTONIC, &ts); + + res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.; +#endif + + return res; +} + /* Required for tweetnacl */ void randombytes (guchar *buf, guint64 len) diff --git a/src/libutil/util.h b/src/libutil/util.h index 9e90b1089..8e3fe090f 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -435,4 +435,10 @@ gchar * rspamd_encode_base32 (const guchar *in, gsize inlen); */ guchar* rspamd_decode_base32 (const gchar *in, gsize inlen, gsize *outlen); +/** + * Portably return the current clock ticks as seconds + * @return + */ +gdouble rspamd_get_ticks (void); + #endif