]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengine.h
Of course, it DOES help to actually initialise the Mutex objects, and delete them...
[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         ~Mutex() { }
93 };
94
95 /** Derive from this class to implement your own threaded sections of
96  * code.
97  */
98 class CoreExport Thread : public Extensible
99 {
100  private:
101         bool ExitFlag;
102  public:
103
104         /** Creator thread engine
105          */
106         ThreadEngine* Creator;
107
108         /** Set Creator to NULL at this point
109          */
110         Thread() : ExitFlag(false), Creator(NULL)
111         {
112         }
113
114         /** If this thread has a Creator set, call it to
115          * free the thread
116          */
117         virtual ~Thread()
118         {
119                 if (Creator)
120                         Creator->FreeThread(this);
121         }
122
123         /** Override this method to put your actual
124          * threaded code here
125          */
126         virtual void Run() = 0;
127
128         void SetExitFlag()
129         {
130                 ExitFlag = true;
131         }
132
133         void ClearExitFlag()
134         {
135                 ExitFlag = false;
136         }
137
138         bool GetExitFlag()
139         {
140                 return ExitFlag;
141         }
142 };
143
144
145
146 #endif
147