Parcourir la source

Make unload() thread-safe and other fixes

Daniele Bartolini il y a 12 ans
Parent
commit
2ee417b706
2 fichiers modifiés avec 11 ajouts et 13 suppressions
  1. 11 12
      src/ResourceManager.cpp
  2. 0 1
      src/ResourceManager.h

+ 11 - 12
src/ResourceManager.cpp

@@ -48,7 +48,6 @@ ResourceManager::ResourceManager(ResourceArchive& archive, Allocator& allocator)
 	m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
 	m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
 {
 {
 	// FIXME hardcoded seed
 	// FIXME hardcoded seed
-	m_config_hash = hash::murmur2_32("config", 6, 0);
 	m_texture_hash = hash::murmur2_32("tga", 3, 0);
 	m_texture_hash = hash::murmur2_32("tga", 3, 0);
 	m_mesh_hash = hash::murmur2_32("mesh", 4, 0);
 	m_mesh_hash = hash::murmur2_32("mesh", 4, 0);
 	m_txt_hash = hash::murmur2_32("txt", 3, 0);
 	m_txt_hash = hash::murmur2_32("txt", 3, 0);
@@ -81,17 +80,21 @@ void ResourceManager::unload(ResourceId name)
 {
 {
 	assert(has(name));
 	assert(has(name));
 	
 	
+	m_resources_mutex.lock();
+
 	ResourceEntry& entry = m_resources[name.index];
 	ResourceEntry& entry = m_resources[name.index];
 	
 	
 	entry.references--;
 	entry.references--;
 	
 	
 	if (entry.references == 0 && entry.state == RS_LOADED)
 	if (entry.references == 0 && entry.state == RS_LOADED)
 	{
 	{
-		//m_resource_loader.unload(name, entry.resource);
+		unload_by_type(name, entry.resource);
 
 
 		entry.state = RS_UNLOADED;
 		entry.state = RS_UNLOADED;
 		entry.resource = NULL;
 		entry.resource = NULL;
 	}
 	}
+
+	m_resources_mutex.unlock();
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -264,11 +267,7 @@ void ResourceManager::background_load()
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void* ResourceManager::load_by_type(ResourceId name) const
 void* ResourceManager::load_by_type(ResourceId name) const
 {
 {
-	if (name.type == m_config_hash)
-	{
-		return NULL;
-	}
-	else if (name.type == m_texture_hash)
+	if (name.type == m_texture_hash)
 	{
 	{
 		return TextureResource::load(m_resource_allocator, m_resource_archive, name);
 		return TextureResource::load(m_resource_allocator, m_resource_archive, name);
 	}
 	}
@@ -287,11 +286,7 @@ void* ResourceManager::load_by_type(ResourceId name) const
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void ResourceManager::unload_by_type(ResourceId name, void* resource) const
 void ResourceManager::unload_by_type(ResourceId name, void* resource) const
 {
 {
-	if (name.type == m_config_hash)
-	{
-		return;
-	}
-	else if (name.type == m_texture_hash)
+	if (name.type == m_texture_hash)
 	{
 	{
 		TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
 		TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
 	}
 	}
@@ -314,6 +309,10 @@ void ResourceManager::online(ResourceId name, void* resource)
 	{
 	{
 		TextureResource::online((TextureResource*)resource);
 		TextureResource::online((TextureResource*)resource);
 	}
 	}
+	else if (name.type == m_txt_hash)
+	{
+		TextResource::unload(m_resource_allocator, (TextResource*)resource);
+	}
 	else if (name.type == m_script_hash)
 	else if (name.type == m_script_hash)
 	{
 	{
 		ScriptResource::online((ScriptResource*)resource);
 		ScriptResource::online((ScriptResource*)resource);

+ 0 - 1
src/ResourceManager.h

@@ -157,7 +157,6 @@ private:
 private:
 private:
 
 
 	// Hashes of resource types (FIXME)
 	// Hashes of resource types (FIXME)
-	uint32_t			m_config_hash;
 	uint32_t			m_texture_hash;
 	uint32_t			m_texture_hash;
 	uint32_t			m_mesh_hash;
 	uint32_t			m_mesh_hash;
 	uint32_t			m_txt_hash;
 	uint32_t			m_txt_hash;