1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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;
47 * @param Instance Creator object
49 ThreadEngine(InspIRCd* Instance);
53 virtual ~ThreadEngine();
55 /** Create a new thread. This takes an already allocated
56 * Thread* pointer and initializes it to use this threading
57 * engine. On failure, this function may throw a CoreException.
58 * @param thread_to_init Pointer to a newly allocated Thread
61 virtual void Start(Thread* thread_to_init) = 0;
63 /** Returns the thread engine's name for display purposes
64 * @return The thread engine name
66 virtual const std::string GetName()
68 return "<pure-virtual>";
72 /** The Mutex class represents a mutex, which can be used to keep threads
73 * properly synchronised. Use mutexes sparingly, as they are a good source
74 * of thread deadlocks etc, and should be avoided except where absolutely
75 * neccessary. Note that the internal behaviour of the mutex varies from OS
76 * to OS depending on the thread engine, for example in windows a Mutex
77 * in InspIRCd uses critical sections, as they are faster and simpler to
80 class CoreExport Mutex
83 /** Enable or disable the Mutex. This method has somewhat confusing
84 * wording (e.g. the function name and parameters) so it is protected
85 * in preference of the Lock() and Unlock() methods which are user-
88 * @param enable True to enable the mutex (enter it) and false to
89 * disable the mutex (leave it).
91 virtual void Enable(bool enable) = 0;
98 /** Enter/enable the mutex lock.
100 void Lock() { Enable(true); }
102 /** Leave/disable the mutex lock.
104 void Unlock() { Enable(false); }
111 class CoreExport ThreadData
114 virtual void FreeThread(Thread* thread) { }
117 /** Derive from this class to implement your own threaded sections of
118 * code. Be sure to keep your code thread-safe and not prone to deadlocks
119 * and race conditions if you MUST use threading!
121 class CoreExport Thread : public Extensible
124 /** Set to true when the thread is to exit
128 /** Opaque thread state managed by threading engine
132 /** Set Creator to NULL at this point
134 Thread() : ExitFlag(false), state(NULL)
138 /** If this thread has a Creator set, call it to
145 state->FreeThread(this);
150 /** Override this method to put your actual
151 * threaded code here.
153 virtual void Run() = 0;
155 /** Signal the thread to exit gracefully.
157 void SetExitFlag(bool value)
162 /** Get thread's current exit status.
163 * (are we being asked to exit?)