core/thread/BaseWorker.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 16 16:06:18 2008 -0800 (2008-11-16)
changeset 2411 8dfaa3d730b5
parent 2289 44045ead61cd
permissions -rw-r--r--
Removed pointless "systems" folder from core.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_BASEWORKER_H
    33 #define _INCLUDE_SOURCEMOD_BASEWORKER_H
    34 
    35 #include "sh_list.h"
    36 #include "ThreadSupport.h"
    37 
    38 #define SM_DEFAULT_THREADS_PER_FRAME	1
    39 
    40 class BaseWorker;
    41 
    42 //SW = Simple Wrapper
    43 class SWThreadHandle : public IThreadHandle
    44 {
    45 	friend class BaseWorker;
    46 public:
    47 	SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread);
    48 	IThread *GetThread();
    49 public:
    50 	//NOTE: We don't support this by default.
    51 	//It's specific usage that'd require many mutexes
    52 	virtual bool WaitForThread();
    53 public:
    54 	virtual void DestroyThis();
    55 	virtual IThreadCreator *Parent();
    56 	virtual void GetParams(ThreadParams *ptparams);
    57 public:
    58 	//Priorities not supported by default.
    59 	virtual ThreadPriority GetPriority();
    60 	virtual bool SetPriority(ThreadPriority prio);
    61 public:
    62 	virtual ThreadState GetState();
    63 	virtual bool Unpause();
    64 private:
    65 	ThreadState m_state;
    66 	ThreadParams m_params;
    67 	IThreadCreator *m_parent;
    68 	IThread *pThread;
    69 };
    70 
    71 class BaseWorker : public IThreadWorker
    72 {
    73 public:
    74 	BaseWorker(IThreadWorkerCallbacks *hooks);
    75 	virtual ~BaseWorker();
    76 public:	//IWorker
    77 	virtual unsigned int RunFrame();
    78 	//Controls the worker
    79 	virtual bool Pause();
    80 	virtual bool Unpause();
    81 	virtual bool Start();
    82 	virtual bool Stop(bool flush_cancel);
    83 	//Flushes out any remaining threads
    84 	virtual unsigned int Flush(bool flush_cancel);
    85 	//returns status and number of threads in queue
    86 	virtual WorkerState GetStatus(unsigned int *numThreads);
    87 public:	//IThreadCreator
    88 	virtual void MakeThread(IThread *pThread);
    89 	virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
    90 	virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
    91 	virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
    92 public:	//BaseWorker
    93 	virtual void AddThreadToQueue(SWThreadHandle *pHandle);
    94 	virtual SWThreadHandle *PopThreadFromQueue();
    95 	virtual void SetMaxThreadsPerFrame(unsigned int threads);
    96 	virtual unsigned int GetMaxThreadsPerFrame();
    97 protected:
    98 	SourceHook::List<SWThreadHandle *> m_ThreadQueue;
    99 	unsigned int m_perFrame;
   100 	volatile WorkerState m_state;
   101 	IThreadWorkerCallbacks *m_pHooks;
   102 };
   103 
   104 #endif //_INCLUDE_SOURCEMOD_BASEWORKER_H