core/thread/ThreadWorker.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 19 18:34:36 2007 +0000 (2007-08-19)
changeset 1361 591f6d2fd11d
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
added helper function for panel text
dvander@1084
     1
/**
dvander@1084
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 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_THREADWORKER_H
dvander@1084
    33
#define _INCLUDE_SOURCEMOD_THREADWORKER_H
dvander@1084
    34
dvander@1084
    35
#include "BaseWorker.h"
dvander@1084
    36
dvander@1084
    37
#define DEFAULT_THINK_TIME_MS	500
dvander@1084
    38
dvander@1084
    39
class ThreadWorker : public BaseWorker, public IThread
dvander@1084
    40
{
dvander@1084
    41
public:
dvander@1090
    42
	ThreadWorker(IThreadWorkerCallbacks *hooks);
dvander@1090
    43
	ThreadWorker(IThreadWorkerCallbacks *hooks, IThreader *pThreader, unsigned int thinktime=DEFAULT_THINK_TIME_MS);
dvander@1084
    44
	virtual ~ThreadWorker();
dvander@1084
    45
public:	//IThread
dvander@1084
    46
	virtual void OnTerminate(IThreadHandle *pHandle, bool cancel);
dvander@1084
    47
	virtual void RunThread(IThreadHandle *pHandle);
dvander@1084
    48
public:	//IWorker
dvander@1084
    49
	//Controls the worker
dvander@1084
    50
	virtual bool Pause();
dvander@1084
    51
	virtual bool Unpause();
dvander@1084
    52
	virtual bool Start();
dvander@1084
    53
	virtual bool Stop(bool flush_cancel);
dvander@1084
    54
	//returns status and number of threads in queue
dvander@1084
    55
	virtual WorkerState GetStatus(unsigned int *numThreads);
dvander@1084
    56
public:	//BaseWorker
dvander@1084
    57
	virtual void AddThreadToQueue(SWThreadHandle *pHandle);
dvander@1084
    58
	virtual SWThreadHandle *PopThreadFromQueue();
dvander@1084
    59
protected:
dvander@1084
    60
	IThreader *m_Threader;
dvander@1084
    61
	IMutex *m_QueueLock;
dvander@1084
    62
	IMutex *m_StateLock;
dvander@1084
    63
	IEventSignal *m_PauseSignal;
dvander@1084
    64
	IEventSignal *m_AddSignal;
dvander@1084
    65
	IThreadHandle *me;
dvander@1084
    66
	unsigned int m_think_time;
dvander@1084
    67
	volatile bool m_Waiting;
dvander@1084
    68
	volatile bool m_FlushType;
dvander@1084
    69
};
dvander@1084
    70
dvander@1084
    71
#endif //_INCLUDE_SOURCEMOD_THREADWORKER_H