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