Fixed building the library *without* pthread support.
This commit is contained in:
parent
cc4b9c8af3
commit
4383c5f858
20
Makefile
20
Makefile
@ -26,7 +26,7 @@ SUBDIR_LIB := libslunkcrypt
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
CONFIG =
|
CONFIG =
|
||||||
LDFLGS = -lpthread
|
LDFLGS =
|
||||||
CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall -pedantic
|
CFLAGS = -I$(SUBDIR_LIB)/include -std=gnu99 -Wall -pedantic
|
||||||
|
|
||||||
ifneq (,$(firstword $(filter 32 64,$(CPU))))
|
ifneq (,$(firstword $(filter 32 64,$(CPU))))
|
||||||
@ -76,20 +76,22 @@ else
|
|||||||
SUFFIX :=
|
SUFFIX :=
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(THREAD),0)
|
ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))))
|
||||||
CFLAGS += -DSLUNKBUILD_NOTHREADS
|
LDFLGS += -mconsole -municode
|
||||||
endif
|
|
||||||
|
|
||||||
ifneq ($(STRIP),0)
|
|
||||||
LDFLGS += -s
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(STATIC),1)
|
ifeq ($(STATIC),1)
|
||||||
LDFLGS += -static
|
LDFLGS += -static
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifneq (,$(firstword $(filter %-w64-mingw32 %w64-windows-gnu,$(MACHINE))))
|
ifneq ($(STRIP),0)
|
||||||
LDFLGS += -mconsole -municode
|
LDFLGS += -s
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(THREAD),1)
|
||||||
|
LDFLGS += -lpthread
|
||||||
|
else
|
||||||
|
CFLAGS += -DSLUNKBUILD_NOTHREADS
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifneq (,$(firstword $(filter %-unknown-haiku,$(MACHINE))))
|
ifneq (,$(firstword $(filter %-unknown-haiku,$(MACHINE))))
|
||||||
|
@ -19,12 +19,6 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
/* PThread */
|
|
||||||
#if defined(_MSC_VER) && !defined(_DLL)
|
|
||||||
# define PTW32_STATIC_LIB 1
|
|
||||||
#endif
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
/* Utils */
|
/* Utils */
|
||||||
static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ? b : a; }
|
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 <io.h>
|
# include <io.h>
|
||||||
#else
|
#else
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <pthread.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* detect compiler destructor support */
|
/* 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!")
|
# pragma message("Function explicit_bzero() is *not* available -> using fallback!")
|
||||||
#endif
|
#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
|
// 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
|
#define MAX_COUNT 1048576U
|
||||||
|
|
||||||
/* Global state */
|
/* Global state */
|
||||||
static pthread_once_t s_random_is_initialized = PTHREAD_ONCE_INIT;
|
static volatile long s_random_is_initialized = 0L;
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
typedef BOOLEAN(WINAPI *ptr_genrandom_t)(void *buffer, ULONG length);
|
typedef BOOLEAN(WINAPI *ptr_genrandom_t)(void *buffer, ULONG length);
|
||||||
static HMODULE s_advapi32 = NULL;
|
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 slunkcrypt_random_bytes(uint8_t *const buffer, const size_t length)
|
||||||
{
|
{
|
||||||
size_t offset;
|
size_t offset;
|
||||||
pthread_once(&s_random_is_initialized, init_random_source);
|
initialize_once(&s_random_is_initialized, init_random_source);
|
||||||
#ifdef HAVE_WIN32RTLGENRANDOM
|
#ifdef HAVE_WIN32RTLGENRANDOM
|
||||||
if (s_genrandom)
|
if (s_genrandom)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user