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/>.
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!
32 class CoreExport Thread
35 /** Set to true when the thread is to exit
39 /** Opaque thread state managed by the ThreadEngine
41 ThreadEngine::ThreadState state;
43 /** ThreadEngine manages Thread::state
45 friend class ThreadEngine;
48 /** Get thread's current exit status.
49 * (are we being asked to exit?)
56 /** Set Creator to NULL at this point
58 Thread() : ExitFlag(false)
62 /** Override this method to put your actual
65 virtual void Run() = 0;
67 /** Signal the thread to exit gracefully.
69 virtual void SetExitFlag();
71 /** Join the thread (calls SetExitFlag and waits for exit)
77 class CoreExport QueuedThread : public Thread
79 ThreadQueueData queue;
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
103 /** Unlock queue and wake up worker
105 void UnlockQueueWakeup()
110 void SetExitFlag() CXX11_OVERRIDE
113 Thread::SetExitFlag();
119 class CoreExport SocketThread : public Thread
121 ThreadQueueData queue;
122 ThreadSignalData signal;
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
134 /** Notifies parent by making the SignalFD ready to read
135 * No requirements on locking
139 virtual ~SocketThread();
152 /** Unlock queue and send wakeup to worker
154 void UnlockQueueWakeup()
159 void SetExitFlag() CXX11_OVERRIDE
162 Thread::SetExitFlag();
168 * Called in the context of the parent thread after a notification
169 * has passed through the socket
171 virtual void OnNotify() = 0;