Removed pointless "systems" folder from core.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #define _WIN32_WINNT 0x0400
33 #include "WinThreads.h"
34 #include "ThreadWorker.h"
36 IThreadWorker *WinThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded)
40 return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS);
42 return new BaseWorker(hooks);
46 void WinThreader::DestroyWorker(IThreadWorker *pWorker)
51 void WinThreader::ThreadSleep(unsigned int ms)
56 IMutex *WinThreader::MakeMutex()
58 WinMutex *pMutex = new WinMutex();
63 IThreadHandle *WinThreader::MakeThread(IThread *pThread, ThreadFlags flags)
65 ThreadParams defparams;
67 defparams.flags = flags;
68 defparams.prio = ThreadPrio_Normal;
70 return MakeThread(pThread, &defparams);
73 void WinThreader::MakeThread(IThread *pThread)
75 ThreadParams defparams;
77 defparams.flags = Thread_AutoRelease;
78 defparams.prio = ThreadPrio_Normal;
80 MakeThread(pThread, &defparams);
83 DWORD WINAPI Win32_ThreadGate(LPVOID param)
85 WinThreader::ThreadHandle *pHandle =
86 reinterpret_cast<WinThreader::ThreadHandle *>(param);
88 pHandle->m_run->RunThread(pHandle);
91 EnterCriticalSection(&pHandle->m_crit);
92 pHandle->m_state = Thread_Done;
93 pHandle->GetParams(¶ms);
94 LeaveCriticalSection(&pHandle->m_crit);
96 pHandle->m_run->OnTerminate(pHandle, false);
97 if (params.flags & Thread_AutoRelease)
103 void WinThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min)
105 max = ThreadPrio_Maximum;
106 min = ThreadPrio_Minimum;
109 ThreadParams g_defparams;
110 IThreadHandle *WinThreader::MakeThread(IThread *pThread, const ThreadParams *params)
113 params = &g_defparams;
115 WinThreader::ThreadHandle *pHandle =
116 new WinThreader::ThreadHandle(this, NULL, pThread, params);
120 CreateThread(NULL, 0, &Win32_ThreadGate, (LPVOID)pHandle, CREATE_SUSPENDED, &tid);
122 if (!pHandle->m_thread)
128 if (pHandle->m_params.prio != ThreadPrio_Normal)
130 pHandle->SetPriority(pHandle->m_params.prio);
133 if (!(pHandle->m_params.flags & Thread_CreateSuspended))
141 IEventSignal *WinThreader::MakeEventSignal()
143 HANDLE event = CreateEventA(NULL, FALSE, FALSE, NULL);
148 WinEvent *pEvent = new WinEvent(event);
157 WinThreader::WinMutex::WinMutex()
159 InitializeCriticalSection(&m_crit);
162 WinThreader::WinMutex::~WinMutex()
164 DeleteCriticalSection(&m_crit);
167 bool WinThreader::WinMutex::TryLock()
169 return (TryEnterCriticalSection(&m_crit) != FALSE);
172 void WinThreader::WinMutex::Lock()
174 EnterCriticalSection(&m_crit);
177 void WinThreader::WinMutex::Unlock()
179 LeaveCriticalSection(&m_crit);
182 void WinThreader::WinMutex::DestroyThis()
191 WinThreader::ThreadHandle::ThreadHandle(IThreader *parent, HANDLE hthread, IThread *run, const ThreadParams *params) :
192 m_parent(parent), m_thread(hthread), m_run(run), m_params(*params),
193 m_state(Thread_Paused)
195 InitializeCriticalSection(&m_crit);
198 WinThreader::ThreadHandle::~ThreadHandle()
202 CloseHandle(m_thread);
205 DeleteCriticalSection(&m_crit);
208 bool WinThreader::ThreadHandle::WaitForThread()
210 if (m_thread == NULL)
213 if (WaitForSingleObject(m_thread, INFINITE) != 0)
219 ThreadState WinThreader::ThreadHandle::GetState()
223 EnterCriticalSection(&m_crit);
225 LeaveCriticalSection(&m_crit);
230 IThreadCreator *WinThreader::ThreadHandle::Parent()
235 void WinThreader::ThreadHandle::DestroyThis()
237 if (m_params.flags & Thread_AutoRelease)
243 void WinThreader::ThreadHandle::GetParams(ThreadParams *ptparams)
248 *ptparams = m_params;
251 ThreadPriority WinThreader::ThreadHandle::GetPriority()
253 return m_params.prio;
256 bool WinThreader::ThreadHandle::SetPriority(ThreadPriority prio)
263 if (prio >= ThreadPrio_Maximum)
264 res = SetThreadPriority(m_thread, THREAD_PRIORITY_HIGHEST);
265 else if (prio <= ThreadPrio_Minimum)
266 res = SetThreadPriority(m_thread, THREAD_PRIORITY_LOWEST);
267 else if (prio == ThreadPrio_Normal)
268 res = SetThreadPriority(m_thread, THREAD_PRIORITY_NORMAL);
269 else if (prio == ThreadPrio_High)
270 res = SetThreadPriority(m_thread, THREAD_PRIORITY_ABOVE_NORMAL);
271 else if (prio == ThreadPrio_Low)
272 res = SetThreadPriority(m_thread, THREAD_PRIORITY_BELOW_NORMAL);
274 m_params.prio = prio;
276 return (res != FALSE);
279 bool WinThreader::ThreadHandle::Unpause()
284 if (m_state != Thread_Paused)
287 m_state = Thread_Running;
289 if (ResumeThread(m_thread) == -1)
291 m_state = Thread_Paused;
302 WinThreader::WinEvent::~WinEvent()
304 CloseHandle(m_event);
307 void WinThreader::WinEvent::Wait()
309 WaitForSingleObject(m_event, INFINITE);
312 void WinThreader::WinEvent::Signal()
317 void WinThreader::WinEvent::DestroyThis()