Skip to content

Commit

Permalink
renderer: Fix lock of resource reads in hud render entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Oct 15, 2024
1 parent 8eacc71 commit 51416c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions libopenage/renderer/stages/hud/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion libopenage/renderer/stages/hud/render_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ void DragRenderEntity::update(const coord::input drag_pos,
}

const curve::Continuous<coord::input> &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;
}

Expand Down
9 changes: 8 additions & 1 deletion libopenage/renderer/stages/hud/render_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -37,16 +39,21 @@ 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<coord::input> &get_drag_pos();

/**
* 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:
/**
Expand Down

0 comments on commit 51416c3

Please sign in to comment.