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