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! If you really MUST
33 * access non-threadsafe code from a Thread, use the Mutex class to wrap
34 * access to the code carefully.
36 class CoreExport ThreadEngine : public Extensible
42 InspIRCd* ServerInstance;
43 /** New Thread being created.
50 * @param Instance Creator object
52 ThreadEngine(InspIRCd* Instance);
56 virtual ~ThreadEngine();
58 /** Enable or disable system-wide mutex for threading.
59 * Remember that if you toggle the mutex you MUST UNSET
60 * IT LATER otherwise the program will DEADLOCK!
61 * It is recommended that you AVOID USE OF THIS METHOD
62 * and use your own Mutex class, this function is mainly
63 * reserved for use by the core and by the Thread engine
65 * @param enable True to lock the mutex.
67 virtual bool Mutex(bool enable) = 0;
69 /** Run the newly created thread.
71 virtual void Run() = 0;
73 /** Create a new thread. This takes an already allocated
74 * Thread* pointer and initializes it to use this threading
75 * engine. On failure, this function may throw a CoreException.
76 * @param thread_to_init Pointer to a newly allocated Thread
79 virtual void Create(Thread* thread_to_init) = 0;
81 /** This is called by the default destructor of the Thread
82 * class to ensure that the thread engine which created the thread
83 * is responsible for destroying it.
84 * @param thread Existing and active thread to delete.
86 virtual void FreeThread(Thread* thread) = 0;
88 /** Returns the thread engine's name for display purposes
89 * @return The thread engine name
91 virtual const std::string GetName()
93 return "<pure-virtual>";
97 /** The Mutex class represents a mutex, which can be used to keep threads
98 * properly synchronised. Use mutexes sparingly, as they are a good source
99 * of thread deadlocks etc, and should be avoided except where absolutely
100 * neccessary. Note that the internal behaviour of the mutex varies from OS
101 * to OS depending on the thread engine, for example in windows a Mutex
102 * in InspIRCd uses critical sections, as they are faster and simpler to
105 class CoreExport Mutex : public Extensible
111 InspIRCd* ServerInstance;
113 /** Enable or disable the Mutex. This method has somewhat confusing
114 * wording (e.g. the function name and parameters) so it is protected
115 * in preference of the Lock() and Unlock() methods which are user-
118 * @param enable True to enable the mutex (enter it) and false to
119 * disable the mutex (leave it).
121 virtual void Enable(bool enable) = 0;
125 * @param Instance Creator object
127 Mutex(InspIRCd* Instance);
129 /** Enter/enable the mutex lock.
131 void Lock() { Enable(true); }
133 /** Leave/disable the mutex lock.
135 void Unlock() { Enable(false); }
142 /** Derive from this class to implement your own threaded sections of
143 * code. Be sure to keep your code thread-safe and not prone to deadlocks
144 * and race conditions if you MUST use threading!
146 class CoreExport Thread : public Extensible
149 /** Set to true when the thread is to exit
154 /** Creator thread engine
156 ThreadEngine* Creator;
158 /** Set Creator to NULL at this point
160 Thread() : ExitFlag(false), Creator(NULL)
164 /** If this thread has a Creator set, call it to
170 Creator->FreeThread(this);
173 /** Override this method to put your actual
174 * threaded code here.
176 virtual void Run() = 0;
178 /** Signal the thread to exit gracefully.
185 /** Cancel an exit state.
192 /** Get thread's current exit status.
193 * (are we being asked to exit?)