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/>.
21 #ifndef THREADENGINE_H
22 #define THREADENGINE_H
27 #include "inspircd_config.h"
31 #include "threadengines/threadengine_win32.h"
36 /** Derive from this class to implement your own threaded sections of
37 * code. Be sure to keep your code thread-safe and not prone to deadlocks
38 * and race conditions if you MUST use threading!
40 class CoreExport Thread
43 /** Set to true when the thread is to exit
47 /** Get thread's current exit status.
48 * (are we being asked to exit?)
55 /** Opaque thread state managed by threading engine
59 /** Set Creator to NULL at this point
61 Thread() : ExitFlag(false), state(NULL)
65 /* If the thread is running, you MUST join BEFORE deletion */
68 /** Override this method to put your actual
71 virtual void Run() = 0;
73 /** Signal the thread to exit gracefully.
75 virtual void SetExitFlag();
77 /** Join the thread (calls SetExitFlag and waits for exit)
83 class CoreExport QueuedThread : public Thread
85 ThreadQueueData queue;
87 /** Waits for an enqueue operation to complete
88 * You MUST hold the queue lock when you call this.
89 * It will be unlocked while you wait, and will be relocked
90 * before the function returns
109 /** Unlock queue and wake up worker
111 void UnlockQueueWakeup()
116 virtual void SetExitFlag()
119 Thread::SetExitFlag();
125 class CoreExport SocketThread : public Thread
127 ThreadQueueData queue;
128 ThreadSignalData signal;
130 /** Waits for an enqueue operation to complete
131 * You MUST hold the queue lock when you call this.
132 * It will be unlocked while you wait, and will be relocked
133 * before the function returns
140 /** Notifies parent by making the SignalFD ready to read
141 * No requirements on locking
145 virtual ~SocketThread();
158 /** Unlock queue and send wakeup to worker
160 void UnlockQueueWakeup()
165 virtual void SetExitFlag()
168 Thread::SetExitFlag();
174 * Called in the context of the parent thread after a notification
175 * has passed through the socket
177 virtual void OnNotify() = 0;