core/logic/thread/PosixThreads.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:25:30 2014 -0400 (2014-05-21)
changeset 4377 7a44be5b7162
parent 4025 542dde661d5c
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet:
     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 <unistd.h>
    33 #include "PosixThreads.h"
    34 #include "ThreadWorker.h"
    35 
    36 IThreadWorker *PosixThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
    37 {
    38 	if (threaded)
    39 	{
    40 		return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
    41 	} else {
    42 		return new BaseWorker(hooks);
    43 	}
    44 }
    45 
    46 void PosixThreader::DestroyWorker(IThreadWorker *pWorker)
    47 {
    48 	delete pWorker;
    49 }
    50 
    51 void PosixThreader::ThreadSleep(unsigned int ms)
    52 {
    53 	usleep( ms * 1000 );
    54 }
    55 
    56 void PosixThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
    57 {
    58 	max = ThreadPrio_Normal;
    59 	min = ThreadPrio_Normal;
    60 }
    61 
    62 IMutex *PosixThreader::MakeMutex()
    63 {
    64 	return new CompatMutex();
    65 }
    66 
    67 void PosixThreader::MakeThread(IThread *pThread)
    68 {
    69 	ThreadParams defparams;
    70 
    71 	defparams.flags = Thread_AutoRelease;
    72 	defparams.prio = ThreadPrio_Normal;
    73 
    74 	MakeThread(pThread, &defparams);
    75 }
    76 
    77 IThreadHandle *PosixThreader::MakeThread(IThread *pThread, ThreadFlags flags)
    78 {
    79 	ThreadParams defparams;
    80 
    81 	defparams.flags = flags;
    82 	defparams.prio = ThreadPrio_Normal;
    83 
    84 	return MakeThread(pThread, &defparams);
    85 }
    86 
    87 void PosixThreader::ThreadHandle::Run()
    88 {
    89 	// Wait for an unpause if necessary.
    90 	{
    91 		ke::AutoLock lock(&m_runlock);
    92 		if (m_state == Thread_Paused)
    93 			m_runlock.Wait();
    94 	}
    95 
    96 	m_run->RunThread(this);
    97 	m_state = Thread_Done;
    98 	m_run->OnTerminate(this, false);
    99 
   100 	if (m_params.flags & Thread_AutoRelease)
   101 		delete this;
   102 }
   103 
   104 ThreadParams g_defparams;
   105 IThreadHandle *PosixThreader::MakeThread(IThread *pThread, const ThreadParams *params)
   106 {
   107 	if (params == NULL)
   108 		params = &g_defparams;
   109 
   110 	ke::AutoPtr<ThreadHandle> pHandle(new ThreadHandle(this, pThread, params));
   111 
   112 	pHandle->m_thread = new ke::Thread(pHandle, "SourceMod");
   113 	if (!pHandle->m_thread->Succeeded())
   114 		return NULL;
   115 
   116 	if (!(params->flags & Thread_CreateSuspended))
   117 		pHandle->Unpause();
   118 
   119 	return pHandle.take();
   120 }
   121 
   122 IEventSignal *PosixThreader::MakeEventSignal()
   123 {
   124 	return new CompatCondVar();
   125 }
   126 
   127 /******************
   128 * Thread Handles *
   129 ******************/
   130 
   131 PosixThreader::ThreadHandle::ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params) : 
   132 	m_parent(parent), m_params(*params), m_run(run), m_state(Thread_Paused)
   133 {
   134 }
   135 
   136 PosixThreader::ThreadHandle::~ThreadHandle()
   137 {
   138 }
   139 
   140 bool PosixThreader::ThreadHandle::WaitForThread()
   141 {
   142 	if (!m_thread)
   143 		return false;
   144 
   145 	m_thread->Join();
   146 	return true;
   147 }
   148 
   149 ThreadState PosixThreader::ThreadHandle::GetState()
   150 {
   151 	return m_state;
   152 }
   153 
   154 IThreadCreator *PosixThreader::ThreadHandle::Parent()
   155 {
   156 	return m_parent;
   157 }
   158 
   159 void PosixThreader::ThreadHandle::DestroyThis()
   160 {
   161 	if (m_params.flags & Thread_AutoRelease)
   162 		return;
   163 
   164 	delete this;
   165 }
   166 
   167 void PosixThreader::ThreadHandle::GetParams(ThreadParams *ptparams)
   168 {
   169 	if (!ptparams)
   170 		return;
   171 
   172 	*ptparams = m_params;
   173 }
   174 
   175 ThreadPriority PosixThreader::ThreadHandle::GetPriority()
   176 {
   177 	return ThreadPrio_Normal;
   178 }
   179 
   180 bool PosixThreader::ThreadHandle::SetPriority(ThreadPriority prio)
   181 {
   182 	return (prio == ThreadPrio_Normal);
   183 }
   184 
   185 bool PosixThreader::ThreadHandle::Unpause()
   186 {
   187 	if (m_state != Thread_Paused)
   188 		return false;
   189 
   190 	ke::AutoLock lock(&m_runlock);
   191 	m_state = Thread_Running;
   192 	m_runlock.Notify();
   193 	return true;
   194 }
   195