1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/logic/thread/PosixThreads.h Wed May 13 21:55:50 2009 -0400
1.3 @@ -0,0 +1,115 @@
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 +#ifndef _INCLUDE_POSIXTHREADS_H_
1.36 +#define _INCLUDE_POSIXTHREADS_H_
1.37 +
1.38 +#include <pthread.h>
1.39 +#include "IThreader.h"
1.40 +
1.41 +using namespace SourceMod;
1.42 +
1.43 +void *Posix_ThreadGate(void *param);
1.44 +
1.45 +class PosixThreader : public IThreader
1.46 +{
1.47 +public:
1.48 + class ThreadHandle : public IThreadHandle
1.49 + {
1.50 + friend class PosixThreader;
1.51 + friend void *Posix_ThreadGate(void *param);
1.52 + public:
1.53 + ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
1.54 + virtual ~ThreadHandle();
1.55 + public:
1.56 + virtual bool WaitForThread();
1.57 + virtual void DestroyThis();
1.58 + virtual IThreadCreator *Parent();
1.59 + virtual void GetParams(ThreadParams *ptparams);
1.60 + virtual ThreadPriority GetPriority();
1.61 + virtual bool SetPriority(ThreadPriority prio);
1.62 + virtual ThreadState GetState();
1.63 + virtual bool Unpause();
1.64 + protected:
1.65 + IThreader *m_parent; //Parent handle
1.66 + pthread_t m_thread; //Windows HANDLE
1.67 + ThreadParams m_params; //Current Parameters
1.68 + IThread *m_run; //Runnable context
1.69 + pthread_mutex_t m_statelock;
1.70 + pthread_mutex_t m_runlock;
1.71 + ThreadState m_state; //internal state
1.72 + };
1.73 + class PosixMutex : public IMutex
1.74 + {
1.75 + public:
1.76 + PosixMutex(pthread_mutex_t m) : m_mutex(m)
1.77 + {
1.78 + };
1.79 + virtual ~PosixMutex();
1.80 + public:
1.81 + virtual bool TryLock();
1.82 + virtual void Lock();
1.83 + virtual void Unlock();
1.84 + virtual void DestroyThis();
1.85 + protected:
1.86 + pthread_mutex_t m_mutex;
1.87 + };
1.88 + class PosixEventSignal : public IEventSignal
1.89 + {
1.90 + public:
1.91 + PosixEventSignal();
1.92 + virtual ~PosixEventSignal();
1.93 + public:
1.94 + virtual void Wait();
1.95 + virtual void Signal();
1.96 + virtual void DestroyThis();
1.97 + protected:
1.98 + pthread_cond_t m_cond;
1.99 + pthread_mutex_t m_mutex;
1.100 + };
1.101 +public:
1.102 + IMutex *MakeMutex();
1.103 + void MakeThread(IThread *pThread);
1.104 + IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
1.105 + IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
1.106 + void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
1.107 + void ThreadSleep(unsigned int ms);
1.108 + IEventSignal *MakeEventSignal();
1.109 + IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
1.110 + void DestroyWorker(IThreadWorker *pWorker);
1.111 +};
1.112 +
1.113 +#if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
1.114 +#define SM_MAIN_THREADER PosixThreader;
1.115 +typedef class PosixThreader MainThreader;
1.116 +#endif
1.117 +
1.118 +#endif //_INCLUDE_POSIXTHREADS_H_