core/logic/thread/BaseWorker.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3704 dd325be8f8a1
permissions -rw-r--r--
Updated some CS:GO gamedata.
dvander@2726
     1
/**
dvander@2726
     2
 * vim: set ts=4 :
dvander@2726
     3
 * =============================================================================
dvander@2726
     4
 * SourceMod
dvander@2726
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
dvander@2726
     6
 * =============================================================================
dvander@2726
     7
 *
dvander@2726
     8
 * This program is free software; you can redistribute it and/or modify it under
dvander@2726
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
dvander@2726
    10
 * Free Software Foundation.
dvander@2726
    11
 * 
dvander@2726
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
dvander@2726
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@2726
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
dvander@2726
    15
 * details.
dvander@2726
    16
 *
dvander@2726
    17
 * You should have received a copy of the GNU General Public License along with
dvander@2726
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@2726
    19
 *
dvander@2726
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
dvander@2726
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
dvander@2726
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
dvander@2726
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
dvander@2726
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
dvander@2726
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
dvander@2726
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
dvander@2726
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@2726
    28
 *
dvander@2726
    29
 * Version: $Id$
dvander@2726
    30
 */
dvander@2726
    31
dvander@2726
    32
#ifndef _INCLUDE_SOURCEMOD_BASEWORKER_H
dvander@2726
    33
#define _INCLUDE_SOURCEMOD_BASEWORKER_H
dvander@2726
    34
dvander@2726
    35
#include "sh_list.h"
dvander@2726
    36
#include "ThreadSupport.h"
dvander@2726
    37
dvander@2726
    38
#define SM_DEFAULT_THREADS_PER_FRAME	1
dvander@2726
    39
dvander@2726
    40
class BaseWorker;
dvander@2726
    41
dvander@2726
    42
//SW = Simple Wrapper
dvander@2726
    43
class SWThreadHandle : public IThreadHandle
dvander@2726
    44
{
dvander@2726
    45
	friend class BaseWorker;
dvander@2726
    46
public:
dvander@2726
    47
	SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread);
dvander@2726
    48
	IThread *GetThread();
dvander@2726
    49
public:
dvander@2726
    50
	//NOTE: We don't support this by default.
dvander@2726
    51
	//It's specific usage that'd require many mutexes
dvander@2726
    52
	virtual bool WaitForThread();
dvander@2726
    53
public:
dvander@2726
    54
	virtual void DestroyThis();
dvander@2726
    55
	virtual IThreadCreator *Parent();
dvander@2726
    56
	virtual void GetParams(ThreadParams *ptparams);
dvander@2726
    57
public:
dvander@2726
    58
	//Priorities not supported by default.
dvander@2726
    59
	virtual ThreadPriority GetPriority();
dvander@2726
    60
	virtual bool SetPriority(ThreadPriority prio);
dvander@2726
    61
public:
dvander@2726
    62
	virtual ThreadState GetState();
dvander@2726
    63
	virtual bool Unpause();
dvander@2726
    64
private:
dvander@2726
    65
	ThreadState m_state;
dvander@2726
    66
	ThreadParams m_params;
dvander@2726
    67
	IThreadCreator *m_parent;
dvander@2726
    68
	IThread *pThread;
dvander@2726
    69
};
dvander@2726
    70
dvander@2726
    71
class BaseWorker : public IThreadWorker
dvander@2726
    72
{
dvander@2726
    73
public:
dvander@2726
    74
	BaseWorker(IThreadWorkerCallbacks *hooks);
dvander@2726
    75
	virtual ~BaseWorker();
dvander@2726
    76
public:	//IWorker
dvander@2726
    77
	virtual unsigned int RunFrame();
dvander@2726
    78
	//Controls the worker
dvander@2726
    79
	virtual bool Pause();
dvander@2726
    80
	virtual bool Unpause();
dvander@2726
    81
	virtual bool Start();
dvander@2726
    82
	virtual bool Stop(bool flush_cancel);
dvander@2726
    83
	//Flushes out any remaining threads
dvander@2726
    84
	virtual unsigned int Flush(bool flush_cancel);
dvander@2726
    85
	//returns status and number of threads in queue
dvander@2726
    86
	virtual WorkerState GetStatus(unsigned int *numThreads);
asherkin@3704
    87
	virtual void SetMaxThreadsPerFrame(unsigned int threads);
asherkin@3704
    88
	virtual void SetThinkTimePerFrame(unsigned int thinktime) {}
dvander@2726
    89
public:	//IThreadCreator
dvander@2726
    90
	virtual void MakeThread(IThread *pThread);
dvander@2726
    91
	virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
dvander@2726
    92
	virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
dvander@2726
    93
	virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
dvander@2726
    94
public:	//BaseWorker
dvander@2726
    95
	virtual void AddThreadToQueue(SWThreadHandle *pHandle);
dvander@2726
    96
	virtual SWThreadHandle *PopThreadFromQueue();
dvander@2726
    97
protected:
dvander@2726
    98
	SourceHook::List<SWThreadHandle *> m_ThreadQueue;
dvander@2726
    99
	unsigned int m_perFrame;
dvander@2726
   100
	volatile WorkerState m_state;
dvander@2726
   101
	IThreadWorkerCallbacks *m_pHooks;
dvander@2726
   102
};
dvander@2726
   103
dvander@2726
   104
#endif //_INCLUDE_SOURCEMOD_BASEWORKER_H