diff options
-rw-r--r-- | gen_inl.c | 1 | ||||
-rw-r--r-- | include/kit.inl.h | 52 |
2 files changed, 45 insertions, 8 deletions
@@ -11,6 +11,7 @@ exit char *HEADERS[] = { "source/kit/types.h", "source/kit/status.h", "source/kit/allocator.h", + "source/kit/time.h", "source/kit/thread_defs.h", "source/kit/thread.h", "source/kit/atomic.h", diff --git a/include/kit.inl.h b/include/kit.inl.h index c1c054c..c50dd61 100644 --- a/include/kit.inl.h +++ b/include/kit.inl.h @@ -83,6 +83,44 @@ kit_allocator_t kit_alloc_default(void); #endif /********************************************************************* * * + * File: source/kit/time.h * + * * + *********************************************************************/ +#ifndef KIT_TIME_H +#define KIT_TIME_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif +#include <time.h> +#ifndef TIME_UTC +# define TIME_UTC 1 +#endif +#ifdef __MINGW32__ +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN 1 +# endif +# include <Windows.h> +# define KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS 116444736000000000ull +# define KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull +static int timespec_get(struct timespec *ts, int base) { + if (ts == NULL || base != TIME_UTC) + return 0; + FILETIME ft; + ULARGE_INTEGER date; + LONGLONG ticks; + GetSystemTimeAsFileTime(&ft); + date.HighPart = ft.dwHighDateTime; + date.LowPart = ft.dwLowDateTime; + ticks = (LONGLONG) (date.QuadPart - + KIT_TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS); + ts->tv_sec = ticks / KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS; + ts->tv_nsec = (ticks % KIT_TIMESPEC_IMPL_TICKS_PER_SECONDS) * 100; + return base; +} +#endif +#endif +/********************************************************************* + * * * File: source/kit/thread_defs.h * * * *********************************************************************/ @@ -338,10 +376,6 @@ uint64_t kit_atomic_fetch_add_explicit_64(uint64_t volatile *var, # ifndef _GNU_SOURCE # define _GNU_SOURCE # endif -# include <time.h> -# ifndef TIME_UTC -# define TIME_UTC 1 -# endif # if !defined(_WIN32) || defined(__CYGWIN__) # include <pthread.h> # endif @@ -2177,7 +2211,8 @@ static void impl_tss_dtor_invoke(void) { int i; for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) { if (impl_tss_dtor_tbl[i].dtor) { - void *val = (void *) (size_t) tss_get(impl_tss_dtor_tbl[i].key); + void *val = (void *) (size_t) TlsGetValue( + impl_tss_dtor_tbl[i].key); if (val) (impl_tss_dtor_tbl[i].dtor)(val); } @@ -3297,14 +3332,15 @@ static void secure_random_fallback(ptrdiff_t size, void *data) { /* Try to get some unpredictable system properties and use them to * seed the pseudo random number generator. */ + static int8_t first_run = 1; static uint64_t n = 0; static uint64_t time_sec = 0; static uint64_t time_nsec = 0; struct timespec t; - ptrdiff_t i; timespec_get(&t, TIME_UTC); kit_mt64_state_t state; - if (time_sec == 0 && time_nsec == 0) { + if (first_run) { + first_run = 0; uint64_t seed[] = { n, get_available_memory(), (uint64_t) t.tv_sec, (uint64_t) t.tv_nsec }; kit_mt64_init_array(&state, sizeof seed / sizeof *seed, seed); @@ -3321,7 +3357,7 @@ static void secure_random_fallback(ptrdiff_t size, void *data) { n = kit_mt64_generate(&state); time_sec = (uint64_t) t.tv_sec; time_nsec = (uint64_t) t.tv_nsec; - for (i = 0; i < size; i++) + for (ptrdiff_t i = 0; i < size; i++) ((uint8_t *) data)[i] = (uint8_t) (kit_mt64_generate(&state) >> 56); #ifndef KIT_DISABLE_SYSTEM_THREADS |