sc/source/core/tool/scmatrix.cxx |   45 +++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

New commits:
commit 1ce2d11a042448f81e035647a380e46662e9633e
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Mon Jan 30 12:12:43 2023 +0200
Commit:     Michael Stahl <michael.st...@allotropia.de>
CommitDate: Tue Jan 31 09:49:24 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
    (cherry picked from commit f7db0f22dcff0b08acccf326b62d72a12b6e28f6)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146321

diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx
index 3c0aa14966db..7cec2602252d 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>
@@ -343,8 +344,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.
 
@@ -2813,30 +2814,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))
     {

Reply via email to