2015-04-07 12:13:12 +02:00
|
|
|
/*
|
|
|
|
This file is a part of the JThread package, which contains some object-
|
|
|
|
oriented thread wrappers for different thread implementations.
|
|
|
|
|
|
|
|
Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
copy of this software and associated documentation files (the "Software"),
|
|
|
|
to deal in the Software without restriction, including without limitation
|
|
|
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "threading/thread.h"
|
|
|
|
#include "threading/mutex_auto_lock.h"
|
|
|
|
#include "log.h"
|
2015-10-17 05:43:29 +02:00
|
|
|
#include "porting.h"
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
// for setName
|
2016-07-04 21:00:57 +02:00
|
|
|
#if defined(__linux__)
|
2015-04-07 12:13:12 +02:00
|
|
|
#include <sys/prctl.h>
|
2019-02-03 09:53:54 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
2015-04-07 12:13:12 +02:00
|
|
|
#include <pthread_np.h>
|
2020-09-09 19:12:58 +02:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
#include <sched.h>
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
struct THREADNAME_INFO {
|
2015-10-17 05:43:29 +02:00
|
|
|
DWORD dwType; // Must be 0x1000
|
|
|
|
LPCSTR szName; // Pointer to name (in user addr space)
|
2015-04-07 12:13:12 +02:00
|
|
|
DWORD dwThreadID; // Thread ID (-1=caller thread)
|
2015-10-17 05:43:29 +02:00
|
|
|
DWORD dwFlags; // Reserved for future use, must be zero
|
2015-04-07 12:13:12 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
// for bindToProcessor
|
2015-04-07 12:13:12 +02:00
|
|
|
#if __FreeBSD_version >= 702106
|
|
|
|
typedef cpuset_t cpu_set_t;
|
|
|
|
#elif defined(__sun) || defined(sun)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/processor.h>
|
|
|
|
#include <sys/procset.h>
|
|
|
|
#elif defined(_AIX)
|
|
|
|
#include <sys/processor.h>
|
2015-10-17 05:43:29 +02:00
|
|
|
#include <sys/thread.h>
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <mach/mach_init.h>
|
2015-08-27 20:51:07 +02:00
|
|
|
#include <mach/thread_act.h>
|
2015-04-07 12:13:12 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
Thread::Thread(const std::string &name) :
|
2015-10-17 05:43:29 +02:00
|
|
|
m_name(name),
|
|
|
|
m_request_stop(false),
|
|
|
|
m_running(false)
|
|
|
|
{
|
|
|
|
#ifdef _AIX
|
|
|
|
m_kernel_thread_id = -1;
|
2015-04-07 12:13:12 +02:00
|
|
|
#endif
|
2015-10-17 05:43:29 +02:00
|
|
|
}
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
|
|
|
|
Thread::~Thread()
|
2015-04-07 12:13:12 +02:00
|
|
|
{
|
2020-09-10 12:19:18 +02:00
|
|
|
// kill the thread if running
|
|
|
|
if (!m_running) {
|
|
|
|
wait();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
m_running = false;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method
|
|
|
|
TerminateThread((HANDLE) m_thread_obj->native_handle(), 0);
|
|
|
|
CloseHandle((HANDLE) m_thread_obj->native_handle());
|
|
|
|
#else
|
|
|
|
// We need to pthread_kill instead on Android since NDKv5's pthread
|
|
|
|
// implementation is incomplete.
|
|
|
|
# ifdef __ANDROID__
|
|
|
|
pthread_kill(getThreadHandle(), SIGKILL);
|
|
|
|
# else
|
|
|
|
pthread_cancel(getThreadHandle());
|
|
|
|
# endif
|
|
|
|
wait();
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-28 23:02:43 +01:00
|
|
|
|
|
|
|
// Make sure start finished mutex is unlocked before it's destroyed
|
2017-10-10 12:27:08 +02:00
|
|
|
if (m_start_finished_mutex.try_lock())
|
|
|
|
m_start_finished_mutex.unlock();
|
2017-01-28 23:02:43 +01:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Thread::start()
|
|
|
|
{
|
2015-10-18 04:42:48 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2015-10-17 05:43:29 +02:00
|
|
|
|
|
|
|
if (m_running)
|
2015-04-07 12:13:12 +02:00
|
|
|
return false;
|
|
|
|
|
2015-10-18 04:42:48 +02:00
|
|
|
m_request_stop = false;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2017-01-28 23:02:43 +01:00
|
|
|
// The mutex may already be locked if the thread is being restarted
|
|
|
|
m_start_finished_mutex.try_lock();
|
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
try {
|
2016-03-07 22:55:32 +01:00
|
|
|
m_thread_obj = new std::thread(threadProc, this);
|
2015-10-31 07:38:23 +01:00
|
|
|
} catch (const std::system_error &e) {
|
2015-04-07 12:13:12 +02:00
|
|
|
return false;
|
2015-10-17 05:43:29 +02:00
|
|
|
}
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
while (!m_running)
|
|
|
|
sleep_ms(1);
|
|
|
|
|
2022-04-30 16:04:19 +02:00
|
|
|
// Allow spawned thread to continue
|
|
|
|
m_start_finished_mutex.unlock();
|
|
|
|
|
2015-10-18 04:42:48 +02:00
|
|
|
m_joinable = true;
|
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Thread::stop()
|
|
|
|
{
|
|
|
|
m_request_stop = true;
|
2015-04-07 12:13:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-18 04:42:48 +02:00
|
|
|
bool Thread::wait()
|
2015-10-17 05:43:29 +02:00
|
|
|
{
|
2015-10-18 04:42:48 +02:00
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
|
|
|
|
if (!m_joinable)
|
|
|
|
return false;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
m_thread_obj->join();
|
|
|
|
|
2015-10-18 04:42:48 +02:00
|
|
|
delete m_thread_obj;
|
2017-06-19 23:54:58 +02:00
|
|
|
m_thread_obj = nullptr;
|
2015-10-18 04:42:48 +02:00
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
assert(m_running == false);
|
2015-10-18 04:42:48 +02:00
|
|
|
m_joinable = false;
|
|
|
|
return true;
|
2015-10-17 05:43:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
bool Thread::getReturnValue(void **ret)
|
|
|
|
{
|
|
|
|
if (m_running)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*ret = m_retval;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-11 09:43:05 +02:00
|
|
|
void Thread::threadProc(Thread *thr)
|
2015-04-07 12:13:12 +02:00
|
|
|
{
|
2015-10-17 05:43:29 +02:00
|
|
|
#ifdef _AIX
|
2017-01-28 23:23:30 +01:00
|
|
|
thr->m_kernel_thread_id = thread_self();
|
2015-04-07 12:13:12 +02:00
|
|
|
#endif
|
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
thr->setName(thr->m_name);
|
|
|
|
|
|
|
|
g_logger.registerThread(thr->m_name);
|
|
|
|
thr->m_running = true;
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2017-01-28 23:02:43 +01:00
|
|
|
// Wait for the thread that started this one to finish initializing the
|
|
|
|
// thread handle so that getThreadId/getThreadHandle will work.
|
|
|
|
thr->m_start_finished_mutex.lock();
|
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
thr->m_retval = thr->run();
|
2015-04-07 12:13:12 +02:00
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
thr->m_running = false;
|
2017-10-10 12:27:08 +02:00
|
|
|
// Unlock m_start_finished_mutex to prevent data race condition on Windows.
|
|
|
|
// On Windows with VS2017 build TerminateThread is called and this mutex is not
|
|
|
|
// released. We try to unlock it from caller thread and it's refused by system.
|
|
|
|
thr->m_start_finished_mutex.unlock();
|
2015-10-13 09:57:44 +02:00
|
|
|
g_logger.deregisterThread();
|
2015-04-07 12:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Thread::setName(const std::string &name)
|
|
|
|
{
|
2016-07-04 21:00:57 +02:00
|
|
|
#if defined(__linux__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
|
|
|
// It would be cleaner to do this with pthread_setname_np,
|
|
|
|
// which was added to glibc in version 2.12, but some major
|
|
|
|
// distributions are still runing 2.11 and previous versions.
|
2015-04-07 12:13:12 +02:00
|
|
|
prctl(PR_SET_NAME, name.c_str());
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2019-02-03 09:53:54 +01:00
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
pthread_set_name_np(pthread_self(), name.c_str());
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(__NetBSD__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2020-08-25 20:50:41 +02:00
|
|
|
pthread_setname_np(pthread_self(), "%s", const_cast<char*>(name.c_str()));
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(__APPLE__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
pthread_setname_np(name.c_str());
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2020-08-23 15:39:32 +02:00
|
|
|
#elif defined(__HAIKU__)
|
|
|
|
|
|
|
|
rename_thread(find_thread(NULL), name.c_str());
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(_MSC_VER)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
// Windows itself doesn't support thread names,
|
|
|
|
// but the MSVC debugger does...
|
|
|
|
THREADNAME_INFO info;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = name.c_str();
|
|
|
|
info.dwThreadID = -1;
|
|
|
|
info.dwFlags = 0;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
__try {
|
2015-10-17 05:43:29 +02:00
|
|
|
RaiseException(0x406D1388, 0,
|
|
|
|
sizeof(info) / sizeof(DWORD), (ULONG_PTR *)&info);
|
2015-04-07 12:13:12 +02:00
|
|
|
} __except (EXCEPTION_CONTINUE_EXECUTION) {
|
|
|
|
}
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(_WIN32) || defined(__GNU__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
// These platforms are known to not support thread names.
|
|
|
|
// Silently ignore the request.
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#else
|
|
|
|
#warning "Unrecognized platform, thread names will not be available."
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int Thread::getNumberOfProcessors()
|
|
|
|
{
|
|
|
|
return std::thread::hardware_concurrency();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-17 05:43:29 +02:00
|
|
|
bool Thread::bindToProcessor(unsigned int proc_number)
|
2015-04-07 12:13:12 +02:00
|
|
|
{
|
|
|
|
#if defined(__ANDROID__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
return false;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2017-06-27 11:54:40 +02:00
|
|
|
#elif _MSC_VER
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2016-04-30 15:44:28 +02:00
|
|
|
return SetThreadAffinityMask(getThreadHandle(), 1 << proc_number);
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2017-06-27 11:54:40 +02:00
|
|
|
#elif __MINGW32__
|
|
|
|
|
|
|
|
return SetThreadAffinityMask(pthread_gethandle(getThreadHandle()), 1 << proc_number);
|
|
|
|
|
2019-02-03 09:53:54 +01:00
|
|
|
#elif __FreeBSD_version >= 702106 || defined(__linux__) || defined(__DragonFly__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
cpu_set_t cpuset;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
CPU_ZERO(&cpuset);
|
2015-10-17 05:43:29 +02:00
|
|
|
CPU_SET(proc_number, &cpuset);
|
|
|
|
|
2016-04-30 15:44:28 +02:00
|
|
|
return pthread_setaffinity_np(getThreadHandle(), sizeof(cpuset), &cpuset) == 0;
|
2020-09-09 19:12:58 +02:00
|
|
|
#elif defined(__NetBSD__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2020-09-09 19:12:58 +02:00
|
|
|
cpuset_t *cpuset = cpuset_create();
|
|
|
|
if (cpuset == NULL)
|
|
|
|
return false;
|
|
|
|
int r = pthread_setaffinity_np(getThreadHandle(), cpuset_size(cpuset), cpuset);
|
|
|
|
cpuset_destroy(cpuset);
|
|
|
|
return r == 0;
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(__sun) || defined(sun)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
|
|
|
return processor_bind(P_LWPID, P_MYID, proc_number, NULL) == 0
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(_AIX)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
|
|
|
return bindprocessor(BINDTHREAD, m_kernel_thread_id, proc_number) == 0;
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(__hpux) || defined(hpux)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
pthread_spu_t answer;
|
|
|
|
|
|
|
|
return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
|
2016-04-30 15:44:28 +02:00
|
|
|
&answer, proc_number, getThreadHandle()) == 0;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#elif defined(__APPLE__)
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
struct thread_affinity_policy tapol;
|
|
|
|
|
2016-04-30 15:44:28 +02:00
|
|
|
thread_port_t threadport = pthread_mach_thread_np(getThreadHandle());
|
2015-10-17 05:43:29 +02:00
|
|
|
tapol.affinity_tag = proc_number + 1;
|
2015-04-07 12:13:12 +02:00
|
|
|
return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
|
|
|
|
(thread_policy_t)&tapol,
|
|
|
|
THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#else
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
return false;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Thread::setPriority(int prio)
|
|
|
|
{
|
2017-06-27 11:54:40 +02:00
|
|
|
#ifdef _MSC_VER
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2016-04-30 15:44:28 +02:00
|
|
|
return SetThreadPriority(getThreadHandle(), prio);
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2017-06-27 11:54:40 +02:00
|
|
|
#elif __MINGW32__
|
|
|
|
|
|
|
|
return SetThreadPriority(pthread_gethandle(getThreadHandle()), prio);
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#else
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
struct sched_param sparam;
|
|
|
|
int policy;
|
|
|
|
|
2016-04-30 15:44:28 +02:00
|
|
|
if (pthread_getschedparam(getThreadHandle(), &policy, &sparam) != 0)
|
2015-04-07 12:13:12 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
int min = sched_get_priority_min(policy);
|
|
|
|
int max = sched_get_priority_max(policy);
|
|
|
|
|
|
|
|
sparam.sched_priority = min + prio * (max - min) / THREAD_PRIORITY_HIGHEST;
|
2016-04-30 15:44:28 +02:00
|
|
|
return pthread_setschedparam(getThreadHandle(), policy, &sparam) == 0;
|
2015-10-17 05:43:29 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|