3 * ===============================================================
4 * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC.
6 * ===============================================================
8 * This file is part of the SourceMod/SourcePawn SDK. This file may only be
9 * used or modified under the Terms and Conditions of its License Agreement,
10 * which is found in public/licenses/LICENSE.txt. As of this notice, derivative
11 * works must be licensed under the GNU General Public License (version 2 or
12 * greater). A copy of the GPL is included under public/licenses/GPL.txt.
14 * To view the latest information, see: http://www.sourcemod.net/license.php
19 #ifndef _INCLUDE_SOURCEMOD_THREADER_H
20 #define _INCLUDE_SOURCEMOD_THREADER_H
24 * @brief Contains platform independent routines for threading.
27 #include <IShareSys.h>
29 #define SMINTERFACE_THREADER_NAME "IThreader"
30 #define SMINTERFACE_THREADER_VERSION 1
35 * @brief Thread creation flags
41 * @brief Auto-release handle on finish
43 * You are not guaranteed the handle for this is valid after
44 * calling MakeThread(), so never use it until OnTerminate is called.
46 Thread_AutoRelease = 1,
48 * @brief Thread is created "suspended", meaning it is inactive until unpaused.
50 Thread_CreateSuspended = 2,
54 * @brief Specifies thread priority levels.
58 ThreadPrio_Minimum = -8,
60 ThreadPrio_Normal = 0,
62 ThreadPrio_Maximum = 8,
66 * @brief The current state of a thread.
76 * @brief Thread-specific parameters.
82 flags(Thread_Default),
83 prio(ThreadPrio_Normal)
86 ThreadFlags flags; /**< Flags to set on the thread */
87 ThreadPriority prio; /**< Priority to set on the thread */
93 * @brief Describes a handle to a thread.
98 /** Virtual destructor */
99 virtual ~IThreadHandle() { };
102 * @brief Pauses parent thread until this thread completes.
104 * @return True if successful, false otherwise.
106 virtual bool WaitForThread() =0;
109 * @brief Destroys the thread handle. This will not necessarily cancel the thread.
111 virtual void DestroyThis() =0;
114 * @brief Returns the parent threader.
116 * @return IThreadCreator that created this thread.
118 virtual IThreadCreator *Parent() =0;
121 * @brief Returns the thread states.
123 * @param ptparams Pointer to a ThreadParams buffer.
125 virtual void GetParams(ThreadParams *ptparams) =0;
128 * @brief Returns the thread priority.
130 * @return Thread priority.
132 virtual ThreadPriority GetPriority() =0;
135 * @brief Sets thread priority.
136 * NOTE: On Linux, this always returns false.
138 * @param prio Thread priority to set.
139 * @return True if successful, false otherwise.
141 virtual bool SetPriority(ThreadPriority prio) =0;
144 * @brief Returns the thread state.
146 * @return Current thread state.
148 virtual ThreadState GetState() =0;
151 * @brief Attempts to unpause a paused thread.
153 * @return True on success, false otherwise.
155 virtual bool Unpause() =0;
159 * @brief Handles a single thread's execution.
164 /** Virtual destructor */
165 virtual ~IThread() { };
168 * @brief Called when the thread runs (in its own thread).
170 * @param pHandle Pointer to the thread's handle.
172 virtual void RunThread(IThreadHandle *pHandle) =0;
175 * @brief Called when the thread terminates. This occurs inside the thread as well.
177 * @param pHandle Pointer to the thread's handle.
178 * @param cancel True if the thread did not finish, false otherwise.
180 virtual void OnTerminate(IThreadHandle *pHandle, bool cancel) =0;
185 * @brief Describes a thread creator
190 /** Virtual Destructor */
191 virtual ~IThreadCreator() { };
194 * @brief Creates a basic thread.
196 * @param pThread IThread pointer for callbacks.
198 virtual void MakeThread(IThread *pThread) =0;
201 * @brief Creates a thread with specific options.
203 * @param pThread IThread pointer for callbacks.
204 * @param flags Flags for the thread.
205 * @return IThreadHandle pointer (must be released).
207 virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags) =0;
210 * @brief Creates a thread with specific options.
212 * @param pThread IThread pointer for callbacks.
213 * @param params Extended options for the thread.
214 * @return IThreadHandle pointer (must be released).
216 virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params) =0;
219 * @brief Returns the priority bounds.
220 * Note: On Linux, the min and max are both Thread_Normal.
222 * @param max Stores the maximum priority level.
223 * @param min Stores the minimum priority level.
225 virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min) =0;
229 * @brief Describes a simple locking mutex.
234 /** Virtual Destructor */
235 virtual ~IMutex() { };
238 * @brief Attempts to lock, but returns instantly.
240 * @return True if lock was obtained, false otherwise.
242 virtual bool TryLock() =0;
245 * @brief Attempts to lock by waiting for release.
247 virtual void Lock() =0;
250 * @brief Unlocks the mutex.
252 virtual void Unlock() =0;
255 * @brief Destroys the mutex handle.
257 virtual void DestroyThis() =0;
261 * @brief Describes a simple "condition variable"/signal lock.
266 /** Virtual Destructor */
267 virtual ~IEventSignal() { };
270 * @brief Waits for a signal.
272 virtual void Wait() =0;
275 * @brief Triggers the signal and resets the signal after triggering.
277 virtual void Signal() =0;
280 * @brief Frees the signal handle.
282 virtual void DestroyThis() =0;
286 * @brief Describes possible worker states
297 * @brief This is a "worker pool." A single thread places tasks in a queue.
298 * Each IThread is then a task, rather than its own separate thread.
300 class IThreadWorker : public IThreadCreator
303 /** Virtual Destructor */
304 virtual ~IThreadWorker()
309 * @brief Runs one "frame" of the worker.
311 * @return Number of tasks processed.
313 virtual unsigned int RunFrame() =0;
316 * @brief Pauses the worker.
318 * @return True on success, false otherwise.
320 virtual bool Pause() =0;
323 * @brief Unpauses the worker.
325 * @return True on success, false otherwise.
327 virtual bool Unpause() =0;
330 * @brief Starts the worker thread.
332 * @return True on success, false otherwise.
334 virtual bool Start() =0;
337 * @brief Stops the worker thread.
339 * @param flush If true, all remaining tasks will be cancelled.
340 * Otherwise, the threader will wait until the queue is empty.
341 * @return True on success, false otherwise.
343 virtual bool Stop(bool flush) =0;
346 * @brief Returns the status of the worker.
348 * @param numThreads Pointer to store number of threads in the queue.
349 * @return State of the worker.
351 virtual WorkerState GetStatus(unsigned int *numThreads) =0;
355 * @brief Describes a threading system
357 class IThreader : public SMInterface, public IThreadCreator
360 virtual const char *GetInterfaceName()
362 return SMINTERFACE_THREADER_NAME;
364 virtual unsigned int GetInterfaceVersion()
366 return SMINTERFACE_THREADER_VERSION;
370 * @brief Creates a mutex (mutual exclusion lock).
372 * @return A new IMutex pointer (must be destroyed).
374 virtual IMutex *MakeMutex() =0;
377 * @brief Sleeps the calling thread for a number of milliseconds.
379 * @param ms Millisecond count to sleep.
381 virtual void ThreadSleep(unsigned int ms) =0;
384 * @brief Creates a non-signalled event.
386 * @return A new IEventSignal pointer (must be destroyed).
388 virtual IEventSignal *MakeEventSignal() =0;
391 * @brief Creates a thread worker.
393 * @param threaded If true, the worker will be threaded.
394 * If false, the worker will require manual frame execution.
395 * @return A new IThreadWorker pointer (must be destroyed).
397 virtual IThreadWorker *MakeWorker(bool threaded) =0;
400 * @brief Destroys an IThreadWorker pointer.
402 * @param pWorker IThreadWorker pointer to destroy.
404 virtual void DestroyWorker(IThreadWorker *pWorker) =0;
408 #endif //_INCLUDE_SOURCEMOD_THREADER_H