]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengine.h
ThreadEngine: remove excessive mutex use on thread creation
[user/henk/code/inspircd.git] / include / threadengine.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __THREADENGINE__
15 #define __THREADENGINE__
16
17 #include <vector>
18 #include <string>
19 #include <map>
20 #include "inspircd_config.h"
21 #include "base.h"
22
23 class InspIRCd;
24 class Thread;
25
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.
35  */
36 class CoreExport ThreadEngine : public Extensible
37 {
38  protected:
39
40          /** Creator instance
41           */
42          InspIRCd* ServerInstance;
43
44  public:
45
46         /** Constructor.
47          * @param Instance Creator object
48          */
49         ThreadEngine(InspIRCd* Instance);
50
51         /** Destructor
52          */
53         virtual ~ThreadEngine();
54
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
59          * derived object.
60          */
61         virtual void Start(Thread* thread_to_init) = 0;
62
63         /** Returns the thread engine's name for display purposes
64          * @return The thread engine name
65          */
66         virtual const std::string GetName()
67         {
68                 return "<pure-virtual>";
69         }
70 };
71
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
78  * manage.
79  */
80 class CoreExport Mutex
81 {
82  protected:
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-
86          * accessible.
87          *
88          * @param enable True to enable the mutex (enter it) and false to
89          * disable the mutex (leave it).
90          */
91         virtual void Enable(bool enable) = 0;
92  public:
93
94         /** Constructor.
95          */
96         Mutex();
97
98         /** Enter/enable the mutex lock.
99          */
100         void Lock() { Enable(true); }
101
102         /** Leave/disable the mutex lock.
103          */
104         void Unlock() { Enable(false); }
105
106         /** Destructor
107          */
108         ~Mutex() { }
109 };
110
111 class CoreExport ThreadData
112 {
113  public:
114         virtual void FreeThread(Thread* thread) { }
115 };
116
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!
120  */
121 class CoreExport Thread : public Extensible
122 {
123  private:
124         /** Set to true when the thread is to exit
125          */
126         bool ExitFlag;
127  public:
128         /** Opaque thread state managed by threading engine
129          */
130         ThreadData* state;
131
132         /** Set Creator to NULL at this point
133          */
134         Thread() : ExitFlag(false), state(NULL)
135         {
136         }
137
138         /** If this thread has a Creator set, call it to
139          * free the thread
140          */
141         virtual ~Thread()
142         {
143                 if (state)
144                 {
145                         state->FreeThread(this);
146                         delete state;
147                 }
148         }
149
150         /** Override this method to put your actual
151          * threaded code here.
152          */
153         virtual void Run() = 0;
154
155         /** Signal the thread to exit gracefully.
156          */
157         void SetExitFlag(bool value)
158         {
159                 ExitFlag = value;
160         }
161
162         /** Get thread's current exit status.
163          * (are we being asked to exit?)
164          */
165         bool GetExitFlag()
166         {
167                 return ExitFlag;
168         }
169 };
170
171
172
173 #endif
174