diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-10-10 02:19:37 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-10-10 02:21:35 +0200 |
commit | 2fa2187e68d24648bf77780aaf72c5708681632d (patch) | |
tree | 389155c060a85d9be87f53288ae772957de390f5 /src | |
parent | f2087f2da2752b84e000b42575bcc3abafbf57b4 (diff) | |
download | fastd-2fa2187e68d24648bf77780aaf72c5708681632d.tar fastd-2fa2187e68d24648bf77780aaf72c5708681632d.zip |
time, compat: don't redefine clock_gettime on MacOS
MacOS X 10.12 has intruduced a clock_gettime function. Use an own function
name instead.
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/compat.h | 25 | ||||
-rw-r--r-- | src/fastd.h | 6 | ||||
-rw-r--r-- | src/time.c | 63 |
4 files changed, 66 insertions, 29 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 829b3ab..4990a83 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,6 +41,7 @@ add_executable(fastd socket.c status.c task.c + time.c vector.c verify.c ${BISON_fastd_config_parse_OUTPUTS} diff --git a/src/compat.h b/src/compat.h index 0668ad9..5c71d1e 100644 --- a/src/compat.h +++ b/src/compat.h @@ -85,28 +85,3 @@ static inline char *get_current_dir_name(void) { } #endif - - -#ifdef __APPLE__ - -#include <mach/mach_time.h> - -#define CLOCK_MONOTONIC 0 -#define clockid_t int - -static inline int clock_gettime(clockid_t clk_id __attribute__((unused)), struct timespec *tp) { - static mach_timebase_info_data_t timebase_info = {}; - - if (!timebase_info.denom) - mach_timebase_info(&timebase_info); - - uint64_t time = (((long double)mach_absolute_time())*timebase_info.numer) / timebase_info.denom; - - tp->tv_sec = time / 1000000000; - tp->tv_nsec = time % 1000000000; - - return 0; -} - - -#endif diff --git a/src/fastd.h b/src/fastd.h index 1206cd8..88adbf9 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -379,6 +379,7 @@ void fastd_iface_write(fastd_iface_t *iface, fastd_buffer_t buffer); void fastd_iface_close(fastd_iface_t *iface); void fastd_random_bytes(void *buffer, size_t len, bool secure); +int64_t fastd_get_time(void); #ifdef __ANDROID__ @@ -558,10 +559,7 @@ static inline void fastd_timeout_advance(fastd_timeout_t *a, fastd_timeout_t v) /** Updates the current time */ static inline void fastd_update_time(void) { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - - ctx.now = (1000*(int64_t)ts.tv_sec) + ts.tv_nsec/1000000; + ctx.now = fastd_get_time(); } /** Checks if a on-verify command is set */ diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..964d2f0 --- /dev/null +++ b/src/time.c @@ -0,0 +1,63 @@ +/* + Copyright (c) 2012-2016, Matthias Schiffer <mschiffer@universe-factory.net> + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/** + \file + + Abstractions for monotonic timestamps +*/ + + +#include "fastd.h" + +#include <time.h> + + +#ifdef __APPLE__ + +#include <mach/mach_time.h> + +/** Returns a monotonic timestamp in milliseconds */ +int64_t fastd_get_time(void) { + static mach_timebase_info_data_t timebase_info = {}; + + if (!timebase_info.denom) + mach_timebase_info(&timebase_info); + + int64_t nsecs = (((long double)mach_absolute_time())*timebase_info.numer) / timebase_info.denom; + return nsecs / 1000000; +} + +#else + +/** Returns a monotonic timestamp in milliseconds */ +int64_t fastd_get_time(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + + return (1000*(int64_t)ts.tv_sec) + ts.tv_nsec/1000000; +} + +#endif |