SourceMod SDK  1.7
IThreader.h
Go to the documentation of this file.
1 
32 #ifndef _INCLUDE_SOURCEMOD_THREADER_H
33 #define _INCLUDE_SOURCEMOD_THREADER_H
34 
42 #include <IShareSys.h>
43 
44 #define SMINTERFACE_THREADER_NAME "IThreader"
45 #define SMINTERFACE_THREADER_VERSION 3
46 
47 namespace SourceMod
48 {
52  enum ThreadFlags
53  {
54  Thread_Default = 0,
61  Thread_AutoRelease = 1,
65  Thread_CreateSuspended = 2,
66  };
67 
71  enum ThreadPriority
72  {
73  ThreadPrio_Minimum = -8,
74  ThreadPrio_Low = -3,
75  ThreadPrio_Normal = 0,
76  ThreadPrio_High = 3,
77  ThreadPrio_Maximum = 8,
78  };
79 
83  enum ThreadState
84  {
85  Thread_Running = 0,
86  Thread_Paused = 1,
87  Thread_Done = 2,
88  };
89 
93  struct ThreadParams
94  {
97  flags(Thread_Default),
98  prio(ThreadPrio_Normal)
99  {
100  };
101  ThreadFlags flags;
102  ThreadPriority prio;
103  };
104 
105  class IThreadCreator;
106 
111  {
112  public:
114  virtual ~IThreadHandle() { };
115  public:
121  virtual bool WaitForThread() =0;
122 
126  virtual void DestroyThis() =0;
127 
133  virtual IThreadCreator *Parent() =0;
134 
140  virtual void GetParams(ThreadParams *ptparams) =0;
141 
147  virtual ThreadPriority GetPriority() =0;
148 
156  virtual bool SetPriority(ThreadPriority prio) =0;
157 
163  virtual ThreadState GetState() =0;
164 
170  virtual bool Unpause() =0;
171  };
172 
176  class IThread
177  {
178  public:
180  virtual ~IThread() { };
181  public:
187  virtual void RunThread(IThreadHandle *pHandle) =0;
188 
195  virtual void OnTerminate(IThreadHandle *pHandle, bool cancel) =0;
196  };
197 
198 
203  {
204  public:
206  virtual ~IThreadCreator() { };
207  public:
213  virtual void MakeThread(IThread *pThread) =0;
214 
222  virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags) =0;
223 
231  virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params) =0;
232 
240  virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min) =0;
241  };
242 
246  class IMutex
247  {
248  public:
250  virtual ~IMutex() { };
251  public:
257  virtual bool TryLock() =0;
258 
262  virtual void Lock() =0;
263 
267  virtual void Unlock() =0;
268 
272  virtual void DestroyThis() =0;
273  };
274 
279  {
280  public:
282  virtual ~IEventSignal() { };
283  public:
287  virtual void Wait() =0;
288 
292  virtual void Signal() =0;
293 
297  virtual void DestroyThis() =0;
298  };
299 
303  enum WorkerState
304  {
305  Worker_Invalid = -3,
306  Worker_Stopped = -2,
307  Worker_Paused = -1,
308  Worker_Running,
309  };
310 
316  {
317  public:
319  virtual ~IThreadWorker()
320  {
321  };
322  public:
328  virtual unsigned int RunFrame() =0;
329  public:
335  virtual bool Pause() =0;
336 
342  virtual bool Unpause() =0;
343 
349  virtual bool Start() =0;
350 
358  virtual bool Stop(bool flush) =0;
359 
366  virtual WorkerState GetStatus(unsigned int *numThreads) =0;
367 
374  virtual void SetMaxThreadsPerFrame(unsigned int threads) =0;
375 
383  virtual void SetThinkTimePerFrame(unsigned int thinktime) =0;
384  };
385 
390  {
391  public:
397  virtual void OnWorkerStart(IThreadWorker *pWorker)
398  {
399  }
400 
406  virtual void OnWorkerStop(IThreadWorker *pWorker)
407  {
408  }
409  };
410 
414  class IThreader : public SMInterface, public IThreadCreator
415  {
416  public:
417  virtual const char *GetInterfaceName()
418  {
419  return SMINTERFACE_THREADER_NAME;
420  }
421  virtual unsigned int GetInterfaceVersion()
422  {
423  return SMINTERFACE_THREADER_VERSION;
424  }
425  virtual bool IsVersionCompatible(unsigned int version)
426  {
427  if (version < 2)
428  {
429  return false;
430  }
431  return SMInterface::IsVersionCompatible(version);
432  }
433  public:
439  virtual IMutex *MakeMutex() =0;
440 
446  virtual void ThreadSleep(unsigned int ms) =0;
447 
453  virtual IEventSignal *MakeEventSignal() =0;
454 
463  virtual IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded) =0;
464 
470  virtual void DestroyWorker(IThreadWorker *pWorker) =0;
471  };
472 };
473 
474 #endif //_INCLUDE_SOURCEMOD_THREADER_H
virtual void DestroyWorker(IThreadWorker *pWorker)=0
Destroys an IThreadWorker pointer.
ThreadPriority prio
Definition: IThreader.h:102
Describes thread worker callbacks.
Definition: IThreader.h:389
virtual void OnWorkerStart(IThreadWorker *pWorker)
Called when the worker thread is initialized.
Definition: IThreader.h:397
virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)=0
Returns the priority bounds. Note: On Linux, the min and max are both Thread_Normal.
virtual bool WaitForThread()=0
Pauses parent thread until this thread completes.
Defines the Share System, responsible for shared resources and dependencies.
virtual void ThreadSleep(unsigned int ms)=0
Sleeps the calling thread for a number of milliseconds.
virtual ThreadPriority GetPriority()=0
Returns the thread priority.
This is a "worker pool." A single thread places tasks in a queue. Each IThread is then a task...
Definition: IThreader.h:315
virtual const char * GetInterfaceName()
Must return a string defining the interface's unique name.
Definition: IThreader.h:417
Defines the base functionality required by a shared interface.
Definition: IShareSys.h:92
Describes a simple "condition variable"/signal lock.
Definition: IThreader.h:278
virtual IMutex * MakeMutex()=0
Creates a mutex (mutual exclusion lock).
virtual bool Pause()=0
Pauses the worker.
virtual void Lock()=0
Attempts to lock by waiting for release.
Thread-specific parameters.
Definition: IThreader.h:93
virtual bool IsVersionCompatible(unsigned int version)
Must return whether the requested version number is backwards compatible. Note: This can be overridde...
Definition: IThreader.h:425
virtual ~IMutex()
Definition: IThreader.h:250
virtual IThreadCreator * Parent()=0
Returns the parent threader.
virtual void DestroyThis()=0
Destroys the mutex handle.
virtual void GetParams(ThreadParams *ptparams)=0
Returns the thread states.
virtual void RunThread(IThreadHandle *pHandle)=0
Called when the thread runs (in its own thread).
ThreadFlags flags
Definition: IThreader.h:100
virtual void SetThinkTimePerFrame(unsigned int thinktime)=0
For threaded workers, the think time of a frame. Has no effect for non-threaded workers. Default value is 50ms.
virtual bool TryLock()=0
Attempts to lock, but returns instantly.
Describes a handle to a thread.
Definition: IThreader.h:110
virtual void OnTerminate(IThreadHandle *pHandle, bool cancel)=0
Called when the thread terminates. This occurs inside the thread as well.
virtual void DestroyThis()=0
Frees the signal handle.
Describes a threading system.
Definition: IThreader.h:414
virtual void OnWorkerStop(IThreadWorker *pWorker)
Called when the worker thread is cleaning up.
Definition: IThreader.h:406
virtual bool Unpause()=0
Unpauses the worker.
virtual void DestroyThis()=0
Destroys the thread handle. This will not necessarily cancel the thread.
virtual void MakeThread(IThread *pThread)=0
Creates a basic thread.
virtual ~IEventSignal()
Definition: IThreader.h:282
virtual bool Stop(bool flush)=0
Stops the worker thread.
virtual IEventSignal * MakeEventSignal()=0
Creates a non-signalled event.
virtual ~IThread()
Definition: IThreader.h:180
virtual bool IsVersionCompatible(unsigned int version)
Must return whether the requested version number is backwards compatible. Note: This can be overridde...
Definition: IShareSys.h:112
Definition: IAdminSystem.h:63
virtual ~IThreadCreator()
Definition: IThreader.h:206
virtual void Unlock()=0
Unlocks the mutex.
virtual WorkerState GetStatus(unsigned int *numThreads)=0
Returns the status of the worker.
virtual ThreadState GetState()=0
Returns the thread state.
Describes a simple locking mutex.
Definition: IThreader.h:246
virtual unsigned int RunFrame()=0
Runs one "frame" of the worker.
virtual ~IThreadWorker()
Definition: IThreader.h:319
virtual bool SetPriority(ThreadPriority prio)=0
Sets thread priority. NOTE: On Linux, this always returns false.
virtual IThreadWorker * MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)=0
Creates a thread worker.
Handles a single thread's execution.
Definition: IThreader.h:176
virtual bool Unpause()=0
Attempts to unpause a paused thread.
virtual bool Start()=0
Starts the worker thread.
Describes a thread creator.
Definition: IThreader.h:202
virtual unsigned int GetInterfaceVersion()
Must return an integer defining the interface's version.
Definition: IThreader.h:421
virtual void SetMaxThreadsPerFrame(unsigned int threads)=0
Sets the number of threads to run per frame. Default value is 1 thread per frame. ...
virtual void Wait()=0
Waits for a signal.
ThreadParams()
Definition: IThreader.h:96
virtual void Signal()=0
Triggers the signal and resets the signal after triggering.
virtual ~IThreadHandle()
Definition: IThreader.h:114