ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #define _WIN32_WINNT 0x0400 ds@1952: #include "WinThreads.h" ds@1952: #include "ThreadWorker.h" ds@1952: ds@1952: IThreadWorker *WinThreader::MakeWorker(IThreadWorkerCallbacks *hooks, bool threaded) ds@1952: { ds@1952: if (threaded) ds@1952: { ds@1952: return new ThreadWorker(hooks, this, DEFAULT_THINK_TIME_MS); ds@1952: } else { ds@1952: return new BaseWorker(hooks); ds@1952: } ds@1952: } ds@1952: ds@1952: void WinThreader::DestroyWorker(IThreadWorker *pWorker) ds@1952: { ds@1952: delete pWorker; ds@1952: } ds@1952: ds@1952: void WinThreader::ThreadSleep(unsigned int ms) ds@1952: { ds@1952: Sleep((DWORD)ms); ds@1952: } ds@1952: ds@1952: IMutex *WinThreader::MakeMutex() ds@1952: { ds@1952: WinMutex *pMutex = new WinMutex(); ds@1952: ds@1952: return pMutex; ds@1952: } ds@1952: ds@1952: IThreadHandle *WinThreader::MakeThread(IThread *pThread, ThreadFlags flags) ds@1952: { ds@1952: ThreadParams defparams; ds@1952: ds@1952: defparams.flags = flags; ds@1952: defparams.prio = ThreadPrio_Normal; ds@1952: ds@1952: return MakeThread(pThread, &defparams); ds@1952: } ds@1952: ds@1952: void WinThreader::MakeThread(IThread *pThread) ds@1952: { ds@1952: ThreadParams defparams; ds@1952: ds@1952: defparams.flags = Thread_AutoRelease; ds@1952: defparams.prio = ThreadPrio_Normal; ds@1952: ds@1952: MakeThread(pThread, &defparams); ds@1952: } ds@1952: ds@1952: DWORD WINAPI Win32_ThreadGate(LPVOID param) ds@1952: { ds@1952: WinThreader::ThreadHandle *pHandle = ds@1952: reinterpret_cast(param); ds@1952: ds@1952: pHandle->m_run->RunThread(pHandle); ds@1952: ds@1952: ThreadParams params; ds@1952: EnterCriticalSection(&pHandle->m_crit); ds@1952: pHandle->m_state = Thread_Done; ds@1952: pHandle->GetParams(¶ms); ds@1952: LeaveCriticalSection(&pHandle->m_crit); ds@1952: ds@1952: pHandle->m_run->OnTerminate(pHandle, false); ds@1952: if (params.flags & Thread_AutoRelease) ds@1952: delete pHandle; ds@1952: ds@1952: return 0; ds@1952: } ds@1952: ds@1952: void WinThreader::GetPriorityBounds(ThreadPriority &max, ThreadPriority &min) ds@1952: { ds@1952: max = ThreadPrio_Maximum; ds@1952: min = ThreadPrio_Minimum; ds@1952: } ds@1952: ds@1952: ThreadParams g_defparams; ds@1952: IThreadHandle *WinThreader::MakeThread(IThread *pThread, const ThreadParams *params) ds@1952: { ds@1952: if (params == NULL) ds@1952: params = &g_defparams; ds@1952: ds@1952: WinThreader::ThreadHandle *pHandle = ds@1952: new WinThreader::ThreadHandle(this, NULL, pThread, params); ds@1952: ds@1952: DWORD tid; ds@1952: pHandle->m_thread = ds@1952: CreateThread(NULL, 0, &Win32_ThreadGate, (LPVOID)pHandle, CREATE_SUSPENDED, &tid); ds@1952: ds@1952: if (!pHandle->m_thread) ds@1952: { ds@1952: delete pHandle; ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: if (pHandle->m_params.prio != ThreadPrio_Normal) ds@1952: { ds@1952: pHandle->SetPriority(pHandle->m_params.prio); ds@1952: } ds@1952: ds@1952: if (!(pHandle->m_params.flags & Thread_CreateSuspended)) ds@1952: { ds@1952: pHandle->Unpause(); ds@1952: } ds@1952: ds@1952: return pHandle; ds@1952: } ds@1952: ds@1952: IEventSignal *WinThreader::MakeEventSignal() ds@1952: { ds@1952: HANDLE event = CreateEventA(NULL, FALSE, FALSE, NULL); ds@1952: ds@1952: if (!event) ds@1952: return NULL; ds@1952: ds@1952: WinEvent *pEvent = new WinEvent(event); ds@1952: ds@1952: return pEvent; ds@1952: } ds@1952: ds@1952: /***************** ds@1952: **** Mutexes **** ds@1952: *****************/ ds@1952: ds@1952: WinThreader::WinMutex::WinMutex() ds@1952: { ds@1952: InitializeCriticalSection(&m_crit); ds@1952: } ds@1952: ds@1952: WinThreader::WinMutex::~WinMutex() ds@1952: { ds@1952: DeleteCriticalSection(&m_crit); ds@1952: } ds@1952: ds@1952: bool WinThreader::WinMutex::TryLock() ds@1952: { ds@1952: return (TryEnterCriticalSection(&m_crit) != FALSE); ds@1952: } ds@1952: ds@1952: void WinThreader::WinMutex::Lock() ds@1952: { ds@1952: EnterCriticalSection(&m_crit); ds@1952: } ds@1952: ds@1952: void WinThreader::WinMutex::Unlock() ds@1952: { ds@1952: LeaveCriticalSection(&m_crit); ds@1952: } ds@1952: ds@1952: void WinThreader::WinMutex::DestroyThis() ds@1952: { ds@1952: delete this; ds@1952: } ds@1952: ds@1952: /****************** ds@1952: * Thread Handles * ds@1952: ******************/ ds@1952: ds@1952: WinThreader::ThreadHandle::ThreadHandle(IThreader *parent, HANDLE hthread, IThread *run, const ThreadParams *params) : ds@1952: m_parent(parent), m_thread(hthread), m_run(run), m_params(*params), ds@1952: m_state(Thread_Paused) ds@1952: { ds@1952: InitializeCriticalSection(&m_crit); ds@1952: } ds@1952: ds@1952: WinThreader::ThreadHandle::~ThreadHandle() ds@1952: { ds@1952: if (m_thread) ds@1952: { ds@1952: CloseHandle(m_thread); ds@1952: m_thread = NULL; ds@1952: } ds@1952: DeleteCriticalSection(&m_crit); ds@1952: } ds@1952: ds@1952: bool WinThreader::ThreadHandle::WaitForThread() ds@1952: { ds@1952: if (m_thread == NULL) ds@1952: return false; ds@1952: ds@1952: if (WaitForSingleObject(m_thread, INFINITE) != 0) ds@1952: return false; ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: ThreadState WinThreader::ThreadHandle::GetState() ds@1952: { ds@1952: ThreadState state; ds@1952: ds@1952: EnterCriticalSection(&m_crit); ds@1952: state = m_state; ds@1952: LeaveCriticalSection(&m_crit); ds@1952: ds@1952: return state; ds@1952: } ds@1952: ds@1952: IThreadCreator *WinThreader::ThreadHandle::Parent() ds@1952: { ds@1952: return m_parent; ds@1952: } ds@1952: ds@1952: void WinThreader::ThreadHandle::DestroyThis() ds@1952: { ds@1952: if (m_params.flags & Thread_AutoRelease) ds@1952: return; ds@1952: ds@1952: delete this; ds@1952: } ds@1952: ds@1952: void WinThreader::ThreadHandle::GetParams(ThreadParams *ptparams) ds@1952: { ds@1952: if (!ptparams) ds@1952: return; ds@1952: ds@1952: *ptparams = m_params; ds@1952: } ds@1952: ds@1952: ThreadPriority WinThreader::ThreadHandle::GetPriority() ds@1952: { ds@1952: return m_params.prio; ds@1952: } ds@1952: ds@1952: bool WinThreader::ThreadHandle::SetPriority(ThreadPriority prio) ds@1952: { ds@1952: if (!m_thread) ds@1952: return false; ds@1952: ds@1952: BOOL res = FALSE; ds@1952: ds@1952: if (prio >= ThreadPrio_Maximum) ds@1952: res = SetThreadPriority(m_thread, THREAD_PRIORITY_HIGHEST); ds@1952: else if (prio <= ThreadPrio_Minimum) ds@1952: res = SetThreadPriority(m_thread, THREAD_PRIORITY_LOWEST); ds@1952: else if (prio == ThreadPrio_Normal) ds@1952: res = SetThreadPriority(m_thread, THREAD_PRIORITY_NORMAL); ds@1952: else if (prio == ThreadPrio_High) ds@1952: res = SetThreadPriority(m_thread, THREAD_PRIORITY_ABOVE_NORMAL); ds@1952: else if (prio == ThreadPrio_Low) ds@1952: res = SetThreadPriority(m_thread, THREAD_PRIORITY_BELOW_NORMAL); ds@1952: ds@1952: m_params.prio = prio; ds@1952: ds@1952: return (res != FALSE); ds@1952: } ds@1952: ds@1952: bool WinThreader::ThreadHandle::Unpause() ds@1952: { ds@1952: if (!m_thread) ds@1952: return false; ds@1952: ds@1952: if (m_state != Thread_Paused) ds@1952: return false; ds@1952: ds@1952: m_state = Thread_Running; ds@1952: ds@1952: if (ResumeThread(m_thread) == -1) ds@1952: { ds@1952: m_state = Thread_Paused; ds@1952: return false; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: /***************** ds@1952: * EVENT SIGNALS * ds@1952: *****************/ ds@1952: ds@1952: WinThreader::WinEvent::~WinEvent() ds@1952: { ds@1952: CloseHandle(m_event); ds@1952: } ds@1952: ds@1952: void WinThreader::WinEvent::Wait() ds@1952: { ds@1952: WaitForSingleObject(m_event, INFINITE); ds@1952: } ds@1952: ds@1952: void WinThreader::WinEvent::Signal() ds@1952: { ds@1952: SetEvent(m_event); ds@1952: } ds@1952: ds@1952: void WinThreader::WinEvent::DestroyThis() ds@1952: { ds@1952: delete this; ds@1952: }