diff --git a/CMakeLists.txt b/CMakeLists.txt index eee1630..ca01c01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,8 @@ if (NOT MSVC) endif() if (NOT WIN32) check_symbol_exists(clock_gettime "time.h" HAVE_CLOCK_GETTIME) + check_symbol_exists(usleep "unistd.h" HAVE_USLEEP) + check_symbol_exists(nanosleep "time.h" HAVE_NANOSLEEP) endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/utils_config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/utils_config.h") diff --git a/cfileop.h b/cfileop.h index 3435614..6708345 100644 --- a/cfileop.h +++ b/cfileop.h @@ -11,6 +11,8 @@ extern "C" { #include #if _WIN32 #include +#else +#include #endif /** * @brief Check file exists diff --git a/http_client.h b/http_client.h index 1685579..74ef7e2 100644 --- a/http_client.h +++ b/http_client.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "cstr_util.h" #include "utils_config.h" diff --git a/time_util.cpp b/time_util.cpp index b6e41bc..9f10c26 100644 --- a/time_util.cpp +++ b/time_util.cpp @@ -17,6 +17,10 @@ #include "err.h" +#if HAVE_USLEEP +#include +#endif + #if _WIN32 void time_util::time_t_to_file_time(time_t t, LPFILETIME pft) { ULARGE_INTEGER time_value; @@ -64,11 +68,11 @@ time_t time_util::timegm(struct tm* tm) { #endif } -time_t time_util::time_ns() { +size_t time_util::time_ns() { #if _WIN32 FILETIME ft; GetSystemTimeAsFileTime(&ft); - time_t t = ((size_t)ft.dwHighDateTime << 32) | (size_t)ft.dwLowDateTime; + size_t t = ((size_t)ft.dwHighDateTime << 32) | (size_t)ft.dwLowDateTime; return t; #elif HAVE_CLOCK_GETTIME struct timespec ts; @@ -78,3 +82,28 @@ time_t time_util::time_ns() { return time(NULL) * 1000000000LL; #endif } + +size_t time_time_ns() { + return time_util::time_ns(); +} + +int time_util::mssleep(unsigned int ms) { +#if _WIN32 + Sleep(ms); + return 0; +#elif HAVE_USLEEP + return usleep(ms); +#elif HAVE_NANOSLEEP + struct timespec ts; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000; + return nanosleep(&ts, NULL); +#else + printf("mssleep failed: not implemented\n"); + return -1; +#endif +} + +int mssleep(unsigned int ms) { + return time_util::mssleep(ms); +} diff --git a/time_util.h b/time_util.h index dfc5193..541ec30 100644 --- a/time_util.h +++ b/time_util.h @@ -5,6 +5,8 @@ #include #endif +#if __cplusplus + namespace time_util { #if _WIN32 /** @@ -17,6 +19,18 @@ namespace time_util { char* strptime(const char* s, const char* format, struct tm* tm); long get_timezone(); time_t timegm(struct tm* tm); - time_t time_ns(); + size_t time_ns(); + int mssleep(unsigned int ms); +} + +extern "C" { +#endif + +size_t time_time_ns(); +int mssleep(unsigned int ms); + +#if __cplusplus } #endif + +#endif diff --git a/utils_config.h.in b/utils_config.h.in index a29b5b9..25b207c 100644 --- a/utils_config.h.in +++ b/utils_config.h.in @@ -27,3 +27,5 @@ #cmakedefine HAVE_TZSET @HAVE_TZSET@ #cmakedefine HAVE_FPRINTF_S @HAVE_FPRINTF_S@ #cmakedefine HAVE_CLOCK_GETTIME @HAVE_CLOCK_GETTIME@ +#cmakedefine HAVE_USLEEP @HAVE_USLEEP@ +#cmakedefine HAVE_NANOSLEEP @HAVE_NANOSLEEP@