public/IThreader.h
author David Anderson <dvander@alliedmods.net>
Mon May 26 07:52:27 2008 +0000 (2008-05-26)
changeset 2088 c84cd855ca10
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
committed two forgotten files and updated the linux makefile
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_SOURCEMOD_THREADER_H
ds@1952
    33
#define _INCLUDE_SOURCEMOD_THREADER_H
ds@1952
    34
ds@1952
    35
/**
ds@1952
    36
 * @file IThreader.h
ds@1952
    37
 * @brief Contains platform independent routines for threading.
ds@1952
    38
 */
ds@1952
    39
ds@1952
    40
#include <IShareSys.h>
ds@1952
    41
ds@1952
    42
#define SMINTERFACE_THREADER_NAME		"IThreader"
ds@1952
    43
#define SMINTERFACE_THREADER_VERSION	2
ds@1952
    44
ds@1952
    45
namespace SourceMod
ds@1952
    46
{
ds@1952
    47
	/**
ds@1952
    48
	 * @brief Thread creation flags
ds@1952
    49
	 */
ds@1952
    50
	enum ThreadFlags
ds@1952
    51
	{
ds@1952
    52
		Thread_Default = 0,
ds@1952
    53
		/**
ds@1952
    54
		 * @brief Auto-release handle on finish
ds@1952
    55
		 *
ds@1952
    56
		 * You are not guaranteed the handle for this is valid after
ds@1952
    57
		 * calling MakeThread(), so never use it until OnTerminate is called.
ds@1952
    58
		 */
ds@1952
    59
		Thread_AutoRelease = 1,
ds@1952
    60
		/**
ds@1952
    61
		 * @brief Thread is created "suspended", meaning it is inactive until unpaused.
ds@1952
    62
		 */
ds@1952
    63
		Thread_CreateSuspended = 2,
ds@1952
    64
	};
ds@1952
    65
ds@1952
    66
	/**
ds@1952
    67
	 * @brief Specifies thread priority levels.
ds@1952
    68
	 */
ds@1952
    69
	enum ThreadPriority
ds@1952
    70
	{
ds@1952
    71
		ThreadPrio_Minimum = -8,
ds@1952
    72
		ThreadPrio_Low = -3,
ds@1952
    73
		ThreadPrio_Normal = 0,
ds@1952
    74
		ThreadPrio_High = 3,
ds@1952
    75
		ThreadPrio_Maximum = 8,
ds@1952
    76
	};
ds@1952
    77
ds@1952
    78
	/**
ds@1952
    79
	 * @brief The current state of a thread.
ds@1952
    80
	 */
ds@1952
    81
	enum ThreadState
ds@1952
    82
	{
ds@1952
    83
		Thread_Running = 0,
ds@1952
    84
		Thread_Paused = 1,
ds@1952
    85
		Thread_Done = 2,
ds@1952
    86
	};
ds@1952
    87
ds@1952
    88
	/**
ds@1952
    89
	 * @brief Thread-specific parameters.
ds@1952
    90
	 */
ds@1952
    91
	struct ThreadParams
ds@1952
    92
	{
ds@1952
    93
		/** Constructor */
ds@1952
    94
		ThreadParams() : 
ds@1952
    95
			flags(Thread_Default), 
ds@1952
    96
			prio(ThreadPrio_Normal)
ds@1952
    97
		{
ds@1952
    98
		};
ds@1952
    99
		ThreadFlags flags;		/**< Flags to set on the thread */
ds@1952
   100
		ThreadPriority prio;	/**< Priority to set on the thread */
ds@1952
   101
	};
ds@1952
   102
ds@1952
   103
	class IThreadCreator;
ds@1952
   104
ds@1952
   105
	/**
ds@1952
   106
	 * @brief Describes a handle to a thread.
ds@1952
   107
	 */
ds@1952
   108
	class IThreadHandle
ds@1952
   109
	{
ds@1952
   110
	public:
ds@1952
   111
		/** Virtual destructor */
ds@1952
   112
		virtual ~IThreadHandle() { };
ds@1952
   113
	public:
ds@1952
   114
		/**
ds@1952
   115
		 * @brief Pauses parent thread until this thread completes.
ds@1952
   116
		 * 
ds@1952
   117
		 * @return			True if successful, false otherwise.
ds@1952
   118
		 */
ds@1952
   119
		virtual bool WaitForThread() =0;
ds@1952
   120
ds@1952
   121
		/**
ds@1952
   122
		 * @brief Destroys the thread handle.  This will not necessarily cancel the thread.
ds@1952
   123
		 */
ds@1952
   124
		virtual void DestroyThis() =0;
ds@1952
   125
ds@1952
   126
		/**
ds@1952
   127
		 * @brief Returns the parent threader.
ds@1952
   128
		 *
ds@1952
   129
		 * @return			IThreadCreator that created this thread.
ds@1952
   130
		 */
ds@1952
   131
		virtual IThreadCreator *Parent() =0;
ds@1952
   132
ds@1952
   133
		/**
ds@1952
   134
		 * @brief Returns the thread states.
ds@1952
   135
		 *
ds@1952
   136
		 * @param ptparams	Pointer to a ThreadParams buffer.
ds@1952
   137
		 */
ds@1952
   138
		virtual void GetParams(ThreadParams *ptparams) =0;
ds@1952
   139
ds@1952
   140
		/**
ds@1952
   141
		 * @brief Returns the thread priority.
ds@1952
   142
		 *
ds@1952
   143
		 * @return			Thread priority.
ds@1952
   144
		 */
ds@1952
   145
		virtual ThreadPriority GetPriority() =0;
ds@1952
   146
ds@1952
   147
		/**
ds@1952
   148
		 * @brief Sets thread priority.
ds@1952
   149
		 * NOTE: On Linux, this always returns false.
ds@1952
   150
		 *
ds@1952
   151
		 * @param prio		Thread priority to set.
ds@1952
   152
		 * @return			True if successful, false otherwise.
ds@1952
   153
		 */
ds@1952
   154
		virtual bool SetPriority(ThreadPriority prio) =0;
ds@1952
   155
ds@1952
   156
		/**
ds@1952
   157
		 * @brief Returns the thread state.
ds@1952
   158
		 * 
ds@1952
   159
		 * @return			Current thread state.
ds@1952
   160
		 */
ds@1952
   161
		virtual ThreadState GetState() =0;
ds@1952
   162
ds@1952
   163
		/**
ds@1952
   164
		 * @brief Attempts to unpause a paused thread.
ds@1952
   165
		 *
ds@1952
   166
		 * @return			True on success, false otherwise.
ds@1952
   167
		 */
ds@1952
   168
		virtual bool Unpause() =0;
ds@1952
   169
	};
ds@1952
   170
ds@1952
   171
	/**
ds@1952
   172
	 * @brief Handles a single thread's execution.
ds@1952
   173
	 */
ds@1952
   174
	class IThread
ds@1952
   175
	{
ds@1952
   176
	public:
ds@1952
   177
		/** Virtual destructor */
ds@1952
   178
		virtual ~IThread() { };
ds@1952
   179
	public:
ds@1952
   180
		/**
ds@1952
   181
		 * @brief Called when the thread runs (in its own thread).
ds@1952
   182
		 *
ds@1952
   183
		 * @param pHandle		Pointer to the thread's handle.
ds@1952
   184
		 */
ds@1952
   185
		virtual void RunThread(IThreadHandle *pHandle) =0;
ds@1952
   186
ds@1952
   187
		/**
ds@1952
   188
		 * @brief Called when the thread terminates.  This occurs inside the thread as well.
ds@1952
   189
		 *
ds@1952
   190
		 * @param pHandle		Pointer to the thread's handle.
ds@1952
   191
		 * @param cancel		True if the thread did not finish, false otherwise.
ds@1952
   192
		 */
ds@1952
   193
		virtual void OnTerminate(IThreadHandle *pHandle, bool cancel) =0;
ds@1952
   194
	};
ds@1952
   195
ds@1952
   196
ds@1952
   197
	/**
ds@1952
   198
	 * @brief Describes a thread creator
ds@1952
   199
	 */
ds@1952
   200
	class IThreadCreator
ds@1952
   201
	{
ds@1952
   202
	public:
ds@1952
   203
		/** Virtual Destructor */
ds@1952
   204
		virtual ~IThreadCreator() { };
ds@1952
   205
	public:
ds@1952
   206
		/**
ds@1952
   207
		 * @brief Creates a basic thread.
ds@1952
   208
		 *
ds@1952
   209
		 * @param pThread		IThread pointer for callbacks.
ds@1952
   210
		 */
ds@1952
   211
		virtual void MakeThread(IThread *pThread) =0;
ds@1952
   212
ds@1952
   213
		/**
ds@1952
   214
		 * @brief Creates a thread with specific options.
ds@1952
   215
		 *
ds@1952
   216
		 * @param pThread		IThread pointer for callbacks.
ds@1952
   217
		 * @param flags			Flags for the thread.
ds@1952
   218
		 * @return				IThreadHandle pointer (must be released).
ds@1952
   219
		 */
ds@1952
   220
		virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags) =0;
ds@1952
   221
ds@1952
   222
		/**
ds@1952
   223
		 * @brief Creates a thread with specific options.
ds@1952
   224
		 *
ds@1952
   225
		 * @param pThread		IThread pointer for callbacks.
ds@1952
   226
		 * @param params		Extended options for the thread.
ds@1952
   227
		 * @return				IThreadHandle pointer (must be released).
ds@1952
   228
		 */
ds@1952
   229
		virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params) =0;
