diff --git a/src/client/tile.cpp b/src/client/tile.cpp index 10a9d5f0d..e47a40ea1 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -386,7 +386,7 @@ public: private: // The id of the thread that is allowed to use irrlicht directly - threadid_t m_main_thread; + std::thread::id m_main_thread; // The irrlicht device IrrlichtDevice *m_device; @@ -445,7 +445,7 @@ TextureSource::TextureSource(IrrlichtDevice *device): { assert(m_device); // Pre-condition - m_main_thread = thr_get_current_thread_id(); + m_main_thread = std::this_thread::get_id(); // Add a NULL TextureInfo as the first index, named "" m_textureinfo_cache.push_back(TextureInfo("")); @@ -508,7 +508,7 @@ u32 TextureSource::getTextureId(const std::string &name) /* Get texture */ - if (thr_is_current_thread(m_main_thread)) + if (std::this_thread::get_id() == m_main_thread) { return generateTexture(name); } @@ -616,7 +616,7 @@ u32 TextureSource::generateTexture(const std::string &name) /* Calling only allowed from main thread */ - if (!thr_is_current_thread(m_main_thread)) { + if (std::this_thread::get_id() != m_main_thread) { errorstream<<"TextureSource::generateTexture() " "called not from main thread"< #include -#include "threads.h" #include #include #include "util/numeric.h" diff --git a/src/debug.cpp b/src/debug.cpp index 0490fcf4e..86b727812 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -21,12 +21,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "debug.h" #include "exceptions.h" -#include "threads.h" #include #include #include #include #include +#include #include "threading/mutex_auto_lock.h" #include "config.h" @@ -52,7 +52,7 @@ void sanity_check_fn(const char *assertion, const char *file, #endif errorstream << std::endl << "In thread " << std::hex - << thr_get_current_thread_id() << ":" << std::endl; + << std::this_thread::get_id() << ":" << std::endl; errorstream << file << ":" << line << ": " << function << ": An engine assumption '" << assertion << "' failed." << std::endl; @@ -69,7 +69,7 @@ void fatal_error_fn(const char *msg, const char *file, #endif errorstream << std::endl << "In thread " << std::hex - << thr_get_current_thread_id() << ":" << std::endl; + << std::this_thread::get_id() << ":" << std::endl; errorstream << file << ":" << line << ": " << function << ": A fatal error occured: " << msg << std::endl; @@ -84,19 +84,19 @@ void fatal_error_fn(const char *msg, const char *file, struct DebugStack { - DebugStack(threadid_t id); + DebugStack(std::thread::id id); void print(FILE *file, bool everything); void print(std::ostream &os, bool everything); - threadid_t threadid; + std::thread::id thread_id; char stack[DEBUG_STACK_SIZE][DEBUG_STACK_TEXT_SIZE]; int stack_i; // Points to the lowest empty position int stack_max_i; // Highest i that was seen }; -DebugStack::DebugStack(threadid_t id) +DebugStack::DebugStack(std::thread::id id) { - threadid = id; + thread_id = id; stack_i = 0; stack_max_i = 0; memset(stack, 0, DEBUG_STACK_SIZE*DEBUG_STACK_TEXT_SIZE); @@ -105,52 +105,43 @@ DebugStack::DebugStack(threadid_t id) void DebugStack::print(FILE *file, bool everything) { std::ostringstream os; - os << threadid; + os << thread_id; fprintf(file, "DEBUG STACK FOR THREAD %s:\n", os.str().c_str()); - for(int i=0; i g_debug_stacks; +std::map g_debug_stacks; std::mutex g_debug_stacks_mutex; void debug_stacks_init() @@ -163,11 +154,8 @@ void debug_stacks_print_to(std::ostream &os) os<<"Debug stacks:"<::iterator - i = g_debug_stacks.begin(); - i != g_debug_stacks.end(); ++i) - { - i->second->print(os, false); + for (auto it : g_debug_stacks) { + it.second->print(os, false); } } @@ -178,36 +166,29 @@ void debug_stacks_print() DebugStacker::DebugStacker(const char *text) { - threadid_t threadid = thr_get_current_thread_id(); + std::thread::id thread_id = std::this_thread::get_id(); MutexAutoLock lock(g_debug_stacks_mutex); - std::map::iterator n; - n = g_debug_stacks.find(threadid); - if(n != g_debug_stacks.end()) - { + auto n = g_debug_stacks.find(thread_id); + if (n != g_debug_stacks.end()) { m_stack = n->second; - } - else - { + } else { /*DEBUGPRINT("Creating new debug stack for thread %x\n", - (unsigned int)threadid);*/ - m_stack = new DebugStack(threadid); - g_debug_stacks[threadid] = m_stack; + (unsigned int)thread_id);*/ + m_stack = new DebugStack(thread_id); + g_debug_stacks[thread_id] = m_stack; } - if(m_stack->stack_i >= DEBUG_STACK_SIZE) - { + if (m_stack->stack_i >= DEBUG_STACK_SIZE) { m_overflowed = true; - } - else - { + } else { m_overflowed = false; snprintf(m_stack->stack[m_stack->stack_i], DEBUG_STACK_TEXT_SIZE, "%s", text); m_stack->stack_i++; - if(m_stack->stack_i > m_stack->stack_max_i) + if (m_stack->stack_i > m_stack->stack_max_i) m_stack->stack_max_i = m_stack->stack_i; } } @@ -216,18 +197,17 @@ DebugStacker::~DebugStacker() { MutexAutoLock lock(g_debug_stacks_mutex); - if(m_overflowed == true) + if (m_overflowed == true) return; m_stack->stack_i--; - if(m_stack->stack_i == 0) - { - threadid_t threadid = m_stack->threadid; + if (m_stack->stack_i == 0) { + std::thread::id thread_id = m_stack->thread_id; /*DEBUGPRINT("Deleting debug stack for thread %x\n", - (unsigned int)threadid);*/ + (unsigned int)thread_id);*/ delete m_stack; - g_debug_stacks.erase(threadid); + g_debug_stacks.erase(thread_id); } } diff --git a/src/itemdef.cpp b/src/itemdef.cpp index 53b6e04cd..f1cc03c4b 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -245,7 +245,7 @@ public: { #ifndef SERVER - m_main_thread = thr_get_current_thread_id(); + m_main_thread = std::this_thread::get_id(); #endif clear(); } @@ -320,7 +320,7 @@ public: < result_queue; @@ -580,7 +577,7 @@ private: StringMap m_aliases; #ifndef SERVER // The id of the thread that is allowed to use irrlicht directly - threadid_t m_main_thread; + std::thread::id m_main_thread; // A reference to this can be returned when nothing is found, to avoid NULLs mutable ClientCached m_dummy_clientcached; // Cached textures and meshes diff --git a/src/log.cpp b/src/log.cpp index 589cfd909..28118066f 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -223,14 +223,14 @@ void Logger::setLevelSilenced(LogLevel lev, bool silenced) void Logger::registerThread(const std::string &name) { - threadid_t id = thr_get_current_thread_id(); + std::thread::id id = std::this_thread::get_id(); MutexAutoLock lock(m_mutex); m_thread_names[id] = name; } void Logger::deregisterThread() { - threadid_t id = thr_get_current_thread_id(); + std::thread::id id = std::this_thread::get_id(); MutexAutoLock lock(m_mutex); m_thread_names.erase(id); } @@ -253,9 +253,9 @@ const std::string Logger::getLevelLabel(LogLevel lev) const std::string Logger::getThreadName() { - std::map::const_iterator it; + std::map::const_iterator it; - threadid_t id = thr_get_current_thread_id(); + std::thread::id id = std::this_thread::get_id(); it = m_thread_names.find(id); if (it != m_thread_names.end()) return it->second; diff --git a/src/log.h b/src/log.h index c017d127e..56492ff86 100644 --- a/src/log.h +++ b/src/log.h @@ -24,8 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include -#include "threads.h" #include "irrlichttypes.h" class ILogOutput; @@ -79,7 +79,7 @@ private: // written to when one thread has access currently). // Works on all known architectures (x86, ARM, MIPS). volatile bool m_silenced_levels[LL_MAX]; - std::map m_thread_names; + std::map m_thread_names; mutable std::mutex m_mutex; bool m_trace_enabled; }; diff --git a/src/porting.h b/src/porting.h index 7034d956b..05614543a 100644 --- a/src/porting.h +++ b/src/porting.h @@ -40,7 +40,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "debug.h" #include "constants.h" #include "gettime.h" -#include "threads.h" #ifdef _MSC_VER #define SWPRINTF_CHARSTRING L"%S" diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index eda4e73ac..ed056db31 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include +#include #include "util/basic_macros.h" extern "C" { @@ -29,7 +30,6 @@ extern "C" { } #include "irrlichttypes.h" -#include "threads.h" #include "threading/mutex_auto_lock.h" #include "common/c_types.h" #include "common/c_internal.h" @@ -122,7 +122,7 @@ protected: bool m_secure; #ifdef SCRIPTAPI_LOCK_DEBUG int m_lock_recursion_count; - threadid_t m_owning_thread; + std::thread::id m_owning_thread; #endif private: diff --git a/src/script/cpp_api/s_internal.h b/src/script/cpp_api/s_internal.h index 37473c497..315561e08 100644 --- a/src/script/cpp_api/s_internal.h +++ b/src/script/cpp_api/s_internal.h @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef S_INTERNAL_H_ #define S_INTERNAL_H_ +#include #include "common/c_internal.h" #include "cpp_api/s_base.h" @@ -35,23 +36,24 @@ with this program; if not, write to the Free Software Foundation, Inc., class LockChecker { public: - LockChecker(int *recursion_counter, threadid_t *owning_thread) + LockChecker(int *recursion_counter, std::thread::id *owning_thread) { m_lock_recursion_counter = recursion_counter; m_owning_thread = owning_thread; m_original_level = *recursion_counter; - if (*m_lock_recursion_counter > 0) - assert(thr_is_current_thread(*m_owning_thread)); - else - *m_owning_thread = thr_get_current_thread_id(); + if (*m_lock_recursion_counter > 0) { + assert(*m_owning_thread == std::this_thread::get_id()); + } else { + *m_owning_thread = std::this_thread::get_id(); + } (*m_lock_recursion_counter)++; } ~LockChecker() { - assert(thr_is_current_thread(*m_owning_thread)); + assert(*m_owning_thread == std::this_thread::get_id()); assert(*m_lock_recursion_counter > 0); (*m_lock_recursion_counter)--; @@ -62,7 +64,7 @@ public: private: int *m_lock_recursion_counter; int m_original_level; - threadid_t *m_owning_thread; + std::thread::id *m_owning_thread; }; #define SCRIPTAPI_LOCK_CHECK \ diff --git a/src/shader.cpp b/src/shader.cpp index 1d3f2f6a1..5ff8c910b 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -308,7 +308,7 @@ public: private: // The id of the thread that is allowed to use irrlicht directly - threadid_t m_main_thread; + std::thread::id m_main_thread; // The irrlicht device IrrlichtDevice *m_device; @@ -359,7 +359,7 @@ ShaderSource::ShaderSource(IrrlichtDevice *device): { assert(m_device); // Pre-condition - m_main_thread = thr_get_current_thread_id(); + m_main_thread = std::this_thread::get_id(); // Add a dummy ShaderInfo as the first index, named "" m_shaderinfo_cache.push_back(ShaderInfo()); @@ -387,7 +387,7 @@ u32 ShaderSource::getShader(const std::string &name, Get shader */ - if (thr_is_current_thread(m_main_thread)) { + if (std::this_thread::get_id() == m_main_thread) { return getShaderIdDirect(name, material_type, drawtype); } else { /*errorstream<<"getShader(): Queued: name=\""< #include "irrlichttypes_extrabloated.h" -#include "threads.h" #include class IGameDef; diff --git a/src/threading/event.cpp b/src/threading/event.cpp index 4e8d4bb3e..885e732c8 100644 --- a/src/threading/event.cpp +++ b/src/threading/event.cpp @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. */ #include "threading/event.h" +#include "threading/mutex_auto_lock.h" void Event::wait() { diff --git a/src/threading/event.h b/src/threading/event.h index 458864c82..af91d04c7 100644 --- a/src/threading/event.h +++ b/src/threading/event.h @@ -26,10 +26,7 @@ DEALINGS IN THE SOFTWARE. #ifndef THREADING_EVENT_H #define THREADING_EVENT_H -#include "threads.h" - #include -#include "threading/mutex_auto_lock.h" /** A syncronization primitive that will wake up one waiting thread when signaled. * Calling @c signal() multiple times before a waiting thread has had a chance diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index e566824f7..cc4d65656 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -180,10 +180,8 @@ bool Thread::getReturnValue(void **ret) } -void *Thread::threadProc(void *param) +void Thread::threadProc(Thread *thr) { - Thread *thr = (Thread *)param; - #ifdef _AIX thr->m_kernel_thread_id = thread_self(); #endif @@ -201,9 +199,6 @@ void *Thread::threadProc(void *param) thr->m_running = false; g_logger.deregisterThread(); - - // 0 is returned here to avoid an unnecessary ifdef clause - return 0; } diff --git a/src/threading/thread.h b/src/threading/thread.h index 6292d9ed7..284c8e46c 100644 --- a/src/threading/thread.h +++ b/src/threading/thread.h @@ -26,10 +26,10 @@ DEALINGS IN THE SOFTWARE. #pragma once #include "util/basic_macros.h" -#include "threads.h" #include #include +#include #include #ifdef _AIX @@ -49,10 +49,12 @@ DEALINGS IN THE SOFTWARE. #endif + class Thread { public: Thread(const std::string &name=""); virtual ~Thread(); + DISABLE_CLASS_COPY(Thread) /* * Begins execution of a new thread at the pure virtual method Thread::run(). @@ -87,13 +89,12 @@ public: /* * Returns true if the calling thread is this Thread object. */ - bool isCurrentThread() { return thr_is_current_thread(getThreadId()); } + bool isCurrentThread() { return std::this_thread::get_id() == getThreadId(); } - inline bool isRunning() { return m_running; } - inline bool stopRequested() { return m_request_stop; } + bool isRunning() { return m_running; } + bool stopRequested() { return m_request_stop; } - inline threadid_t getThreadId() { return m_thread_obj->get_id(); } - inline threadhandle_t getThreadHandle() { return m_thread_obj->native_handle(); } + std::thread::id getThreadId() { return m_thread_obj->get_id(); } /* * Gets the thread return value. @@ -139,6 +140,11 @@ protected: virtual void *run() = 0; private: + std::thread::native_handle_type getThreadHandle() + { return m_thread_obj->native_handle(); } + + static void threadProc(Thread *thr); + void *m_retval; bool m_joinable; std::atomic m_request_stop; @@ -148,13 +154,11 @@ private: std::thread *m_thread_obj; - static ThreadStartFunc threadProc; #ifdef _AIX // For AIX, there does not exist any mapping from pthread_t to tid_t // available to us, so we maintain one ourselves. This is set on thread start. tid_t m_kernel_thread_id; #endif - Thread(const Thread &) = delete; }; diff --git a/src/threads.h b/src/threads.h deleted file mode 100644 index ea84abd3e..000000000 --- a/src/threads.h +++ /dev/null @@ -1,51 +0,0 @@ -/* -Minetest -Copyright (C) 2013 celeron55, Perttu Ahola -Copyright (C) 2017 nerzhul, Loic Blot - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#pragma once - -/////////////// -#include - -// -// threadid_t, threadhandle_t -// -typedef std::thread::id threadid_t; -typedef std::thread::native_handle_type threadhandle_t; - -// -// ThreadStartFunc -// -typedef void *ThreadStartFunc(void *param); - - -inline threadid_t thr_get_current_thread_id() -{ - return std::this_thread::get_id(); -} - -inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2) -{ - return thr1 == thr2; -} - -inline bool thr_is_current_thread(threadid_t thr) -{ - return thr_compare_thread_id(thr_get_current_thread_id(), thr); -}