X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fthreadengine.h;h=41e3fc68612cfe42a5270187ff416cd9d6d2a653;hb=6d03943426dcce76ba66567a9b18425a5ebb4c0c;hp=9c87f83b267fadf58c442e7a06c97724b9f9b63e;hpb=0d6df835657bda160288fe8d9567381a25bac91e;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/threadengine.h b/include/threadengine.h index 9c87f83b2..41e3fc686 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,130 +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(); }; -class CoreExport Mutex : public Extensible + +class CoreExport QueuedThread : public Thread { + ThreadQueueData queue; protected: - InspIRCd* ServerInstance; + /** 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: - Mutex(InspIRCd* Instance); - virtual void Enable(bool enable) = 0; - void Lock() { Enable(true); } - void Unlock() { Enable(false); } - ~Mutex() { } + /** Lock queue. + */ + void LockQueue() + { + queue.Lock(); + } + /** Unlock queue. + */ + void UnlockQueue() + { + queue.Unlock(); + } + /** Unlock queue and wake up worker + */ + void UnlockQueueWakeup() + { + queue.Wakeup(); + queue.Unlock(); + } + virtual void SetExitFlag() + { + queue.Lock(); + Thread::SetExitFlag(); + queue.Wakeup(); + queue.Unlock(); + } }; -/** Derive from this class to implement your own threaded sections of - * code. - */ -class CoreExport Thread : public Extensible +class CoreExport SocketThread : public Thread { - private: - bool ExitFlag; - public: - - /** Creator thread engine - */ - ThreadEngine* Creator; - - /** Set Creator to NULL at this point + 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 */ - Thread() : ExitFlag(false), Creator(NULL) + void WaitForQueue() { + queue.Wait(); } - - /** If this thread has a Creator set, call it to - * free the thread + /** Notifies parent by making the SignalFD ready to read + * No requirements on locking */ - virtual ~Thread() + void NotifyParent(); + public: + SocketThread(); + virtual ~SocketThread(); + /** Lock queue. + */ + void LockQueue() { - if (Creator) - Creator->FreeThread(this); + queue.Lock(); } - - /** Override this method to put your actual - * threaded code here + /** Unlock queue. */ - virtual void Run() = 0; - - void SetExitFlag() + void UnlockQueue() { - ExitFlag = true; + queue.Unlock(); } - - void ClearExitFlag() + /** Unlock queue and send wakeup to worker + */ + void UnlockQueueWakeup() { - ExitFlag = false; + queue.Wakeup(); + queue.Unlock(); } - - bool GetExitFlag() + virtual void SetExitFlag() { - return ExitFlag; + 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