]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengine.h
Fix some of the include guard names (requested by SaberUK)
[user/henk/code/inspircd.git] / include / threadengine.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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_H
15 #define THREADENGINE_H
16
17 #include <vector>
18 #include <string>
19 #include <map>
20 #include "inspircd_config.h"
21 #include "base.h"
22
23 #ifdef WINDOWS
24 #include "threadengines/threadengine_win32.h"
25 #endif
26
27 class ThreadData;
28
29 /** Derive from this class to implement your own threaded sections of
30  * code. Be sure to keep your code thread-safe and not prone to deadlocks
31  * and race conditions if you MUST use threading!
32  */
33 class CoreExport Thread
34 {
35  private:
36         /** Set to true when the thread is to exit
37          */
38         bool ExitFlag;
39  protected:
40         /** Get thread's current exit status.
41          * (are we being asked to exit?)
42          */
43         bool GetExitFlag()
44         {
45                 return ExitFlag;
46         }
47  public:
48         /** Opaque thread state managed by threading engine
49          */
50         ThreadData* state;
51
52         /** Set Creator to NULL at this point
53          */
54         Thread() : ExitFlag(false), state(NULL)
55         {
56         }
57
58         /* If the thread is running, you MUST join BEFORE deletion */
59         virtual ~Thread();
60
61         /** Override this method to put your actual
62          * threaded code here.
63          */
64         virtual void Run() = 0;
65
66         /** Signal the thread to exit gracefully.
67          */
68         virtual void SetExitFlag();
69
70         /** Join the thread (calls SetExitFlag and waits for exit)
71          */
72         void join();
73 };
74
75
76 class CoreExport QueuedThread : public Thread
77 {
78         ThreadQueueData queue;
79  protected:
80         /** Waits for an enqueue operation to complete
81          * You MUST hold the queue lock when you call this.
82          * It will be unlocked while you wait, and will be relocked
83          * before the function returns
84          */
85         void WaitForQueue()
86         {
87                 queue.Wait();
88         }
89  public:
90         /** Lock queue.
91          */
92         void LockQueue()
93         {
94                 queue.Lock();
95         }
96         /** Unlock queue.
97          */
98         void UnlockQueue()
99         {
100                 queue.Unlock();
101         }
102         /** Unlock queue and wake up worker
103          */
104         void UnlockQueueWakeup()
105         {
106                 queue.Wakeup();
107                 queue.Unlock();
108         }
109         virtual void SetExitFlag()
110         {
111                 queue.Lock();
112                 Thread::SetExitFlag();
113                 queue.Wakeup();
114                 queue.Unlock();
115         }
116 };
117
118 class CoreExport SocketThread : public Thread
119 {
120         ThreadQueueData queue;
121         ThreadSignalData signal;
122  protected:
123         /** Waits for an enqueue operation to complete
124          * You MUST hold the queue lock when you call this.
125          * It will be unlocked while you wait, and will be relocked
126          * before the function returns
127          */
128         void WaitForQueue()
129         {
130                 queue.Wait();
131         }
132  public:
133         /** Notifies parent by making the SignalFD ready to read
134          * No requirements on locking
135          */
136         void NotifyParent();
137         SocketThread();
138         virtual ~SocketThread();
139         /** Lock queue.
140          */
141         void LockQueue()
142         {
143                 queue.Lock();
144         }
145         /** Unlock queue.
146          */
147         void UnlockQueue()
148         {
149                 queue.Unlock();
150         }
151         /** Unlock queue and send wakeup to worker
152          */
153         void UnlockQueueWakeup()
154         {
155                 queue.Wakeup();
156                 queue.Unlock();
157         }
158         virtual void SetExitFlag()
159         {
160                 queue.Lock();
161                 Thread::SetExitFlag();
162                 queue.Wakeup();
163                 queue.Unlock();
164         }
165
166         /**
167          * Called in the context of the parent thread after a notification
168          * has passed through the socket
169          */
170         virtual void OnNotify() = 0;
171 };
172
173 #endif
174