X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fthreadengines%2Fthreadengine_pthread.cpp;h=2f09cc30556e7b4cfe189bf4ed675ecc6954963c;hb=82bf46fa061a4f18071e73756e4e0d618eb57320;hp=94365bd30182ab084fca565426792d1ee973fa4b;hpb=a0369fc40d272bba0ce9e8350ae6c38d7bc0c1fa;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index 94365bd30..2f09cc305 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -28,9 +28,16 @@ void PThreadEngine::Create(Thread* thread_to_init) pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE); pthread_t* MyPThread = new pthread_t; + /* Create a thread in a mutex. This prevents whacking the member value NewThread, + * and also prevents recursive creation of threads by mistake (instead, the thread + * will just deadlock itself) + */ + Mutex(true); + if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0) { delete MyPThread; + Mutex(false); throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno))); } @@ -39,6 +46,15 @@ void PThreadEngine::Create(Thread* thread_to_init) NewThread = thread_to_init; NewThread->Creator = this; NewThread->Extend("pthread", MyPThread); + + /* Always unset a mutex if you set it */ + Mutex(false); + + /* Wait for the PThreadEngine::Run method to take a copy of the + * pointer and clear this member value + */ + while (NewThread) + usleep(1000); } PThreadEngine::~PThreadEngine() @@ -47,7 +63,16 @@ PThreadEngine::~PThreadEngine() void PThreadEngine::Run() { - NewThread->Run(); + /* Take a copy of the member value, then clear it. Do this + * in a mutex so that we can be sure nothing else is looking + * at it. + */ + Mutex(true); + Thread* nt = NewThread; + NewThread = NULL; + Mutex(false); + /* Now we have our own safe copy, call the object on it */ + nt->Run(); } bool PThreadEngine::Mutex(bool enable)