-/* +------------------------------------+
- * | Inspire Internet Relay Chat Daemon |
- * +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
*
- * InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
*
- * This program is free but copyrighted software; see
- * the file COPYING for details.
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
*
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef __THREADENGINE__
-#define __THREADENGINE__
+
+#pragma once
#include <vector>
#include <string>
#include <map>
-#include "inspircd_config.h"
+#include "config.h"
#include "base.h"
-class InspIRCd;
-class Thread;
-
-/** 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
{
- protected:
-
- /** Creator instance
- */
- InspIRCd* ServerInstance;
- /** New Thread being created.
- */
- Thread* NewThread;
-
- public:
+ private:
+ /** Set to true when the thread is to exit
+ */
+ bool ExitFlag;
- /** Constructor
+ /** Opaque thread state managed by the ThreadEngine
*/
- ThreadEngine(InspIRCd* Instance);
+ ThreadEngine::ThreadState state;
- /** Destructor
+ /** ThreadEngine manages Thread::state
*/
- virtual ~ThreadEngine();
+ friend class ThreadEngine;
- /** 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!
+ protected:
+ /** Get thread's current exit status.
+ * (are we being asked to exit?)
+ */
+ bool GetExitFlag()
+ {
+ return ExitFlag;
+ }
+ public:
+ /** Set Creator to NULL at this point
*/
- virtual bool Mutex(bool enable) = 0;
+ Thread() : ExitFlag(false)
+ {
+ }
- /** 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;
+ 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()
+ public:
+ /** Notifies parent by making the SignalFD ready to read
+ * No requirements on locking
+ */
+ void NotifyParent();
+ SocketThread();
+ 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();
}
-};
-
-
-
-#endif
+ /**
+ * Called in the context of the parent thread after a notification
+ * has passed through the socket
+ */
+ virtual void OnNotify() = 0;
+};