Skip to content

Commit 51416c3

Browse files
committed
renderer: Fix lock of resource reads in hud render entity.
1 parent 8eacc71 commit 51416c3

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

libopenage/renderer/stages/hud/object.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ void HudDragObject::fetch_updates(const time::time_t &time) {
3838

3939
// Get data from render entity
4040
this->drag_start = this->render_entity->get_drag_start();
41+
42+
// Thread-safe access to curves needs a lock on the render entity's mutex
43+
auto read_lock = this->render_entity->get_read_lock();
44+
4145
this->drag_pos.sync(this->render_entity->get_drag_pos() /* , this->last_update */);
4246

47+
// Unlock the render entity mutex
48+
read_lock.unlock();
49+
4350
// Set self to changed so that world renderer can update the renderable
4451
this->changed = true;
4552
this->render_entity->clear_changed_flag();

libopenage/renderer/stages/hud/render_entity.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ void DragRenderEntity::update(const coord::input drag_pos,
2424
}
2525

2626
const curve::Continuous<coord::input> &DragRenderEntity::get_drag_pos() {
27+
std::shared_lock lock{this->mutex};
28+
2729
return this->drag_pos;
2830
}
2931

30-
const coord::input &DragRenderEntity::get_drag_start() {
32+
const coord::input DragRenderEntity::get_drag_start() {
33+
std::shared_lock lock{this->mutex};
34+
3135
return this->drag_start;
3236
}
3337

libopenage/renderer/stages/hud/render_entity.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class DragRenderEntity final : public renderer::RenderEntity {
2828
* Update the render entity with information from the gamestate
2929
* or input system.
3030
*
31+
* Updating the render entity with this method is thread-safe.
32+
*
3133
* @param drag_pos Position of the dragged corner.
3234
* @param time Current simulation time.
3335
*/
@@ -37,16 +39,21 @@ class DragRenderEntity final : public renderer::RenderEntity {
3739
/**
3840
* Get the position of the dragged corner.
3941
*
42+
* Accessing the drag position curve REQUIRES a read lock on the render entity
43+
* (using \p get_read_lock()) to ensure thread safety.
44+
*
4045
* @return Coordinates of the dragged corner.
4146
*/
4247
const curve::Continuous<coord::input> &get_drag_pos();
4348

4449
/**
4550
* Get the position of the start corner.
4651
*
52+
* Accessing the drag start is thread-safe.
53+
*
4754
* @return Coordinates of the start corner.
4855
*/
49-
const coord::input &get_drag_start();
56+
const coord::input get_drag_start();
5057

5158
private:
5259
/**

0 commit comments

Comments
 (0)