From 7b6eae36661bc798f69b22393b85a4f06d533cf6 Mon Sep 17 00:00:00 2001 From: danieldg Date: Mon, 23 Mar 2009 18:48:32 +0000 Subject: ThreadEngine: remove excessive mutex use on thread creation git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11249 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/threadengine.h | 78 +++++++--------------------- include/threadengines/threadengine_pthread.h | 19 ++++--- include/threadengines/threadengine_win32.h | 19 ++++--- 3 files changed, 37 insertions(+), 79 deletions(-) (limited to 'include') diff --git a/include/threadengine.h b/include/threadengine.h index eb831ea46..228f817ca 100644 --- a/include/threadengine.h +++ b/include/threadengine.h @@ -41,20 +41,6 @@ class CoreExport ThreadEngine : public Extensible */ InspIRCd* ServerInstance; - /** New Thread being created. - */ - Thread* NewThread; - - /** Enable or disable system-wide mutex for threading. - * Remember that if you toggle the mutex you MUST UNSET - * IT LATER otherwise the program will DEADLOCK! - * It is recommended that you AVOID USE OF THIS METHOD - * and use your own Mutex class, this function is mainly - * reserved for use by the core and by the Thread engine - * itself. - * @param enable True to lock the mutex. - */ - virtual bool Mutex(bool enable) = 0; public: /** Constructor. @@ -66,34 +52,13 @@ class CoreExport ThreadEngine : public Extensible */ virtual ~ThreadEngine(); - /** Lock the system wide mutex. See the documentation for - * ThreadEngine::Mutex(). - */ - void Lock() { this->Mutex(true); } - - /** Unlock the system wide mutex. See the documentation for - * ThreadEngine::Mutex() - */ - void Unlock() { this->Mutex(false); } - - /** Run the newly created thread. - */ - virtual void Run() = 0; - /** Create a new thread. This takes an already allocated * Thread* pointer and initializes it to use this threading * engine. On failure, this function may throw a CoreException. * @param thread_to_init Pointer to a newly allocated Thread * derived object. */ - virtual void Create(Thread* thread_to_init) = 0; - - /** This is called by the default destructor of the Thread - * class to ensure that the thread engine which created the thread - * is responsible for destroying it. - * @param thread Existing and active thread to delete. - */ - virtual void FreeThread(Thread* thread) = 0; + virtual void Start(Thread* thread_to_init) = 0; /** Returns the thread engine's name for display purposes * @return The thread engine name @@ -112,14 +77,9 @@ class CoreExport ThreadEngine : public Extensible * in InspIRCd uses critical sections, as they are faster and simpler to * manage. */ -class CoreExport Mutex : public Extensible +class CoreExport Mutex { protected: - - /** Creator object - */ - InspIRCd* ServerInstance; - /** Enable or disable the Mutex. This method has somewhat confusing * wording (e.g. the function name and parameters) so it is protected * in preference of the Lock() and Unlock() methods which are user- @@ -132,9 +92,8 @@ class CoreExport Mutex : public Extensible public: /** Constructor. - * @param Instance Creator object */ - Mutex(InspIRCd* Instance); + Mutex(); /** Enter/enable the mutex lock. */ @@ -149,6 +108,12 @@ class CoreExport Mutex : public Extensible ~Mutex() { } }; +class CoreExport ThreadData +{ + public: + virtual void FreeThread(Thread* thread) { } +}; + /** Derive from this class to implement your own threaded sections of * code. Be sure to keep your code thread-safe and not prone to deadlocks * and race conditions if you MUST use threading! @@ -160,14 +125,13 @@ class CoreExport Thread : public Extensible */ bool ExitFlag; public: - - /** Creator thread engine + /** Opaque thread state managed by threading engine */ - ThreadEngine* Creator; + ThreadData* state; /** Set Creator to NULL at this point */ - Thread() : ExitFlag(false), Creator(NULL) + Thread() : ExitFlag(false), state(NULL) { } @@ -176,8 +140,11 @@ class CoreExport Thread : public Extensible */ virtual ~Thread() { - if (Creator) - Creator->FreeThread(this); + if (state) + { + state->FreeThread(this); + delete state; + } } /** Override this method to put your actual @@ -187,16 +154,9 @@ class CoreExport Thread : public Extensible /** Signal the thread to exit gracefully. */ - void SetExitFlag() - { - ExitFlag = true; - } - - /** Cancel an exit state. - */ - void ClearExitFlag() + void SetExitFlag(bool value) { - ExitFlag = false; + ExitFlag = value; } /** Get thread's current exit status. diff --git a/include/threadengines/threadengine_pthread.h b/include/threadengines/threadengine_pthread.h index 4db1a6908..72fa1d219 100644 --- a/include/threadengines/threadengine_pthread.h +++ b/include/threadengines/threadengine_pthread.h @@ -23,21 +23,13 @@ class InspIRCd; class CoreExport PThreadEngine : public ThreadEngine { - private: - - bool Mutex(bool enable); - public: PThreadEngine(InspIRCd* Instance); virtual ~PThreadEngine(); - void Run(); - - static void* Entry(void* parameter); - - void Create(Thread* thread_to_init); + void Start(Thread* thread_to_init); void FreeThread(Thread* thread); @@ -56,12 +48,19 @@ class CoreExport ThreadEngineFactory : public classbase } }; +class CoreExport PThreadData : public ThreadData +{ + public: + pthread_t pthread_id; + void FreeThread(Thread* toFree); +}; + class CoreExport PosixMutex : public Mutex { private: pthread_mutex_t putex; public: - PosixMutex(InspIRCd* Instance); + PosixMutex(); virtual void Enable(bool enable); ~PosixMutex(); }; diff --git a/include/threadengines/threadengine_win32.h b/include/threadengines/threadengine_win32.h index d6d98b011..3388cead0 100644 --- a/include/threadengines/threadengine_win32.h +++ b/include/threadengines/threadengine_win32.h @@ -22,23 +22,15 @@ class InspIRCd; class CoreExport Win32ThreadEngine : public ThreadEngine { - protected: - - bool Mutex(bool enable); - public: Win32ThreadEngine(InspIRCd* Instance); virtual ~Win32ThreadEngine(); - void Run(); - static DWORD WINAPI Entry(void* parameter); - void Create(Thread* thread_to_init); - - void FreeThread(Thread* thread); + void Start(Thread* thread_to_init); const std::string GetName() { @@ -55,12 +47,19 @@ class CoreExport ThreadEngineFactory : public classbase } }; +class CoreExport Win32ThreadData : public ThreadData +{ + public: + HANDLE handle; + void FreeThread(Thread* toFree); +}; + class CoreExport Win32Mutex : public Mutex { private: CRITICAL_SECTION wutex; public: - Win32Mutex(InspIRCd* Instance); + Win32Mutex(); virtual void Enable(bool enable); ~Win32Mutex(); }; -- cgit v1.2.3