[Bf-blender-cvs] [01a3c6b204b] master: Tweak to fix for thread concurency in looptri generation.

Bastien Montagne noreply at git.blender.org
Mon Sep 25 10:00:40 CEST 2017


Commit: 01a3c6b204bfbe149dc2cc2278dd58f206af7ae2
Author: Bastien Montagne
Date:   Mon Sep 25 09:56:02 2017 +0200
Branches: master
https://developer.blender.org/rB01a3c6b204bfbe149dc2cc2278dd58f206af7ae2

Tweak to fix for thread concurency in looptri generation.

Even if pointer assignment may be atomic, it does not prevent reordering
and other nifty compiler tricks, we need a memory barrier to ensure not
only that transferring pointer from wip array to final one is atomic,
but also that all previous writing to memory are “flushed” to
(visible by) all CPUs...

Thanks @sergey for finding the potential (though quite unlikely) issue.

===================================================================

M	source/blender/blenkernel/intern/cdderivedmesh.c
M	source/blender/blenkernel/intern/editderivedmesh.c
M	source/blender/blenkernel/intern/subsurf_ccg.c

===================================================================

diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c
index 42ca4359a58..4d94ebfed77 100644
--- a/source/blender/blenkernel/intern/cdderivedmesh.c
+++ b/source/blender/blenkernel/intern/cdderivedmesh.c
@@ -34,6 +34,8 @@
  *  \ingroup bke
  */
 
+#include "atomic_ops.h"
+
 #include "BLI_math.h"
 #include "BLI_edgehash.h"
 #include "BLI_utildefines.h"
@@ -1928,7 +1930,8 @@ void CDDM_recalc_looptri(DerivedMesh *dm)
 	        cddm->dm.looptris.array_wip);
 
 	BLI_assert(cddm->dm.looptris.array == NULL);
-	SWAP(MLoopTri *, cddm->dm.looptris.array, cddm->dm.looptris.array_wip);
+	atomic_cas_z((size_t *)&cddm->dm.looptris.array, *(size_t *)&cddm->dm.looptris.array, *(size_t *)&cddm->dm.looptris.array_wip);
+	cddm->dm.looptris.array_wip = NULL;
 }
 
 static void cdDM_free_internal(CDDerivedMesh *cddm)
diff --git a/source/blender/blenkernel/intern/editderivedmesh.c b/source/blender/blenkernel/intern/editderivedmesh.c
index 9f688432988..357420179dd 100644
--- a/source/blender/blenkernel/intern/editderivedmesh.c
+++ b/source/blender/blenkernel/intern/editderivedmesh.c
@@ -41,6 +41,8 @@
  * is likely to be a little slow.
  */
 
+#include "atomic_ops.h"
+
 #include "BLI_math.h"
 #include "BLI_jitter.h"
 #include "BLI_bitmap.h"
@@ -662,7 +664,8 @@ static void emDM_recalcLoopTri(DerivedMesh *dm)
 	}
 
 	BLI_assert(dm->looptris.array == NULL);
-	SWAP(MLoopTri *, dm->looptris.array, dm->looptris.array_wip);
+	atomic_cas_z((size_t *)&dm->looptris.array, *(size_t *)&dm->looptris.array, *(size_t *)&dm->looptris.array_wip);
+	dm->looptris.array_wip = NULL;
 }
 
 static void emDM_foreachMappedVert(
diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c
index 27bbdf228d4..c580c04e0df 100644
--- a/source/blender/blenkernel/intern/subsurf_ccg.c
+++ b/source/blender/blenkernel/intern/subsurf_ccg.c
@@ -42,6 +42,8 @@
 #include <math.h>
 #include <float.h>
 
+#include "atomic_ops.h"
+
 #include "MEM_guardedalloc.h"
 
 #include "DNA_mesh_types.h"
@@ -4505,7 +4507,8 @@ static void ccgDM_recalcLoopTri(DerivedMesh *dm)
 	}
 
 	BLI_assert(dm->looptris.array == NULL);
-	SWAP(MLoopTri *, dm->looptris.array, dm->looptris.array_wip);
+	atomic_cas_z((size_t *)&dm->looptris.array, *(size_t *)&dm->looptris.array, *(size_t *)&dm->looptris.array_wip);
+	dm->looptris.array_wip = NULL;
 }
 
 static void ccgDM_calcNormals(DerivedMesh *dm)



More information about the Bf-blender-cvs mailing list