Made Thread more stable (I’m not sure this works because it was already stable on OS X)

This commit is contained in:
yvt 2013-11-27 00:46:12 +09:00
parent e2aa810524
commit b51a751dc0
4 changed files with 63 additions and 4 deletions

View File

@ -211,6 +211,7 @@ namespace spades {
while((ent = internal->Poll()) != NULL){ while((ent = internal->Poll()) != NULL){
ent->dispatch->Execute(); ent->dispatch->Execute();
} }
Thread::CleanupExitedThreads();
} }
void DispatchQueue::EnterEventLoop() throw() { void DispatchQueue::EnterEventLoop() throw() {

View File

@ -23,6 +23,7 @@
#include "AutoLocker.h" #include "AutoLocker.h"
#include "Debug.h" #include "Debug.h"
#include "ThreadLocalStorage.h" #include "ThreadLocalStorage.h"
#include "ConcurrentDispatch.h"
namespace spades { namespace spades {
@ -33,12 +34,55 @@ namespace spades {
} }
Thread::Thread(IRunnable *r): Thread::Thread(IRunnable *r):
runnable(r){ runnable(r),
autoDelete(false){
threadInfo = NULL; threadInfo = NULL;
threadId = 0;
}
class ThreadCleanuper: public ConcurrentDispatch {
std::vector<SDL_Thread *> threads;
Mutex mutex;
public:
void Add(SDL_Thread *thread) {
AutoLocker locker(&mutex);
threads.push_back(thread);
}
void Cleanup() {
AutoLocker locker(&mutex);
for(size_t i = 0; i < threads.size(); i++)
SDL_WaitThread(threads[i], NULL);
threads.clear();
}
};
static ThreadCleanuper *cleanuper;
void Thread::InitThreadSystem() {
cleanuper = new ThreadCleanuper();
}
void Thread::CleanupExitedThreads() {
cleanuper->Cleanup();
} }
Thread::~Thread() { Thread::~Thread() {
SDL_Thread *th = NULL;
{
AutoLocker locker(&lock);
th = (SDL_Thread *)threadInfo;
if(!th)
return;
}
// we have to ensure thread handle is destroyed.
if(SDL_ThreadID() == threadId) {
// thread is deleting itself.
// SDL_WaitThread would cause deadlock.
cleanuper->Add(th);
}else{
SDL_WaitThread(th, NULL);
}
} }
void Thread::Start() { void Thread::Start() {
@ -46,10 +90,14 @@ namespace spades {
if(threadInfo) if(threadInfo)
return; return;
threadId = 0;
threadInfo = SDL_CreateThread(InternalRunner, this); threadInfo = SDL_CreateThread(InternalRunner, this);
} }
void Thread::Join() { void Thread::Join() {
if(autoDelete) {
SPRaise("Attempted join a thread that is marked for auto deletion.");
}
SDL_Thread *th = NULL; SDL_Thread *th = NULL;
{ {
AutoLocker locker(&lock); AutoLocker locker(&lock);
@ -61,13 +109,17 @@ namespace spades {
} }
bool Thread::IsAlive() { bool Thread::IsAlive() {
if(autoDelete) {
SPRaise("Attempted query the state of a thread that is marked for auto deletion.");
}
return threadInfo != NULL; return threadInfo != NULL;
} }
int Thread::InternalRunner(void *th) { int Thread::InternalRunner(void *th) {
Thread *self = (Thread *)th;
self->threadId = SDL_ThreadID();
try{ try{
SPADES_MARK_FUNCTION(); SPADES_MARK_FUNCTION();
Thread *self = (Thread *)th;
self->Run(); self->Run();
self->Quited(); self->Quited();
}catch(const std::exception& ex){ }catch(const std::exception& ex){

View File

@ -26,10 +26,11 @@
namespace spades { namespace spades {
class Thread { class Thread {
void *threadInfo; void * volatile threadInfo;
Mutex lock; Mutex lock;
IRunnable *runnable; IRunnable *runnable;
bool autoDelete; bool volatile autoDelete;
unsigned int volatile threadId;
static int InternalRunner(void *); static int InternalRunner(void *);
void Quited(); void Quited();
@ -40,6 +41,9 @@ namespace spades {
virtual void Run(); virtual void Run();
static void InitThreadSystem();
static void CleanupExitedThreads();
void Start(); void Start();
void Join(); void Join();

View File

@ -28,6 +28,7 @@
#include <Core/Debug.h> #include <Core/Debug.h>
#include <Core/Settings.h> #include <Core/Settings.h>
#include <Core/ConcurrentDispatch.h> #include <Core/ConcurrentDispatch.h>
#include <Core/Thread.h>
#include <Core/ZipFileSystem.h> #include <Core/ZipFileSystem.h>
#include <Core/ServerAddress.h> #include <Core/ServerAddress.h>
#include "ErrorDialog.h" #include "ErrorDialog.h"
@ -159,6 +160,7 @@ int main(int argc, char ** argv)
spades::reflection::Backtrace::StartBacktrace(); spades::reflection::Backtrace::StartBacktrace();
SPADES_MARK_FUNCTION(); SPADES_MARK_FUNCTION();
spades::Thread::InitThreadSystem();
spades::DispatchQueue::GetThreadQueue()->MarkSDLVideoThread(); spades::DispatchQueue::GetThreadQueue()->MarkSDLVideoThread();
SPLog("Package: " PACKAGE_STRING); SPLog("Package: " PACKAGE_STRING);