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