]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/threadengine.h
Remove the Kiwi links from the readme.
[user/henk/code/inspircd.git] / include / threadengine.h
index 9c87f83b267fadf58c442e7a06c97724b9f9b63e..0c2d26ce6e14f4546cb8b5a952c3ac40372838b4 100644 (file)
-/*       +------------------------------------+
- *       | 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) 2014 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2013, 2017 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2008 Craig Edwards <brain@inspircd.org>
  *
- * 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__
 
-#include <vector>
-#include <string>
-#include <map>
-#include "inspircd_config.h"
-#include "base.h"
+#pragma once
 
-class InspIRCd;
-class Thread;
+#include "base.h"
 
-/** 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?)
         */
-       virtual bool Mutex(bool enable) = 0;
+       bool GetExitFlag()
+       {
+               return ExitFlag;
+       }
+ public:
+       /** Set Creator to NULL at this point
+        */
+       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;
-
-       virtual const std::string GetName()
-       {
-               return "<pure-virtual>";
-       }
+       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();
+       }
+       void SetExitFlag() CXX11_OVERRIDE
+       {
+               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;
+       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
+        */
+       void WaitForQueue()
+       {
+               queue.Wait();
+       }
  public:
-
-       /** Creator thread engine
+       /** Notifies parent by making the SignalFD ready to read
+        * No requirements on locking
         */
-       ThreadEngine* Creator;
-
-       /** Set Creator to NULL at this point
+       void NotifyParent();
+       SocketThread();
+       virtual ~SocketThread();
+       /** Lock queue.
         */
-       Thread() : ExitFlag(false), Creator(NULL)
+       void LockQueue()
        {
+               queue.Lock();
        }
-
-       /** If this thread has a Creator set, call it to
-        * free the thread
+       /** Unlock queue.
         */
-       virtual ~Thread()
+       void UnlockQueue()
        {
-               if (Creator)
-                       Creator->FreeThread(this);
+               queue.Unlock();
        }
-
-       /** Override this method to put your actual
-        * threaded code here
+       /** Unlock queue and send wakeup to worker
         */
-       virtual void Run() = 0;
-
-       void SetExitFlag()
+       void UnlockQueueWakeup()
        {
-               ExitFlag = true;
+               queue.Wakeup();
+               queue.Unlock();
        }
-
-       void ClearExitFlag()
+       void SetExitFlag() CXX11_OVERRIDE
        {
-               ExitFlag = false;
+               queue.Lock();
+               Thread::SetExitFlag();
+               queue.Wakeup();
+               queue.Unlock();
        }
 
-       bool GetExitFlag()
-       {
-               return ExitFlag;
-       }
+       /**
+        * Called in the context of the parent thread after a notification
+        * has passed through the socket
+        */
+       virtual void OnNotify() = 0;
 };
-
-
-
-#endif
-