1.1 --- a/core/thread/PosixThreads.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,309 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include <unistd.h>
1.36 -#include "PosixThreads.h"
1.37 -#include "ThreadWorker.h"
1.38 -
1.39 -IThreadWorker *PosixThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
1.40 -{
1.41 - if (threaded)
1.42 - {
1.43 - return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
1.44 - } else {
1.45 - return new BaseWorker(hooks);
1.46 - }
1.47 -}
1.48 -
1.49 -void PosixThreader::DestroyWorker(IThreadWorker *pWorker)
1.50 -{
1.51 - delete pWorker;
1.52 -}
1.53 -
1.54 -void PosixThreader::ThreadSleep(unsigned int ms)
1.55 -{
1.56 - usleep( ms * 1000 );
1.57 -}
1.58 -
1.59 -void PosixThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
1.60 -{
1.61 - max = ThreadPrio_Normal;
1.62 - min = ThreadPrio_Normal;
1.63 -}
1.64 -
1.65 -IMutex *PosixThreader::MakeMutex()
1.66 -{
1.67 - pthread_mutex_t mutex;
1.68 -
1.69 - if (pthread_mutex_init(&mutex, NULL) != 0)
1.70 - return NULL;
1.71 -
1.72 - PosixMutex *pMutex = new PosixMutex(mutex);
1.73 -
1.74 - return pMutex;
1.75 -}
1.76 -
1.77 -
1.78 -void PosixThreader::MakeThread(IThread *pThread)
1.79 -{
1.80 - ThreadParams defparams;
1.81 -
1.82 - defparams.flags = Thread_AutoRelease;
1.83 - defparams.prio = ThreadPrio_Normal;
1.84 -
1.85 - MakeThread(pThread, &defparams);
1.86 -}
1.87 -
1.88 -IThreadHandle *PosixThreader::MakeThread(IThread *pThread, ThreadFlags flags)
1.89 -{
1.90 - ThreadParams defparams;
1.91 -
1.92 - defparams.flags = flags;
1.93 - defparams.prio = ThreadPrio_Normal;
1.94 -
1.95 - return MakeThread(pThread, &defparams);
1.96 -}
1.97 -
1.98 -void *Posix_ThreadGate(void *param)
1.99 -{
1.100 - PosixThreader::ThreadHandle *pHandle =
1.101 - reinterpret_cast<PosixThreader::ThreadHandle *>(param);
1.102 -
1.103 - //Block this thread from being started initially.
1.104 - pthread_mutex_lock(&pHandle->m_runlock);
1.105 - //if we get here, we've obtained the lock and are allowed to run.
1.106 - //unlock and continue.
1.107 - pthread_mutex_unlock(&pHandle->m_runlock);
1.108 -
1.109 - pHandle->m_run->RunThread(pHandle);
1.110 -
1.111 - ThreadParams params;
1.112 - pthread_mutex_lock(&pHandle->m_statelock);
1.113 - pHandle->m_state = Thread_Done;
1.114 - pHandle->GetParams(¶ms);
1.115 - pthread_mutex_unlock(&pHandle->m_statelock);
1.116 -
1.117 - pHandle->m_run->OnTerminate(pHandle, false);
1.118 - if (params.flags & Thread_AutoRelease)
1.119 - delete pHandle;
1.120 -
1.121 - return 0;
1.122 -}
1.123 -
1.124 -ThreadParams g_defparams;
1.125 -IThreadHandle *PosixThreader::MakeThread(IThread *pThread, const ThreadParams *params)
1.126 -{
1.127 - if (params == NULL)
1.128 - params = &g_defparams;
1.129 -
1.130 - PosixThreader::ThreadHandle *pHandle =
1.131 - new PosixThreader::ThreadHandle(this, pThread, params);
1.132 -
1.133 - pthread_mutex_lock(&pHandle->m_runlock);
1.134 -
1.135 - int err;
1.136 - err = pthread_create(&pHandle->m_thread, NULL, Posix_ThreadGate, (void *)pHandle);
1.137 -
1.138 - if (err != 0)
1.139 - {
1.140 - pthread_mutex_unlock(&pHandle->m_runlock);
1.141 - delete pHandle;
1.142 - return NULL;
1.143 - }
1.144 -
1.145 - //Don't bother setting priority...
1.146 -
1.147 - if (!(pHandle->m_params.flags & Thread_CreateSuspended))
1.148 - {
1.149 - pHandle->m_state = Thread_Running;
1.150 - err = pthread_mutex_unlock(&pHandle->m_runlock);
1.151 - if (err != 0)
1.152 - pHandle->m_state = Thread_Paused;
1.153 - }
1.154 -
1.155 - return pHandle;
1.156 -}
1.157 -
1.158 -IEventSignal *PosixThreader::MakeEventSignal()
1.159 -{
1.160 - return new PosixEventSignal();
1.161 -}
1.162 -
1.163 -/*****************
1.164 -**** Mutexes ****
1.165 -*****************/
1.166 -
1.167 -PosixThreader::PosixMutex::~PosixMutex()
1.168 -{
1.169 - pthread_mutex_destroy(&m_mutex);
1.170 -}
1.171 -
1.172 -bool PosixThreader::PosixMutex::TryLock()
1.173 -{
1.174 - int err = pthread_mutex_trylock(&m_mutex);
1.175 -
1.176 - return (err == 0);
1.177 -}
1.178 -
1.179 -void PosixThreader::PosixMutex::Lock()
1.180 -{
1.181 - pthread_mutex_lock(&m_mutex);
1.182 -}
1.183 -
1.184 -void PosixThreader::PosixMutex::Unlock()
1.185 -{
1.186 - pthread_mutex_unlock(&m_mutex);
1.187 -}
1.188 -
1.189 -void PosixThreader::PosixMutex::DestroyThis()
1.190 -{
1.191 - delete this;
1.192 -}
1.193 -
1.194 -/******************
1.195 -* Thread Handles *
1.196 -******************/
1.197 -
1.198 -PosixThreader::ThreadHandle::ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params) :
1.199 - m_parent(parent), m_params(*params), m_run(run), m_state(Thread_Paused)
1.200 -{
1.201 - pthread_mutex_init(&m_runlock, NULL);
1.202 - pthread_mutex_init(&m_statelock, NULL);
1.203 -}
1.204 -
1.205 -PosixThreader::ThreadHandle::~ThreadHandle()
1.206 -{
1.207 - pthread_mutex_destroy(&m_runlock);
1.208 - pthread_mutex_destroy(&m_statelock);
1.209 -}
1.210 -
1.211 -bool PosixThreader::ThreadHandle::WaitForThread()
1.212 -{
1.213 - void *arg;
1.214 -
1.215 - if (pthread_join(m_thread, &arg) != 0)
1.216 - return false;
1.217 -
1.218 - return true;
1.219 -}
1.220 -
1.221 -ThreadState PosixThreader::ThreadHandle::GetState()
1.222 -{
1.223 - ThreadState state;
1.224 -
1.225 - pthread_mutex_lock(&m_statelock);
1.226 - state = m_state;
1.227 - pthread_mutex_unlock(&m_statelock);
1.228 -
1.229 - return state;
1.230 -}
1.231 -
1.232 -IThreadCreator *PosixThreader::ThreadHandle::Parent()
1.233 -{
1.234 - return m_parent;
1.235 -}
1.236 -
1.237 -void PosixThreader::ThreadHandle::DestroyThis()
1.238 -{
1.239 - if (m_params.flags & Thread_AutoRelease)
1.240 - return;
1.241 -
1.242 - delete this;
1.243 -}
1.244 -
1.245 -void PosixThreader::ThreadHandle::GetParams(ThreadParams *ptparams)
1.246 -{
1.247 - if (!ptparams)
1.248 - return;
1.249 -
1.250 - *ptparams = m_params;
1.251 -}
1.252 -
1.253 -ThreadPriority PosixThreader::ThreadHandle::GetPriority()
1.254 -{
1.255 - return ThreadPrio_Normal;
1.256 -}
1.257 -
1.258 -bool PosixThreader::ThreadHandle::SetPriority(ThreadPriority prio)
1.259 -{
1.260 - return (prio == ThreadPrio_Normal);
1.261 -}
1.262 -
1.263 -bool PosixThreader::ThreadHandle::Unpause()
1.264 -{
1.265 - if (m_state != Thread_Paused)
1.266 - return false;
1.267 -
1.268 - m_state = Thread_Running;
1.269 -
1.270 - if (pthread_mutex_unlock(&m_runlock) != 0)
1.271 - {
1.272 - m_state = Thread_Paused;
1.273 - return false;
1.274 - }
1.275 -
1.276 - return true;
1.277 -}
1.278 -
1.279 -/*****************
1.280 - * EVENT SIGNALS *
1.281 - *****************/
1.282 -
1.283 -PosixThreader::PosixEventSignal::PosixEventSignal()
1.284 -{
1.285 - pthread_cond_init(&m_cond, NULL);
1.286 - pthread_mutex_init(&m_mutex, NULL);
1.287 -}
1.288 -
1.289 -PosixThreader::PosixEventSignal::~PosixEventSignal()
1.290 -{
1.291 - pthread_cond_destroy(&m_cond);
1.292 - pthread_mutex_destroy(&m_mutex);
1.293 -}
1.294 -
1.295 -void PosixThreader::PosixEventSignal::Wait()
1.296 -{
1.297 - pthread_mutex_lock(&m_mutex);
1.298 - pthread_cond_wait(&m_cond, &m_mutex);
1.299 - pthread_mutex_unlock(&m_mutex);
1.300 -}
1.301 -
1.302 -void PosixThreader::PosixEventSignal::Signal()
1.303 -{
1.304 - pthread_mutex_lock(&m_mutex);
1.305 - pthread_cond_broadcast(&m_cond);
1.306 - pthread_mutex_unlock(&m_mutex);
1.307 -}
1.308 -
1.309 -void PosixThreader::PosixEventSignal::DestroyThis()
1.310 -{
1.311 - delete this;
1.312 -}