1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/thread/BaseWorker.h Mon Jul 09 16:10:23 2007 +0000
1.3 @@ -0,0 +1,86 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ===============================================================
1.7 + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
1.8 + * ===============================================================
1.9 + *
1.10 + * This file is not open source and may not be copied without explicit
1.11 + * written permission of AlliedModders LLC. This file may not be redistributed
1.12 + * in whole or significant part.
1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
1.14 + *
1.15 + * Version: $Id$
1.16 + */
1.17 +
1.18 +#ifndef _INCLUDE_SOURCEMOD_BASEWORKER_H
1.19 +#define _INCLUDE_SOURCEMOD_BASEWORKER_H
1.20 +
1.21 +#include "sh_list.h"
1.22 +#include "ThreadSupport.h"
1.23 +
1.24 +#define SM_DEFAULT_THREADS_PER_FRAME 1
1.25 +
1.26 +class BaseWorker;
1.27 +
1.28 +//SW = Simple Wrapper
1.29 +class SWThreadHandle : public IThreadHandle
1.30 +{
1.31 + friend class BaseWorker;
1.32 +public:
1.33 + SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread);
1.34 + IThread *GetThread();
1.35 +public:
1.36 + //NOTE: We don't support this by default.
1.37 + //It's specific usage that'd require many mutexes
1.38 + virtual bool WaitForThread();
1.39 +public:
1.40 + virtual void DestroyThis();
1.41 + virtual IThreadCreator *Parent();
1.42 + virtual void GetParams(ThreadParams *ptparams);
1.43 +public:
1.44 + //Priorities not supported by default.
1.45 + virtual ThreadPriority GetPriority();
1.46 + virtual bool SetPriority(ThreadPriority prio);
1.47 +public:
1.48 + virtual ThreadState GetState();
1.49 + virtual bool Unpause();
1.50 +private:
1.51 + ThreadState m_state;
1.52 + ThreadParams m_params;
1.53 + IThreadCreator *m_parent;
1.54 + IThread *pThread;
1.55 +};
1.56 +
1.57 +class BaseWorker : public IThreadWorker
1.58 +{
1.59 +public:
1.60 + BaseWorker();
1.61 + virtual ~BaseWorker();
1.62 +public: //IWorker
1.63 + virtual unsigned int RunFrame();
1.64 + //Controls the worker
1.65 + virtual bool Pause();
1.66 + virtual bool Unpause();
1.67 + virtual bool Start();
1.68 + virtual bool Stop(bool flush_cancel);
1.69 + //Flushes out any remaining threads
1.70 + virtual unsigned int Flush(bool flush_cancel);
1.71 + //returns status and number of threads in queue
1.72 + virtual WorkerState GetStatus(unsigned int *numThreads);
1.73 +public: //IThreadCreator
1.74 + virtual void MakeThread(IThread *pThread);
1.75 + virtual IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
1.76 + virtual IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
1.77 + virtual void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
1.78 +public: //BaseWorker
1.79 + virtual void AddThreadToQueue(SWThreadHandle *pHandle);
1.80 + virtual SWThreadHandle *PopThreadFromQueue();
1.81 + virtual void SetMaxThreadsPerFrame(unsigned int threads);
1.82 + virtual unsigned int GetMaxThreadsPerFrame();
1.83 +protected:
1.84 + SourceHook::List<SWThreadHandle *> m_ThreadQueue;
1.85 + unsigned int m_perFrame;
1.86 + volatile WorkerState m_state;
1.87 +};
1.88 +
1.89 +#endif //_INCLUDE_SOURCEMOD_BASEWORKER_H