From: Eric Haszlakiewicz Date: Tue, 7 Jun 2016 03:26:46 +0000 (+0000) Subject: Check the __GCC_HAVE_SYNC_COMPARE_AND_SWAP_{2,4,8} defines to decide whether to use... X-Git-Tag: json-c-0.13-20171207~157 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0539191d187050faad0ed2657cddfd411e4c7900;p=json-c Check the __GCC_HAVE_SYNC_COMPARE_AND_SWAP_{2,4,8} defines to decide whether to use __sync_val_compare_and_swap(), as described at https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html Also, fix the types of the variables when building on Windows. Also, should address issue #214. --- diff --git a/linkhash.c b/linkhash.c index c89e35b..4ea4921 100644 --- a/linkhash.c +++ b/linkhash.c @@ -441,16 +441,30 @@ static unsigned long lh_perllike_str_hash(const void *k) static unsigned long lh_char_hash(const void *k) { - static volatile int random_seed = -1; +#if defined _MSC_VER +#define RANDOM_SEED_TYPE LONG +#else +#define RANDOM_SEED_TYPE int +#endif + static volatile RANDOM_SEED_TYPE random_seed = -1; if (random_seed == -1) { - int seed; + RANDOM_SEED_TYPE seed; /* we can't use -1 as it is the unitialized sentinel */ while ((seed = json_c_get_random_seed()) == -1); -#if defined __GNUC__ +#if SIZEOF_INT == 8 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 +#define USE_SYNC_COMPARE_AND_SWAP 1 +#endif +#if SIZEOF_INT == 4 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 +#define USE_SYNC_COMPARE_AND_SWAP 1 +#endif +#if SIZEOF_INT == 2 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 +#define USE_SYNC_COMPARE_AND_SWAP 1 +#endif +#if defined USE_SYNC_COMPARE_AND_SWAP (void)__sync_val_compare_and_swap(&random_seed, -1, seed); #elif defined _MSC_VER - InterlockedCompareExchange((LONG *)&random_seed, seed, -1); + InterlockedCompareExchange(&random_seed, seed, -1); #else #warning "racy random seed initializtion if used by multiple threads" random_seed = seed; /* potentially racy */