]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/threadengines/threadengine_pthread.cpp
Remove unneeded "fd" member of issl_session
[user/henk/code/inspircd.git] / src / threadengines / threadengine_pthread.cpp
index c35388cdff9fb5900e533870960f36e3fb84cf39..15f5567a4a90d2609d30177168417e4f5443c4fe 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
  * See: http://www.inspircd.org/wiki/index.php/Credits
  *
  * This program is free but copyrighted software; see
  * ---------------------------------------------------
  */
 
-/* $Core: libIRCDthreadengine */
-
-/*********        DEFAULTS       **********/
-/* $ExtraSources: socketengines/socketengine_pthread.cpp */
-/* $ExtraObjects: socketengine_pthread.o */
-
-/* $If: USE_WIN32 */
-/* $ExtraSources: socketengines/socketengine_win32.cpp */
-/* $ExtraObjects: socketengine_win32.o */
-/* $EndIf */
-
 #include "inspircd.h"
 #include "threadengines/threadengine_pthread.h"
 #include <pthread.h>
+#include <signal.h>
 
 pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;
 
@@ -36,27 +26,54 @@ 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;
+
+       /* 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)));
        }
+
+       pthread_attr_destroy(&attribs);
+
        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()
 {
-       //pthread_kill(this->MyPThread, SIGKILL);
 }
 
 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();
+       /* 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)
@@ -71,8 +88,54 @@ bool PThreadEngine::Mutex(bool enable)
 
 void* PThreadEngine::Entry(void* parameter)
 {
+       /* Recommended by nenolod, signal safety on a per-thread basis */
+       sigset_t set;
+       sigemptyset(&set);
+       sigaddset(&set, SIGPIPE);
+       if(pthread_sigmask(SIG_BLOCK, &set, NULL))
+               signal(SIGPIPE, SIG_IGN);
+
        ThreadEngine * pt = (ThreadEngine*)parameter;
        pt->Run();
        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;
+       }
+}
+
+MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
+{
+}
+
+Mutex* MutexFactory::CreateMutex()
+{
+       return new PosixMutex(this->ServerInstance);
+}
+
+PosixMutex::PosixMutex(InspIRCd* Instance) : Mutex(Instance)
+{
+       pthread_mutex_init(&putex, NULL);
+}
+
+PosixMutex::~PosixMutex()
+{
+       pthread_mutex_destroy(&putex);
+}
+
+void PosixMutex::Enable(bool enable)
+{
+       if (enable)
+               pthread_mutex_lock(&putex);
+       else
+               pthread_mutex_unlock(&putex);
+}