Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor thread utility interface
- Add "thr_" prefix to thread utility functions
- Compare threadid_ts in a portable manner, where possible
  • Loading branch information
kwolekr committed Oct 17, 2015
1 parent 836486a commit 6be74d1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/client/tile.cpp
Expand Up @@ -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(""));
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"<<std::endl;
return 0;
Expand Down Expand Up @@ -704,7 +704,7 @@ void TextureSource::insertSourceImage(const std::string &name, video::IImage *im
{
//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);
Expand Down Expand Up @@ -2057,7 +2057,7 @@ video::ITexture *TextureSource::getShaderFlagsTexture(bool normalmap_present)
{
std::string tname = "__shaderFlagsTexture";
tname += normalmap_present ? "1" : "0";

if (isKnownSourceImage(tname)) {
return getTexture(tname);
} else {
Expand Down
13 changes: 10 additions & 3 deletions src/debug.cpp
Expand Up @@ -45,7 +45,7 @@ void sanity_check_fn(const char *assertion, const char *file,
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;

Expand All @@ -58,7 +58,7 @@ void fatal_error_fn(const char *msg, const char *file,
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;

Expand Down Expand Up @@ -130,6 +130,13 @@ void DebugStack::print(std::ostream &os, bool everything)
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;

Expand Down Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/itemdef.cpp
Expand Up @@ -241,7 +241,7 @@ class CItemDefManager: public IWritableItemDefManager
{

#ifndef SERVER
m_main_thread = get_current_thread_id();
m_main_thread = thr_get_current_thread_id();
#endif
clear();
}
Expand Down Expand Up @@ -317,7 +317,7 @@ class CItemDefManager: public IWritableItemDefManager
<<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;
Expand Down Expand Up @@ -448,7 +448,7 @@ class CItemDefManager: public IWritableItemDefManager
if(cc)
return cc;

if(get_current_thread_id() == m_main_thread)
if(thr_is_current_thread(m_main_thread))
{
return createClientCachedDirect(name, gamedef);
}
Expand Down
6 changes: 3 additions & 3 deletions src/log.cpp
Expand Up @@ -184,14 +184,14 @@ void Logger::setLevelSilenced(LogLevel lev, bool silenced)

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);
}
Expand All @@ -215,7 +215,7 @@ const std::string Logger::getThreadName()
{
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;
Expand Down
8 changes: 4 additions & 4 deletions src/shader.cpp
Expand Up @@ -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());
Expand Down Expand Up @@ -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=\""<<name<<"\""<<std::endl;*/
Expand Down Expand Up @@ -456,7 +456,7 @@ u32 ShaderSource::getShaderIdDirect(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<<"ShaderSource::getShaderIdDirect() "
"called not from main thread"<<std::endl;
return 0;
Expand Down Expand Up @@ -504,7 +504,7 @@ void ShaderSource::insertSourceShader(const std::string &name_of_shader,
"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);
}
Expand Down
67 changes: 60 additions & 7 deletions src/threads.h
Expand Up @@ -20,22 +20,75 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#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

0 comments on commit 6be74d1

Please sign in to comment.