ds@1952
   230
ds@1952
   231
		/**
ds@1952
   232
		 * @brief Returns the priority bounds.
ds@1952
   233
		 * Note: On Linux, the min and max are both Thread_Normal.
ds@1952
   234
		 *
ds@1952
   235
		 * @param max			Stores the maximum priority level.
ds@1952
   236
		 * @param min			Stores the minimum priority level.
ds@1952
   237
		 */
ds@1952
   238
		virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min) =0;
ds@1952
   239
	};
ds@1952
   240
ds@1952
   241
	/**
ds@1952
   242
	 * @brief Describes a simple locking mutex.
ds@1952
   243
	 */
ds@1952
   244
	class IMutex
ds@1952
   245
	{
ds@1952
   246
	public:
ds@1952
   247
		/** Virtual Destructor */
ds@1952
   248
		virtual ~IMutex() { };
ds@1952
   249
	public:
ds@1952
   250
		/**
ds@1952
   251
		 * @brief Attempts to lock, but returns instantly.
ds@1952
   252
		 *
ds@1952
   253
		 * @return		True if lock was obtained, false otherwise.
ds@1952
   254
		 */
ds@1952
   255
		virtual bool TryLock() =0;
ds@1952
   256
ds@1952
   257
		/**
ds@1952
   258
 		 * @brief Attempts to lock by waiting for release.
ds@1952
   259
		 */
ds@1952
   260
		virtual void Lock() =0;
ds@1952
   261
ds@1952
   262
		/**
ds@1952
   263
		 * @brief Unlocks the mutex.
ds@1952
   264
		 */
ds@1952
   265
		virtual void Unlock() =0;
ds@1952
   266
ds@1952
   267
		/**
ds@1952
   268
		 * @brief Destroys the mutex handle.
ds@1952
   269
		 */
ds@1952
   270
		virtual void DestroyThis() =0;
ds@1952
   271
	};
