[Bf-blender-cvs] [c67e5628d22] master: Cleanup: Use std::mutex for mesh runtime mutexes

Hans Goudey noreply at git.blender.org
Thu Oct 13 05:33:31 CEST 2022


Commit: c67e5628d22f8348492b6205081fe1798d50689c
Author: Hans Goudey
Date:   Wed Oct 12 22:31:02 2022 -0500
Branches: master
https://developer.blender.org/rBc67e5628d22f8348492b6205081fe1798d50689c

Cleanup: Use std::mutex for mesh runtime mutexes

Instead of allocating three separate ThreadMutex pointers,
just embed std::mutex into the struct directly.

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

M	source/blender/blenkernel/BKE_bvhutils.h
M	source/blender/blenkernel/BKE_mesh_types.h
M	source/blender/blenkernel/intern/DerivedMesh.cc
M	source/blender/blenkernel/intern/bvhutils.cc
M	source/blender/blenkernel/intern/mesh_normals.cc
M	source/blender/blenkernel/intern/mesh_runtime.cc
M	source/blender/blenkernel/intern/mesh_wrapper.cc
M	source/blender/blenkernel/intern/object_update.cc
M	source/blender/draw/intern/draw_attributes.cc
M	source/blender/draw/intern/draw_attributes.h
M	source/blender/draw/intern/draw_cache_impl_curves.cc
M	source/blender/draw/intern/draw_cache_impl_mesh.cc
M	source/blender/editors/transform/transform_snap_object.cc

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

diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index d22abd235df..a0a6ac58c58 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -10,6 +10,10 @@
 #include "BLI_kdopbvh.h"
 #include "BLI_threads.h"
 
+#ifdef __cplusplus
+#  include <mutex>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -196,6 +200,8 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
                                    BVHCacheType bvh_cache_type,
                                    int tree_type);
 
+#ifdef __cplusplus
+
 /**
  * Builds or queries a BVH-cache for the cache BVH-tree of the request type.
  */
@@ -204,7 +210,9 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data,
                                        int tree_type,
                                        BVHCacheType bvh_cache_type,
                                        struct BVHCache **bvh_cache_p,
-                                       ThreadMutex *mesh_eval_mutex);
+                                       std::mutex *mesh_eval_mutex);
+
+#endif
 
 /**
  * Frees data allocated by a call to `bvhtree_from_editmesh_*`.
diff --git a/source/blender/blenkernel/BKE_mesh_types.h b/source/blender/blenkernel/BKE_mesh_types.h
index 8f3fee70c7f..dacc2188abb 100644
--- a/source/blender/blenkernel/BKE_mesh_types.h
+++ b/source/blender/blenkernel/BKE_mesh_types.h
@@ -9,6 +9,8 @@
 
 #ifdef __cplusplus
 
+#  include <mutex>
+
 #  include "BLI_span.hh"
 
 #  include "DNA_customdata_types.h"
@@ -73,14 +75,14 @@ struct MeshRuntime {
    * This mesh is used as a result of modifier stack evaluation.
    * Since modifier stack evaluation is threaded on object level we need some synchronization. */
   Mesh *mesh_eval = nullptr;
-  void *eval_mutex = nullptr;
+  std::mutex eval_mutex;
 
   /* A separate mutex is needed for normal calculation, because sometimes
    * the normals are needed while #eval_mutex is already locked. */
-  void *normals_mutex = nullptr;
+  std::mutex normals_mutex;
 
   /** Needed to ensure some thread-safety during render data pre-processing. */
-  void *render_mutex = nullptr;
+  std::mutex render_mutex;
 
   /** Lazily initialized SoA data from the #edit_mesh field in #Mesh. */
   EditMeshData *edit_data = nullptr;
@@ -148,9 +150,9 @@ struct MeshRuntime {
    */
   uint32_t *subsurf_face_dot_tags = nullptr;
 
-  MeshRuntime();
+  MeshRuntime() = default;
   /** \warning This does not free all data currently. See #BKE_mesh_runtime_free_data. */
-  ~MeshRuntime();
+  ~MeshRuntime() = default;
 
   MEM_CXX_CLASS_ALLOC_FUNCS("MeshRuntime")
 };
