]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengine.h
New methods in mutex purely for readability:
[user/henk/code/inspircd.git] / include / threadengine.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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!
33  */
34 class CoreExport ThreadEngine : public Extensible
35 {
36  protected:
37
38          /** Creator instance
39           */
40          InspIRCd* ServerInstance;
41          /** New Thread being created.
42           */
43          Thread* NewThread;
44
45  public:
46
47         /** Constructor
48          */
49         ThreadEngine(InspIRCd* Instance);
50
51         /** Destructor
52          */
53         virtual ~ThreadEngine();
54
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!
60          */
61         virtual bool Mutex(bool enable) = 0;
62
63         /** Run the newly created thread
64          */
65         virtual void Run() = 0;
66
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.
70          */
71         virtual void Create(Thread* thread_to_init) = 0;
72
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.
76          */
77         virtual void FreeThread(Thread* thread) = 0;
78
79         virtual const std::string GetName()
80         {
81                 return "<pure-virtual>";
82         }
83 };
84
85 class CoreExport Mutex : public Extensible
86 {
87  protected:
88         InspIRCd* ServerInstance;
89  public:
90         Mutex(InspIRCd* Instance);
91         virtual void Enable(bool enable) = 0;
92         void Lock() { Enable(true); }
93         void Unlock() { Enable(false); }
94         ~Mutex() { }
95 };
96
97 /** Derive from this class to implement your own threaded sections of
98  * code.
99  */
100 class CoreExport Thread : public Extensible
101 {
102  private:
103         bool ExitFlag;
104  public:
105
106         /** Creator thread engine
107          */
108         ThreadEngine* Creator;
109
110         /** Set Creator to NULL at this point
111          */
112         Thread() : ExitFlag(false), Creator(NULL)
113         {
114         }
115
116         /** If this thread has a Creator set, call it to
117          * free the thread
118          */
119         virtual ~Thread()
120         {
121                 if (Creator)
122                         Creator->FreeThread(this);
123         }
124
125         /** Override this method to put your actual
126          * threaded code here
127          */
128         virtual void Run() = 0;
129
130         void SetExitFlag()
131         {
132                 ExitFlag = true;
133         }
134
135         void ClearExitFlag()
136         {
137                 ExitFlag = false;
138         }
139
140         bool GetExitFlag()
141         {
142                 return ExitFlag;
143         }
144 };
145
146
147
148 #endif
149