From 4f210e15a07a4f00d4321f5fe17d9c0554c1019d Mon Sep 17 00:00:00 2001 From: Reece Date: Fri, 1 Oct 2021 18:22:59 +0100 Subject: [PATCH] [*] Made WELL_NextBytes atomic --- Source/RNG/WELL.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Source/RNG/WELL.cpp b/Source/RNG/WELL.cpp index b0ac7db9..5e238f06 100644 --- a/Source/RNG/WELL.cpp +++ b/Source/RNG/WELL.cpp @@ -58,11 +58,10 @@ WELLRand WELL_SeedRand64(AuUInt64 seed) /** * Generates a pseudo-randomly generated long. */ -AuUInt32 WELL_NextLong(WELLRand *rand) +AuUInt32 WELL_NextLong_Unlocked(WELLRand *rand) { AuUInt32 a, b, c, d, ret; - rand->lock.Lock(); a = rand->state[rand->index]; c = rand->state[(rand->index + 13) & 15]; b = a ^ c ^ (a << 16) ^ (c << 15); @@ -74,6 +73,17 @@ AuUInt32 WELL_NextLong(WELLRand *rand) a = rand->state[rand->index]; rand->state[rand->index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28); ret = rand->state[rand->index]; + return ret; +} + + +/** + * Generates a pseudo-randomly generated long. + */ +AuUInt32 WELL_NextLong(WELLRand *rand) +{ + rand->lock.Lock(); + AuUInt32 ret = WELL_NextLong_Unlocked(rand); rand->lock.Unlock(); return ret; } @@ -85,17 +95,21 @@ void WELL_NextBytes(WELLRand *rand, void *in, AuUInt32 length) i = 0; base = reinterpret_cast(in); + + rand->lock.Lock(); for (; i < length; i += 4) { - AuUInt32 rng = WELL_NextLong(rand); + AuUInt32 rng = WELL_NextLong_Unlocked(rand); std::memcpy(base + i, &rng, 4); } if (i > length) { i -= 4; - AuUInt32 padRng = WELL_NextLong(rand); + AuUInt32 padRng = WELL_NextLong_Unlocked(rand); std::memcpy(base + i, &padRng, length - i); } + + rand->lock.Unlock(); } \ No newline at end of file