From 6be74d17df75714066b36cfa6ae40081526ef477 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Fri, 16 Oct 2015 21:12:30 -0400 Subject: [PATCH] Refactor thread utility interface - Add "thr_" prefix to thread utility functions - Compare threadid_ts in a portable manner, where possible --- src/client/tile.cpp | 10 +++---- src/debug.cpp | 13 +++++++-- src/itemdef.cpp | 6 ++-- src/log.cpp | 6 ++-- src/shader.cpp | 8 +++--- src/threads.h | 69 +++++++++++++++++++++++++++++++++++++++------ 6 files changed, 86 insertions(+), 26 deletions(-) diff --git a/src/client/tile.cpp b/src/client/tile.cpp index f918dee29..995526ea8 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -439,7 +439,7 @@ TextureSource::TextureSource(IrrlichtDevice *device): { assert(m_device); // Pre-condition - m_main_thread = get_current_thread_id(); + m_main_thread = thr_get_current_thread_id(); // Add a NULL TextureInfo as the first index, named "" m_textureinfo_cache.push_back(TextureInfo("")); @@ -502,7 +502,7 @@ u32 TextureSource::getTextureId(const std::string &name) /* Get texture */ - if (get_current_thread_id() == m_main_thread) + if (thr_is_current_thread(m_main_thread)) { return generateTexture(name); } @@ -604,7 +604,7 @@ u32 TextureSource::generateTexture(const std::string &name) /* Calling only allowed from main thread */ - if (get_current_thread_id() != m_main_thread) { + if (!thr_is_current_thread(m_main_thread)) { errorstream<<"TextureSource::generateTexture() " "called not from main thread"< g_debug_stacks; Mutex g_debug_stacks_mutex; @@ -158,7 +165,7 @@ void debug_stacks_print() DebugStacker::DebugStacker(const char *text) { - threadid_t threadid = get_current_thread_id(); + threadid_t threadid = thr_get_current_thread_id(); MutexAutoLock lock(g_debug_stacks_mutex); diff --git a/src/itemdef.cpp b/src/itemdef.cpp index 08ff887b2..2971d4e88 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -241,7 +241,7 @@ public: { #ifndef SERVER - m_main_thread = get_current_thread_id(); + m_main_thread = thr_get_current_thread_id(); #endif clear(); } @@ -317,7 +317,7 @@ public: <::const_iterator it; - threadid_t id = get_current_thread_id(); + threadid_t id = thr_get_current_thread_id(); it = m_thread_names.find(id); if (it != m_thread_names.end()) return it->second; diff --git a/src/shader.cpp b/src/shader.cpp index cbde9295e..917d878bb 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -367,7 +367,7 @@ ShaderSource::ShaderSource(IrrlichtDevice *device): m_shader_callback = new ShaderCallback(this, "default"); - m_main_thread = get_current_thread_id(); + m_main_thread = thr_get_current_thread_id(); // Add a dummy ShaderInfo as the first index, named "" m_shaderinfo_cache.push_back(ShaderInfo()); @@ -397,7 +397,7 @@ u32 ShaderSource::getShader(const std::string &name, Get shader */ - if(get_current_thread_id() == m_main_thread){ + if (thr_is_current_thread(m_main_thread)) { return getShaderIdDirect(name, material_type, drawtype); } else { /*errorstream<<"getShader(): Queued: name=\""<= 201103L + #define USE_CPP11_THREADS 1 +#elif defined(_WIN32) + #define USE_WIN_THREADS 1 #else -typedef pthread_t threadid_t; + #define USE_POSIX_THREADS 1 #endif -inline threadid_t get_current_thread_id() +/////////////// + + +#if USE_CPP11_THREADS + #include +#endif + +#include "threading/mutex.h" + +// +// threadid_t, threadhandle_t +// +#if USE_CPP11_THREADS + typedef std::thread::id threadid_t; + typedef std::thread::native_handle_type threadhandle_t; +#elif USE_WIN_THREADS + typedef DWORD threadid_t; + typedef HANDLE threadhandle_t; +#elif USE_POSIX_THREADS + typedef pthread_t threadid_t; + typedef pthread_t threadhandle_t; +#endif + +// +// ThreadStartFunc +// +#if USE_CPP11_THREADS || USE_POSIX_THREADS + typedef void *(ThreadStartFunc)(void *param); +#elif defined(_WIN32_WCE) + typedef DWORD (ThreadStartFunc)(LPVOID param); +#elif defined(_WIN32) + typedef DWORD WINAPI (ThreadStartFunc)(LPVOID param); +#endif + + +inline threadid_t thr_get_current_thread_id() { -#if defined(WIN32) || defined(_WIN32_WCE) +#if USE_CPP11_THREADS + return std::this_thread::get_id(); +#elif USE_WIN_THREADS return GetCurrentThreadId(); -#else +#elif USE_POSIX_THREADS return pthread_self(); #endif } +inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2) +{ +#if USE_POSIX_THREADS + return pthread_equal(thr1, thr2); +#else + return thr1 == thr2; #endif +} +inline bool thr_is_current_thread(threadid_t thr) +{ + return thr_compare_thread_id(thr_get_current_thread_id(), thr); +} + +#endif