Added a bool option to disable threads in in the configfile regarding resource loading.

This commit is contained in:
William Moberg
2016-01-15 16:18:39 +01:00
parent f5ea4d513c
commit c09fe4ae55
4 changed files with 17 additions and 12 deletions
+8 -8
View File
@@ -65,6 +65,7 @@ private:
ResourceManager(); ResourceManager();
public: public:
static bool UseThreading;
/*static ResourceManager& Instance() /*static ResourceManager& Instance()
{ {
static ResourceManager s; static ResourceManager s;
@@ -94,7 +95,7 @@ public:
@param resourceName Fully qualified name of the resource to load. @param resourceName Fully qualified name of the resource to load.
*/ */
template <typename T, bool async = false> template <typename T, bool async = false>
static T* Load(std::string resourceName, Resource* parent = nullptr); static T* Load(const std::string& resourceName, Resource* parent = nullptr);
/** Reloads an already loaded resource, keeping its resource ID intact. /** Reloads an already loaded resource, keeping its resource ID intact.
@@ -141,9 +142,9 @@ private:
static unsigned int GetNewResourceID(unsigned int typeID); static unsigned int GetNewResourceID(unsigned int typeID);
// Internal: Create a resource and cache it // Internal: Create a resource and cache it
static Resource* createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent); static Resource* createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent);
static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception); static Resource* createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception);
static Resource* cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent); static Resource* cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent);
static bool IsMainThread(); static bool IsMainThread();
}; };
@@ -156,7 +157,7 @@ void ResourceManager::RegisterType(std::string typeName)
} }
template <typename T, bool async> template <typename T, bool async>
static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) static T* ResourceManager::Load(const std::string& resourceName, Resource* parent /* = nullptr */)
{ {
auto resourceTypename = typeid(T).name(); auto resourceTypename = typeid(T).name();
auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); auto iter = m_CompilerTypenameToResourceType.find(resourceTypename);
@@ -176,7 +177,7 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* =
decltype(m_ResourceCache)::iterator it; decltype(m_ResourceCache)::iterator it;
//If a thread has already been launched to load this resource. //If a thread has already been launched to load this resource.
auto tIt = m_LoadingThreads.find(cacheKey); auto tIt = m_LoadingThreads.find(cacheKey);
if (tIt != m_LoadingThreads.end()) { if (UseThreading && tIt != m_LoadingThreads.end()) {
if (async) { if (async) {
//Throw StillLoadingException if the thread is still working. //Throw StillLoadingException if the thread is still working.
if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) {
@@ -209,9 +210,8 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* =
} }
} }
Resource* res = nullptr;
//If resource is not cached.. //If resource is not cached..
if (async) { if (UseThreading && async) {
if (mustNotLoadInThread) { if (mustNotLoadInThread) {
try { try {
return static_cast<T*>(createResourceThrowing(resourceType, resourceName, parent)); return static_cast<T*>(createResourceThrowing(resourceType, resourceName, parent));
+3
View File
@@ -17,3 +17,6 @@ IsServer=false
Name=Bob Name=Bob
Address=127.0.0.1 Address=127.0.0.1
Port=13 Port=13
[Multithreading]
ResourceLoading=true
+4 -3
View File
@@ -9,6 +9,7 @@ std::unordered_map<std::pair<std::string, std::string>, Resource*> ResourceManag
std::unordered_map<std::string, Resource*> ResourceManager::m_ResourceFromName; std::unordered_map<std::string, Resource*> ResourceManager::m_ResourceFromName;
std::unordered_map<Resource*, Resource*> ResourceManager::m_ResourceParents; std::unordered_map<Resource*, Resource*> ResourceManager::m_ResourceParents;
unsigned int ResourceManager::m_CurrentResourceTypeID = 0; unsigned int ResourceManager::m_CurrentResourceTypeID = 0;
bool ResourceManager::UseThreading = false;
std::unordered_map<std::string, unsigned int> ResourceManager::m_ResourceTypeIDs; std::unordered_map<std::string, unsigned int> ResourceManager::m_ResourceTypeIDs;
std::unordered_map<unsigned int, unsigned int> ResourceManager::m_ResourceCount; std::unordered_map<unsigned int, unsigned int> ResourceManager::m_ResourceCount;
FileWatcher ResourceManager::m_FileWatcher; FileWatcher ResourceManager::m_FileWatcher;
@@ -79,7 +80,7 @@ void ResourceManager::Update()
m_FileWatcher.Check(); m_FileWatcher.Check();
} }
Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception) Resource* ResourceManager::createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception)
{ {
auto facIt = m_FactoryFunctions.find(resourceType); auto facIt = m_FactoryFunctions.find(resourceType);
if (facIt == m_FactoryFunctions.end()) { if (facIt == m_FactoryFunctions.end()) {
@@ -102,7 +103,7 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string
} }
Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent) Resource* ResourceManager::createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent)
{ {
std::exception_ptr exception; std::exception_ptr exception;
Resource* res = createResource(resourceType, resourceName, parent, exception); Resource* res = createResource(resourceType, resourceName, parent, exception);
@@ -112,7 +113,7 @@ Resource* ResourceManager::createResourceThrowing(std::string resourceType, std:
return res; return res;
} }
Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent) Resource* ResourceManager::cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent)
{ {
//Lock the mutex immediately, and unlock it when leaving the code block. //Lock the mutex immediately, and unlock it when leaving the code block.
boost::lock_guard<decltype(m_Mutex)> guard(m_Mutex); boost::lock_guard<decltype(m_Mutex)> guard(m_Mutex);
+1
View File
@@ -14,6 +14,7 @@ Game::Game(int argc, char* argv[])
ResourceManager::RegisterType<EntityFile>("EntityFile"); ResourceManager::RegisterType<EntityFile>("EntityFile");
m_Config = ResourceManager::Load<ConfigFile>("Config.ini"); m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
ResourceManager::UseThreading = m_Config->Get<bool>("Multithreading.ResourceLoading", true);
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1)); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
// Create the core event broker // Create the core event broker