ds@1952
   272
ds@1952
   273
	/**
ds@1952
   274
	 * @brief Describes a simple "condition variable"/signal lock.
ds@1952
   275
	 */
ds@1952
   276
	class IEventSignal
ds@1952
   277
	{
ds@1952
   278
	public:
ds@1952
   279
		/** Virtual Destructor */
ds@1952
   280
		virtual ~IEventSignal() { };
ds@1952
   281
	public:
ds@1952
   282
		/**
ds@1952
   283
		 * @brief Waits for a signal.
ds@1952
   284
		 */
ds@1952
   285
		virtual void Wait() =0;
ds@1952
   286
ds@1952
   287
		/** 
ds@1952
   288
		 * @brief Triggers the signal and resets the signal after triggering.
ds@1952
   289
		 */
ds@1952
   290
		virtual void Signal() =0;
ds@1952
   291
ds@1952
   292
		/**
ds@1952
   293
		 * @brief Frees the signal handle.
ds@1952
   294
		 */
ds@1952
   295
		virtual void DestroyThis() =0;
ds@1952
   296
	};
ds@1952
   297
ds@1952
   298
	/**
ds@1952
   299
	 * @brief Describes possible worker states
ds@1952
   300
	 */
ds@1952
   301
	enum WorkerState
ds@1952
   302
	{
ds@1952
   303
		Worker_Invalid = -3,
ds@1952
   304
		Worker_Stopped = -2,
ds@1952
   305
		Worker_Paused = -1,
ds@1952
   306
		Worker_Running,
ds@1952
   307
	};
