core/logic/thread/WinThreads.h
author David Anderson <dvander@alliedmods.net>
Mon Aug 19 20:58:02 2013 -0700 (2013-08-19)
changeset 4025 542dde661d5c
parent 2726 c96f3fab8211
child 4033 48eaea10c202
permissions -rw-r--r--
Rewrite IThreader implementation around new synchronization primitives (bug 5862, r=fyren).
dvander@2726
     1
/**
dvander@2726
     2
 * vim: set ts=4 :
dvander@2726
     3
 * =============================================================================
dvander@2726
     4
 * SourceMod
dvander@2726
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
dvander@2726
     6
 * =============================================================================
dvander@2726
     7
 *
dvander@2726
     8
 * This program is free software; you can redistribute it and/or modify it under
dvander@2726
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
dvander@2726
    10
 * Free Software Foundation.
dvander@2726
    11
 * 
dvander@2726
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
dvander@2726
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
dvander@2726
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
dvander@2726
    15
 * details.
dvander@2726
    16
 *
dvander@2726
    17
 * You should have received a copy of the GNU General Public License along with
dvander@2726
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@2726
    19
 *
dvander@2726
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
dvander@2726
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
dvander@2726
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
dvander@2726
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
dvander@2726
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
dvander@2726
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
dvander@2726
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
dvander@2726
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@2726
    28
 *
dvander@2726
    29
 * Version: $Id$
dvander@2726
    30
 */
dvander@2726
    31
dvander@2726
    32
#ifndef _INCLUDE_WINTHREADS_H_
dvander@2726
    33
#define _INCLUDE_WINTHREADS_H_
dvander@2726
    34
dvander@4025
    35
#include <ke_thread_utils.h>
dvander@4025
    36
#include <ke_utility.h>
dvander@2726
    37
#include <windows.h>
dvander@2726
    38
#include "IThreader.h"
dvander@2726
    39
dvander@2726
    40
using namespace SourceMod;
dvander@2726
    41
dvander@2726
    42
class WinThreader : public IThreader
dvander@2726
    43
{
dvander@2726
    44
public:
dvander@4025
    45
	class ThreadHandle : public IThreadHandle, public ke::IRunnable
dvander@2726
    46
	{
dvander@2726
    47
		friend class WinThreader;
dvander@2726
    48
		friend DWORD WINAPI Win32_ThreadGate(LPVOID param);
dvander@2726
    49
	public:
dvander@4025
    50
		ThreadHandle(IThreader *parent, IThread *run, const ThreadParams *params);
dvander@2726
    51
		virtual ~ThreadHandle();
dvander@2726
    52
	public:
dvander@2726
    53
		virtual bool WaitForThread();
dvander@2726
    54
		virtual void DestroyThis();
dvander@2726
    55
		virtual IThreadCreator *Parent();
dvander@2726
    56
		virtual void GetParams(ThreadParams *ptparams);
dvander@2726
    57
		virtual ThreadPriority GetPriority();
dvander@2726
    58
		virtual bool SetPriority(ThreadPriority prio);
dvander@2726
    59
		virtual ThreadState GetState();
dvander@2726
    60
		virtual bool Unpause();
dvander@4025
    61
		virtual void Run();
dvander@2726
    62
	protected:
dvander@2726
    63
		IThreader *m_parent;		//Parent handle
dvander@4025
    64
		ke::AutoPtr<ke::Thread> m_thread;
dvander@2726
    65
		ThreadParams m_params;		//Current Parameters
dvander@4025
    66
		IThread *m_run;				//Runnable context
dvander@2726
    67
		ThreadState m_state;		//internal state
dvander@4025
    68
		ke::ConditionVariable suspend_;
dvander@2726
    69
	};
dvander@4025
    70
dvander@2726
    71
public:
dvander@2726
    72
	IMutex *MakeMutex();
dvander@2726
    73
	void MakeThread(IThread *pThread);
dvander@2726
    74
	IThreadHandle *MakeThread(IThread *pThread, ThreadFlags flags);
dvander@2726
    75
	IThreadHandle *MakeThread(IThread *pThread, const ThreadParams *params);
dvander@2726
    76
	void GetPriorityBounds(ThreadPriority &max, ThreadPriority &min);
dvander@2726
    77
	void ThreadSleep(unsigned int ms);
dvander@2726
    78
	IEventSignal *MakeEventSignal();
dvander@2726
    79
	IThreadWorker *MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded);
dvander@2726
    80
	void DestroyWorker(IThreadWorker *pWorker);
dvander@2726
    81
};
dvander@2726
    82
dvander@2726
    83
#if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
dvander@2726
    84
#define SM_MAIN_THREADER WinThreader;
dvander@2726
    85
typedef class WinThreader MainThreader;
dvander@2726
    86
#endif
dvander@2726
    87
dvander@2726
    88
#endif //_INCLUDE_WINTHREADS_H_