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;
{
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(""));
/*
Get texture
*/
- if (thr_is_current_thread(m_main_thread))
+ if (std::this_thread::get_id() == m_main_thread)
{
return generateTexture(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;
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;
{
//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);
#include "irr_v3d.h"
#include <ITexture.h>
#include <IrrlichtDevice.h>
-#include "threads.h"
#include <string>
#include <vector>
#include "util/numeric.h"
#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"
#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;
#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;
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);
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()
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);
}
}
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;
}
}
{
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);
}
}
{
#ifndef SERVER
- m_main_thread = thr_get_current_thread_id();
+ m_main_thread = std::this_thread::get_id();
#endif
clear();
}
<<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;
{
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;
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
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);
}
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;
#include <queue>
#include <string>
#include <fstream>
+#include <thread>
#include <mutex>
-#include "threads.h"
#include "irrlichttypes.h"
class ILogOutput;
// 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;
};
#include "debug.h"
#include "constants.h"
#include "gettime.h"
-#include "threads.h"
#ifdef _MSC_VER
#define SWPRINTF_CHARSTRING L"%S"
#include <iostream>
#include <string>
+#include <thread>
#include "util/basic_macros.h"
extern "C" {
}
#include "irrlichttypes.h"
-#include "threads.h"
#include "threading/mutex_auto_lock.h"
#include "common/c_types.h"
#include "common/c_internal.h"
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:
#ifndef S_INTERNAL_H_
#define S_INTERNAL_H_
+#include <thread>
#include "common/c_internal.h"
#include "cpp_api/s_base.h"
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)--;
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 \
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;
{
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());
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=\""<<name<<"\""<<std::endl;*/
/*
Calling only allowed from main thread
*/
- if (!thr_is_current_thread(m_main_thread)) {
+ if (std::this_thread::get_id() != 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(thr_is_current_thread(m_main_thread));
+ sanity_check(std::this_thread::get_id() == m_main_thread);
m_sourcecache.insert(name_of_shader, filename, program, true);
}
#include <IMaterialRendererServices.h>
#include "irrlichttypes_extrabloated.h"
-#include "threads.h"
#include <string>
class IGameDef;
*/
#include "threading/event.h"
+#include "threading/mutex_auto_lock.h"
void Event::wait()
{
#ifndef THREADING_EVENT_H
#define THREADING_EVENT_H
-#include "threads.h"
-
#include <condition_variable>
-#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
}
-void *Thread::threadProc(void *param)
+void Thread::threadProc(Thread *thr)
{
- Thread *thr = (Thread *)param;
-
#ifdef _AIX
thr->m_kernel_thread_id = thread_self();
#endif
thr->m_running = false;
g_logger.deregisterThread();
-
- // 0 is returned here to avoid an unnecessary ifdef clause
- return 0;
}
#pragma once
#include "util/basic_macros.h"
-#include "threads.h"
#include <string>
#include <atomic>
+#include <thread>
#include <mutex>
#ifdef _AIX
#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().
/*
* 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.
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<bool> m_request_stop;
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;
};
+++ /dev/null
-/*
-Minetest
-Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
-Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
-
-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 <thread>
-
-//
-// 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);
-}