X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fthreadengine.h;h=10f3fed13b5fa6684397f105deace4386244e65e;hb=a97624bd4b0ea052ca1380f9164769d6a6cab2a3;hp=59d425481bc142aca3a878e987b9d514b4f835cd;hpb=9bc7a6139db7ec7f1da676c486ca309f070a78e4;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/threadengine.h b/include/threadengine.h index 59d425481..10f3fed13 100644 --- a/include/threadengine.h +++ b/include/threadengine.h @@ -2,8 +2,8 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd: (C) 2002-2008 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * See: http://wiki.inspircd.org/Credits * * This program is free but copyrighted software; see * the file COPYING for details. @@ -20,118 +20,155 @@ #include "inspircd_config.h" #include "base.h" -class InspIRCd; -class Thread; +#ifdef WINDOWS +#include "threadengines/threadengine_win32.h" +#endif + +class ThreadData; -/** The ThreadEngine class has the responsibility of initialising - * Thread derived classes. It does this by creating operating system - * level threads which are then associated with the class transparently. - * This allows Thread classes to be derived without needing to know how - * the OS implements threads. You should ensure that any sections of code - * that use threads are threadsafe and do not interact with any other - * parts of the code which are NOT known threadsafe! +/** 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! */ -class CoreExport ThreadEngine : public Extensible +class CoreExport Thread { + private: + /** Set to true when the thread is to exit + */ + bool ExitFlag; protected: - - /** Creator instance - */ - InspIRCd* ServerInstance; - /** New Thread being created. - */ - Thread* NewThread; - + /** Get thread's current exit status. + * (are we being asked to exit?) + */ + bool GetExitFlag() + { + return ExitFlag; + } public: - - /** Constructor + /** Opaque thread state managed by threading engine */ - ThreadEngine(InspIRCd* Instance); + ThreadData* state; - /** Destructor + /** Set Creator to NULL at this point */ - virtual ~ThreadEngine(); + Thread() : ExitFlag(false), state(NULL) + { + } - /** Enable or disable system-wide mutex for threading. - * This MUST be called when you deal with ANYTHING that - * isnt known thread-safe, this INCLUDES STL! - * Remember that if you toggle the mutex you MUST UNSET - * IT LATER otherwise the program will DEADLOCK! - */ - virtual bool Mutex(bool enable) = 0; + /* If the thread is running, you MUST join BEFORE deletion */ + virtual ~Thread(); - /** Run the newly created thread + /** Override this method to put your actual + * threaded code here. */ 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. + /** Signal the thread to exit gracefully. */ - virtual void Create(Thread* thread_to_init) = 0; + virtual void SetExitFlag(); - /** 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. + /** Join the thread (calls SetExitFlag and waits for exit) */ - virtual void FreeThread(Thread* thread) = 0; - - virtual const std::string GetName() - { - return ""; - } + void join(); }; -/** Derive from this class to implement your own threaded sections of - * code. - */ -class CoreExport Thread : public Extensible + +class CoreExport QueuedThread : public Thread { - private: - bool ExitFlag; + ThreadQueueData queue; + protected: + /** Waits for an enqueue operation to complete + * You MUST hold the queue lock when you call this. + * It will be unlocked while you wait, and will be relocked + * before the function returns + */ + void WaitForQueue() + { + queue.Wait(); + } public: - - /** Creator thread engine + /** Lock queue. */ - ThreadEngine* Creator; - - /** Set Creator to NULL at this point + void LockQueue() + { + queue.Lock(); + } + /** Unlock queue. */ - Thread() : ExitFlag(false), Creator(NULL) + void UnlockQueue() { + queue.Unlock(); } - - /** If this thread has a Creator set, call it to - * free the thread + /** Unlock queue and wake up worker */ - virtual ~Thread() + void UnlockQueueWakeup() + { + queue.Wakeup(); + queue.Unlock(); + } + virtual void SetExitFlag() { - if (Creator) - Creator->FreeThread(this); + queue.Lock(); + Thread::SetExitFlag(); + queue.Wakeup(); + queue.Unlock(); } +}; - /** Override this method to put your actual - * threaded code here +class CoreExport SocketThread : public Thread +{ + ThreadQueueData queue; + ThreadSignalData signal; + protected: + /** Waits for an enqueue operation to complete + * You MUST hold the queue lock when you call this. + * It will be unlocked while you wait, and will be relocked + * before the function returns */ - virtual void Run() = 0; - - void SetExitFlag() + void WaitForQueue() { - ExitFlag = true; + queue.Wait(); } - - void ClearExitFlag() + /** Notifies parent by making the SignalFD ready to read + * No requirements on locking + */ + void NotifyParent(); + public: + SocketThread(InspIRCd* SI); + virtual ~SocketThread(); + /** Lock queue. + */ + void LockQueue() { - ExitFlag = false; + queue.Lock(); } - - bool GetExitFlag() + /** Unlock queue. + */ + void UnlockQueue() { - return ExitFlag; + queue.Unlock(); + } + /** Unlock queue and send wakeup to worker + */ + void UnlockQueueWakeup() + { + queue.Wakeup(); + queue.Unlock(); + } + virtual void SetExitFlag() + { + queue.Lock(); + Thread::SetExitFlag(); + queue.Wakeup(); + queue.Unlock(); } -}; - + /** + * Called in the context of the parent thread after a notification + * has passed through the socket + */ + virtual void OnNotify() = 0; +}; #endif