This has to be the worst GCC error ever.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #ifndef _INCLUDE_SOURCEMOD_THREADER_H
33 #define _INCLUDE_SOURCEMOD_THREADER_H
37 * @brief Contains platform independent routines for threading.
40 #include <IShareSys.h>
42 #define SMINTERFACE_THREADER_NAME "IThreader"
43 #define SMINTERFACE_THREADER_VERSION 2
48 * @brief Thread creation flags
54 * @brief Auto-release handle on finish
56 * You are not guaranteed the handle for this is valid after
57 * calling MakeThread(), so never use it until OnTerminate is called.
59 Thread_AutoRelease = 1,
61 * @brief Thread is created "suspended", meaning it is inactive until unpaused.
63 Thread_CreateSuspended = 2,
67 * @brief Specifies thread priority levels.
71 ThreadPrio_Minimum = -8,
73 ThreadPrio_Normal = 0,
75 ThreadPrio_Maximum = 8,
79 * @brief The current state of a thread.
89 * @brief Thread-specific parameters.
95 flags(Thread_Default),
96 prio(ThreadPrio_Normal)
99 ThreadFlags flags; /**< Flags to set on the thread */
100 ThreadPriority prio; /**< Priority to set on the thread */
103 class IThreadCreator;
106 * @brief Describes a handle to a thread.
111 /** Virtual destructor */
112 virtual ~IThreadHandle() { };
115 * @brief Pauses parent thread until this thread completes.
117 * @return True if successful, false otherwise.
119 virtual bool WaitForThread() =0;
122 * @brief Destroys the thread handle. This will not necessarily cancel the thread.
124 virtual void DestroyThis() =0;
127 * @brief Returns the parent threader.
129 * @return IThreadCreator that created this thread.
131 virtual IThreadCreator *Parent() =0;
134 * @brief Returns the thread states.
136 * @param ptparams Pointer to a ThreadParams buffer.
138 virtual void GetParams(ThreadParams *ptparams) =0;
141 * @brief Returns the thread priority.
143 * @return Thread priority.
145 virtual ThreadPriority GetPriority() =0;
148 * @brief Sets thread priority.
149 * NOTE: On Linux, this always returns false.
151 * @param prio Thread priority to set.
152 * @return True if successful, false otherwise.
154 virtual bool SetPriority(ThreadPriority prio) =0;
157 * @brief Returns the thread state.
159 * @return Current thread state.
161 virtual ThreadState GetState() =0;
164 * @brief Attempts to unpause a paused thread.
166 * @return True on success, false otherwise.
168 virtual bool Unpause() =0;
172 * @brief Handles a single thread's execution.
177 /** Virtual destructor */
178 virtual ~IThread() { };
181 * @brief Called when the thread runs (in its own thread).
183 * @param pHandle Pointer to the thread's handle.
185 virtual void RunThread(IThreadHandle *pHandle) =0;
188 * @brief Called when the thread terminates. This occurs inside the thread as well.
190 * @param pHandle Pointer to the thread's handle.
191 * @param cancel True if the thread did not finish, false otherwise.
193 virtual void OnTerminate(IThreadHandle *pHandle, bool cancel) =0;
198 * @brief Describes a thread creator
203 /** Virtual Destructor */
204 virtual ~IThreadCreator() { };
207 * @brief Creates a basic thread.
209 * @param pThread IThread pointer for callbacks.
211 virtual void MakeThread(IThread *pThread) =0;
214 * @brief Creates a thread with specific options.
216 * @param pThread IThread pointer for callbacks.
217 * @param flags Flags for the thread.
218 * @return IThreadHandle pointer (must be released).
220 virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags) =0;
223 * @brief Creates a thread with specific options.
225 * @param pThread IThread pointer for callbacks.
226 * @param params Extended options for the thread.
227 * @return IThreadHandle pointer (must be released).
229 virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params) =0;
232 * @brief Returns the priority bounds.
233 * Note: On Linux, the min and max are both Thread_Normal.
235 * @param max Stores the maximum priority level.
236 * @param min Stores the minimum priority level.
238 virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min) =0;
242 * @brief Describes a simple locking mutex.
247 /** Virtual Destructor */
248 virtual ~IMutex() { };
251 * @brief Attempts to lock, but returns instantly.
253 * @return True if lock was obtained, false otherwise.
255 virtual bool TryLock() =0;
258 * @brief Attempts to lock by waiting for release.
260 virtual void Lock() =0;
263 * @brief Unlocks the mutex.
265 virtual void Unlock() =0;
268 * @brief Destroys the mutex handle.
270 virtual void DestroyThis() =0;
274 * @brief Describes a simple "condition variable"/signal lock.
279 /** Virtual Destructor */
280 virtual ~IEventSignal() { };
283 * @brief Waits for a signal.
285 virtual void Wait() =0;
288 * @brief Triggers the signal and resets the signal after triggering.
290 virtual void Signal() =0;
293 * @brief Frees the signal handle.
295 virtual void DestroyThis() =0;
299 * @brief Describes possible worker states
310 * @brief This is a "worker pool." A single thread places tasks in a queue.
311 * Each IThread is then a task, rather than its own separate thread.
313 class IThreadWorker : public IThreadCreator
316 /** Virtual Destructor */
317 virtual ~IThreadWorker()
322 * @brief Runs one "frame" of the worker.
324 * @return Number of tasks processed.
326 virtual unsigned int RunFrame() =0;
329 * @brief Pauses the worker.
331 * @return True on success, false otherwise.
333 virtual bool Pause() =0;
336 * @brief Unpauses the worker.
338 * @return True on success, false otherwise.
340 virtual bool Unpause() =0;
343 * @brief Starts the worker thread.
345 * @return True on success, false otherwise.
347 virtual bool Start() =0;
350 * @brief Stops the worker thread.
352 * @param flush If true, all remaining tasks will be cancelled.
353 * Otherwise, the threader will wait until the queue is empty.
354 * @return True on success, false otherwise.
356 virtual bool Stop(bool flush) =0;
359 * @brief Returns the status of the worker.
361 * @param numThreads Pointer to store number of threads in the queue.
362 * @return State of the worker.
364 virtual WorkerState GetStatus(unsigned int *numThreads) =0;
368 * @brief Describes thread worker callbacks.
370 class IThreadWorkerCallbacks
374 * @brief Called when the worker thread is initialized.
376 * @param pWorker Pointer to the worker.
378 virtual void OnWorkerStart(IThreadWorker *pWorker)
383 * @brief Called when the worker thread is cleaning up.
385 * @param pWorker Pointer to the worker.
387 virtual void OnWorkerStop(IThreadWorker *pWorker)
393 * @brief Describes a threading system
395 class IThreader : public SMInterface, public IThreadCreator
398 virtual const char *GetInterfaceName()
400 return SMINTERFACE_THREADER_NAME;
402 virtual unsigned int GetInterfaceVersion()
404 return SMINTERFACE_THREADER_VERSION;
406 virtual bool IsVersionCompatible(unsigned int version)
412 return SMInterface::IsVersionCompatible(version);
416 * @brief Creates a mutex (mutual exclusion lock).
418 * @return A new IMutex pointer (must be destroyed).
420 virtual IMutex *MakeMutex() =0;
423 * @brief Sleeps the calling thread for a number of milliseconds.
425 * @param ms Millisecond count to sleep.
427 virtual void ThreadSleep(unsigned int ms) =0;
430 * @brief Creates a non-signalled event.
432 * @return A new IEventSignal pointer (must be destroyed).
434 virtual IEventSignal *MakeEventSignal() =0;
437 * @brief Creates a thread worker.
439 * @param hooks Optional pointer to callback interface.
440 * @param threaded If true, the worker will be threaded.
441 * If false, the worker will require manual frame execution.
442 * @return A new IThreadWorker pointer (must be destroyed).
444 virtual IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded) =0;
447 * @brief Destroys an IThreadWorker pointer.
449 * @param pWorker IThreadWorker pointer to destroy.
451 virtual void DestroyWorker(IThreadWorker *pWorker) =0;
455 #endif //_INCLUDE_SOURCEMOD_THREADER_H