Use std::mutex instead of ArchMutex in ArchMultithreadPosix
This commit is contained in:
parent
e31ebc1b22
commit
f71c68506e
|
@ -114,9 +114,6 @@ ArchMultithreadPosix::ArchMultithreadPosix() :
|
||||||
m_signalUserData[i] = NULL;
|
m_signalUserData[i] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create mutex for thread list
|
|
||||||
m_threadMutex = newMutex();
|
|
||||||
|
|
||||||
// create thread for calling (main) thread and add it to our
|
// create thread for calling (main) thread and add it to our
|
||||||
// list. no need to lock the mutex since we're the only thread.
|
// list. no need to lock the mutex since we're the only thread.
|
||||||
m_mainThread = new ArchThreadImpl;
|
m_mainThread = new ArchThreadImpl;
|
||||||
|
@ -153,26 +150,22 @@ ArchMultithreadPosix::~ArchMultithreadPosix()
|
||||||
{
|
{
|
||||||
assert(s_instance != NULL);
|
assert(s_instance != NULL);
|
||||||
|
|
||||||
closeMutex(m_threadMutex);
|
|
||||||
s_instance = NULL;
|
s_instance = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ArchMultithreadPosix::setNetworkDataForCurrentThread(void* data)
|
ArchMultithreadPosix::setNetworkDataForCurrentThread(void* data)
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
ArchThreadImpl* thread = find(pthread_self());
|
ArchThreadImpl* thread = find(pthread_self());
|
||||||
thread->m_networkData = data;
|
thread->m_networkData = data;
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void*
|
void*
|
||||||
ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread)
|
ArchMultithreadPosix::getNetworkDataForThread(ArchThread thread)
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
void* data = thread->m_networkData;
|
return thread->m_networkData;
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ArchMultithreadPosix*
|
ArchMultithreadPosix*
|
||||||
|
@ -356,7 +349,8 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
lockMutex(m_threadMutex);
|
// note that the child thread will wait until we release this mutex
|
||||||
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
|
|
||||||
// create thread impl for new thread
|
// create thread impl for new thread
|
||||||
ArchThreadImpl* thread = new ArchThreadImpl;
|
ArchThreadImpl* thread = new ArchThreadImpl;
|
||||||
|
@ -387,18 +381,15 @@ ArchMultithreadPosix::newThread(ThreadFunc func, void* data)
|
||||||
refThread(thread);
|
refThread(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
// note that the child thread will wait until we release this mutex
|
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
|
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArchThread
|
ArchThread
|
||||||
ArchMultithreadPosix::newCurrentThread()
|
ArchMultithreadPosix::newCurrentThread()
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
|
|
||||||
ArchThreadImpl* thread = find(pthread_self());
|
ArchThreadImpl* thread = find(pthread_self());
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
assert(thread != NULL);
|
assert(thread != NULL);
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
@ -416,10 +407,11 @@ ArchMultithreadPosix::closeThread(ArchThread thread)
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove thread from list
|
// remove thread from list
|
||||||
lockMutex(m_threadMutex);
|
{
|
||||||
assert(findNoRef(thread->m_thread) == thread);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
erase(thread);
|
assert(findNoRef(thread->m_thread) == thread);
|
||||||
unlockMutex(m_threadMutex);
|
erase(thread);
|
||||||
|
}
|
||||||
|
|
||||||
// done with thread
|
// done with thread
|
||||||
delete thread;
|
delete thread;
|
||||||
|
@ -440,12 +432,14 @@ ArchMultithreadPosix::cancelThread(ArchThread thread)
|
||||||
|
|
||||||
// set cancel and wakeup flags if thread can be cancelled
|
// set cancel and wakeup flags if thread can be cancelled
|
||||||
bool wakeup = false;
|
bool wakeup = false;
|
||||||
lockMutex(m_threadMutex);
|
|
||||||
if (!thread->m_exited && !thread->m_cancelling) {
|
{
|
||||||
thread->m_cancel = true;
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
wakeup = true;
|
if (!thread->m_exited && !thread->m_cancelling) {
|
||||||
|
thread->m_cancel = true;
|
||||||
|
wakeup = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
|
|
||||||
// force thread to exit system calls if wakeup is true
|
// force thread to exit system calls if wakeup is true
|
||||||
if (wakeup) {
|
if (wakeup) {
|
||||||
|
@ -465,9 +459,11 @@ void
|
||||||
ArchMultithreadPosix::testCancelThread()
|
ArchMultithreadPosix::testCancelThread()
|
||||||
{
|
{
|
||||||
// find current thread
|
// find current thread
|
||||||
lockMutex(m_threadMutex);
|
ArchThreadImpl* thread = nullptr;
|
||||||
ArchThreadImpl* thread = findNoRef(pthread_self());
|
{
|
||||||
unlockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
|
thread = findNoRef(pthread_self());
|
||||||
|
}
|
||||||
|
|
||||||
// test cancel on thread
|
// test cancel on thread
|
||||||
testCancelThreadImpl(thread);
|
testCancelThreadImpl(thread);
|
||||||
|
@ -478,22 +474,23 @@ ArchMultithreadPosix::wait(ArchThread target, double timeout)
|
||||||
{
|
{
|
||||||
assert(target != NULL);
|
assert(target != NULL);
|
||||||
|
|
||||||
lockMutex(m_threadMutex);
|
ArchThreadImpl* self = nullptr;
|
||||||
|
|
||||||
// find current thread
|
{
|
||||||
ArchThreadImpl* self = findNoRef(pthread_self());
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
|
|
||||||
// ignore wait if trying to wait on ourself
|
// find current thread
|
||||||
if (target == self) {
|
self = findNoRef(pthread_self());
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
return false;
|
// ignore wait if trying to wait on ourself
|
||||||
|
if (target == self) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ref the target so it can't go away while we're watching it
|
||||||
|
refThread(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ref the target so it can't go away while we're watching it
|
|
||||||
refThread(target);
|
|
||||||
|
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// do first test regardless of timeout
|
// do first test regardless of timeout
|
||||||
testCancelThreadImpl(self);
|
testCancelThreadImpl(self);
|
||||||
|
@ -538,19 +535,15 @@ ArchMultithreadPosix::isSameThread(ArchThread thread1, ArchThread thread2)
|
||||||
bool
|
bool
|
||||||
ArchMultithreadPosix::isExitedThread(ArchThread thread)
|
ArchMultithreadPosix::isExitedThread(ArchThread thread)
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
bool exited = thread->m_exited;
|
return thread->m_exited;
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
return exited;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void*
|
void*
|
||||||
ArchMultithreadPosix::getResultOfThread(ArchThread thread)
|
ArchMultithreadPosix::getResultOfThread(ArchThread thread)
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
void* result = thread->m_result;
|
return thread->m_result;
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IArchMultithread::ThreadID
|
IArchMultithread::ThreadID
|
||||||
|
@ -563,16 +556,15 @@ void
|
||||||
ArchMultithreadPosix::setSignalHandler(
|
ArchMultithreadPosix::setSignalHandler(
|
||||||
ESignal signal, SignalFunc func, void* userData)
|
ESignal signal, SignalFunc func, void* userData)
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
m_signalFunc[signal] = func;
|
m_signalFunc[signal] = func;
|
||||||
m_signalUserData[signal] = userData;
|
m_signalUserData[signal] = userData;
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ArchMultithreadPosix::raiseSignal(ESignal signal)
|
ArchMultithreadPosix::raiseSignal(ESignal signal)
|
||||||
{
|
{
|
||||||
lockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
if (m_signalFunc[signal] != NULL) {
|
if (m_signalFunc[signal] != NULL) {
|
||||||
m_signalFunc[signal](signal, m_signalUserData[signal]);
|
m_signalFunc[signal](signal, m_signalUserData[signal]);
|
||||||
pthread_kill(m_mainThread->m_thread, SIGWAKEUP);
|
pthread_kill(m_mainThread->m_thread, SIGWAKEUP);
|
||||||
|
@ -580,7 +572,6 @@ ArchMultithreadPosix::raiseSignal(ESignal signal)
|
||||||
else if (signal == kINTERRUPT || signal == kTERMINATE) {
|
else if (signal == kINTERRUPT || signal == kTERMINATE) {
|
||||||
ARCH->cancelThread(m_mainThread);
|
ARCH->cancelThread(m_mainThread);
|
||||||
}
|
}
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -677,15 +668,15 @@ ArchMultithreadPosix::testCancelThreadImpl(ArchThreadImpl* thread)
|
||||||
{
|
{
|
||||||
assert(thread != NULL);
|
assert(thread != NULL);
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
|
|
||||||
// update cancel state
|
// update cancel state
|
||||||
lockMutex(m_threadMutex);
|
|
||||||
bool cancel = false;
|
bool cancel = false;
|
||||||
if (thread->m_cancel && !thread->m_cancelling) {
|
if (thread->m_cancel && !thread->m_cancelling) {
|
||||||
thread->m_cancelling = true;
|
thread->m_cancelling = true;
|
||||||
thread->m_cancel = false;
|
thread->m_cancel = false;
|
||||||
cancel = true;
|
cancel = true;
|
||||||
}
|
}
|
||||||
unlockMutex(m_threadMutex);
|
|
||||||
|
|
||||||
// unwind thread's stack if cancelling
|
// unwind thread's stack if cancelling
|
||||||
if (cancel) {
|
if (cancel) {
|
||||||
|
@ -717,8 +708,9 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
|
||||||
setPriorityOfThread(thread, 1);
|
setPriorityOfThread(thread, 1);
|
||||||
|
|
||||||
// wait for parent to initialize this object
|
// wait for parent to initialize this object
|
||||||
lockMutex(m_threadMutex);
|
{
|
||||||
unlockMutex(m_threadMutex);
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
|
}
|
||||||
|
|
||||||
void* result = NULL;
|
void* result = NULL;
|
||||||
try {
|
try {
|
||||||
|
@ -731,18 +723,20 @@ ArchMultithreadPosix::doThreadFunc(ArchThread thread)
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
// note -- don't catch (...) to avoid masking bugs
|
// note -- don't catch (...) to avoid masking bugs
|
||||||
lockMutex(m_threadMutex);
|
{
|
||||||
thread->m_exited = true;
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
unlockMutex(m_threadMutex);
|
thread->m_exited = true;
|
||||||
|
}
|
||||||
closeThread(thread);
|
closeThread(thread);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
// thread has exited
|
// thread has exited
|
||||||
lockMutex(m_threadMutex);
|
{
|
||||||
thread->m_result = result;
|
std::lock_guard<std::mutex> lock(m_threadMutex);
|
||||||
thread->m_exited = true;
|
thread->m_result = result;
|
||||||
unlockMutex(m_threadMutex);
|
thread->m_exited = true;
|
||||||
|
}
|
||||||
|
|
||||||
// done with thread
|
// done with thread
|
||||||
closeThread(thread);
|
closeThread(thread);
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include "common/stdlist.h"
|
#include "common/stdlist.h"
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#define ARCH_MULTITHREAD ArchMultithreadPosix
|
#define ARCH_MULTITHREAD ArchMultithreadPosix
|
||||||
|
|
||||||
|
@ -104,7 +105,7 @@ private:
|
||||||
|
|
||||||
bool m_newThreadCalled;
|
bool m_newThreadCalled;
|
||||||
|
|
||||||
ArchMutex m_threadMutex;
|
std::mutex m_threadMutex;
|
||||||
ArchThread m_mainThread;
|
ArchThread m_mainThread;
|
||||||
ThreadList m_threadList;
|
ThreadList m_threadList;
|
||||||
ThreadID m_nextID;
|
ThreadID m_nextID;
|
||||||
|
|
Loading…
Reference in New Issue