core/thread/BaseWorker.cpp
author David Anderson <dvander@alliedmods.net>
Tue Sep 11 18:08:49 2007 +0000 (2007-09-11)
changeset 1408 2fd63cc4bd9c
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
timelimit function now gets initialized
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "BaseWorker.h"
    33 
    34 BaseWorker::BaseWorker(IThreadWorkerCallbacks *hooks) : 
    35 	m_perFrame(SM_DEFAULT_THREADS_PER_FRAME),
    36 	m_state(Worker_Stopped),
    37 	m_pHooks(hooks)
    38 {
    39 }
    40 
    41 BaseWorker::~BaseWorker()
    42 {
    43 	if (m_state != Worker_Stopped || m_state != Worker_Invalid)
    44 		Stop(true);
    45 
    46 	if (m_ThreadQueue.size())
    47 		Flush(true);
    48 }
    49 
    50 void BaseWorker::MakeThread(IThread *pThread)
    51 {
    52 	ThreadParams pt;
    53 
    54 	pt.flags = Thread_AutoRelease;
    55 	pt.prio = ThreadPrio_Normal;
    56 
    57 	MakeThread(pThread, &pt);
    58 }
    59 
    60 IThreadHandle *BaseWorker::MakeThread(IThread *pThread, ThreadFlags flags)
    61 {
    62 	ThreadParams pt;
    63 
    64 	pt.flags = flags;
    65 	pt.prio = ThreadPrio_Normal;
    66 
    67 	return MakeThread(pThread, &pt);
    68 }
    69 
    70 IThreadHandle *BaseWorker::MakeThread(IThread *pThread, const ThreadParams *params)
    71 {
    72 	if (m_state != Worker_Running)
    73 		return NULL;
    74 
    75 	SWThreadHandle *swt = new SWThreadHandle(this, params, pThread);
    76 
    77 	AddThreadToQueue(swt);
    78 
    79 	return swt;
    80 }
    81 
    82 void BaseWorker::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
    83 {
    84 	max = ThreadPrio_Normal;
    85 	min = ThreadPrio_Normal;
    86 }
    87 
    88 unsigned int BaseWorker::Flush(bool flush_cancel)
    89 {
    90 	SWThreadHandle *swt;
    91 	unsigned int num = 0;
    92 
    93 	while ((swt=PopThreadFromQueue()) != NULL)
    94 	{
    95 		swt->m_state = Thread_Done;
    96 		if (!flush_cancel)
    97 			swt->pThread->RunThread(swt);
    98 		swt->pThread->OnTerminate(swt, flush_cancel);
    99 		if (swt->m_params.flags & Thread_AutoRelease)
   100 			delete swt;
   101 		num++;
   102 	}
   103 
   104 	return num;
   105 }
   106 
   107 SWThreadHandle *BaseWorker::PopThreadFromQueue()
   108 {
   109 	if (!m_ThreadQueue.size())
   110 		return NULL;
   111 
   112 	SourceHook::List<SWThreadHandle *>::iterator begin;
   113 	SWThreadHandle *swt;
   114 
   115 	begin = m_ThreadQueue.begin();
   116 	swt = (*begin);
   117 	m_ThreadQueue.erase(begin);
   118 
   119 	return swt;
   120 }
   121 
   122 void BaseWorker::AddThreadToQueue(SWThreadHandle *pHandle)
   123 {
   124 	m_ThreadQueue.push_back(pHandle);
   125 }
   126 
   127 unsigned int BaseWorker::GetMaxThreadsPerFrame()
   128 {
   129 	return m_perFrame;
   130 }
   131 
   132 WorkerState BaseWorker::GetStatus(unsigned int *threads)
   133 {
   134 	if (threads)
   135 		*threads = m_perFrame;
   136 
   137 	return m_state;
   138 }
   139 
   140 unsigned int BaseWorker::RunFrame()
   141 {
   142 	unsigned int done = 0;
   143 	unsigned int max = GetMaxThreadsPerFrame();
   144 	SWThreadHandle *swt = NULL;
   145 	IThread *pThread = NULL;
   146 
   147 	while (done < max)
   148 	{
   149 		if ((swt=PopThreadFromQueue()) == NULL)
   150 		{
   151 			break;
   152 		}
   153 		pThread = swt->pThread;
   154 		swt->m_state = Thread_Running;
   155 		pThread->RunThread(swt);
   156 		swt->m_state = Thread_Done;
   157 		pThread->OnTerminate(swt, false);
   158 		if (swt->m_params.flags & Thread_AutoRelease)
   159 		{
   160 			delete swt;
   161 		}
   162 		done++;
   163 	}
   164 
   165 	return done;
   166 }
   167 
   168 void BaseWorker::SetMaxThreadsPerFrame(unsigned int threads)
   169 {
   170 	m_perFrame = threads;
   171 }
   172 
   173 bool BaseWorker::Start()
   174 {
   175 	if (m_state != Worker_Invalid && m_state != Worker_Stopped)
   176 	{
   177 		return false;
   178 	}
   179 
   180 	m_state = Worker_Running;
   181 
   182 	if (m_pHooks)
   183 	{
   184 		m_pHooks->OnWorkerStart(this);
   185 	}
   186 
   187     return true;
   188 }
   189 
   190 bool BaseWorker::Stop(bool flush_cancel)
   191 {
   192 	if (m_state == Worker_Invalid || m_state == Worker_Stopped)
   193 	{
   194 		return false;
   195 	}
   196 
   197 	if (m_state == Worker_Paused)
   198 	{
   199 		if (!Unpause())
   200 		{
   201 			return false;
   202 		}
   203 	}
   204 
   205 	m_state = Worker_Stopped;
   206 	Flush(flush_cancel);
   207 
   208 	if (m_pHooks)
   209 	{
   210 		m_pHooks->OnWorkerStop(this);
   211 	}
   212 
   213 	return true;
   214 }
   215 
   216 bool BaseWorker::Pause()
   217 {
   218 	if (m_state != Worker_Running)
   219 	{
   220 		return false;
   221 	}
   222 
   223 	m_state = Worker_Paused;
   224 
   225 	return true;
   226 }
   227 
   228 
   229 bool BaseWorker::Unpause()
   230 {
   231 	if (m_state != Worker_Paused)
   232 	{
   233 		return false;
   234 	}
   235 
   236 	m_state = Worker_Running;
   237 
   238 	return true;
   239 }
   240 
   241 /***********************
   242  * THREAD HANDLE STUFF *
   243  ***********************/
   244 
   245 void SWThreadHandle::DestroyThis()
   246 {
   247 	delete this;
   248 }
   249 
   250 void SWThreadHandle::GetParams(ThreadParams *p)
   251 {
   252 	*p = m_params;
   253 }
   254 
   255 ThreadPriority SWThreadHandle::GetPriority()
   256 {
   257 	return m_params.prio;
   258 }
   259 
   260 ThreadState SWThreadHandle::GetState()
   261 {
   262 	return m_state;
   263 }
   264 
   265 IThreadCreator *SWThreadHandle::Parent()
   266 {
   267 	return m_parent;
   268 }
   269 
   270 bool SWThreadHandle::SetPriority(ThreadPriority prio)
   271 {
   272 	if (m_params.prio != ThreadPrio_Normal)
   273 		return false;
   274 
   275 	m_params.prio = prio;
   276 
   277 	return true;
   278 }
   279 
   280 bool SWThreadHandle::Unpause()
   281 {
   282 	if (m_state != Thread_Paused)
   283 		return false;
   284 
   285 	m_state = Thread_Running;
   286 
   287 	return true;
   288 }
   289 
   290 bool SWThreadHandle::WaitForThread()
   291 {
   292 	return false;
   293 }
   294 
   295 SWThreadHandle::SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread) : 
   296 	m_state(Thread_Paused), m_params(*p), m_parent(parent), pThread(thread)
   297 {
   298 }
   299 
   300 IThread *SWThreadHandle::GetThread()
   301 {
   302 	return pThread;
   303 }