ds@1952
   308
ds@1952
   309
	/**
ds@1952
   310
	 * @brief This is a "worker pool."  A single thread places tasks in a queue.
ds@1952
   311
	 * Each IThread is then a task, rather than its own separate thread.
ds@1952
   312
	 */
ds@1952
   313
	class IThreadWorker : public IThreadCreator
ds@1952
   314
	{
ds@1952
   315
	public:
ds@1952
   316
		/** Virtual Destructor */
ds@1952
   317
		virtual ~IThreadWorker()
ds@1952
   318
		{
ds@1952
   319
		};
ds@1952
   320
	public:
ds@1952
   321
		/**
ds@1952
   322
		 * @brief Runs one "frame" of the worker.
ds@1952
   323
		 *
ds@1952
   324
		 * @return			Number of tasks processed.
ds@1952
   325
		 */
ds@1952
   326
		virtual unsigned int RunFrame() =0;
ds@1952
   327
	public:
ds@1952
   328
		/**
ds@1952
   329
		 * @brief Pauses the worker.
ds@1952
   330
		 *
ds@1952
   331
		 * @return			True on success, false otherwise.
ds@1952
   332
		 */
ds@1952
   333
		virtual bool Pause() =0;
ds@1952
   334
ds@1952
   335
		/**
ds@1952
   336
		 * @brief Unpauses the worker.
ds@1952
   337
		 *
ds@1952
   338
		 * @return			True on success, false otherwise.
ds@1952
   339
		 */
ds@1952
   340
		virtual bool Unpause() =0;
ds@1952
   341
ds@1952
   342
		/** 
ds@1952
   343
		 * @brief Starts the worker thread.
ds@1952
   344
		 *
ds@1952
   345
		 * @return			True on success, false otherwise.
ds@1952
   346
		 */
ds@1952
   347
		virtual bool Start() =0;
ds@1952
   348
ds@1952
   349
		/**
ds@1952
   350
		 * @brief Stops the worker thread.
ds@1952
   351
		 *
ds@1952
   352
		 * @param flush		If true, all remaining tasks will be cancelled.
ds@1952
   353
		 *					Otherwise, the threader will wait until the queue is empty.
ds@1952
   354
		 * @return			True on success, false otherwise.
ds@1952
   355
		 */
ds@1952
   356
		virtual bool Stop(bool flush) =0;
ds@1952
   357
ds@1952
   358
		/**
ds@1952
   359
		 * @brief Returns the status of the worker.
ds@1952
   360
		 *
ds@1952
   361
		 * @param numThreads	Pointer to store number of threads in the queue.
ds@1952
   362
		 * @return				State of the worker.
ds@1952
   363
		 */
ds@1952
   364
		virtual WorkerState GetStatus(unsigned int *numThreads) =0;
ds@1952
   365
	};
ds@1952
   366
ds@1952
   367
	/**
ds@1952
   368
	 * @brief Describes thread worker callbacks.
ds@1952
   369
	 */
ds@1952
   370
	class IThreadWorkerCallbacks
