diff options
author | Hans Goudey <h.goudey@me.com> | 2022-10-13 06:31:02 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-10-13 06:31:50 +0300 |
commit | c67e5628d22f8348492b6205081fe1798d50689c (patch) | |
tree | 01b7b5e46f080f0359c06b389d7d1a544279e325 /source/blender/blenkernel | |
parent | 375dafe3ef40eb61e831902a1ae45ecca555fec6 (diff) |
Cleanup: Use std::mutex for mesh runtime mutexes
Instead of allocating three separate ThreadMutex pointers,
just embed std::mutex into the struct directly.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_bvhutils.h | 10 | ||||
-rw-r--r-- | source/blender/blenkernel/BKE_mesh_types.h | 12 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/DerivedMesh.cc | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/bvhutils.cc | 12 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh_normals.cc | 10 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh_runtime.cc | 38 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh_wrapper.cc | 12 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/object_update.cc | 4 |
8 files changed, 28 insertions, 74 deletions
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 @@ -11,6 +11,10 @@ #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->looptris.array; } - BLI_mutex_unlock(mesh_eval_mutex); - return looptri; } diff --git a/source/blender/blenkernel/intern/mesh_wrapper.cc b/source/blender/blenkernel/intern/mesh_wrapper.cc index db15789e847..61a95fb4d0e 100644 --- a/source/blender/blenkernel/intern/mesh_wrapper.cc +++ b/source/blender/blenkernel/intern/mesh_wrapper.cc @@ -95,11 +95,8 @@ Mesh *BKE_mesh_wrapper_from_editmesh(BMEditMesh *em, void BKE_mesh_wrapper_ensure_mdata(Mesh *me) { - ThreadMutex *mesh_eval_mutex = (ThreadMutex *)me->runtime->eval_mutex; - BLI_mutex_lock(mesh_eval_mutex); - + std::lock_guard lock{me->runtime->eval_mutex}; if (me->runtime->wrapper_type == ME_WRAPPER_TYPE_MDATA) { - BLI_mutex_unlock(mesh_eval_mutex); return; } @@ -149,8 +146,6 @@ void BKE_mesh_wrapper_ensure_mdata(Mesh *me) * the underlying data has been initialized. */ me->runtime->wrapper_type = ME_WRAPPER_TYPE_MDATA; }); - - BLI_mutex_unlock(mesh_eval_mutex); } bool BKE_mesh_wrapper_minmax(const Mesh *me, float min[3], float max[3]) @@ -371,11 +366,9 @@ static Mesh *mesh_wrapper_ensure_subdivision(Mesh *me) Mesh *BKE_mesh_wrapper_ensure_subdivision(Mesh *me) { - ThreadMutex *mesh_eval_mutex = (ThreadMutex *)me->runtime->eval_mutex; - BLI_mutex_lock(mesh_eval_mutex); + std::lock_guard lock{me->runtime->eval_mutex}; if (me->runtime->wrapper_type == ME_WRAPPER_TYPE_SUBD) { - BLI_mutex_unlock(mesh_eval_mutex); return me->runtime->mesh_eval; } @@ -384,7 +377,6 @@ Mesh *BKE_mesh_wrapper_ensure_subdivision(Mesh *me) /* Must isolate multithreaded tasks while holding a mutex lock. */ blender::threading::isolate_task([&]() { result = mesh_wrapper_ensure_subdivision(me); }); - BLI_mutex_unlock(mesh_eval_mutex); return result; } diff --git a/source/blender/blenkernel/intern/object_update.cc b/source/blender/blenkernel/intern/object_update.cc index f6a765e8809..ceef6404bf0 100644 --- a/source/blender/blenkernel/intern/object_update.cc +++ b/source/blender/blenkernel/intern/object_update.cc @@ -373,10 +373,8 @@ void BKE_object_select_update(Depsgraph *depsgraph, Object *object) DEG_debug_print_eval(depsgraph, __func__, object->id.name, object); if (object->type == OB_MESH && !object->runtime.is_data_eval_owned) { Mesh *mesh_input = (Mesh *)object->runtime.data_orig; - blender::bke::MeshRuntime *mesh_runtime = mesh_input->runtime; - BLI_mutex_lock(static_cast<ThreadMutex *>(mesh_runtime->eval_mutex)); + std::lock_guard lock{mesh_input->runtime->eval_mutex}; BKE_object_data_select_update(depsgraph, static_cast<ID *>(object->data)); - BLI_mutex_unlock(static_cast<ThreadMutex *>(mesh_runtime->eval_mutex)); } else { BKE_object_data_select_update(depsgraph, static_cast<ID *>(object->data)); |