Change the _LOCK macro on Windows and the variable initialization to be more VC compatible

This commit is contained in:
Ondřej Surý 2018-05-30 07:03:35 +02:00
parent 12bdee3aa7
commit 430e8d6858
2 changed files with 18 additions and 14 deletions

View file

@ -92,15 +92,15 @@ isc_random32(void) {
void
isc_random_buf(void *buf, size_t buflen) {
int i;
isc_uint32_t r;
REQUIRE(buf);
REQUIRE(buflen > 0);
RUNTIME_CHECK(isc_once_do(&isc_random_once,
isc_random_initialize) == ISC_R_SUCCESS);
int i;
isc_uint32_t r;
for (i = 0; i + sizeof(r) <= buflen; i += sizeof(r)) {
r = next();
memmove((uint8_t *)buf + i, &r, sizeof(r)); /* Buffers cannot

View file

@ -44,14 +44,17 @@ static volatile HANDLE _mutex = NULL;
* will attempt to allocate a mutex and compare-and-swap it into place as the
* global mutex. On failure to swap in the global mutex, the mutex is closed.
*/
#define _LOCK() { \
if (!_mutex) { \
HANDLE p = CreateMutex(NULL, FALSE, NULL); \
if (InterlockedCompareExchangePointer((void **)&_mutex, (void *)p, NULL)) \
CloseHandle(p); \
} \
WaitForSingleObject(_mutex, INFINITE); \
}
#define _LOCK() \
do { \
if (!_mutex) { \
HANDLE p = CreateMutex(NULL, FALSE, NULL); \
if (InterlockedCompareExchangePointer \
((void **)&_mutex, (void *)p, NULL)) { \
CloseHandle(p); \
} \
} \
WaitForSingleObject(_mutex, INFINITE); \
} while (0)
#define _UNLOCK() ReleaseMutex(_mutex)
@ -75,11 +78,12 @@ static isc_uint32_t seed[4];
static inline isc_uint32_t
next(void) {
isc_uint32_t result_starstar, t;
_LOCK();
const isc_uint32_t result_starstar = rotl(seed[0] * 5, 7) * 9;
const isc_uint32_t t = seed[1] << 9;
result_starstar = rotl(seed[0] * 5, 7) * 9;
t = seed[1] << 9;
seed[2] ^= seed[0];
seed[3] ^= seed[1];