Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove threads.h and replace its definitions with their C++11 equival…
…ents (#5957)

This also changes threadProc's signature, since C++11 supports arbitrary
thread function signatures.
  • Loading branch information
ShadowNinja authored and nerzhul committed Jun 11, 2017
1 parent 5cc8ad9 commit 6c5e5e2
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 160 deletions.
12 changes: 6 additions & 6 deletions src/client/tile.cpp
Expand Up @@ -386,7 +386,7 @@ class TextureSource : public IWritableTextureSource
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;

Expand Down Expand Up @@ -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(""));
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"<<std::endl;
return 0;
Expand Down Expand Up @@ -695,7 +695,7 @@ video::ITexture* TextureSource::getTextureForMesh(const std::string &name, u32 *
Palette* TextureSource::getPalette(const std::string &name)
{
// Only the main thread may load images
sanity_check(thr_is_current_thread(m_main_thread));
sanity_check(std::this_thread::get_id() == m_main_thread);

if (name == "")
return NULL;
Expand Down Expand Up @@ -771,7 +771,7 @@ void TextureSource::insertSourceImage(const std::string &name, video::IImage *im
{
//infostream<<"TextureSource::insertSourceImage(): name="<<name<<std::endl;

sanity_check(thr_is_current_thread(m_main_thread));
sanity_check(std::this_thread::get_id() == m_main_thread);

m_sourcecache.insert(name, img, true, m_device->getVideoDriver());
m_source_image_existence.set(name, true);
Expand Down
1 change: 0 additions & 1 deletion src/client/tile.h
Expand Up @@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irr_v3d.h"
#include <ITexture.h>
#include <IrrlichtDevice.h>
#include "threads.h"
#include <string>
#include <vector>
#include "util/numeric.h"
Expand Down
88 changes: 34 additions & 54 deletions src/debug.cpp
Expand Up @@ -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 <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <map>
#include <sstream>
#include <thread>
#include "threading/mutex_auto_lock.h"
#include "config.h"

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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<stack_max_i; i++)
{
if(i == stack_i && everything == false)
for (int i = 0; i < stack_max_i; i++) {
if (i == stack_i && everything == false)
break;

if(i < stack_i)
if (i < stack_i)
fprintf(file, "#%d %s\n", i, stack[i]);
else
fprintf(file, "(Leftover data: #%d %s)\n", i, stack[i]);
}

if(stack_i == DEBUG_STACK_SIZE)
if (stack_i == DEBUG_STACK_SIZE)
fprintf(file, "Probably overflown.\n");
}

void DebugStack::print(std::ostream &os, bool everything)
{
os<<"DEBUG STACK FOR THREAD "<<threadid<<": "<<std::endl;
os<<"DEBUG STACK FOR THREAD "<<thread_id<<": "<<std::endl;

for(int i=0; i<stack_max_i; i++)
{
for(int i = 0; i < stack_max_i; i++) {
if(i == stack_i && everything == false)
break;

if(i < stack_i)
if (i < stack_i)
os<<"#"<<i<<" "<<stack[i]<<std::endl;
else
os<<"(Leftover data: #"<<i<<" "<<stack[i]<<")"<<std::endl;
}

if(stack_i == DEBUG_STACK_SIZE)
if (stack_i == DEBUG_STACK_SIZE)
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;
std::map<std::thread::id, DebugStack*> g_debug_stacks;
std::mutex g_debug_stacks_mutex;

void debug_stacks_init()
Expand All @@ -163,11 +154,8 @@ void debug_stacks_print_to(std::ostream &os)

os<<"Debug stacks:"<<std::endl;

for(std::map<threadid_t, DebugStack*>::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);
}
}

Expand All @@ -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<threadid_t, DebugStack*>::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;
}
}
Expand All @@ -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);
}
}

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

#ifndef SERVER
m_main_thread = thr_get_current_thread_id();
m_main_thread = std::this_thread::get_id();
#endif
clear();
}
Expand Down Expand Up @@ -320,7 +320,7 @@ class CItemDefManager: public IWritableItemDefManager
<<name<<"\""<<std::endl;

// This is not thread-safe
sanity_check(thr_is_current_thread(m_main_thread));
sanity_check(std::this_thread::get_id() == m_main_thread);

// Skip if already in cache
ClientCached *cc = NULL;
Expand Down Expand Up @@ -356,15 +356,12 @@ class CItemDefManager: public IWritableItemDefManager
{
ClientCached *cc = NULL;
m_clientcached.get(name, &cc);
if(cc)
if (cc)
return cc;

if(thr_is_current_thread(m_main_thread))
{
if (std::this_thread::get_id() == m_main_thread) {
return createClientCachedDirect(name, client);
}
else
{
} else {
// We're gonna ask the result to be put into here
static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;

Expand Down Expand Up @@ -580,7 +577,7 @@ class CItemDefManager: public IWritableItemDefManager
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
Expand Down
8 changes: 4 additions & 4 deletions src/log.cpp
Expand Up @@ -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);
}
Expand All @@ -253,9 +253,9 @@ const std::string Logger::getLevelLabel(LogLevel lev)

const std::string Logger::getThreadName()
{
std::map<threadid_t, std::string>::const_iterator it;
std::map<std::thread::id, std::string>::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;
Expand Down
4 changes: 2 additions & 2 deletions src/log.h
Expand Up @@ -24,8 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <queue>
#include <string>
#include <fstream>
#include <thread>
#include <mutex>
#include "threads.h"
#include "irrlichttypes.h"

class ILogOutput;
Expand Down Expand Up @@ -79,7 +79,7 @@ class Logger {
// 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<threadid_t, std::string> m_thread_names;
std::map<std::thread::id, std::string> m_thread_names;
mutable std::mutex m_mutex;
bool m_trace_enabled;
};
Expand Down
1 change: 0 additions & 1 deletion src/porting.h
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/script/cpp_api/s_base.h
Expand Up @@ -22,14 +22,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include <iostream>
#include <string>
#include <thread>
#include "util/basic_macros.h"

extern "C" {
#include <lua.h>
}

#include "irrlichttypes.h"
#include "threads.h"
#include "threading/mutex_auto_lock.h"
#include "common/c_types.h"
#include "common/c_internal.h"
Expand Down Expand Up @@ -122,7 +122,7 @@ class ScriptApiBase {
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:
Expand Down

0 comments on commit 6c5e5e2

Please sign in to comment.