ds@1952
   371
	{
ds@1952
   372
	public:
ds@1952
   373
		/**
ds@1952
   374
		 * @brief Called when the worker thread is initialized.
ds@1952
   375
		 *
ds@1952
   376
		 * @param pWorker		Pointer to the worker.
ds@1952
   377
		 */
ds@1952
   378
		virtual void OnWorkerStart(IThreadWorker *pWorker)
ds@1952
   379
		{
ds@1952
   380
		}
ds@1952
   381
ds@1952
   382
		/**
ds@1952
   383
		 * @brief Called when the worker thread is cleaning up.
ds@1952
   384
		 *
ds@1952
   385
		 * @param pWorker		Pointer to the worker.
ds@1952
   386
		 */
ds@1952
   387
		virtual void OnWorkerStop(IThreadWorker *pWorker)
ds@1952
   388
		{
ds@1952
   389
		}
ds@1952
   390
	};
ds@1952
   391
ds@1952
   392
	/**
ds@1952
   393
	 * @brief Describes a threading system
ds@1952
   394
	 */
ds@1952
   395
	class IThreader : public SMInterface, public IThreadCreator
ds@1952
   396
	{
ds@1952
   397
	public:
ds@1952
   398
		virtual const char *GetInterfaceName()
ds@1952
   399
		{
ds@1952
   400
			return SMINTERFACE_THREADER_NAME;
ds@1952
   401
		}
ds@1952
   402
		virtual unsigned int GetInterfaceVersion()
ds@1952
   403
		{
ds@1952
   404
			return SMINTERFACE_THREADER_VERSION;
ds@1952
   405
		}
ds@1952
   406
		virtual bool IsVersionCompatible(unsigned int version)
ds@1952
   407
		{
ds@1952
   408
			if (version < 2)
ds@1952
   409
			{
ds@1952
   410
				return false;
ds@1952
   411
			}
ds@1952
   412
			return SMInterface::IsVersionCompatible(version);
ds@1952
   413
		}
ds@1952
   414
	public:
ds@1952
   415
		/**
ds@1952
   416
		 * @brief Creates a mutex (mutual exclusion lock).
ds@1952
   417
		 *
ds@1952
   418
		 * @return			A new IMutex pointer (must be destroyed).
ds@1952
   419
		 */
ds@1952
   420
		virtual IMutex *MakeMutex() =0;
ds@1952
   421
ds@1952
   422
		/**
ds@1952
   423
		 * @brief Sleeps the calling thread for a number of milliseconds.
ds@1952
   424
		 *
ds@1952
   425
		 * @param ms		Millisecond count to sleep.
ds@1952
   426
		 */
ds@1952
   427
		virtual void ThreadSleep(unsigned int ms) =0;
ds@1952
   428
ds@1952
   429
		/**
ds@1952
   430
		 * @brief Creates a non-signalled event.
ds@1952
   431
		 *
ds@1952
   432
		 * @return			A new IEventSignal pointer (must be destroyed).
ds@1952
   433
		 */
ds@1952
   434
		virtual IEventSignal *MakeEventSignal() =0;
ds@1952
   435
ds@1952
   436
		/**
ds@1952
   437
		 * @brief Creates a thread worker.
ds@1952
   438
		 *
ds@1952
   439
		 * @param hooks		Optional pointer to callback interface.
ds@1952
   440
		 * @param threaded	If true, the worker will be threaded.
ds@1952
   441
		 *					If false, the worker will require manual frame execution.
ds@1952
   442
		 * @return			A new IThreadWorker pointer (must be destroyed).
ds@1952
   443
		 */
ds@1952
   444
		virtual IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded) =0;
ds@1952
   445
ds@1952
   446
		/**
ds@1952
   447
		 * @brief Destroys an IThreadWorker pointer.
ds@1952
   448
		 *
ds@1952
   449
		 * @param pWorker	IThreadWorker pointer to destroy.
ds@1952
   450
		 */
ds@1952
   451
		virtual void DestroyWorker(IThreadWorker *pWorker) =0;
ds@1952
   452
	};
ds@1952
   453
};
ds@1952
   454
ds@1952
   455
#endif //_INCLUDE_SOURCEMOD_THREADER_H