2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
32 /** Derive from this class to implement your own threaded sections of
33 * code. Be sure to keep your code thread-safe and not prone to deadlocks
34 * and race conditions if you MUST use threading!
36 class CoreExport Thread
39 /** Set to true when the thread is to exit
43 /** Opaque thread state managed by the ThreadEngine
45 ThreadEngine::ThreadState state;
47 /** ThreadEngine manages Thread::state
49 friend class ThreadEngine;
52 /** Get thread's current exit status.
53 * (are we being asked to exit?)
60 /** Set Creator to NULL at this point
62 Thread() : ExitFlag(false)
66 /** Override this method to put your actual
69 virtual void Run() = 0;
71 /** Signal the thread to exit gracefully.
73 virtual void SetExitFlag();
75 /** Join the thread (calls SetExitFlag and waits for exit)
81 class CoreExport QueuedThread : public Thread
83 ThreadQueueData queue;
85 /** Waits for an enqueue operation to complete
86 * You MUST hold the queue lock when you call this.
87 * It will be unlocked while you wait, and will be relocked
88 * before the function returns
107 /** Unlock queue and wake up worker
109 void UnlockQueueWakeup()
114 void SetExitFlag() CXX11_OVERRIDE
117 Thread::SetExitFlag();
123 class CoreExport SocketThread : public Thread
125 ThreadQueueData queue;
126 ThreadSignalData signal;
128 /** Waits for an enqueue operation to complete
129 * You MUST hold the queue lock when you call this.
130 * It will be unlocked while you wait, and will be relocked
131 * before the function returns
138 /** Notifies parent by making the SignalFD ready to read
139 * No requirements on locking
143 virtual ~SocketThread();
156 /** Unlock queue and send wakeup to worker
158 void UnlockQueueWakeup()
163 void SetExitFlag() CXX11_OVERRIDE
166 Thread::SetExitFlag();
172 * Called in the context of the parent thread after a notification
173 * has passed through the socket
175 virtual void OnNotify() = 0;