2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
29 /** Derive from this class to implement your own threaded sections of
30 * code. Be sure to keep your code thread-safe and not prone to deadlocks
31 * and race conditions if you MUST use threading!
33 class CoreExport Thread
36 /** Set to true when the thread is to exit
40 /** Opaque thread state managed by the ThreadEngine
42 ThreadEngine::ThreadState state;
44 /** ThreadEngine manages Thread::state
46 friend class ThreadEngine;
49 /** Get thread's current exit status.
50 * (are we being asked to exit?)
57 /** Set Creator to NULL at this point
59 Thread() : ExitFlag(false)
63 /** Override this method to put your actual
66 virtual void Run() = 0;
68 /** Signal the thread to exit gracefully.
70 virtual void SetExitFlag();
72 /** Join the thread (calls SetExitFlag and waits for exit)
78 class CoreExport QueuedThread : public Thread
80 ThreadQueueData queue;
82 /** Waits for an enqueue operation to complete
83 * You MUST hold the queue lock when you call this.
84 * It will be unlocked while you wait, and will be relocked
85 * before the function returns
104 /** Unlock queue and wake up worker
106 void UnlockQueueWakeup()
111 void SetExitFlag() CXX11_OVERRIDE
114 Thread::SetExitFlag();
120 class CoreExport SocketThread : public Thread
122 ThreadQueueData queue;
123 ThreadSignalData signal;
125 /** Waits for an enqueue operation to complete
126 * You MUST hold the queue lock when you call this.
127 * It will be unlocked while you wait, and will be relocked
128 * before the function returns
135 /** Notifies parent by making the SignalFD ready to read
136 * No requirements on locking
140 virtual ~SocketThread();
153 /** Unlock queue and send wakeup to worker
155 void UnlockQueueWakeup()
160 void SetExitFlag() CXX11_OVERRIDE
163 Thread::SetExitFlag();
169 * Called in the context of the parent thread after a notification
170 * has passed through the socket
172 virtual void OnNotify() = 0;