]> granicus.if.org Git - json-c/commitdiff
Check the __GCC_HAVE_SYNC_COMPARE_AND_SWAP_{2,4,8} defines to decide whether to use...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Tue, 7 Jun 2016 03:26:46 +0000 (03:26 +0000)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Tue, 7 Jun 2016 03:26:46 +0000 (03:26 +0000)
Also, fix the types of the variables when building on Windows.
Also, should address issue #214.

linkhash.c

index c89e35b33dd48a21ca4b6e9ccf89bbf2cebbd569..4ea4921bc25e9f1245bc700dfc30088708e0d55d 100644 (file)
@@ -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 */