dvander@1084: /**
dvander@1084: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@1231: * SourceMod
ds@1947: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
dvander@1084: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
dvander@1231: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
dvander@1231: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
dvander@1231: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
dvander@1084: *
dvander@1084: * Version: $Id$
dvander@1084: */
dvander@1084:
dvander@1084: #ifndef _INCLUDE_SOURCEMOD_THREADWORKER_H
dvander@1084: #define _INCLUDE_SOURCEMOD_THREADWORKER_H
dvander@1084:
dvander@1084: #include "BaseWorker.h"
dvander@1084:
dvander@2163: #define DEFAULT_THINK_TIME_MS 50
dvander@1084:
dvander@1084: class ThreadWorker : public BaseWorker, public IThread
dvander@1084: {
dvander@1084: public:
dvander@1090: ThreadWorker(IThreadWorkerCallbacks *hooks);
dvander@1090: ThreadWorker(IThreadWorkerCallbacks *hooks, IThreader *pThreader, unsigned int thinktime=DEFAULT_THINK_TIME_MS);
dvander@1084: virtual ~ThreadWorker();
dvander@1084: public: //IThread
dvander@1084: virtual void OnTerminate(IThreadHandle *pHandle, bool cancel);
dvander@1084: virtual void RunThread(IThreadHandle *pHandle);
dvander@1084: public: //IWorker
dvander@1084: //Controls the worker
dvander@1084: virtual bool Pause();
dvander@1084: virtual bool Unpause();
dvander@1084: virtual bool Start();
dvander@1084: virtual bool Stop(bool flush_cancel);
dvander@1084: //returns status and number of threads in queue
dvander@1084: virtual WorkerState GetStatus(unsigned int *numThreads);
dvander@1084: public: //BaseWorker
dvander@1084: virtual void AddThreadToQueue(SWThreadHandle *pHandle);
dvander@1084: virtual SWThreadHandle *PopThreadFromQueue();
dvander@1084: protected:
dvander@1084: IThreader *m_Threader;
dvander@1084: IMutex *m_QueueLock;
dvander@1084: IMutex *m_StateLock;
dvander@1084: IEventSignal *m_PauseSignal;
dvander@1084: IEventSignal *m_AddSignal;
dvander@1084: IThreadHandle *me;
dvander@1084: unsigned int m_think_time;
dvander@1084: volatile bool m_Waiting;
dvander@1084: volatile bool m_FlushType;
dvander@1084: };
dvander@1084:
dvander@1084: #endif //_INCLUDE_SOURCEMOD_THREADWORKER_H