core/thread/PosixThreads.h
changeset 1084 241c1b9b4048
child 1090 5c54e353c019
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/thread/PosixThreads.h	Mon Jul 09 16:10:23 2007 +0000
     1.3 @@ -0,0 +1,107 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod Threader API
     1.8 + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
     1.9 + * ===============================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or
    1.12 + * modify it under the terms of the GNU General Public License
    1.13 + * as published by the Free Software Foundation; either version 2
    1.14 + * of the License, or (at your option) any later version.
    1.15 + * This program is distributed in the hope that it will be useful,
    1.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 + * GNU General Public License for more details.
    1.19 + * 
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program; if not, write to the Free Software
    1.22 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    1.23 + *
    1.24 + * Version: $Id$
    1.25 + */
    1.26 +
    1.27 +#ifndef _INCLUDE_POSIXTHREADS_H_
    1.28 +#define _INCLUDE_POSIXTHREADS_H_
    1.29 +
    1.30 +#include <pthread.h>
    1.31 +#include "IThreader.h"
    1.32 +
    1.33 +using namespace SourceMod;
    1.34 +
    1.35 +void *Posix_ThreadGate(void *param);
    1.36 +
    1.37 +class PosixThreader : public IThreader
    1.38 +{
    1.39 +public:
    1.40 +	class ThreadHandle : public IThreadHandle
    1.41 +	{
    1.42 +		friend class PosixThreader;
    1.43 +		friend void *Posix_ThreadGate(void *param);
    1.44 +	public:
    1.45 +		ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
    1.46 +		virtual ~ThreadHandle();
    1.47 +	public:
    1.48 +		virtual bool WaitForThread();
    1.49 +		virtual void DestroyThis();
    1.50 +		virtual IThreadCreator *Parent();
    1.51 +		virtual void GetParams(ThreadParams *ptparams);
    1.52 +		virtual ThreadPriority GetPriority();
    1.53 +		virtual bool SetPriority(ThreadPriority prio);
    1.54 +		virtual ThreadState GetState();
    1.55 +		virtual bool Unpause();
    1.56 +	protected:
    1.57 +		IThreader *m_parent;		//Parent handle
    1.58 +		pthread_t m_thread;			//Windows HANDLE	
    1.59 +		ThreadParams m_params;		//Current Parameters
    1.60 +		IThread *m_run;	//Runnable context
    1.61 +		pthread_mutex_t m_statelock;
    1.62 +		pthread_mutex_t m_runlock;
    1.63 +		ThreadState m_state;		//internal state
    1.64 +	};
    1.65 +	class PosixMutex : public IMutex
    1.66 +	{
    1.67 +	public:
    1.68 +		PosixMutex(pthread_mutex_t m) : m_mutex(m)
    1.69 +		{
    1.70 +		};
    1.71 +		virtual ~PosixMutex();
    1.72 +	public:
    1.73 +		virtual bool TryLock();
    1.74 +		virtual void Lock();
    1.75 +		virtual void Unlock();
    1.76 +		virtual void DestroyThis();
    1.77 +	protected:
    1.78 +		pthread_mutex_t m_mutex;
    1.79 +	};
    1.80 +	class PosixEventSignal : public IEventSignal
    1.81 +	{
    1.82 +	public:
    1.83 +		PosixEventSignal();
    1.84 +		virtual ~PosixEventSignal();
    1.85 +	public:
    1.86 +		virtual void Wait();
    1.87 +		virtual void Signal();
    1.88 +		virtual void DestroyThis();
    1.89 +	protected:
    1.90 +		pthread_cond_t m_cond;
    1.91 +		pthread_mutex_t m_mutex;
    1.92 +	};
    1.93 +public:
    1.94 +	IMutex *MakeMutex();
    1.95 +	void MakeThread(IThread *pThread);
    1.96 +	IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
    1.97 +	IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
    1.98 +	void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
    1.99 +	void ThreadSleep(unsigned int ms);
   1.100 +	IEventSignal *MakeEventSignal();
   1.101 +	IThreadWorker *MakeWorker(bool threaded);
   1.102 +	void DestroyWorker(IThreadWorker *pWorker);
   1.103 +};
   1.104 +
   1.105 +#if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
   1.106 +#define SM_MAIN_THREADER PosixThreader;
   1.107 +typedef class PosixThreader MainThreader;
   1.108 +#endif
   1.109 +
   1.110 +#endif //_INCLUDE_POSIXTHREADS_H_