InspIRCd  3.0
threadengine.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2014 Attila Molnar <[email protected]>
5  * Copyright (C) 2013, 2017 Sadie Powell <[email protected]>
6  * Copyright (C) 2012 Robby <[email protected]>
7  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
8  * Copyright (C) 2008 Craig Edwards <[email protected]>
9  *
10  * This file is part of InspIRCd. InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 
24 #pragma once
25 
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include "config.h"
30 #include "base.h"
31 
36 class CoreExport Thread
37 {
38  private:
41  bool ExitFlag;
42 
46 
49  friend class ThreadEngine;
50 
51  protected:
55  bool GetExitFlag()
56  {
57  return ExitFlag;
58  }
59  public:
62  Thread() : ExitFlag(false)
63  {
64  }
65 
69  virtual void Run() = 0;
70 
73  virtual void SetExitFlag();
74 
77  void join();
78 };
79 
80 
81 class CoreExport QueuedThread : public Thread
82 {
83  ThreadQueueData queue;
84  protected:
90  void WaitForQueue()
91  {
92  queue.Wait();
93  }
94  public:
97  void LockQueue()
98  {
99  queue.Lock();
100  }
103  void UnlockQueue()
104  {
105  queue.Unlock();
106  }
110  {
111  queue.Wakeup();
112  queue.Unlock();
113  }
114  void SetExitFlag() CXX11_OVERRIDE
115  {
116  queue.Lock();
118  queue.Wakeup();
119  queue.Unlock();
120  }
121 };
122 
123 class CoreExport SocketThread : public Thread
124 {
125  ThreadQueueData queue;
126  ThreadSignalData signal;
127  protected:
134  {
135  queue.Wait();
136  }
137  public:
141  void NotifyParent();
142  SocketThread();
143  virtual ~SocketThread();
146  void LockQueue()
147  {
148  queue.Lock();
149  }
152  void UnlockQueue()
153  {
154  queue.Unlock();
155  }
159  {
160  queue.Wakeup();
161  queue.Unlock();
162  }
163  void SetExitFlag() CXX11_OVERRIDE
164  {
165  queue.Lock();
167  queue.Wakeup();
168  queue.Unlock();
169  }
170 
175  virtual void OnNotify() = 0;
176 };
SocketThread::UnlockQueueWakeup
void UnlockQueueWakeup()
Definition: threadengine.h:158
QueuedThread::LockQueue
void LockQueue()
Definition: threadengine.h:97
ThreadQueueData
Definition: threadengine_pthread.h:109
Thread
Definition: threadengine.h:36
Thread::Thread
Thread()
Definition: threadengine.h:62
Thread::GetExitFlag
bool GetExitFlag()
Definition: threadengine.h:55
QueuedThread::WaitForQueue
void WaitForQueue()
Definition: threadengine.h:90
ThreadSignalData
Definition: threadengine_pthread.h:135
QueuedThread::UnlockQueue
void UnlockQueue()
Definition: threadengine.h:103
ThreadEngine
Definition: threadengine_pthread.h:39
SocketThread::UnlockQueue
void UnlockQueue()
Definition: threadengine.h:152
SocketThread::WaitForQueue
void WaitForQueue()
Definition: threadengine.h:133
SocketThread
Definition: threadengine.h:123
ThreadEngine::ThreadState
Definition: threadengine_pthread.h:44
QueuedThread
Definition: threadengine.h:81
QueuedThread::UnlockQueueWakeup
void UnlockQueueWakeup()
Definition: threadengine.h:109
SocketThread::LockQueue
void LockQueue()
Definition: threadengine.h:146
SocketThread::SetExitFlag
void SetExitFlag() CXX11_OVERRIDE
Definition: threadengine.h:163
QueuedThread::SetExitFlag
void SetExitFlag() CXX11_OVERRIDE
Definition: threadengine.h:114
Thread::SetExitFlag
virtual void SetExitFlag()
Definition: threadengine.cpp:26