aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2017-01-11 19:11:56 -0500
committerClyne Sullivan <tullivan99@gmail.com>2017-01-11 19:11:56 -0500
commit37ae27d47e148f3b4b7eaaa76ce98df680abcc6f (patch)
tree37616a5acfb08c77ee8f1ae7b803f3293d6df5fb /include
parent76a8dfce1c91c8536c940b53883eaf0ed7bd769a (diff)
entity manager lock
Diffstat (limited to 'include')
-rw-r--r--include/engine.hpp36
1 files changed, 34 insertions, 2 deletions
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 <atomic>
+#include <chrono>
+
+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.
*/