diff --git a/source/blender/blenkernel/intern/DerivedMesh.cc b/source/blender/blenkernel/intern/DerivedMesh.cc
index 9930e5f23fa..11ef2b08df4 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.cc
+++ b/source/blender/blenkernel/intern/DerivedMesh.cc
@@ -1123,8 +1123,7 @@ static void mesh_calc_modifiers(struct Depsgraph *depsgraph,
   else {
     blender::bke::MeshRuntime *runtime = mesh_input->runtime;
     if (runtime->mesh_eval == nullptr) {
-      BLI_assert(runtime->eval_mutex != nullptr);
-      BLI_mutex_lock((ThreadMutex *)runtime->eval_mutex);
+      std::lock_guard lock{mesh_input->runtime->eval_mutex};
       if (runtime->mesh_eval == nullptr) {
         /* Not yet finalized by any instance, do it now
          * Isolate since computing normals is multithreaded and we are holding a lock. */
@@ -1140,7 +1139,6 @@ static void mesh_calc_modifiers(struct Depsgraph *depsgraph,
         /* Already finalized by another instance, reuse. */
         mesh_final = runtime->mesh_eval;
       }
-      BLI_mutex_unlock((ThreadMutex *)runtime->eval_mutex);
     }
     else if (!mesh_has_modifier_final_normals(mesh_input, &final_datamask, runtime->mesh_eval)) {
       /* Modifier stack was (re-)evaluated with a request for additional normals
diff --git a/source/blender/blenkernel/intern/bvhutils.cc b/source/blender/blenkernel/intern/bvhutils.cc
index 184356615d4..afc3e525143 100644
--- a/source/blender/blenkernel/intern/bvhutils.cc
+++ b/source/blender/blenkernel/intern/bvhutils.cc
@@ -51,13 +51,13 @@ struct BVHCache {
  * When the `r_locked` is filled and the tree could not be found the caches mutex will be
  * locked. This mutex can be unlocked by calling `bvhcache_unlock`.
  *
- * When `r_locked` is used the `mesh_eval_mutex` must contain the `Mesh_Runtime.eval_mutex`.
+ * When `r_locked` is used the `mesh_eval_mutex` must contain the `MeshRuntime.eval_mutex`.
  */
 static bool bvhcache_find(BVHCache **bvh_cache_p,
                           BVHCacheType type,
                           BVHTree **r_tree,
                           bool *r_locked,
-                          ThreadMutex *mesh_eval_mutex)
+                          std::mutex *mesh_eval_mutex)
 {
   bool do_lock = r_locked;
   if (r_locked) {
@@ -69,11 +69,10 @@ static bool bvhcache_find(BVHCache **bvh_cache_p,
       return false;
     }
     /* Lazy initialization of the bvh_cache using the `mesh_eval_mutex`. */
-    BLI_mutex_lock(mesh_eval_mutex);
+    std::lock_guard lock{*mesh_eval_mutex};
     if (*bvh_cache_p == nullptr) {
       *bvh_cache_p = bvhcache_init();
     }
-    BLI_mutex_unlock(mesh_eval_mutex);
   }
   BVHCache *bvh_cache = *bvh_cache_p;
 
@@ -1223,7 +1222,6 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
                                    const int tree_type)
 {
   BVHCache **bvh_cache_p = (BVHCache **)&mesh->runtime->bvh_cache;
-  ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime->eval_mutex;
 
   const MLoopTri *looptri = nullptr;
   int looptri_len = 0;
@@ -1248,7 +1246,7 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
 
   bool lock_started = false;
   data->cached = bvhcache_find(
-      bvh_cache_p, bvh_cache_type, &data->tree, &lock_started, mesh_eval_mutex);
+      bvh_cache_p, bvh_cache_type, &data->tree, &lock_started, &mesh->runtime->eval_mutex);
 
   if (data->cached) {
     BLI_assert(lock_started == false);
@@ -1352,7 +1350,7 @@ BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data,
                                        const int tree_type,
                                        const BVHCacheType bvh_cache_type,
                                        BVHCache **bvh_cache_p,
-                                       ThreadMutex *mesh_eval_mutex)
+                                       std::mutex *mesh_eval_mutex)
 {
   bool lock_started = false;
 
diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc
index c2594ea3816..ebb5a72d137 100644
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@ -356,11 +356,9 @@ const float (*BKE_mesh_vertex_normals_ensure(const Mesh *mesh))[3]
     return nullptr;
   }
 
-  ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime->normals_mutex;
-  BLI_mutex_lock(normals_mutex);
+  std::lock_guard lock{mesh->runtime->normals_mutex};
   if (!BKE_mesh_vertex_normals_are_dirty(mesh)) {
     BLI_assert(mesh->runtime->vert_normals != nullptr);
-    BLI_mutex_unlock(normals_mutex);
     return mesh->runtime->vert_normals;
   }
 
@@ -390,7 +388,6 @@ const float (*BKE_mesh_vertex_normals_ensure(const Mesh *mesh))[3]
     BKE_mesh_poly_normals_clear_dirty(&mesh_mutable);
   });
 
