InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
threadengine.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd. InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef THREADENGINE_H
22 #define THREADENGINE_H
23 
24 #include <vector>
25 #include <string>
26 #include <map>
27 #include "inspircd_config.h"
28 #include "base.h"
29 
30 class ThreadData;
31 
36 class CoreExport Thread
37 {
38  private:
41  bool ExitFlag;
42  protected:
46  bool GetExitFlag()
47  {
48  return ExitFlag;
49  }
50  public:
54 
57  Thread() : ExitFlag(false), state(NULL)
58  {
59  }
60 
61  /* If the thread is running, you MUST join BEFORE deletion */
62  virtual ~Thread();
63 
67  virtual void Run() = 0;
68 
71  virtual void SetExitFlag();
72 
75  void join();
76 };
77 
78 
79 class CoreExport QueuedThread : public Thread
80 {
81  ThreadQueueData queue;
82  protected:
88  void WaitForQueue()
89  {
90  queue.Wait();
91  }
92  public:
95  void LockQueue()
96  {
97  queue.Lock();
98  }
101  void UnlockQueue()
102  {
103  queue.Unlock();
104  }
108  {
109  queue.Wakeup();
110  queue.Unlock();
111  }
112  virtual void SetExitFlag()
113  {
114  queue.Lock();
116  queue.Wakeup();
117  queue.Unlock();
118  }
119 };
120 
121 class CoreExport SocketThread : public Thread
122 {
123  ThreadQueueData queue;
124  ThreadSignalData signal;
125  protected:
132  {
133  queue.Wait();
134  }
135  public:
139  void NotifyParent();
140  SocketThread();
141  virtual ~SocketThread();
144  void LockQueue()
145  {
146  queue.Lock();
147  }
150  void UnlockQueue()
151  {
152  queue.Unlock();
153  }
157  {
158  queue.Wakeup();
159  queue.Unlock();
160  }
161  virtual void SetExitFlag()
162  {
163  queue.Lock();
165  queue.Wakeup();
166  queue.Unlock();
167  }
168 
173  virtual void OnNotify() = 0;
174 };
175 
176 #endif
177 
Definition: threadengine.h:121
bool GetExitFlag()
Definition: threadengine.h:46
Definition: threadengine.h:79
virtual void SetExitFlag()
Definition: threadengine.cpp:29
virtual void SetExitFlag()
Definition: threadengine.h:112
Definition: threadengine_pthread.h:112
void WaitForQueue()
Definition: threadengine.h:131
ThreadData * state
Definition: threadengine.h:53
Definition: threadengine_pthread.h:151
Definition: threadengine.h:36
void UnlockQueueWakeup()
Definition: threadengine.h:107
void LockQueue()
Definition: threadengine.h:144
void LockQueue()
Definition: threadengine.h:95
void UnlockQueueWakeup()
Definition: threadengine.h:156
void WaitForQueue()
Definition: threadengine.h:88
Thread()
Definition: threadengine.h:57
void UnlockQueue()
Definition: threadengine.h:101
virtual void SetExitFlag()
Definition: threadengine.h:161
Definition: threadengine_pthread.h:66
void UnlockQueue()
Definition: threadengine.h:150