Made Thread
more stable (I’m not sure this works because it was already stable on OS X)
This commit is contained in:
parent
e2aa810524
commit
b51a751dc0
@ -211,6 +211,7 @@ namespace spades {
|
||||
while((ent = internal->Poll()) != NULL){
|
||||
ent->dispatch->Execute();
|
||||
}
|
||||
Thread::CleanupExitedThreads();
|
||||
}
|
||||
|
||||
void DispatchQueue::EnterEventLoop() throw() {
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "AutoLocker.h"
|
||||
#include "Debug.h"
|
||||
#include "ThreadLocalStorage.h"
|
||||
#include "ConcurrentDispatch.h"
|
||||
|
||||
namespace spades {
|
||||
|
||||
@ -33,12 +34,55 @@ namespace spades {
|
||||
}
|
||||
|
||||
Thread::Thread(IRunnable *r):
|
||||
runnable(r){
|
||||
runnable(r),
|
||||
autoDelete(false){
|
||||
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() {
|
||||
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() {
|
||||
@ -46,10 +90,14 @@ namespace spades {
|
||||
if(threadInfo)
|
||||
return;
|
||||
|
||||
threadId = 0;
|
||||
threadInfo = SDL_CreateThread(InternalRunner, this);
|
||||
}
|
||||
|
||||
void Thread::Join() {
|
||||
if(autoDelete) {
|
||||
SPRaise("Attempted join a thread that is marked for auto deletion.");
|
||||
}
|
||||
SDL_Thread *th = NULL;
|
||||
{
|
||||
AutoLocker locker(&lock);
|
||||
@ -61,13 +109,17 @@ namespace spades {
|
||||
}
|
||||
|
||||
bool Thread::IsAlive() {
|
||||
if(autoDelete) {
|
||||
SPRaise("Attempted query the state of a thread that is marked for auto deletion.");
|
||||
}
|
||||
return threadInfo != NULL;
|
||||
}
|
||||
|
||||
int Thread::InternalRunner(void *th) {
|
||||
Thread *self = (Thread *)th;
|
||||
self->threadId = SDL_ThreadID();
|
||||
try{
|
||||
SPADES_MARK_FUNCTION();
|
||||
Thread *self = (Thread *)th;
|
||||
self->Run();
|
||||
self->Quited();
|
||||
}catch(const std::exception& ex){
|
||||
|
@ -26,10 +26,11 @@
|
||||
namespace spades {
|
||||
|
||||
class Thread {
|
||||
void *threadInfo;
|
||||
void * volatile threadInfo;
|
||||
Mutex lock;
|
||||
IRunnable *runnable;
|
||||
bool autoDelete;
|
||||
bool volatile autoDelete;
|
||||
unsigned int volatile threadId;
|
||||
|
||||
static int InternalRunner(void *);
|
||||
void Quited();
|
||||
@ -40,6 +41,9 @@ namespace spades {
|
||||
|
||||
virtual void Run();
|
||||
|
||||
static void InitThreadSystem();
|
||||
static void CleanupExitedThreads();
|
||||
|
||||
void Start();
|
||||
void Join();
|
||||
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include <Core/Debug.h>
|
||||
#include <Core/Settings.h>
|
||||
#include <Core/ConcurrentDispatch.h>
|
||||
#include <Core/Thread.h>
|
||||
#include <Core/ZipFileSystem.h>
|
||||
#include <Core/ServerAddress.h>
|
||||
#include "ErrorDialog.h"
|
||||
@ -159,6 +160,7 @@ int main(int argc, char ** argv)
|
||||
spades::reflection::Backtrace::StartBacktrace();
|
||||
|
||||
SPADES_MARK_FUNCTION();
|
||||
spades::Thread::InitThreadSystem();
|
||||
spades::DispatchQueue::GetThreadQueue()->MarkSDLVideoThread();
|
||||
|
||||
SPLog("Package: " PACKAGE_STRING);
|
||||
|
Loading…
x
Reference in New Issue
Block a user