dvander@1084: /**
dvander@1084: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@1231: * SourceMod
dvander@1231: * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
dvander@1084: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
dvander@1231: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
dvander@1231: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
dvander@1231: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
dvander@1084: *
dvander@1084: * Version: $Id$
dvander@1084: */
dvander@1084:
dvander@1084: #ifndef _INCLUDE_POSIXTHREADS_H_
dvander@1084: #define _INCLUDE_POSIXTHREADS_H_
dvander@1084:
dvander@1084: #include
dvander@1084: #include "IThreader.h"
dvander@1084:
dvander@1084: using namespace SourceMod;
dvander@1084:
dvander@1084: void *Posix_ThreadGate(void *param);
dvander@1084:
dvander@1084: class PosixThreader : public IThreader
dvander@1084: {
dvander@1084: public:
dvander@1084: class ThreadHandle : public IThreadHandle
dvander@1084: {
dvander@1084: friend class PosixThreader;
dvander@1084: friend void *Posix_ThreadGate(void *param);
dvander@1084: public:
dvander@1084: ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
dvander@1084: virtual ~ThreadHandle();
dvander@1084: public:
dvander@1084: virtual bool WaitForThread();
dvander@1084: virtual void DestroyThis();
dvander@1084: virtual IThreadCreator *Parent();
dvander@1084: virtual void GetParams(ThreadParams *ptparams);
dvander@1084: virtual ThreadPriority GetPriority();
dvander@1084: virtual bool SetPriority(ThreadPriority prio);
dvander@1084: virtual ThreadState GetState();
dvander@1084: virtual bool Unpause();
dvander@1084: protected:
dvander@1084: IThreader *m_parent; //Parent handle
dvander@1084: pthread_t m_thread; //Windows HANDLE
dvander@1084: ThreadParams m_params; //Current Parameters
dvander@1084: IThread *m_run; //Runnable context
dvander@1084: pthread_mutex_t m_statelock;
dvander@1084: pthread_mutex_t m_runlock;
dvander@1084: ThreadState m_state; //internal state
dvander@1084: };
dvander@1084: class PosixMutex : public IMutex
dvander@1084: {
dvander@1084: public:
dvander@1084: PosixMutex(pthread_mutex_t m) : m_mutex(m)
dvander@1084: {
dvander@1084: };
dvander@1084: virtual ~PosixMutex();
dvander@1084: public:
dvander@1084: virtual bool TryLock();
dvander@1084: virtual void Lock();
dvander@1084: virtual void Unlock();
dvander@1084: virtual void DestroyThis();
dvander@1084: protected:
dvander@1084: pthread_mutex_t m_mutex;
dvander@1084: };
dvander@1084: class PosixEventSignal : public IEventSignal
dvander@1084: {
dvander@1084: public:
dvander@1084: PosixEventSignal();
dvander@1084: virtual ~PosixEventSignal();
dvander@1084: public:
dvander@1084: virtual void Wait();
dvander@1084: virtual void Signal();
dvander@1084: virtual void DestroyThis();
dvander@1084: protected:
dvander@1084: pthread_cond_t m_cond;
dvander@1084: pthread_mutex_t m_mutex;
dvander@1084: };
dvander@1084: public:
dvander@1084: IMutex *MakeMutex();
dvander@1084: void MakeThread(IThread *pThread);
dvander@1084: IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
dvander@1084: IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
dvander@1084: void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
dvander@1084: void ThreadSleep(unsigned int ms);
dvander@1084: IEventSignal *MakeEventSignal();
dvander@1090: IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
dvander@1084: void DestroyWorker(IThreadWorker *pWorker);
dvander@1084: };
dvander@1084:
dvander@1084: #if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
dvander@1084: #define SM_MAIN_THREADER PosixThreader;
dvander@1084: typedef class PosixThreader MainThreader;
dvander@1084: #endif
dvander@1084:
dvander@1084: #endif //_INCLUDE_POSIXTHREADS_H_