From edd35ae3af70075e0d59b6409f6d206c6c08d85b Mon Sep 17 00:00:00 2001 From: brain Date: Thu, 4 Sep 2008 10:06:59 +0000 Subject: ability to create mutexes (rather than just having one system wide mutex) in the threadengines, allows for migration of m_mysql etc. git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@10381 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/inspircd.h | 4 ++++ include/threadengine.h | 10 ++++++++++ include/threadengines/threadengine_pthread.h | 19 +++++++++++++++++++ include/threadengines/threadengine_win32.h | 19 +++++++++++++++++++ src/threadengine.cpp | 3 +++ src/threadengines/threadengine_pthread.cpp | 26 ++++++++++++++++++++++++++ src/threadengines/threadengine_win32.cpp | 26 ++++++++++++++++++++++++++ 7 files changed, 107 insertions(+) diff --git a/include/inspircd.h b/include/inspircd.h index 12e22794d..09bf02ba5 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -446,6 +446,10 @@ class CoreExport InspIRCd : public classbase */ ThreadEngine* Threads; + /** Mutex engine, handles mutexes for threading where required + */ + MutexEngine* Mutexes; + /** The thread/class used to read config files in REHASH and on startup */ ConfigReaderThread* ConfigThread; diff --git a/include/threadengine.h b/include/threadengine.h index 59d425481..4e0db0bfc 100644 --- a/include/threadengine.h +++ b/include/threadengine.h @@ -82,6 +82,16 @@ class CoreExport ThreadEngine : public Extensible } }; +class CoreExport Mutex : public Extensible +{ + protected: + InspIRCd* ServerInstance; + public: + Mutex(InspIRCd* Instance); + virtual void Enable(bool enable) = 0; + ~Mutex() { } +}; + /** Derive from this class to implement your own threaded sections of * code. */ diff --git a/include/threadengines/threadengine_pthread.h b/include/threadengines/threadengine_pthread.h index 602484b4e..07bda4598 100644 --- a/include/threadengines/threadengine_pthread.h +++ b/include/threadengines/threadengine_pthread.h @@ -54,4 +54,23 @@ class CoreExport ThreadEngineFactory : public classbase } }; +class CoreExport PosixMutex : public Mutex +{ + private: + pthread_mutex_t putex + public: + PosixMutex(InspIRCd* Instance); + virtual void Enable(bool enable); + ~PosixMutex(); +}; + +class CoreExport MutexEngine : public Extensible +{ + protected: + InspIRCd* ServerInstance; + public: + virtual MutexEngine(InspIRCd* Instance); + Mutex* CreateMutex(); +}; + #endif diff --git a/include/threadengines/threadengine_win32.h b/include/threadengines/threadengine_win32.h index 39e6ae88d..e19f24ca0 100644 --- a/include/threadengines/threadengine_win32.h +++ b/include/threadengines/threadengine_win32.h @@ -53,5 +53,24 @@ class CoreExport ThreadEngineFactory : public classbase } }; +class CoreExport Win32Mutex : public Mutex +{ + private: + CRITICAL_SECTION wutex; + public: + Win32Mutex(InspIRCd* Instance); + virtual void Enable(bool enable); + ~Win32Mutex(); +}; + +class CoreExport MutexEngine : public Extensible +{ + protected: + InspIRCd* ServerInstance; + public: + MutexEngine(InspIRCd* Instance); + virtual Mutex* CreateMutex(); +}; + #endif diff --git a/src/threadengine.cpp b/src/threadengine.cpp index a6e17afa0..49538f891 100644 --- a/src/threadengine.cpp +++ b/src/threadengine.cpp @@ -28,3 +28,6 @@ ThreadEngine::~ThreadEngine() { } +Mutex::Mutex(InspIRCd* Instance) : ServerInstance(Instance) +{ +} diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index 2f09cc305..626cd4d55 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -105,3 +105,29 @@ void PThreadEngine::FreeThread(Thread* thread) } } +MutexEngine::MutexEngine(InspIRCd* Instance) : ServerInstance(Instance) +{ +} + +Mutex* MutexEngine::CreateMutex() +{ + return new PosixMutex(this->ServerInstance); +} + +PosixMutex::PosixMutex(InspIRCd* Instance) : Mutex(Instance) +{ + InitializeCriticalSection(&putex); +} + +PosixMutex::~PosixMutex() +{ + DeleteCriticalSection(&putex); +} + +void PosixMutex::Enable(bool enable) +{ + if (enable) + pthread_mutex_lock(&putex); + else + pthread_mutex_unlock(&putex); +} diff --git a/src/threadengines/threadengine_win32.cpp b/src/threadengines/threadengine_win32.cpp index 566611367..e2b58b42e 100644 --- a/src/threadengines/threadengine_win32.cpp +++ b/src/threadengines/threadengine_win32.cpp @@ -86,3 +86,29 @@ void Win32ThreadEngine::FreeThread(Thread* thread) } +MutexEngine::MutexEngine(InspIRCd* Instance) : ServerInstance(Instance) +{ +} + +Mutex* MutexEngine::CreateMutex() +{ + return new Win32Mutex(this->ServerInstance); +} + +Win32Mutex::Win32Mutex(InspIRCd* Instance) : Mutex(Instance) +{ + InitializeCriticalSection(&wutex); +} + +Win32Mutex::~Win32Mutex() +{ + DeleteCriticalSection(&wutex); +} + +void Win32Mutex::Enable(bool enable) +{ + if (enable) + EnterCriticalSection(&wutex); + else + LeaveCriticalSection(&wutex); +} \ No newline at end of file -- cgit v1.2.3