Properly clean up when exit

This commit is contained in:
Benau 2017-03-11 00:23:54 +08:00
parent 579aa8a9d1
commit 6eb69db4ea
5 changed files with 32 additions and 13 deletions

View File

@ -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

View File

@ -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
// ----------------------------------------------------------------------------

View File

@ -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);
// ------------------------------------------------------------------------

View File

@ -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()
{

View File

@ -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<STKTexture*> 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);
}