From 6eb69db4ea633e0dce866f14594f78be0f9cd3ce Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 11 Mar 2017 00:23:54 +0800 Subject: [PATCH] Properly clean up when exit --- src/graphics/stk_tex_manager.cpp | 18 ++++++++++++++++-- src/graphics/stk_texture.cpp | 5 +++-- src/graphics/stk_texture.hpp | 2 +- src/graphics/threaded_tex_loader.cpp | 11 ++++++++--- src/graphics/threaded_tex_loader.hpp | 9 ++++----- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/graphics/stk_tex_manager.cpp b/src/graphics/stk_tex_manager.cpp index 7236eebc5..a1e21c329 100644 --- a/src/graphics/stk_tex_manager.cpp +++ b/src/graphics/stk_tex_manager.cpp @@ -71,9 +71,23 @@ STKTexManager::~STKTexManager() #if !(defined(SERVER_ONLY) || defined(USE_GLES2)) if (CVS->supportsThreadedTextureLoading()) { + STKTexture* delete_ttl = new STKTexture((uint8_t*)NULL, "delete_ttl", + 0, false, true); + for (int i = 0; i < m_thread_size; i++) + addThreadedLoadTexture(delete_ttl); + for (int i = 0; i < m_thread_size; i++) + { + if (!m_all_tex_loaders[i]->waitForReadyToDeleted(2.0f)) + { + Log::info("STKTexManager", "ThreadedTexLoader %d not stopping," + "exiting anyway.", i); + } + delete m_all_tex_loaders[i]; + } + delete delete_ttl; + glDeleteBuffers(1, &m_pbo); + pthread_mutex_destroy(&m_threaded_load_textures_mutex); pthread_cond_destroy(&m_cond_request); - for (ThreadedTexLoader* ttl : m_all_tex_loaders) - delete ttl; } #endif } // ~STKTexManager diff --git a/src/graphics/stk_texture.cpp b/src/graphics/stk_texture.cpp index 6ca9528da..65796dca8 100644 --- a/src/graphics/stk_texture.cpp +++ b/src/graphics/stk_texture.cpp @@ -59,7 +59,7 @@ STKTexture::STKTexture(const std::string& path, bool srgb, bool premul_alpha, // ---------------------------------------------------------------------------- STKTexture::STKTexture(uint8_t* data, const std::string& name, size_t size, - bool single_channel) + bool single_channel, bool delete_ttl) : video::ITexture(name.c_str()), m_texture_handle(0), m_srgb(false), m_premul_alpha(false), m_mesh_texture(false), m_single_channel(single_channel), m_material(NULL), @@ -69,7 +69,8 @@ STKTexture::STKTexture(uint8_t* data, const std::string& name, size_t size, m_size.Width = size; m_size.Height = size; m_orig_size = m_size; - reload(false/*no_upload*/, data); + if (!delete_ttl) + reload(false/*no_upload*/, data); } // STKTexture // ---------------------------------------------------------------------------- diff --git a/src/graphics/stk_texture.hpp b/src/graphics/stk_texture.hpp index f099a069b..da8b10f35 100644 --- a/src/graphics/stk_texture.hpp +++ b/src/graphics/stk_texture.hpp @@ -85,7 +85,7 @@ public: bool single_channel = false); // ------------------------------------------------------------------------ STKTexture(uint8_t* data, const std::string& name, size_t size, - bool single_channel = false); + bool single_channel = false, bool delete_ttl = false); // ------------------------------------------------------------------------ STKTexture(video::IImage* img, const std::string& name); // ------------------------------------------------------------------------ diff --git a/src/graphics/threaded_tex_loader.cpp b/src/graphics/threaded_tex_loader.cpp index 4ac3db459..43190c850 100644 --- a/src/graphics/threaded_tex_loader.cpp +++ b/src/graphics/threaded_tex_loader.cpp @@ -29,7 +29,7 @@ void* ThreadedTexLoader::startRoutine(void *obj) ThreadedTexLoader* ttl = (ThreadedTexLoader*)obj; VS::setThreadName((std::string("ThrTexLoader") + StringUtils::toString(ttl->m_pbo_offset / 1024 / 1024)).c_str()); - while (!ttl->m_destroy) + while (true) { pthread_mutex_lock(&ttl->m_mutex); bool finished = ttl->finishedLoading(); @@ -65,6 +65,13 @@ void* ThreadedTexLoader::startRoutine(void *obj) ttl->m_waiting_timeout = 0; } STKTexture* target_tex = ttl->m_stktm->getThreadedLoadTexture(); + if (strcmp(target_tex->getName().getPtr(), "delete_ttl") == 0) + { + ttl->m_stktm->removeThreadedLoadTexture(); + pthread_mutex_unlock(ttl->m_texture_queue_mutex); + ttl->setCanBeDeleted(); + return NULL; + } if (target_tex->getTextureSize() + ttl->m_tex_size_loaded > ttl->m_tex_capacity) { @@ -83,11 +90,9 @@ void* ThreadedTexLoader::startRoutine(void *obj) ttl->m_tex_size_loaded += target_tex->getTextureSize(); ttl->m_completed_textures.push_back(target_tex); } - pthread_exit(NULL); return NULL; } // startRoutine - // ---------------------------------------------------------------------------- void ThreadedTexLoader::handleCompletedTextures() { diff --git a/src/graphics/threaded_tex_loader.hpp b/src/graphics/threaded_tex_loader.hpp index 21dbe08a1..48a0092aa 100644 --- a/src/graphics/threaded_tex_loader.hpp +++ b/src/graphics/threaded_tex_loader.hpp @@ -18,6 +18,7 @@ #ifndef HEADER_THREADED_TEX_LOADER_HPP #define HEADER_THREADED_TEX_LOADER_HPP +#include "utils/can_be_deleted.hpp" #include "utils/no_copy.hpp" #include "utils/types.hpp" @@ -27,7 +28,7 @@ class STKTexture; class STKTexManager; -class ThreadedTexLoader : public NoCopy +class ThreadedTexLoader : public NoCopy, public CanBeDeleted { private: const unsigned m_tex_capacity; @@ -48,7 +49,7 @@ private: pthread_t m_thread; - bool m_finished_loading, m_destroy, m_locked; + bool m_finished_loading, m_locked; std::vector m_completed_textures; @@ -63,8 +64,7 @@ public: m_pbo_ptr(pbo_ptr), m_texture_queue_mutex(mutex), m_cond_request(cond), m_stktm(stktm), m_tex_size_loaded(0), m_waiting_timeout(0), - m_finished_loading(false), m_destroy(false), - m_locked(false) + m_finished_loading(false), m_locked(false) { pthread_mutex_init(&m_mutex, NULL); pthread_create(&m_thread, NULL, &startRoutine, this); @@ -72,7 +72,6 @@ public: // ------------------------------------------------------------------------ ~ThreadedTexLoader() { - m_destroy = true; pthread_mutex_destroy(&m_mutex); pthread_join(m_thread, NULL); }