3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "BaseWorker.h"
17 BaseWorker::BaseWorker(IThreadWorkerCallbacks *hooks) :
18 m_perFrame(SM_DEFAULT_THREADS_PER_FRAME),
19 m_state(Worker_Stopped),
24 BaseWorker::~BaseWorker()
26 if (m_state != Worker_Stopped || m_state != Worker_Invalid)
29 if (m_ThreadQueue.size())
33 void BaseWorker::MakeThread(IThread *pThread)
37 pt.flags = Thread_AutoRelease;
38 pt.prio = ThreadPrio_Normal;
40 MakeThread(pThread, &pt);
43 IThreadHandle *BaseWorker::MakeThread(IThread *pThread, ThreadFlags flags)
48 pt.prio = ThreadPrio_Normal;
50 return MakeThread(pThread, &pt);
53 IThreadHandle *BaseWorker::MakeThread(IThread *pThread, const ThreadParams *params)
55 if (m_state != Worker_Running)
58 SWThreadHandle *swt = new SWThreadHandle(this, params, pThread);
60 AddThreadToQueue(swt);
65 void BaseWorker::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
67 max = ThreadPrio_Normal;
68 min = ThreadPrio_Normal;
71 unsigned int BaseWorker::Flush(bool flush_cancel)
76 while ((swt=PopThreadFromQueue()) != NULL)
78 swt->m_state = Thread_Done;
80 swt->pThread->RunThread(swt);
81 swt->pThread->OnTerminate(swt, flush_cancel);
82 if (swt->m_params.flags & Thread_AutoRelease)
90 SWThreadHandle *BaseWorker::PopThreadFromQueue()
92 if (!m_ThreadQueue.size())
95 SourceHook::List<SWThreadHandle *>::iterator begin;
98 begin = m_ThreadQueue.begin();
100 m_ThreadQueue.erase(begin);
105 void BaseWorker::AddThreadToQueue(SWThreadHandle *pHandle)
107 m_ThreadQueue.push_back(pHandle);
110 unsigned int BaseWorker::GetMaxThreadsPerFrame()
115 WorkerState BaseWorker::GetStatus(unsigned int *threads)
118 *threads = m_perFrame;
123 unsigned int BaseWorker::RunFrame()
125 unsigned int done = 0;
126 unsigned int max = GetMaxThreadsPerFrame();
127 SWThreadHandle *swt = NULL;
128 IThread *pThread = NULL;
132 if ((swt=PopThreadFromQueue()) == NULL)
136 pThread = swt->pThread;
137 swt->m_state = Thread_Running;
138 pThread->RunThread(swt);
139 swt->m_state = Thread_Done;
140 pThread->OnTerminate(swt, false);
141 if (swt->m_params.flags & Thread_AutoRelease)
151 void BaseWorker::SetMaxThreadsPerFrame(unsigned int threads)
153 m_perFrame = threads;
156 bool BaseWorker::Start()
158 if (m_state != Worker_Invalid && m_state != Worker_Stopped)
163 m_state = Worker_Running;
167 m_pHooks->OnWorkerStart(this);
173 bool BaseWorker::Stop(bool flush_cancel)
175 if (m_state == Worker_Invalid || m_state == Worker_Stopped)
180 if (m_state == Worker_Paused)
188 m_state = Worker_Stopped;
193 m_pHooks->OnWorkerStop(this);
199 bool BaseWorker::Pause()
201 if (m_state != Worker_Running)
206 m_state = Worker_Paused;
212 bool BaseWorker::Unpause()
214 if (m_state != Worker_Paused)
219 m_state = Worker_Running;
224 /***********************
225 * THREAD HANDLE STUFF *
226 ***********************/
228 void SWThreadHandle::DestroyThis()
233 void SWThreadHandle::GetParams(ThreadParams *p)
238 ThreadPriority SWThreadHandle::GetPriority()
240 return m_params.prio;
243 ThreadState SWThreadHandle::GetState()
248 IThreadCreator *SWThreadHandle::Parent()
253 bool SWThreadHandle::SetPriority(ThreadPriority prio)
255 if (m_params.prio != ThreadPrio_Normal)
258 m_params.prio = prio;
263 bool SWThreadHandle::Unpause()
265 if (m_state != Thread_Paused)
268 m_state = Thread_Running;
273 bool SWThreadHandle::WaitForThread()
278 SWThreadHandle::SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread) :
279 m_state(Thread_Paused), m_params(*p), m_parent(parent), pThread(thread)
283 IThread *SWThreadHandle::GetThread()