core/thread/WinThreads.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_WINTHREADS_H_
    34 #define _INCLUDE_WINTHREADS_H_
    35 
    36 #include <windows.h>
    37 #include "IThreader.h"
    38 
    39 using namespace SourceMod;
    40 
    41 DWORD WINAPI Win32_ThreadGate(LPVOID param);
    42 
    43 class WinThreader : public IThreader
    44 {
    45 public:
    46 	class ThreadHandle : public IThreadHandle
    47 	{
    48 		friend class WinThreader;
    49 		friend DWORD WINAPI Win32_ThreadGate(LPVOID param);
    50 	public:
    51 		ThreadHandle(IThreader *parent, HANDLE hthread, 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 		HANDLE m_thread;			//Windows HANDLE	
    65 		ThreadParams m_params;		//Current Parameters
    66 		IThread *m_run;	//Runnable context
    67 		ThreadState m_state;		//internal state
    68 		CRITICAL_SECTION m_crit;
    69 	};
    70 	class WinMutex : public IMutex
    71 	{
    72 	public:
    73 		WinMutex();
    74 		virtual ~WinMutex();
    75 	public:
    76 		virtual bool TryLock();
    77 		virtual void Lock();
    78 		virtual void Unlock();
    79 		virtual void DestroyThis();
    80 	protected:
    81 		CRITICAL_SECTION m_crit;
    82 	};
    83 	class WinEvent : public IEventSignal
    84 	{
    85 	public:
    86 		WinEvent(HANDLE event) : m_event(event)
    87 		{
    88 		};
    89 		virtual ~WinEvent();
    90 	public:
    91 		virtual void Wait();
    92 		virtual void Signal();
    93 		virtual void DestroyThis();
    94 	public:
    95 		HANDLE m_event;
    96 	};
    97 public:
    98 	IMutex *MakeMutex();
    99 	void MakeThread(IThread *pThread);
   100 	IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
   101 	IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
   102 	void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
   103 	void ThreadSleep(unsigned int ms);
   104 	IEventSignal *MakeEventSignal();
   105 	IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
   106 	void DestroyWorker(IThreadWorker *pWorker);
   107 };
   108 
   109 #if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
   110 #define SM_MAIN_THREADER WinThreader;
   111 typedef class WinThreader MainThreader;
   112 #endif
   113 
   114 #endif //_INCLUDE_WINTHREADS_H_