1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __THREADENGINE__
15 #define __THREADENGINE__
20 #include "inspircd_config.h"
26 /** The ThreadEngine class has the responsibility of initialising
27 * Thread derived classes. It does this by creating operating system
28 * level threads which are then associated with the class transparently.
29 * This allows Thread classes to be derived without needing to know how
30 * the OS implements threads. You should ensure that any sections of code
31 * that use threads are threadsafe and do not interact with any other
32 * parts of the code which are NOT known threadsafe!
34 class CoreExport ThreadEngine : public Extensible
40 InspIRCd* ServerInstance;
41 /** New Thread being created.
49 ThreadEngine(InspIRCd* Instance);
53 virtual ~ThreadEngine();
55 /** Enable or disable system-wide mutex for threading.
56 * This MUST be called when you deal with ANYTHING that
57 * isnt known thread-safe, this INCLUDES STL!
58 * Remember that if you toggle the mutex you MUST UNSET
59 * IT LATER otherwise the program will DEADLOCK!
61 virtual bool Mutex(bool enable) = 0;
63 /** Run the newly created thread
65 virtual void Run() = 0;
67 /** Create a new thread. This takes an already allocated
68 * Thread* pointer and initializes it to use this threading
69 * engine. On failure, this function may throw a CoreException.
71 virtual void Create(Thread* thread_to_init) = 0;
73 /** This is called by the default destructor of the Thread
74 * class to ensure that the thread engine which created the thread
75 * is responsible for destroying it.
77 virtual void FreeThread(Thread* thread) = 0;
79 virtual const std::string GetName()
81 return "<pure-virtual>";
85 /** Derive from this class to implement your own threaded sections of
88 class CoreExport Thread : public Extensible
94 /** Creator thread engine
96 ThreadEngine* Creator;
98 /** Set Creator to NULL at this point
100 Thread() : ExitFlag(false), Creator(NULL)
104 /** If this thread has a Creator set, call it to
110 Creator->FreeThread(this);
113 /** Override this method to put your actual
116 virtual void Run() = 0;