dvander@2726: /** dvander@2726: * vim: set ts=4 : dvander@2726: * ============================================================================= dvander@2726: * SourceMod dvander@2726: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. dvander@2726: * ============================================================================= dvander@2726: * dvander@2726: * This program is free software; you can redistribute it and/or modify it under dvander@2726: * the terms of the GNU General Public License, version 3.0, as published by the dvander@2726: * Free Software Foundation. dvander@2726: * dvander@2726: * This program is distributed in the hope that it will be useful, but WITHOUT dvander@2726: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS dvander@2726: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more dvander@2726: * details. dvander@2726: * dvander@2726: * You should have received a copy of the GNU General Public License along with dvander@2726: * this program. If not, see . dvander@2726: * dvander@2726: * As a special exception, AlliedModders LLC gives you permission to link the dvander@2726: * code of this program (as well as its derivative works) to "Half-Life 2," the dvander@2726: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software dvander@2726: * by the Valve Corporation. You must obey the GNU General Public License in dvander@2726: * all respects for all other code used. Additionally, AlliedModders LLC grants dvander@2726: * this exception to all derivative works. AlliedModders LLC defines further dvander@2726: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), dvander@2726: * or . dvander@2726: * dvander@2726: * Version: $Id$ dvander@2726: */ dvander@2726: dvander@2726: #ifndef _INCLUDE_SOURCEMOD_BASEWORKER_H dvander@2726: #define _INCLUDE_SOURCEMOD_BASEWORKER_H dvander@2726: dvander@2726: #include "sh_list.h" dvander@2726: #include "ThreadSupport.h" dvander@2726: dvander@2726: #define SM_DEFAULT_THREADS_PER_FRAME 1 dvander@2726: dvander@2726: class BaseWorker; dvander@2726: dvander@2726: //SW = Simple Wrapper dvander@2726: class SWThreadHandle : public IThreadHandle dvander@2726: { dvander@2726: friend class BaseWorker; dvander@2726: public: dvander@2726: SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread); dvander@2726: IThread *GetThread(); dvander@2726: public: dvander@2726: //NOTE: We don't support this by default. dvander@2726: //It's specific usage that'd require many mutexes dvander@2726: virtual bool WaitForThread(); dvander@2726: public: dvander@2726: virtual void DestroyThis(); dvander@2726: virtual IThreadCreator *Parent(); dvander@2726: virtual void GetParams(ThreadParams *ptparams); dvander@2726: public: dvander@2726: //Priorities not supported by default. dvander@2726: virtual ThreadPriority GetPriority(); dvander@2726: virtual bool SetPriority(ThreadPriority prio); dvander@2726: public: dvander@2726: virtual ThreadState GetState(); dvander@2726: virtual bool Unpause(); dvander@2726: private: dvander@2726: ThreadState m_state; dvander@2726: ThreadParams m_params; dvander@2726: IThreadCreator *m_parent; dvander@2726: IThread *pThread; dvander@2726: }; dvander@2726: dvander@2726: class BaseWorker : public IThreadWorker dvander@2726: { dvander@2726: public: dvander@2726: BaseWorker(IThreadWorkerCallbacks *hooks); dvander@2726: virtual ~BaseWorker(); dvander@2726: public: //IWorker dvander@2726: virtual unsigned int RunFrame(); dvander@2726: //Controls the worker dvander@2726: virtual bool Pause(); dvander@2726: virtual bool Unpause(); dvander@2726: virtual bool Start(); dvander@2726: virtual bool Stop(bool flush_cancel); dvander@2726: //Flushes out any remaining threads dvander@2726: virtual unsigned int Flush(bool flush_cancel); dvander@2726: //returns status and number of threads in queue dvander@2726: virtual WorkerState GetStatus(unsigned int *numThreads); asherkin@3704: virtual void SetMaxThreadsPerFrame(unsigned int threads); asherkin@3704: virtual void SetThinkTimePerFrame(unsigned int thinktime) {} dvander@2726: public: //IThreadCreator dvander@2726: virtual void MakeThread(IThread *pThread); dvander@2726: virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags); dvander@2726: virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params); dvander@2726: virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min); dvander@2726: public: //BaseWorker dvander@2726: virtual void AddThreadToQueue(SWThreadHandle *pHandle); dvander@2726: virtual SWThreadHandle *PopThreadFromQueue(); dvander@2726: protected: dvander@2726: SourceHook::List m_ThreadQueue; dvander@2726: unsigned int m_perFrame; dvander@2726: volatile WorkerState m_state; dvander@2726: IThreadWorkerCallbacks *m_pHooks; dvander@2726: }; dvander@2726: dvander@2726: #endif //_INCLUDE_SOURCEMOD_BASEWORKER_H