core/thread/WinThreads.h
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 1231 17badaf66ce4
child 1947 e73a1e51833b
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
     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 modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_WINTHREADS_H_
    33 #define _INCLUDE_WINTHREADS_H_
    34 
    35 #include <windows.h>
    36 #include "IThreader.h"
    37 
    38 using namespace SourceMod;
    39 
    40 DWORD WINAPI Win32_ThreadGate(LPVOID param);
    41 
    42 class WinThreader : public IThreader
    43 {
    44 public:
    45 	class ThreadHandle : public IThreadHandle
    46 	{
    47 		friend class WinThreader;
    48 		friend DWORD WINAPI Win32_ThreadGate(LPVOID param);
    49 	public:
    50 		ThreadHandle(IThreader *parent, HANDLE hthread, IThread *run, const ThreadParams *params);
    51 		virtual ~ThreadHandle();
    52 	public:
    53 		virtual bool WaitForThread();
    54 		virtual void DestroyThis();
    55 		virtual IThreadCreator *Parent();
    56 		virtual void GetParams(ThreadParams *ptparams);
    57 		virtual ThreadPriority GetPriority();
    58 		virtual bool SetPriority(ThreadPriority prio);
    59 		virtual ThreadState GetState();
    60 		virtual bool Unpause();
    61 	protected:
    62 		IThreader *m_parent;		//Parent handle
    63 		HANDLE m_thread;			//Windows HANDLE	
    64 		ThreadParams m_params;		//Current Parameters
    65 		IThread *m_run;	//Runnable context
    66 		ThreadState m_state;		//internal state
    67 		CRITICAL_SECTION m_crit;
    68 	};
    69 	class WinMutex : public IMutex
    70 	{
    71 	public:
    72 		WinMutex();
    73 		virtual ~WinMutex();
    74 	public:
    75 		virtual bool TryLock();
    76 		virtual void Lock();
    77 		virtual void Unlock();
    78 		virtual void DestroyThis();
    79 	protected:
    80 		CRITICAL_SECTION m_crit;
    81 	};
    82 	class WinEvent : public IEventSignal
    83 	{
    84 	public:
    85 		WinEvent(HANDLE event) : m_event(event)
    86 		{
    87 		};
    88 		virtual ~WinEvent();
    89 	public:
    90 		virtual void Wait();
    91 		virtual void Signal();
    92 		virtual void DestroyThis();
    93 	public:
    94 		HANDLE m_event;
    95 	};
    96 public:
    97 	IMutex *MakeMutex();
    98 	void MakeThread(IThread *pThread);
    99 	IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
   100 	IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
   101 	void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
   102 	void ThreadSleep(unsigned int ms);
   103 	IEventSignal *MakeEventSignal();
   104 	IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
   105 	void DestroyWorker(IThreadWorker *pWorker);
   106 };
   107 
   108 #if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
   109 #define SM_MAIN_THREADER WinThreader;
   110 typedef class WinThreader MainThreader;
   111 #endif
   112 
   113 #endif //_INCLUDE_WINTHREADS_H_