1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __THREADENGINE__
15 #define __THREADENGINE__
20 #include "inspircd_config.h"
23 /** Derive from this class to implement your own threaded sections of
24 * code. Be sure to keep your code thread-safe and not prone to deadlocks
25 * and race conditions if you MUST use threading!
27 class CoreExport Thread : public Extensible
30 /** Set to true when the thread is to exit
34 /** Get thread's current exit status.
35 * (are we being asked to exit?)
42 /** Opaque thread state managed by threading engine
46 /** Set Creator to NULL at this point
48 Thread() : ExitFlag(false), state(NULL)
52 /** If this thread has a Creator set, call it to
59 state->FreeThread(this);
64 /** Override this method to put your actual
67 virtual void Run() = 0;
69 /** Signal the thread to exit gracefully.
71 virtual void SetExitFlag()
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 virtual void SetExitFlag()
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
134 /** Notifies parent by making the SignalFD ready to read
135 * No requirements on locking
139 SocketThread(InspIRCd* SI);
140 virtual ~SocketThread();
153 /** Unlock queue and send wakeup to worker
155 void UnlockQueueWakeup()
160 virtual void SetExitFlag()
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;