C++11 patchset 5: use std::threads and remove old compat layer (#5928)
* C++11 patchset 5: use std::threads and remove old compat layer * use pragma once in modified headers * use C++11 function delete for object copymaster
parent
0a5c3c2852
commit
5bd33a1586
|
@ -28,31 +28,6 @@ DEALINGS IN THE SOFTWARE.
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "porting.h"
|
#include "porting.h"
|
||||||
|
|
||||||
#define UNUSED(expr) do { (void)(expr); } while (0)
|
|
||||||
|
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
#include <chrono>
|
|
||||||
#include <system_error>
|
|
||||||
#elif USE_WIN_THREADS
|
|
||||||
#ifndef _WIN32_WCE
|
|
||||||
#include <process.h>
|
|
||||||
#endif
|
|
||||||
#elif USE_POSIX_THREADS
|
|
||||||
#include <time.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
#if defined(__FreeBSD__) || defined(__APPLE__)
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/sysctl.h>
|
|
||||||
#elif defined(_GNU_SOURCE)
|
|
||||||
#include <sys/sysinfo.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
// for setName
|
// for setName
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
|
@ -70,8 +45,6 @@ DEALINGS IN THE SOFTWARE.
|
||||||
// for bindToProcessor
|
// for bindToProcessor
|
||||||
#if __FreeBSD_version >= 702106
|
#if __FreeBSD_version >= 702106
|
||||||
typedef cpuset_t cpu_set_t;
|
typedef cpuset_t cpu_set_t;
|
||||||
#elif defined(__linux__)
|
|
||||||
#include <sched.h>
|
|
||||||
#elif defined(__sun) || defined(sun)
|
#elif defined(__sun) || defined(sun)
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/processor.h>
|
#include <sys/processor.h>
|
||||||
|
@ -121,28 +94,12 @@ bool Thread::start()
|
||||||
// The mutex may already be locked if the thread is being restarted
|
// The mutex may already be locked if the thread is being restarted
|
||||||
m_start_finished_mutex.try_lock();
|
m_start_finished_mutex.try_lock();
|
||||||
|
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
m_thread_obj = new std::thread(threadProc, this);
|
m_thread_obj = new std::thread(threadProc, this);
|
||||||
} catch (const std::system_error &e) {
|
} catch (const std::system_error &e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif USE_WIN_THREADS
|
|
||||||
|
|
||||||
m_thread_handle = CreateThread(NULL, 0, threadProc, this, 0, &m_thread_id);
|
|
||||||
if (!m_thread_handle)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
#elif USE_POSIX_THREADS
|
|
||||||
|
|
||||||
int status = pthread_create(&m_thread_handle, NULL, threadProc, this);
|
|
||||||
if (status)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Allow spawned thread to continue
|
// Allow spawned thread to continue
|
||||||
m_start_finished_mutex.unlock();
|
m_start_finished_mutex.unlock();
|
||||||
|
|
||||||
|
@ -169,31 +126,12 @@ bool Thread::wait()
|
||||||
if (!m_joinable)
|
if (!m_joinable)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
|
|
||||||
m_thread_obj->join();
|
m_thread_obj->join();
|
||||||
|
|
||||||
delete m_thread_obj;
|
delete m_thread_obj;
|
||||||
m_thread_obj = NULL;
|
m_thread_obj = NULL;
|
||||||
|
|
||||||
#elif USE_WIN_THREADS
|
|
||||||
|
|
||||||
int ret = WaitForSingleObject(m_thread_handle, INFINITE);
|
|
||||||
assert(ret == WAIT_OBJECT_0);
|
|
||||||
UNUSED(ret);
|
|
||||||
|
|
||||||
CloseHandle(m_thread_handle);
|
|
||||||
m_thread_handle = NULL;
|
|
||||||
m_thread_id = -1;
|
|
||||||
|
|
||||||
#elif USE_POSIX_THREADS
|
|
||||||
|
|
||||||
int ret = pthread_join(m_thread_handle, NULL);
|
|
||||||
assert(ret == 0);
|
|
||||||
UNUSED(ret);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
assert(m_running == false);
|
assert(m_running == false);
|
||||||
m_joinable = false;
|
m_joinable = false;
|
||||||
return true;
|
return true;
|
||||||
|
@ -209,9 +147,10 @@ bool Thread::kill()
|
||||||
|
|
||||||
m_running = false;
|
m_running = false;
|
||||||
|
|
||||||
#if USE_WIN_THREADS
|
#if defined(_WIN32)
|
||||||
TerminateThread(m_thread_handle, 0);
|
// See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method
|
||||||
CloseHandle(m_thread_handle);
|
TerminateThread((HANDLE) m_thread_obj->native_handle(), 0);
|
||||||
|
CloseHandle((HANDLE) m_thread_obj->native_handle());
|
||||||
#else
|
#else
|
||||||
// We need to pthread_kill instead on Android since NDKv5's pthread
|
// We need to pthread_kill instead on Android since NDKv5's pthread
|
||||||
// implementation is incomplete.
|
// implementation is incomplete.
|
||||||
|
@ -241,13 +180,7 @@ bool Thread::getReturnValue(void **ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if USE_CPP11_THREADS || USE_POSIX_THREADS
|
|
||||||
void *Thread::threadProc(void *param)
|
void *Thread::threadProc(void *param)
|
||||||
#elif defined(_WIN32_WCE)
|
|
||||||
DWORD Thread::threadProc(LPVOID param)
|
|
||||||
#elif defined(_WIN32)
|
|
||||||
DWORD WINAPI Thread::threadProc(LPVOID param)
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
Thread *thr = (Thread *)param;
|
Thread *thr = (Thread *)param;
|
||||||
|
|
||||||
|
@ -325,46 +258,7 @@ void Thread::setName(const std::string &name)
|
||||||
|
|
||||||
unsigned int Thread::getNumberOfProcessors()
|
unsigned int Thread::getNumberOfProcessors()
|
||||||
{
|
{
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
|
|
||||||
return std::thread::hardware_concurrency();
|
return std::thread::hardware_concurrency();
|
||||||
|
|
||||||
#elif USE_WIN_THREADS
|
|
||||||
|
|
||||||
SYSTEM_INFO sysinfo;
|
|
||||||
GetSystemInfo(&sysinfo);
|
|
||||||
return sysinfo.dwNumberOfProcessors;
|
|
||||||
|
|
||||||
#elif defined(_SC_NPROCESSORS_ONLN)
|
|
||||||
|
|
||||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
||||||
|
|
||||||
#elif defined(__FreeBSD__) || defined(__NetBSD__) || \
|
|
||||||
defined(__DragonFly__) || defined(__APPLE__)
|
|
||||||
|
|
||||||
unsigned int num_cpus = 1;
|
|
||||||
size_t len = sizeof(num_cpus);
|
|
||||||
|
|
||||||
int mib[2];
|
|
||||||
mib[0] = CTL_HW;
|
|
||||||
mib[1] = HW_NCPU;
|
|
||||||
|
|
||||||
sysctl(mib, 2, &num_cpus, &len, NULL, 0);
|
|
||||||
return num_cpus;
|
|
||||||
|
|
||||||
#elif defined(_GNU_SOURCE)
|
|
||||||
|
|
||||||
return get_nprocs();
|
|
||||||
|
|
||||||
#elif defined(PTW32_VERSION) || defined(__hpux)
|
|
||||||
|
|
||||||
return pthread_num_processors_np();
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
DEALINGS IN THE SOFTWARE.
|
DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef THREADING_THREAD_H
|
#pragma once
|
||||||
#define THREADING_THREAD_H
|
|
||||||
|
|
||||||
#include "util/basic_macros.h"
|
#include "util/basic_macros.h"
|
||||||
#include "threads.h"
|
#include "threads.h"
|
||||||
|
@ -93,17 +92,8 @@ public:
|
||||||
inline bool isRunning() { return m_running; }
|
inline bool isRunning() { return m_running; }
|
||||||
inline bool stopRequested() { return m_request_stop; }
|
inline bool stopRequested() { return m_request_stop; }
|
||||||
|
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
inline threadid_t getThreadId() { return m_thread_obj->get_id(); }
|
inline threadid_t getThreadId() { return m_thread_obj->get_id(); }
|
||||||
inline threadhandle_t getThreadHandle() { return m_thread_obj->native_handle(); }
|
inline threadhandle_t getThreadHandle() { return m_thread_obj->native_handle(); }
|
||||||
#else
|
|
||||||
# if USE_WIN_THREADS
|
|
||||||
inline threadid_t getThreadId() { return m_thread_id; }
|
|
||||||
# else
|
|
||||||
inline threadid_t getThreadId() { return m_thread_handle; }
|
|
||||||
# endif
|
|
||||||
inline threadhandle_t getThreadHandle() { return m_thread_handle; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Gets the thread return value.
|
* Gets the thread return value.
|
||||||
|
@ -156,14 +146,7 @@ private:
|
||||||
std::mutex m_mutex;
|
std::mutex m_mutex;
|
||||||
std::mutex m_start_finished_mutex;
|
std::mutex m_start_finished_mutex;
|
||||||
|
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
std::thread *m_thread_obj;
|
std::thread *m_thread_obj;
|
||||||
#else
|
|
||||||
threadhandle_t m_thread_handle;
|
|
||||||
# if USE_WIN_THREADS
|
|
||||||
threadid_t m_thread_id;
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static ThreadStartFunc threadProc;
|
static ThreadStartFunc threadProc;
|
||||||
|
|
||||||
|
@ -172,9 +155,6 @@ private:
|
||||||
// available to us, so we maintain one ourselves. This is set on thread start.
|
// available to us, so we maintain one ourselves. This is set on thread start.
|
||||||
tid_t m_kernel_thread_id;
|
tid_t m_kernel_thread_id;
|
||||||
#endif
|
#endif
|
||||||
|
Thread(const Thread &) = delete;
|
||||||
DISABLE_CLASS_COPY(Thread);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
Minetest
|
Minetest
|
||||||
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
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
|
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
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
@ -17,92 +18,34 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef THREADS_HEADER
|
#pragma once
|
||||||
#define THREADS_HEADER
|
|
||||||
|
|
||||||
//
|
|
||||||
// Determine which threading APIs 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 defined(_WIN32)
|
|
||||||
// Prefer critical section API because std::mutex is much slower on Windows
|
|
||||||
#define USE_WIN_MUTEX 1
|
|
||||||
#elif __cplusplus >= 201103L
|
|
||||||
#define USE_CPP11_MUTEX 1
|
|
||||||
#else
|
|
||||||
#define USE_POSIX_MUTEX 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
|
|
||||||
|
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#elif USE_POSIX_THREADS
|
|
||||||
#include <pthread.h>
|
|
||||||
#else
|
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#endif
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// threadid_t, threadhandle_t
|
// threadid_t, threadhandle_t
|
||||||
//
|
//
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
typedef std::thread::id threadid_t;
|
typedef std::thread::id threadid_t;
|
||||||
typedef std::thread::native_handle_type threadhandle_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
|
// ThreadStartFunc
|
||||||
//
|
//
|
||||||
#if USE_CPP11_THREADS || USE_POSIX_THREADS
|
|
||||||
typedef void *ThreadStartFunc(void *param);
|
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 thr_get_current_thread_id()
|
inline threadid_t thr_get_current_thread_id()
|
||||||
{
|
{
|
||||||
#if USE_CPP11_THREADS
|
|
||||||
return std::this_thread::get_id();
|
return std::this_thread::get_id();
|
||||||
#elif USE_WIN_THREADS
|
|
||||||
return GetCurrentThreadId();
|
|
||||||
#elif USE_POSIX_THREADS
|
|
||||||
return pthread_self();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool thr_compare_thread_id(threadid_t thr1, threadid_t thr2)
|
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;
|
return thr1 == thr2;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool thr_is_current_thread(threadid_t thr)
|
inline bool thr_is_current_thread(threadid_t thr)
|
||||||
{
|
{
|
||||||
return thr_compare_thread_id(thr_get_current_thread_id(), thr);
|
return thr_compare_thread_id(thr_get_current_thread_id(), thr);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
Loading…
Reference in New Issue