]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengine.h
ThreadEngine: remove MutexFactory, mutexes should be constructed using their constructor
[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 /** Derive from this class to implement your own threaded sections of
24  * code. Be sure to keep your code thread-safe and not prone to deadlocks
25  * and race conditions if you MUST use threading!
26  */
27 class CoreExport Thread : public Extensible
28 {
29  private:
30         /** Set to true when the thread is to exit
31          */
32         bool ExitFlag;
33  public:
34         /** Opaque thread state managed by threading engine
35          */
36         ThreadData* state;
37
38         /** Set Creator to NULL at this point
39          */
40         Thread() : ExitFlag(false), state(NULL)
41         {
42         }
43
44         /** If this thread has a Creator set, call it to
45          * free the thread
46          */
47         virtual ~Thread()
48         {
49                 if (state)
50                 {
51                         state->FreeThread(this);
52                         delete state;
53                 }
54         }
55
56         /** Override this method to put your actual
57          * threaded code here.
58          */
59         virtual void Run() = 0;
60
61         /** Signal the thread to exit gracefully.
62          */
63         void SetExitFlag(bool value)
64         {
65                 ExitFlag = value;
66         }
67
68         /** Get thread's current exit status.
69          * (are we being asked to exit?)
70          */
71         bool GetExitFlag()
72         {
73                 return ExitFlag;
74         }
75 };
76
77 #endif
78