core/thread/PosixThreads.h
author David Anderson <dvander@alliedmods.net>
Wed Aug 01 02:12:47 2007 +0000 (2007-08-01)
changeset 1231 17badaf66ce4
parent 1090 5c54e353c019
child 1330 ab94316bb52b
permissions -rw-r--r--
updated more header files (mostly internal now)
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #ifndef _INCLUDE_POSIXTHREADS_H_
    34 #define _INCLUDE_POSIXTHREADS_H_
    35 
    36 #include <pthread.h>
    37 #include "IThreader.h"
    38 
    39 using namespace SourceMod;
    40 
    41 void *Posix_ThreadGate(void *param);
    42 
    43 class PosixThreader : public IThreader
    44 {
    45 public:
    46 	class ThreadHandle : public IThreadHandle
    47 	{
    48 		friend class PosixThreader;
    49 		friend void *Posix_ThreadGate(void *param);
    50 	public:
    51 		ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
    52 		virtual ~ThreadHandle();
    53 	public:
    54 		virtual bool WaitForThread();
    55 		virtual void DestroyThis();
    56 		virtual IThreadCreator *Parent();
    57 		virtual void GetParams(ThreadParams *ptparams);
    58 		virtual ThreadPriority GetPriority();
    59 		virtual bool SetPriority(ThreadPriority prio);
    60 		virtual ThreadState GetState();
    61 		virtual bool Unpause();
    62 	protected:
    63 		IThreader *m_parent;		//Parent handle
    64 		pthread_t m_thread;			//Windows HANDLE	
    65 		ThreadParams m_params;		//Current Parameters
    66 		IThread *m_run;	//Runnable context
    67 		pthread_mutex_t m_statelock;
    68 		pthread_mutex_t m_runlock;
    69 		ThreadState m_state;		//internal state
    70 	};
    71 	class PosixMutex : public IMutex
    72 	{
    73 	public:
    74 		PosixMutex(pthread_mutex_t m) : m_mutex(m)
    75 		{
    76 		};
    77 		virtual ~PosixMutex();
    78 	public:
    79 		virtual bool TryLock();
    80 		virtual void Lock();
    81 		virtual void Unlock();
    82 		virtual void DestroyThis();
    83 	protected:
    84 		pthread_mutex_t m_mutex;
    85 	};
    86 	class PosixEventSignal : public IEventSignal
    87 	{
    88 	public:
    89 		PosixEventSignal();
    90 		virtual ~PosixEventSignal();
    91 	public:
    92 		virtual void Wait();
    93 		virtual void Signal();
    94 		virtual void DestroyThis();
    95 	protected:
    96 		pthread_cond_t m_cond;
    97 		pthread_mutex_t m_mutex;
    98 	};
    99 public:
   100 	IMutex *MakeMutex();
   101 	void MakeThread(IThread *pThread);
   102 	IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
   103 	IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
   104 	void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
   105 	void ThreadSleep(unsigned int ms);
   106 	IEventSignal *MakeEventSignal();
   107 	IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
   108 	void DestroyWorker(IThreadWorker *pWorker);
   109 };
   110 
   111 #if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
   112 #define SM_MAIN_THREADER PosixThreader;
   113 typedef class PosixThreader MainThreader;
   114 #endif
   115 
   116 #endif //_INCLUDE_POSIXTHREADS_H_