From 37ae27d47e148f3b4b7eaaa76ce98df680abcc6f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 11 Jan 2017 19:11:56 -0500 Subject: entity manager lock --- include/engine.hpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/engine.hpp b/include/engine.hpp index 2b03696..a331e50 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -70,6 +70,38 @@ public: } }; +#include +#include + +class LockableEntityManager : public entityx::EntityManager { +private: + std::atomic_bool locked; + +public: + LockableEntityManager(entityx::EventManager& ev) + : EntityManager(ev) { + locked.store(false); + } + + void lock(void) { + while (locked.load()) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + + locked.store(true); + } + + void unlock(void) { + locked.store(false); + } + + bool try_lock(void) { + if (locked.load()) + return false; + + locked.store(true); + return true; + } +}; namespace game { /** @@ -80,8 +112,8 @@ namespace game { /** * Handles entity data. */ - extern entityx::EntityManager entities; - + extern LockableEntityManager entities; + /** * An instance of the main game engine. */ -- cgit v1.2.3