ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include
ds@1952: #include "PosixThreads.h"
ds@1952: #include "ThreadWorker.h"
ds@1952:
ds@1952: IThreadWorker *PosixThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
ds@1952: {
ds@1952: if (threaded)
ds@1952: {
ds@1952: return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
ds@1952: } else {
ds@1952: return new BaseWorker(hooks);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::DestroyWorker(IThreadWorker *pWorker)
ds@1952: {
ds@1952: delete pWorker;
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::ThreadSleep(unsigned int ms)
ds@1952: {
ds@1952: usleep( ms * 1000 );
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
ds@1952: {
ds@1952: max = ThreadPrio_Normal;
ds@1952: min = ThreadPrio_Normal;
ds@1952: }
ds@1952:
ds@1952: IMutex *PosixThreader::MakeMutex()
ds@1952: {
ds@1952: pthread_mutex_t mutex;
ds@1952:
ds@1952: if (pthread_mutex_init(&mutex, NULL) != 0)
ds@1952: return NULL;
ds@1952:
ds@1952: PosixMutex *pMutex = new PosixMutex(mutex);
ds@1952:
ds@1952: return pMutex;
ds@1952: }
ds@1952:
ds@1952:
ds@1952: void PosixThreader::MakeThread(IThread *pThread)
ds@1952: {
ds@1952: ThreadParams defparams;
ds@1952:
ds@1952: defparams.flags = Thread_AutoRelease;
ds@1952: defparams.prio = ThreadPrio_Normal;
ds@1952:
ds@1952: MakeThread(pThread, &defparams);
ds@1952: }
ds@1952:
ds@1952: IThreadHandle *PosixThreader::MakeThread(IThread *pThread, ThreadFlags flags)
ds@1952: {
ds@1952: ThreadParams defparams;
ds@1952:
ds@1952: defparams.flags = flags;
ds@1952: defparams.prio = ThreadPrio_Normal;
ds@1952:
ds@1952: return MakeThread(pThread, &defparams);
ds@1952: }
ds@1952:
ds@1952: void *Posix_ThreadGate(void *param)
ds@1952: {
ds@1952: PosixThreader::ThreadHandle *pHandle =
ds@1952: reinterpret_cast(param);
ds@1952:
ds@1952: //Block this thread from being started initially.
ds@1952: pthread_mutex_lock(&pHandle->m_runlock);
ds@1952: //if we get here, we've obtained the lock and are allowed to run.
ds@1952: //unlock and continue.
ds@1952: pthread_mutex_unlock(&pHandle->m_runlock);
ds@1952:
ds@1952: pHandle->m_run->RunThread(pHandle);
ds@1952:
ds@1952: ThreadParams params;
ds@1952: pthread_mutex_lock(&pHandle->m_statelock);
ds@1952: pHandle->m_state = Thread_Done;
ds@1952: pHandle->GetParams(¶ms);
ds@1952: pthread_mutex_unlock(&pHandle->m_statelock);
ds@1952:
ds@1952: pHandle->m_run->OnTerminate(pHandle, false);
ds@1952: if (params.flags & Thread_AutoRelease)
ds@1952: delete pHandle;
ds@1952:
ds@1952: return 0;
ds@1952: }
ds@1952:
ds@1952: ThreadParams g_defparams;
ds@1952: IThreadHandle *PosixThreader::MakeThread(IThread *pThread, const ThreadParams *params)
ds@1952: {
ds@1952: if (params == NULL)
ds@1952: params = &g_defparams;
ds@1952:
ds@1952: PosixThreader::ThreadHandle *pHandle =
ds@1952: new PosixThreader::ThreadHandle(this, pThread, params);
ds@1952:
ds@1952: pthread_mutex_lock(&pHandle->m_runlock);
ds@1952:
ds@1952: int err;
ds@1952: err = pthread_create(&pHandle->m_thread, NULL, Posix_ThreadGate, (void *)pHandle);
ds@1952:
ds@1952: if (err != 0)
ds@1952: {
ds@1952: pthread_mutex_unlock(&pHandle->m_runlock);
ds@1952: delete pHandle;
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: //Don't bother setting priority...
ds@1952:
ds@1952: if (!(pHandle->m_params.flags & Thread_CreateSuspended))
ds@1952: {
ds@1952: pHandle->m_state = Thread_Running;
ds@1952: err = pthread_mutex_unlock(&pHandle->m_runlock);
ds@1952: if (err != 0)
ds@1952: pHandle->m_state = Thread_Paused;
ds@1952: }
ds@1952:
ds@1952: return pHandle;
ds@1952: }
ds@1952:
ds@1952: IEventSignal *PosixThreader::MakeEventSignal()
ds@1952: {
ds@1952: return new PosixEventSignal();
ds@1952: }
ds@1952:
ds@1952: /*****************
ds@1952: **** Mutexes ****
ds@1952: *****************/
ds@1952:
ds@1952: PosixThreader::PosixMutex::~PosixMutex()
ds@1952: {
ds@1952: pthread_mutex_destroy(&m_mutex);
ds@1952: }
ds@1952:
ds@1952: bool PosixThreader::PosixMutex::TryLock()
ds@1952: {
ds@1952: int err = pthread_mutex_trylock(&m_mutex);
ds@1952:
ds@1952: return (err == 0);
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::PosixMutex::Lock()
ds@1952: {
ds@1952: pthread_mutex_lock(&m_mutex);
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::PosixMutex::Unlock()
ds@1952: {
ds@1952: pthread_mutex_unlock(&m_mutex);
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::PosixMutex::DestroyThis()
ds@1952: {
ds@1952: delete this;
ds@1952: }
ds@1952:
ds@1952: /******************
ds@1952: * Thread Handles *
ds@1952: ******************/
ds@1952:
ds@1952: PosixThreader::ThreadHandle::ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params) :
ds@1952: m_parent(parent), m_params(*params), m_run(run), m_state(Thread_Paused)
ds@1952: {
ds@1952: pthread_mutex_init(&m_runlock, NULL);
ds@1952: pthread_mutex_init(&m_statelock, NULL);
ds@1952: }
ds@1952:
ds@1952: PosixThreader::ThreadHandle::~ThreadHandle()
ds@1952: {
ds@1952: pthread_mutex_destroy(&m_runlock);
ds@1952: pthread_mutex_destroy(&m_statelock);
ds@1952: }
ds@1952:
ds@1952: bool PosixThreader::ThreadHandle::WaitForThread()
ds@1952: {
ds@1952: void *arg;
ds@1952:
ds@1952: if (pthread_join(m_thread, &arg) != 0)
ds@1952: return false;
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: ThreadState PosixThreader::ThreadHandle::GetState()
ds@1952: {
ds@1952: ThreadState state;
ds@1952:
ds@1952: pthread_mutex_lock(&m_statelock);
ds@1952: state = m_state;
ds@1952: pthread_mutex_unlock(&m_statelock);
ds@1952:
ds@1952: return state;
ds@1952: }
ds@1952:
ds@1952: IThreadCreator *PosixThreader::ThreadHandle::Parent()
ds@1952: {
ds@1952: return m_parent;
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::ThreadHandle::DestroyThis()
ds@1952: {
ds@1952: if (m_params.flags & Thread_AutoRelease)
ds@1952: return;
ds@1952:
ds@1952: delete this;
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::ThreadHandle::GetParams(ThreadParams *ptparams)
ds@1952: {
ds@1952: if (!ptparams)
ds@1952: return;
ds@1952:
ds@1952: *ptparams = m_params;
ds@1952: }
ds@1952:
ds@1952: ThreadPriority PosixThreader::ThreadHandle::GetPriority()
ds@1952: {
ds@1952: return ThreadPrio_Normal;
ds@1952: }
ds@1952:
ds@1952: bool PosixThreader::ThreadHandle::SetPriority(ThreadPriority prio)
ds@1952: {
ds@1952: return (prio == ThreadPrio_Normal);
ds@1952: }
ds@1952:
ds@1952: bool PosixThreader::ThreadHandle::Unpause()
ds@1952: {
ds@1952: if (m_state != Thread_Paused)
ds@1952: return false;
ds@1952:
ds@1952: m_state = Thread_Running;
ds@1952:
ds@1952: if (pthread_mutex_unlock(&m_runlock) != 0)
ds@1952: {
ds@1952: m_state = Thread_Paused;
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: /*****************
ds@1952: * EVENT SIGNALS *
ds@1952: *****************/
ds@1952:
ds@1952: PosixThreader::PosixEventSignal::PosixEventSignal()
ds@1952: {
ds@1952: pthread_cond_init(&m_cond, NULL);
ds@1952: pthread_mutex_init(&m_mutex, NULL);
ds@1952: }
ds@1952:
ds@1952: PosixThreader::PosixEventSignal::~PosixEventSignal()
ds@1952: {
ds@1952: pthread_cond_destroy(&m_cond);
ds@1952: pthread_mutex_destroy(&m_mutex);
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::PosixEventSignal::Wait()
ds@1952: {
ds@1952: pthread_mutex_lock(&m_mutex);
ds@1952: pthread_cond_wait(&m_cond, &m_mutex);
ds@1952: pthread_mutex_unlock(&m_mutex);
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::PosixEventSignal::Signal()
ds@1952: {
ds@1952: pthread_mutex_lock(&m_mutex);
ds@1952: pthread_cond_broadcast(&m_cond);
ds@1952: pthread_mutex_unlock(&m_mutex);
ds@1952: }
ds@1952:
ds@1952: void PosixThreader::PosixEventSignal::DestroyThis()
ds@1952: {
ds@1952: delete this;
ds@1952: }