From 127ee7a6ca35363f092d6f17c4b14144c04d1bbe Mon Sep 17 00:00:00 2001 From: Faerbit Date: Tue, 24 Mar 2015 20:37:56 +0100 Subject: [PATCH] Adding mutex to level. --- game/level.cc | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++ game/level.hh | 5 ++ 2 files changed, 135 insertions(+) diff --git a/game/level.cc b/game/level.cc index ceb4dfb..86aa9eb 100644 --- a/game/level.cc +++ b/game/level.cc @@ -10,6 +10,136 @@ Level::Level(std::string xmlFilePath) { this->xmlFilePath = xmlFilePath; } +Level::Level(const Level &&other) { + std::lock_guard lock(other.light_mutex); + luaState = std::move(other.luaState); + crossChunkObjects = std::move(other.crossChunkObjects); + sortedCrossChunkObjects = std::move(other.sortedCrossChunkObjects); + allObjects = std::move(other.allObjects); + physicsObjects = std::move(other.physicsObjects); + chunks = std::move(other.chunks); + lights = std::move(other.lights); + closestLights = std::move(other.closestLights); + triggers = std::move(other.triggers); + waterPlane = std::move(other.waterPlane); + ambientLight = std::move(other.ambientLight); + fogColourDay = std::move(other.fogColourDay); + fogColourRise = std::move(other.fogColourRise); + fogColourNight = std::move(other.fogColourNight); + directionalLight = std::move(other.directionalLight); + cameraCenter = std::move(other.cameraCenter); + playerIndex = std::move(other.playerIndex); + skydome = std::move(other.skydome); + physics = std::move(other.physics); + camera = std::move(other.camera); + terrain = std::move(other.terrain); + skydomeSize = std::move(other.skydomeSize); + strength = std::move(other.strength); + xmlFilePath = std::move(other.xmlFilePath); + nextLightPosition = std::move(other.nextLightPosition); + chunkSize = std::move(other.chunkSize); + farPlane = std::move(other.farPlane); +} + +Level::Level(const Level &other) { + std::lock_guard lock(other.light_mutex); + luaState = other.luaState; + crossChunkObjects = other.crossChunkObjects; + sortedCrossChunkObjects = other.sortedCrossChunkObjects; + allObjects = other.allObjects; + physicsObjects = other.physicsObjects; + chunks = other.chunks; + lights = other.lights; + closestLights = other.closestLights; + triggers = other.triggers; + waterPlane = other.waterPlane; + ambientLight = other.ambientLight; + fogColourDay = other.fogColourDay; + fogColourRise = other.fogColourRise; + fogColourNight = other.fogColourNight; + directionalLight = other.directionalLight; + cameraCenter = other.cameraCenter; + playerIndex = other.playerIndex; + skydome = other.skydome; + physics = other.physics; + camera = other.camera; + terrain = other.terrain; + skydomeSize = other.skydomeSize; + strength = other.strength; + xmlFilePath = other.xmlFilePath; + nextLightPosition = other.nextLightPosition; + chunkSize = other.chunkSize; + farPlane = other.farPlane; +} + +Level& Level::operator= (const Level &other) { + std::lock(light_mutex, other.light_mutex); + std::lock_guard self_lock(light_mutex, std::adopt_lock); + std::lock_guard other_lock(other.light_mutex, std::adopt_lock); + luaState = other.luaState; + crossChunkObjects = other.crossChunkObjects; + sortedCrossChunkObjects = other.sortedCrossChunkObjects; + allObjects = other.allObjects; + physicsObjects = other.physicsObjects; + chunks = other.chunks; + lights = other.lights; + closestLights = other.closestLights; + triggers = other.triggers; + waterPlane = other.waterPlane; + ambientLight = other.ambientLight; + fogColourDay = other.fogColourDay; + fogColourRise = other.fogColourRise; + fogColourNight = other.fogColourNight; + directionalLight = other.directionalLight; + cameraCenter = other.cameraCenter; + playerIndex = other.playerIndex; + skydome = other.skydome; + physics = other.physics; + camera = other.camera; + terrain = other.terrain; + skydomeSize = other.skydomeSize; + strength = other.strength; + xmlFilePath = other.xmlFilePath; + nextLightPosition = other.nextLightPosition; + chunkSize = other.chunkSize; + farPlane = other.farPlane; + return *this; +} + +Level& Level::operator= (const Level &&other) { + std::lock(light_mutex, other.light_mutex); + std::lock_guard self_lock(light_mutex, std::adopt_lock); + std::lock_guard other_lock(other.light_mutex, std::adopt_lock); + luaState = std::move(other.luaState); + crossChunkObjects = std::move(other.crossChunkObjects); + sortedCrossChunkObjects = std::move(other.sortedCrossChunkObjects); + allObjects = std::move(other.allObjects); + physicsObjects = std::move(other.physicsObjects); + chunks = std::move(other.chunks); + lights = std::move(other.lights); + closestLights = std::move(other.closestLights); + triggers = std::move(other.triggers); + waterPlane = std::move(other.waterPlane); + ambientLight = std::move(other.ambientLight); + fogColourDay = std::move(other.fogColourDay); + fogColourRise = std::move(other.fogColourRise); + fogColourNight = std::move(other.fogColourNight); + directionalLight = std::move(other.directionalLight); + cameraCenter = std::move(other.cameraCenter); + playerIndex = std::move(other.playerIndex); + skydome = std::move(other.skydome); + physics = std::move(other.physics); + camera = std::move(other.camera); + terrain = std::move(other.terrain); + skydomeSize = std::move(other.skydomeSize); + strength = std::move(other.strength); + xmlFilePath = std::move(other.xmlFilePath); + nextLightPosition = std::move(other.nextLightPosition); + chunkSize = std::move(other.chunkSize); + farPlane = std::move(other.farPlane); + return *this; +} + Level::Level() { } diff --git a/game/level.hh b/game/level.hh index b28a24b..bc3e060 100644 --- a/game/level.hh +++ b/game/level.hh @@ -27,6 +27,10 @@ class Graphics; class Level { public: Level(std::string xmlFilePath); + Level(const Level &other); + Level(const Level &&other); + Level& operator=(const Level &other); + Level& operator=(const Level &&other); Level(); ~Level(); void load(); @@ -114,6 +118,7 @@ class Level { int chunkSize; float farPlane; bool compareLightDistances(Light* a, Light* b); + mutable std::mutex light_mutex; }; #endif