]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/threadengines/threadengine_pthread.cpp
Working thread test!!!
[user/henk/code/inspircd.git] / src / threadengines / threadengine_pthread.cpp
index c35388cdff9fb5900e533870960f36e3fb84cf39..8bdcd6d96cad3e3183848c6ca042cd85f829c406 100644 (file)
@@ -36,14 +36,20 @@ void PThreadEngine::Create(Thread* thread_to_init)
 {
        pthread_attr_t attribs;
        pthread_attr_init(&attribs);
-       if (pthread_create(&this->MyPThread,
-                               &attribs,
-                               PThreadEngine::Entry,
-                               (void*)this) != 0)
+       pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
+       pthread_t* MyPThread = new pthread_t;
+
+       if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0)
        {
+               delete MyPThread;
                throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
        }
+
+       pthread_attr_destroy(&attribs);
+
        NewThread = thread_to_init;
+       NewThread->Creator = this;
+       NewThread->Extend("pthread", MyPThread);
 }
 
 PThreadEngine::~PThreadEngine()
@@ -53,9 +59,6 @@ PThreadEngine::~PThreadEngine()
 
 void PThreadEngine::Run()
 {
-       /* This is a factory function that will create a class of type 'Thread'. The class of type Thread just
-        * takes an InspIRCd* pointer and a ThreadEngine* pointer in its ctor (so it can easily use the Mutex
-        * methods etc) and runs asyncronously of the ircd. */
        NewThread->Run();
 }
 
@@ -76,3 +79,16 @@ void* PThreadEngine::Entry(void* parameter)
        return NULL;
 }
 
+void PThreadEngine::FreeThread(Thread* thread)
+{
+       pthread_t* pthread = NULL;
+       if (thread->GetExt("pthread", pthread))
+       {
+               thread->SetExitFlag();
+               int rc;
+               void* status;
+               rc = pthread_join(*pthread, &status);
+               delete pthread;
+       }
+}
+