From d41cec35054a73101ce1f5a1a307a47d817ee06b Mon Sep 17 00:00:00 2001 From: heinezen Date: Thu, 3 Oct 2024 14:48:09 +0200 Subject: [PATCH] renderer: Fix lock of resource reads in hud render entity. --- libopenage/renderer/stages/hud/object.cpp | 7 +++++++ libopenage/renderer/stages/hud/render_entity.cpp | 6 +++++- libopenage/renderer/stages/hud/render_entity.h | 9 ++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/libopenage/renderer/stages/hud/object.cpp b/libopenage/renderer/stages/hud/object.cpp index a5ac552a78..6e23b6663a 100644 --- a/libopenage/renderer/stages/hud/object.cpp +++ b/libopenage/renderer/stages/hud/object.cpp @@ -38,8 +38,15 @@ void HudDragObject::fetch_updates(const time::time_t &time) { // Get data from render entity this->drag_start = this->render_entity->get_drag_start(); + + // Thread-safe access to curves needs a lock on the render entity's mutex + auto read_lock = this->render_entity->get_read_lock(); + this->drag_pos.sync(this->render_entity->get_drag_pos() /* , this->last_update */); + // Unlock the render entity mutex + read_lock.unlock(); + // Set self to changed so that world renderer can update the renderable this->changed = true; this->render_entity->clear_changed_flag(); diff --git a/libopenage/renderer/stages/hud/render_entity.cpp b/libopenage/renderer/stages/hud/render_entity.cpp index 5aaea81a84..2ff3f75649 100644 --- a/libopenage/renderer/stages/hud/render_entity.cpp +++ b/libopenage/renderer/stages/hud/render_entity.cpp @@ -24,10 +24,14 @@ void DragRenderEntity::update(const coord::input drag_pos, } const curve::Continuous &DragRenderEntity::get_drag_pos() { + std::shared_lock lock{this->mutex}; + return this->drag_pos; } -const coord::input &DragRenderEntity::get_drag_start() { +const coord::input DragRenderEntity::get_drag_start() { + std::shared_lock lock{this->mutex}; + return this->drag_start; } diff --git a/libopenage/renderer/stages/hud/render_entity.h b/libopenage/renderer/stages/hud/render_entity.h index bfc255814a..9d15204386 100644 --- a/libopenage/renderer/stages/hud/render_entity.h +++ b/libopenage/renderer/stages/hud/render_entity.h @@ -28,6 +28,8 @@ class DragRenderEntity final : public renderer::RenderEntity { * Update the render entity with information from the gamestate * or input system. * + * Updating the render entity with this method is thread-safe. + * * @param drag_pos Position of the dragged corner. * @param time Current simulation time. */ @@ -37,6 +39,9 @@ class DragRenderEntity final : public renderer::RenderEntity { /** * Get the position of the dragged corner. * + * Accessing the drag position curve REQUIRES a read lock on the render entity + * (using \p get_read_lock()) to ensure thread safety. + * * @return Coordinates of the dragged corner. */ const curve::Continuous &get_drag_pos(); @@ -44,9 +49,11 @@ class DragRenderEntity final : public renderer::RenderEntity { /** * Get the position of the start corner. * + * Accessing the drag start is thread-safe. + * * @return Coordinates of the start corner. */ - const coord::input &get_drag_start(); + const coord::input get_drag_start(); private: /**