Make memoryPageSize() functions thread-safe.
These used a static for caching the computation, but didn't use a
static initializer. This meant there was a race between the check
for zero and the assignment.
Bug: b/153803432
Change-Id: Id5b91050ced0b8d4811eb32ffed24885816ade7a
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43909
Tested-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
diff --git a/src/Reactor/ExecutableMemory.cpp b/src/Reactor/ExecutableMemory.cpp
index 3fec84d..c3de6c9 100644
--- a/src/Reactor/ExecutableMemory.cpp
+++ b/src/Reactor/ExecutableMemory.cpp
@@ -199,18 +199,15 @@
size_t memoryPageSize()
{
- static int pageSize = 0;
-
- if(pageSize == 0)
- {
+ static int pageSize = [] {
#if defined(_WIN32)
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
- pageSize = systemInfo.dwPageSize;
+ return systemInfo.dwPageSize;
#else
- pageSize = sysconf(_SC_PAGESIZE);
+ return sysconf(_SC_PAGESIZE);
#endif
- }
+ }();
return pageSize;
}
diff --git a/src/System/Memory.cpp b/src/System/Memory.cpp
index 723db3c..158addf 100644
--- a/src/System/Memory.cpp
+++ b/src/System/Memory.cpp
@@ -91,18 +91,15 @@
size_t memoryPageSize()
{
- static int pageSize = 0;
-
- if(pageSize == 0)
- {
+ static int pageSize = [] {
#if defined(_WIN32)
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
- pageSize = systemInfo.dwPageSize;
+ return systemInfo.dwPageSize;
#else
- pageSize = sysconf(_SC_PAGESIZE);
+ return sysconf(_SC_PAGESIZE);
#endif
- }
+ }();
return pageSize;
}