diff options
-rw-r--r-- | include/threadengine.h | 32 | ||||
-rw-r--r-- | include/threadengines/threadengine_pthread.h | 6 | ||||
-rw-r--r-- | src/threadengines/threadengine_pthread.cpp | 23 |
3 files changed, 43 insertions, 18 deletions
diff --git a/include/threadengine.h b/include/threadengine.h index 7d27b867a..0f729a962 100644 --- a/include/threadengine.h +++ b/include/threadengine.h @@ -21,14 +21,8 @@ #include "base.h" class InspIRCd; +class Thread; -class CoreExport Thread : public Extensible -{ - public: - Thread() { }; - virtual ~Thread() { }; - virtual void Run() = 0; -}; class CoreExport ThreadEngine : public Extensible { @@ -48,6 +42,30 @@ class CoreExport ThreadEngine : public Extensible virtual void Run() = 0; virtual void Create(Thread* thread_to_init) = 0; + + virtual void FreeThread(Thread* thread) = 0; }; +class CoreExport Thread : public Extensible +{ + public: + + ThreadEngine* Creator; + + Thread() : Creator(NULL) + { + } + + virtual ~Thread() + { + if (Creator) + Creator->FreeThread(this); + } + + virtual void Run() = 0; +}; + + + #endif + diff --git a/include/threadengines/threadengine_pthread.h b/include/threadengines/threadengine_pthread.h index db32acbcc..2c821d93d 100644 --- a/include/threadengines/threadengine_pthread.h +++ b/include/threadengines/threadengine_pthread.h @@ -23,10 +23,6 @@ class InspIRCd; class CoreExport PThreadEngine : public ThreadEngine { - private: - - pthread_t MyPThread; - public: PThreadEngine(InspIRCd* Instance); @@ -40,6 +36,8 @@ class CoreExport PThreadEngine : public ThreadEngine static void* Entry(void* parameter); void Create(Thread* thread_to_init); + + void FreeThread(Thread* thread); }; #endif diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index c35388cdf..da120bfab 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -36,14 +36,17 @@ 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_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))); } + NewThread = thread_to_init; + NewThread->Creator = this; + NewThread->Extend("pthread", MyPThread); } PThreadEngine::~PThreadEngine() @@ -53,9 +56,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 +76,12 @@ void* PThreadEngine::Entry(void* parameter) return NULL; } +void PThreadEngine::FreeThread(Thread* thread) +{ + pthread_t* pthread = NULL; + if (thread->GetExt("pthread", pthread)) + { + delete pthread; + } +} + |