{
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(""));
/*
Get texture
*/
- if (get_current_thread_id() == m_main_thread)
+ if (thr_is_current_thread(m_main_thread))
{
return generateTexture(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"<<std::endl;
return 0;
{
//infostream<<"TextureSource::insertSourceImage(): name="<<name<<std::endl;
- sanity_check(get_current_thread_id() == m_main_thread);
+ sanity_check(thr_is_current_thread(m_main_thread));
m_sourcecache.insert(name, img, true, m_device->getVideoDriver());
m_source_image_existence.set(name, true);
{
std::string tname = "__shaderFlagsTexture";
tname += normalmap_present ? "1" : "0";
-
+
if (isKnownSourceImage(tname)) {
return getTexture(tname);
} else {
unsigned int line, const char *function)
{
errorstream << std::endl << "In thread " << std::hex
- << (unsigned long)get_current_thread_id() << ":" << std::endl;
+ << (unsigned long)thr_get_current_thread_id() << ":" << std::endl;
errorstream << file << ":" << line << ": " << function
<< ": An engine assumption '" << assertion << "' failed." << std::endl;
unsigned int line, const char *function)
{
errorstream << std::endl << "In thread " << std::hex
- << (unsigned long)get_current_thread_id() << ":" << std::endl;
+ << (unsigned long)thr_get_current_thread_id() << ":" << std::endl;
errorstream << file << ":" << line << ": " << function
<< ": A fatal error occured: " << msg << std::endl;
os<<"Probably overflown."<<std::endl;
}
+// Note: Using pthread_t (that is, threadid_t on POSIX platforms) as the key to
+// a std::map is naughty. Formally, a pthread_t may only be compared using
+// pthread_equal() - pthread_t lacks the well-ordered property needed for
+// comparisons in binary searches. This should be fixed at some point by
+// defining a custom comparator with an arbitrary but stable ordering of
+// pthread_t, but it isn't too important since none of our supported platforms
+// implement pthread_t as a non-ordinal type.
std::map<threadid_t, DebugStack*> g_debug_stacks;
Mutex g_debug_stacks_mutex;
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);
{
#ifndef SERVER
- m_main_thread = get_current_thread_id();
+ m_main_thread = thr_get_current_thread_id();
#endif
clear();
}
<<name<<"\""<<std::endl;
// This is not thread-safe
- sanity_check(get_current_thread_id() == m_main_thread);
+ sanity_check(thr_is_current_thread(m_main_thread));
// Skip if already in cache
ClientCached *cc = NULL;
if(cc)
return cc;
- if(get_current_thread_id() == m_main_thread)
+ if(thr_is_current_thread(m_main_thread))
{
return createClientCachedDirect(name, gamedef);
}
void Logger::registerThread(const std::string &name)
{
- threadid_t id = get_current_thread_id();
+ threadid_t id = thr_get_current_thread_id();
MutexAutoLock lock(m_mutex);
m_thread_names[id] = name;
}
void Logger::deregisterThread()
{
- threadid_t id = get_current_thread_id();
+ threadid_t id = thr_get_current_thread_id();
MutexAutoLock lock(m_mutex);
m_thread_names.erase(id);
}
{
std::map<threadid_t, std::string>::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;
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());
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=\""<<name<<"\""<<std::endl;*/
/*
Calling only allowed from main thread
*/
- if(get_current_thread_id() != m_main_thread){
+ if (!thr_is_current_thread(m_main_thread)) {
errorstream<<"ShaderSource::getShaderIdDirect() "
"called not from main thread"<<std::endl;
return 0;
"name_of_shader=\""<<name_of_shader<<"\", "
"filename=\""<<filename<<"\""<<std::endl;*/
- sanity_check(get_current_thread_id() == m_main_thread);
+ sanity_check(thr_is_current_thread(m_main_thread));
m_sourcecache.insert(name_of_shader, filename, program, true);
}
#ifndef THREADS_HEADER
#define THREADS_HEADER
+//
+// Determine which threading API we will use
+//
+#if __cplusplus >= 201103L
+ #define USE_CPP11_THREADS 1
+#elif defined(_WIN32)
+ #define USE_WIN_THREADS 1
+#else
+ #define USE_POSIX_THREADS 1
+#endif
+
+///////////////
+
+
+#if USE_CPP11_THREADS
+ #include <thread>
+#endif
+
#include "threading/mutex.h"
-#if defined(WIN32) || defined(_WIN32_WCE)
-typedef DWORD threadid_t;
-#else
-typedef pthread_t threadid_t;
+//
+// 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 get_current_thread_id()
+
+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