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