3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
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.
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.
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/>.
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>.
33 #ifndef _INCLUDE_WINTHREADS_H_
34 #define _INCLUDE_WINTHREADS_H_
37 #include "IThreader.h"
39 using namespace SourceMod;
41 DWORD WINAPI Win32_ThreadGate(LPVOID param);
43 class WinThreader : public IThreader
46 class ThreadHandle : public IThreadHandle
48 friend class WinThreader;
49 friend DWORD WINAPI Win32_ThreadGate(LPVOID param);
51 ThreadHandle(IThreader *parent, HANDLE hthread, IThread *run, const ThreadParams *params);
52 virtual ~ThreadHandle();
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();
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;
70 class WinMutex : public IMutex
76 virtual bool TryLock();
78 virtual void Unlock();
79 virtual void DestroyThis();
81 CRITICAL_SECTION m_crit;
83 class WinEvent : public IEventSignal
86 WinEvent(HANDLE event) : m_event(event)
92 virtual void Signal();
93 virtual void DestroyThis();
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);
109 #if defined SM_DEFAULT_THREADER && !defined SM_MAIN_THREADER
110 #define SM_MAIN_THREADER WinThreader;
111 typedef class WinThreader MainThreader;
114 #endif //_INCLUDE_WINTHREADS_H_