core/thread/BaseWorker.h
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/thread/BaseWorker.h	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,104 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_BASEWORKER_H
    1.36 +#define _INCLUDE_SOURCEMOD_BASEWORKER_H
    1.37 +
    1.38 +#include "sh_list.h"
    1.39 +#include "ThreadSupport.h"
    1.40 +
    1.41 +#define SM_DEFAULT_THREADS_PER_FRAME	1
    1.42 +
    1.43 +class BaseWorker;
    1.44 +
    1.45 +//SW = Simple Wrapper
    1.46 +class SWThreadHandle : public IThreadHandle
    1.47 +{
    1.48 +	friend class BaseWorker;
    1.49 +public:
    1.50 +	SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread);
    1.51 +	IThread *GetThread();
    1.52 +public:
    1.53 +	//NOTE: We don't support this by default.
    1.54 +	//It's specific usage that'd require many mutexes
    1.55 +	virtual bool WaitForThread();
    1.56 +public:
    1.57 +	virtual void DestroyThis();
    1.58 +	virtual IThreadCreator *Parent();
    1.59 +	virtual void GetParams(ThreadParams *ptparams);
    1.60 +public:
    1.61 +	//Priorities not supported by default.
    1.62 +	virtual ThreadPriority GetPriority();
    1.63 +	virtual bool SetPriority(ThreadPriority prio);
    1.64 +public:
    1.65 +	virtual ThreadState GetState();
    1.66 +	virtual bool Unpause();
    1.67 +private:
    1.68 +	ThreadState m_state;
    1.69 +	ThreadParams m_params;
    1.70 +	IThreadCreator *m_parent;
    1.71 +	IThread *pThread;
    1.72 +};
    1.73 +
    1.74 +class BaseWorker : public IThreadWorker
    1.75 +{
    1.76 +public:
    1.77 +	BaseWorker(IThreadWorkerCallbacks *hooks);
    1.78 +	virtual ~BaseWorker();
    1.79 +public:	//IWorker
    1.80 +	virtual unsigned int RunFrame();
    1.81 +	//Controls the worker
    1.82 +	virtual bool Pause();
    1.83 +	virtual bool Unpause();
    1.84 +	virtual bool Start();
    1.85 +	virtual bool Stop(bool flush_cancel);
    1.86 +	//Flushes out any remaining threads
    1.87 +	virtual unsigned int Flush(bool flush_cancel);
    1.88 +	//returns status and number of threads in queue
    1.89 +	virtual WorkerState GetStatus(unsigned int *numThreads);
    1.90 +public:	//IThreadCreator
    1.91 +	virtual void MakeThread(IThread *pThread);
    1.92 +	virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
    1.93 +	virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
    1.94 +	virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
    1.95 +public:	//BaseWorker
    1.96 +	virtual void AddThreadToQueue(SWThreadHandle *pHandle);
    1.97 +	virtual SWThreadHandle *PopThreadFromQueue();
    1.98 +	virtual void SetMaxThreadsPerFrame(unsigned int threads);
    1.99 +	virtual unsigned int GetMaxThreadsPerFrame();
   1.100 +protected:
   1.101 +	SourceHook::List<SWThreadHandle *> m_ThreadQueue;
   1.102 +	unsigned int m_perFrame;
   1.103 +	volatile WorkerState m_state;
   1.104 +	IThreadWorkerCallbacks *m_pHooks;
   1.105 +};
   1.106 +
   1.107 +#endif //_INCLUDE_SOURCEMOD_BASEWORKER_H