core/logic/thread/BaseWorker.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Aug 14 14:20:57 2013 -0400 (2013-08-14)
changeset 4008 490b01aa0683
parent 3704 dd325be8f8a1
permissions -rw-r--r--
Updated some CS:GO gamedata.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 WorkerState BaseWorker::GetStatus(unsigned int *threads)
   128 {
   129 	if (threads)
   130 		*threads = m_perFrame;
   131 
   132 	return m_state;
   133 }
   134 
   135 unsigned int BaseWorker::RunFrame()
   136 {
   137 	unsigned int done = 0;
   138 	unsigned int max = m_perFrame;
   139 	SWThreadHandle *swt = NULL;
   140 	IThread *pThread = NULL;
   141 
   142 	while (done < max)
   143 	{
   144 		if ((swt=PopThreadFromQueue()) == NULL)
   145 		{
   146 			break;
   147 		}
   148 		pThread = swt->pThread;
   149 		swt->m_state = Thread_Running;
   150 		pThread->RunThread(swt);
   151 		swt->m_state = Thread_Done;
   152 		pThread->OnTerminate(swt, false);
   153 		if (swt->m_params.flags & Thread_AutoRelease)
   154 		{
   155 			delete swt;
   156 		}
   157 		done++;
   158 	}
   159 
   160 	return done;
   161 }
   162 
   163 void BaseWorker::SetMaxThreadsPerFrame(unsigned int threads)
   164 {
   165 	m_perFrame = threads;
   166 }
   167 
   168 bool BaseWorker::Start()
   169 {
   170 	if (m_state != Worker_Invalid && m_state != Worker_Stopped)
   171 	{
   172 		return false;
   173 	}
   174 
   175 	m_state = Worker_Running;
   176 
   177 	if (m_pHooks)
   178 	{
   179 		m_pHooks->OnWorkerStart(this);
   180 	}
   181 
   182     return true;
   183 }
   184 
   185 bool BaseWorker::Stop(bool flush_cancel)
   186 {
   187 	if (m_state == Worker_Invalid || m_state == Worker_Stopped)
   188 	{
   189 		return false;
   190 	}
   191 
   192 	if (m_state == Worker_Paused)
   193 	{
   194 		if (!Unpause())
   195 		{
   196 			return false;
   197 		}
   198 	}
   199 
   200 	m_state = Worker_Stopped;
   201 	Flush(flush_cancel);
   202 
   203 	if (m_pHooks)
   204 	{
   205 		m_pHooks->OnWorkerStop(this);
   206 	}
   207 
   208 	return true;
   209 }
   210 
   211 bool BaseWorker::Pause()
   212 {
   213 	if (m_state != Worker_Running)
   214 	{
   215 		return false;
   216 	}
   217 
   218 	m_state = Worker_Paused;
   219 
   220 	return true;
   221 }
   222 
   223 
   224 bool BaseWorker::Unpause()
   225 {
   226 	if (m_state != Worker_Paused)
   227 	{
   228 		return false;
   229 	}
   230 
   231 	m_state = Worker_Running;
   232 
   233 	return true;
   234 }
   235 
   236 /***********************
   237  * THREAD HANDLE STUFF *
   238  ***********************/
   239 
   240 void SWThreadHandle::DestroyThis()
   241 {
   242 	delete this;
   243 }
   244 
   245 void SWThreadHandle::GetParams(ThreadParams *p)
   246 {
   247 	*p = m_params;
   248 }
   249 
   250 ThreadPriority SWThreadHandle::GetPriority()
   251 {
   252 	return m_params.prio;
   253 }
   254 
   255 ThreadState SWThreadHandle::GetState()
   256 {
   257 	return m_state;
   258 }
   259 
   260 IThreadCreator *SWThreadHandle::Parent()
   261 {
   262 	return m_parent;
   263 }
   264 
   265 bool SWThreadHandle::SetPriority(ThreadPriority prio)
   266 {
   267 	if (m_params.prio != ThreadPrio_Normal)
   268 		return false;
   269 
   270 	m_params.prio = prio;
   271 
   272 	return true;
   273 }
   274 
   275 bool SWThreadHandle::Unpause()
   276 {
   277 	if (m_state != Thread_Paused)
   278 		return false;
   279 
   280 	m_state = Thread_Running;
   281 
   282 	return true;
   283 }
   284 
   285 bool SWThreadHandle::WaitForThread()
   286 {
   287 	return false;
   288 }
   289 
   290 SWThreadHandle::SWThreadHandle(IThreadCreator *parent, const ThreadParams *p, IThread *thread) : 
   291 	m_state(Thread_Paused), m_params(*p), m_parent(parent), pThread(thread)
   292 {
   293 }
   294 
   295 IThread *SWThreadHandle::GetThread()
   296 {
   297 	return pThread;
   298 }