]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/threadengine.h
Fix configure check for eventfd, HAS_EVENTFD is true/false not 1/0
[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  protected:
34         /** Get thread's current exit status.
35          * (are we being asked to exit?)
36          */
37         bool GetExitFlag()
38         {
39                 return ExitFlag;
40         }
41  public:
42         /** Opaque thread state managed by threading engine
43          */
44         ThreadData* state;
45
46         /** Set Creator to NULL at this point
47          */
48         Thread() : ExitFlag(false), state(NULL)
49         {
50         }
51
52         /** If this thread has a Creator set, call it to
53          * free the thread
54          */
55         virtual ~Thread()
56         {
57                 if (state)
58                 {
59                         state->FreeThread(this);
60                         delete state;
61                 }
62         }
63
64         /** Override this method to put your actual
65          * threaded code here.
66          */
67         virtual void Run() = 0;
68
69         /** Signal the thread to exit gracefully.
70          */
71         virtual void SetExitFlag()
72         {
73                 ExitFlag = true;
74         }
75 };
76
77
78 class CoreExport QueuedThread : public Thread
79 {
80         ThreadQueueData queue;
81  protected:
82         /** Waits for an enqueue operation to complete
83          * You MUST hold the queue lock when you call this.
84          * It will be unlocked while you wait, and will be relocked
85          * before the function returns
86          */
87         void WaitForQueue()
88         {
89                 queue.Wait();
90         }
91  public:
92         /** Lock queue.
93          */
94         void LockQueue()
95         {
96                 queue.Lock();
97         }
98         /** Unlock queue.
99          */
100         void UnlockQueue()
101         {
102                 queue.Unlock();
103         }
104         /** Unlock queue and wake up worker
105          */
106         void UnlockQueueWakeup()
107         {
108                 queue.Wakeup();
109                 queue.Unlock();
110         }
111         virtual void SetExitFlag()
112         {
113                 queue.Lock();
114                 Thread::SetExitFlag();
115                 queue.Wakeup();
116                 queue.Unlock();
117         }
118 };
119
120 class CoreExport SocketThread : public Thread
121 {
122         ThreadQueueData queue;
123         ThreadSignalData signal;
124  protected:
125         /** Waits for an enqueue operation to complete
126          * You MUST hold the queue lock when you call this.
127          * It will be unlocked while you wait, and will be relocked
128          * before the function returns
129          */
130         void WaitForQueue()
131         {
132                 queue.Wait();
133         }
134         /** Notifies parent by making the SignalFD ready to read
135          * No requirements on locking
136          */
137         void NotifyParent();
138  public:
139         SocketThread(InspIRCd* SI);
140         virtual ~SocketThread();
141         /** Lock queue.
142          */
143         void LockQueue()
144         {
145                 queue.Lock();
146         }
147         /** Unlock queue.
148          */
149         void UnlockQueue()
150         {
151                 queue.Unlock();
152         }
153         /** Unlock queue and send wakeup to worker
154          */
155         void UnlockQueueWakeup()
156         {
157                 queue.Wakeup();
158                 queue.Unlock();
159         }
160         virtual void SetExitFlag()
161         {
162                 queue.Lock();
163                 Thread::SetExitFlag();
164                 queue.Wakeup();
165                 queue.Unlock();
166         }
167
168         /**
169          * Called in the context of the parent thread after a notification
170          * has passed through the socket
171          */
172         virtual void OnNotify() = 0;
173 };
174
175 #endif
176