diff --git a/Makefile b/Makefile index d5a7137..4a33b99 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ SUBDIR_LIB := libslunkcrypt # --------------------------------------------------------------------------- CONFIG = -LDFLGS = -lpthread +LDFLGS = CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall -pedantic ifneq (,$(firstword $(filter 32 64,$(CPU)))) @@ -76,20 +76,22 @@ else SUFFIX := endif -ifeq ($(THREAD),0) - CFLAGS += -DSLUNKBUILD_NOTHREADS -endif - -ifneq ($(STRIP),0) - LDFLGS += -s +ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE)))) + LDFLGS += -mconsole -municode endif ifeq ($(STATIC),1) LDFLGS += -static endif -ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE)))) - LDFLGS += -mconsole -municode +ifneq ($(STRIP),0) + LDFLGS += -s +endif + +ifeq ($(THREAD),1) + LDFLGS += -lpthread +else + CFLAGS += -DSLUNKBUILD_NOTHREADS endif ifneq (,$(firstword $(filter %-unknown-haiku,$(MACHINE)))) diff --git a/libslunkcrypt/src/junk.c b/libslunkcrypt/src/junk.c index 70064ee..c70586c 100644 --- a/libslunkcrypt/src/junk.c +++ b/libslunkcrypt/src/junk.c @@ -19,12 +19,6 @@ #include #include -/* PThread */ -#if defined(_MSC_VER) && !defined(_DLL) -# define PTW32_STATIC_LIB 1 -#endif -#include - /* Utils */ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? b : a; } @@ -37,7 +31,6 @@ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? # include #else # include -# include #endif /* detect compiler destructor support */ @@ -67,6 +60,34 @@ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? # pragma message("Function explicit_bzero() is *not* available -> using fallback!") #endif +// ========================================================================== +// One-time initialization +// ========================================================================== + +/* atomic memory access */ +#if defined(_WIN32) +# define ATOMIC_EXCHANGE(X,Y) InterlockedExchange((X),(Y)) +# define ATOMIC_COMPARE_EXCHANGE(X,Y,Z) InterlockedCompareExchange((X),(Y),(Z)) +#else +# define ATOMIC_EXCHANGE(X,Y) __sync_lock_test_and_set((X),(Y)) +# define ATOMIC_COMPARE_EXCHANGE(X,Y,Z) __sync_val_compare_and_swap((X),(Z),(Y)) +#endif + +/* execute initialization function once */ +static void initialize_once(volatile long *const once_flag, void (*const init_routine)(void)) +{ + long state; + while ((state = ATOMIC_COMPARE_EXCHANGE(once_flag, -1, 0)) != 0) + { + if (state > 0) + { + return; /*already initialized*/ + } + } + init_routine(); + ATOMIC_EXCHANGE(once_flag, 1); +} + // ========================================================================== // Random bytes // ========================================================================== @@ -74,7 +95,7 @@ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? #define MAX_COUNT 1048576U /* Global state */ -static pthread_once_t s_random_is_initialized = PTHREAD_ONCE_INIT; +static volatile long s_random_is_initialized = 0L; #if defined(_WIN32) typedef BOOLEAN(WINAPI *ptr_genrandom_t)(void *buffer, ULONG length); static HMODULE s_advapi32 = NULL; @@ -140,7 +161,7 @@ init_completed: ; size_t slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length) { size_t offset; - pthread_once(&s_random_is_initialized, init_random_source); + initialize_once(&s_random_is_initialized, init_random_source); #ifdef HAVE_WIN32RTLGENRANDOM if (s_genrandom) {