Small improvement in initialize_once() and fixed a Clang warning.

This commit is contained in:
LoRd_MuldeR 2022-09-21 23:42:10 +02:00
parent 4383c5f858
commit c4b869981f
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 22 additions and 7 deletions

View File

@ -8,7 +8,7 @@
/* Intel(R) oneAPI DPC++/C++ Compiler */
#if defined(__INTEL_LLVM_COMPILER) && (!defined(__GNUC__))
# define __GNUC__ 10
# define __GNUC__ 4
#endif
/* Compiler compatibility */

View File

@ -31,6 +31,7 @@ static INLINE size_t MIN_SIZE(const size_t a, const size_t b) { return (a > b) ?
# include <io.h>
#else
# include <unistd.h>
# include <sched.h>
#endif
/* detect compiler destructor support */
@ -60,21 +61,28 @@ 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
/* detect sched_yield() or Sleep() support */
#ifdef _WIN32
# define SCHED_YIELD() Sleep(0)
#else
# define SCHED_YIELD() sched_yield()
#endif
// ==========================================================================
// One-time initialization
// ==========================================================================
/* atomic memory access */
#if defined(_WIN32)
# define ATOMIC_EXCHANGE(X,Y) InterlockedExchange((X),(Y))
#if defined(_MSC_VER) && (!defined(__GNUC__))
# define ATOMIC_COMPARE_EXCHANGE(X,Y,Z) InterlockedCompareExchange((X),(Y),(Z))
# define ATOMIC_EXCHANGE(X,Y) InterlockedExchange((X),(Y))
#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))
# define ATOMIC_EXCHANGE(X,Y) __sync_lock_test_and_set((X),(Y))
#endif
/* execute initialization function once */
static void initialize_once(volatile long *const once_flag, void (*const init_routine)(void))
static INLINE 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)
@ -83,6 +91,7 @@ static void initialize_once(volatile long *const once_flag, void (*const init_ro
{
return; /*already initialized*/
}
SCHED_YIELD();
}
init_routine();
ATOMIC_EXCHANGE(once_flag, 1);
@ -235,7 +244,7 @@ void slunkcrypt_bzero(void* const buffer, const size_t length)
// ==========================================================================
#if defined(HAVE_ATTRIB_DESTRUCTOR)
__attribute__((destructor)) void slunkcrypt_destructor()
__attribute__((destructor)) void slunkcrypt_destructor(void)
{
exit_random_source();
}

View File

@ -3,7 +3,13 @@
/* This work has been released under the CC0 1.0 Universal license! */
/******************************************************************************/
#ifndef SLUNKBUILD_NOTHREADS
#ifdef SLUNKBUILD_NOTHREADS
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wpedantic"
#endif
#else /*SLUNKBUILD_NOTHREADS*/
#ifdef _WIN32
# define _CRT_SECURE_NO_WARNINGS 1