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