sc/source/core/tool/scmatrix.cxx | 45 +++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 22 deletions(-)
New commits: commit f7db0f22dcff0b08acccf326b62d72a12b6e28f6 Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Mon Jan 30 12:12:43 2023 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Mon Jan 30 13:04:04 2023 +0000 fix data-race in ScMatrix when doing threaded load of spreadsheets, we touch these fields from multiple threads Change-Id: Ia9d4dba79cfe7870a3a252a35a23acf71a3971e7 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146346 Reviewed-by: Michael Stahl <michael.st...@allotropia.de> Tested-by: Jenkins diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx index f560e9d92bf4..1f0b6a74d95f 100644 --- a/sc/source/core/tool/scmatrix.cxx +++ b/sc/source/core/tool/scmatrix.cxx @@ -35,6 +35,7 @@ #include <osl/diagnose.h> #include <memory> +#include <mutex> #include <utility> #include <vector> #include <limits> @@ -356,8 +357,8 @@ private: void CalcPosition(SCSIZE nIndex, SCSIZE& rC, SCSIZE& rR) const; }; -static bool bElementsMaxFetched; -static size_t nElementsMax; +static std::once_flag bElementsMaxFetched; +static std::atomic<size_t> nElementsMax; /** The maximum number of elements a matrix or the pool may have at runtime. @@ -3010,30 +3011,30 @@ bool ScMatrix::IsSizeAllocatable( SCSIZE nC, SCSIZE nR ) if (!nC || !nR) return true; - if (!bElementsMaxFetched) - { - const char* pEnv = std::getenv("SC_MAX_MATRIX_ELEMENTS"); - if (pEnv) - { - // Environment specifies the overall elements pool. - nElementsMax = std::atoi(pEnv); - } - else + std::call_once(bElementsMaxFetched, + []() { - // GetElementsMax() uses an (~arbitrary) elements limit. - // The actual allocation depends on the types of individual matrix - // elements and is averaged for type double. + const char* pEnv = std::getenv("SC_MAX_MATRIX_ELEMENTS"); + if (pEnv) + { + // Environment specifies the overall elements pool. + nElementsMax = std::atoi(pEnv); + } + else + { + // GetElementsMax() uses an (~arbitrary) elements limit. + // The actual allocation depends on the types of individual matrix + // elements and is averaged for type double. #if SAL_TYPES_SIZEOFPOINTER < 8 - // Assume 1GB memory could be consumed by matrices. - constexpr size_t nMemMax = 0x40000000; + // Assume 1GB memory could be consumed by matrices. + constexpr size_t nMemMax = 0x40000000; #else - // Assume 6GB memory could be consumed by matrices. - constexpr size_t nMemMax = 0x180000000; + // Assume 6GB memory could be consumed by matrices. + constexpr size_t nMemMax = 0x180000000; #endif - nElementsMax = GetElementsMax( nMemMax); - } - bElementsMaxFetched = true; - } + nElementsMax = GetElementsMax( nMemMax); + } + }); if (nC > (nElementsMax / nR)) {