-  BLI_mutex_unlock(normals_mutex);
   return vert_normals;
 }
 
@@ -405,11 +402,9 @@ const float (*BKE_mesh_poly_normals_ensure(const Mesh *mesh))[3]
     return nullptr;
   }
 
-  ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime->normals_mutex;
-  BLI_mutex_lock(normals_mutex);
+  std::lock_guard lock{mesh->runtime->normals_mutex};
   if (!BKE_mesh_poly_normals_are_dirty(mesh)) {
     BLI_assert(mesh->runtime->poly_normals != nullptr);
-    BLI_mutex_unlock(normals_mutex);
     return mesh->runtime->poly_normals;
   }
 
@@ -435,7 +430,6 @@ const float (*BKE_mesh_poly_normals_ensure(const Mesh *mesh))[3]
     BKE_mesh_poly_normals_clear_dirty(&mesh_mutable);
   });
 
-  BLI_mutex_unlock(normals_mutex);
   return poly_normals;
 }
 
diff --git a/source/blender/blenkernel/intern/mesh_runtime.cc b/source/blender/blenkernel/intern/mesh_runtime.cc
index 100b4de9045..e90a298ad8d 100644
--- a/source/blender/blenkernel/intern/mesh_runtime.cc
+++ b/source/blender/blenkernel/intern/mesh_runtime.cc
@@ -36,39 +36,6 @@ void BKE_mesh_runtime_free_data(Mesh *mesh)
   BKE_mesh_runtime_clear_cache(mesh);
 }
 
-namespace blender::bke {
-
-MeshRuntime::MeshRuntime()
-{
-  this->eval_mutex = MEM_new<ThreadMutex>("mesh runtime eval_mutex");
-  BLI_mutex_init(static_cast<ThreadMutex *>(this->eval_mutex));
-  this->normals_mutex = MEM_new<ThreadMutex>("mesh runtime normals_mutex");
-  BLI_mutex_init(static_cast<ThreadMutex *>(this->normals_mutex));
-  this->render_mutex = MEM_new<ThreadMutex>("mesh runtime render_mutex");
-  BLI_mutex_init(static_cast<ThreadMutex *>(this->render_mutex));
-}
-
-MeshRuntime::~MeshRuntime()
-{
-  if (this->eval_mutex != nullptr) {
-    BLI_mutex_end(static_cast<ThreadMutex *>(this->eval_mutex));
-    MEM_freeN(this->eval_mutex);
-    this->eval_mutex = nullptr;
-  }
-  if (this->normals_mutex != nullptr) {
-    BLI_mutex_end(static_cast<ThreadMutex *>(this->normals_mutex));
-    MEM_freeN(this->normals_mutex);
-    this->normals_mutex = nullptr;
-  }
-  if (this->render_mutex != nullptr) {
-    BLI_mutex_end(static_cast<ThreadMutex *>(this->render_mutex));
-    MEM_freeN(this->render_mutex);
-    this->render_mutex = nullptr;
-  }
-}
-
-}  // namespace blender::bke
-
 void BKE_mesh_runtime_clear_cache(Mesh *mesh)
 {
   if (mesh->runtime->mesh_eval != nullptr) {
@@ -166,8 +133,7 @@ int BKE_mesh_runtime_looptri_len(const Mesh *mesh)
 
 const MLoopTri *BKE_mesh_runtime_looptri_ensure(const Mesh *mesh)
 {
-  ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime->eval_mutex;
-  BLI_mutex_lock(mesh_eval_mutex);
+  std::lock_guard lock{mesh->runtime->eval_mutex};
 
   MLoopTri *looptri = mesh->runtime->looptris.array;
 
@@ -181,8 +147,6 @@ const MLoopTri *BKE_mesh_runtime_looptri_ensure(const Mesh *mesh)
     looptri = mesh->runtime->

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list