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