Skip to content

Commit

Permalink
renderer: Fix lock of resource reads in render entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Oct 1, 2024
1 parent b1dfe87 commit 03fd671
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libopenage/renderer/stages/world/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void WorldObject::fetch_updates(const time::time_t &time) {
}

// Get data from render entity
auto read_lock = this->render_entity->get_read_lock();
this->ref_id = this->render_entity->get_id();
this->position.sync(this->render_entity->get_position());
this->animation_info.sync(this->render_entity->get_animation_path(),
Expand All @@ -78,6 +79,7 @@ void WorldObject::fetch_updates(const time::time_t &time) {
}),
this->last_update);
this->angle.sync(this->render_entity->get_angle(), this->last_update);
read_lock.unlock();

// Set self to changed so that world renderer can update the renderable
this->changed = true;
Expand Down
4 changes: 4 additions & 0 deletions libopenage/renderer/stages/world/render_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ void WorldRenderEntity::clear_changed_flag() {
this->changed = false;
}

std::shared_lock<std::shared_mutex> WorldRenderEntity::get_read_lock() {
return std::shared_lock{this->mutex};
}

} // namespace openage::renderer::world
8 changes: 8 additions & 0 deletions libopenage/renderer/stages/world/render_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cstdint>
#include <list>
#include <mutex>
#include <shared_mutex>
#include <string>

Expand Down Expand Up @@ -105,6 +106,13 @@ class WorldRenderEntity {
*/
void clear_changed_flag();

/**
* Get a lock for thread-safe reading from the render entity.
*
* @return Lock for the render entity.
*/
std::shared_lock<std::shared_mutex> get_read_lock();

private:
/**
* Flag for determining if the render entity has been updated by the
Expand Down

0 comments on commit 03fd671

Please sign in to comment.