Fix race condition invalidating object data's bounding box
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 26 Apr 2017 13:40:32 +0000 (15:40 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 26 Apr 2017 14:00:02 +0000 (16:00 +0200)
source/blender/blenkernel/intern/object.c

index 6e754755cf36c376e8690d3e543279a8f4d4686d..fbca03c1e0724d43eea3d685fe1e3d6b0cb9b633 100644 (file)
 #endif
 
 #include "CCGSubSurf.h"
+#include "atomic_ops.h"
 
 #include "GPU_material.h"
 
@@ -319,19 +320,24 @@ void BKE_object_link_modifiers(struct Object *ob_dst, const struct Object *ob_sr
 /* free data derived from mesh, called when mesh changes or is freed */
 void BKE_object_free_derived_caches(Object *ob)
 {
-       /* also serves as signal to remake texspace */
+       /* Also serves as signal to remake texspace.
+        *
+        * NOTE: This function can be called from threads on different objects
+        * sharing same data datablock. So we need to ensure atomic nature of
+        * data modification here.
+        */
        if (ob->type == OB_MESH) {
                Mesh *me = ob->data;
 
                if (me && me->bb) {
-                       me->bb->flag |= BOUNDBOX_DIRTY;
+                       atomic_fetch_and_or_uint32((uint*)&me->bb->flag, BOUNDBOX_DIRTY);
                }
        }
        else if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
                Curve *cu = ob->data;
 
                if (cu && cu->bb) {
-                       cu->bb->flag |= BOUNDBOX_DIRTY;
+                       atomic_fetch_and_or_uint32((uint*)&cu->bb->flag, BOUNDBOX_DIRTY);
                }
        }