OpenBarnyard
 
Loading...
Searching...
No Matches
TThread_Win.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TThread_Win.h"
3
4#include <process.h>
5
6//-----------------------------------------------------------------------------
7// Enables memory debugging.
8// Note: Should be the last include!
9//-----------------------------------------------------------------------------
10#include "Core/TMemoryDebugOn.h"
11
13
14static TThreadManager oThreadManager;
15
16// $Barnyard: FUNCTION 006bb980
17unsigned long __stdcall ThreadEntry( void* userParam )
18{
19 TThread* pThread = static_cast<TThread*>( userParam );
20 pThread->Main();
21 TThread::Exit( pThread );
22 return 0;
23}
24
25// $Barnyard: FUNCTION 006bb9a0
26TBOOL TThread::Create( TSIZE a_iStackSize, PRIORITY a_ePriority, TUINT8 a_eFlags )
27{
28 m_iThreadID = -1;
29 m_hThreadHnd = CreateThread( NULL, a_iStackSize, ThreadEntry, this, CREATE_SUSPENDED, &m_iThreadID );
30
31 TASSERT( m_hThreadHnd != NULL, "Couldn't create thread" );
32 TBOOL bResult = SetThreadPriority( m_hThreadHnd, a_ePriority );
33 TASSERT( bResult != TFALSE, "Couldn't set thread priority" );
34
36
37 if ( ( a_eFlags & 1 ) == 0 )
38 {
39 DWORD iResult = ResumeThread( m_hThreadHnd );
40 TASSERT( iResult != -1, "Couldn't resume thread" );
41 }
42
43 return TTRUE;
44}
45
47{
48 TASSERT( m_iThreadID != GetCurrentThreadId() );
49
50 BOOL bResult = TerminateThread( m_hThreadHnd, 0 );
51 TASSERT( bResult != FALSE );
52
53 bResult = CloseHandle( m_hThreadHnd );
54 TASSERT( bResult != FALSE );
55
57 m_hThreadHnd = NULL;
58 m_iThreadID = -1;
59
60 return TTRUE;
61}
62
63TBOOL TThread::GetPriority( void* a_hThreadHnd, PRIORITY& a_ePriority )
64{
65 TASSERT( a_hThreadHnd != NULL, "Thread doesn't exist" );
66 TINT iPriority = GetThreadPriority( a_hThreadHnd );
67 TASSERT( iPriority != THREAD_PRIORITY_ERROR_RETURN, "Couldn't get thread priority" );
68 a_ePriority = iPriority;
69 return TTRUE;
70}
71
72TBOOL TThread::SetPriority( void* a_hThreadHnd, PRIORITY a_ePriority )
73{
74 TASSERT( a_hThreadHnd != NULL, "Thread doesn't exist" );
75 BOOL bResult = SetThreadPriority( a_hThreadHnd, a_ePriority );
76 TASSERT( bResult != FALSE, "Couldn't set priority" );
77 return TTRUE;
78}
79
80// $Barnyard: FUNCTION 006bb920
81void TThread::Exit( TThread* a_pThread )
82{
83 TASSERT( a_pThread->m_iThreadID == GetCurrentThreadId(), "Thread cannot be closed outside" );
84
85 BOOL bResult = CloseHandle( a_pThread->m_hThreadHnd );
86 TASSERT( bResult != FALSE, "Couldn't close thread" );
87
89 a_pThread->m_hThreadHnd = NULL;
90 a_pThread->m_iThreadID = -1;
91
92 _endthreadex( 0 );
93}
94
96{
97 EnterCriticalSection( &m_CriticalSection );
98 a_pThread->Remove();
99 LeaveCriticalSection( &m_CriticalSection );
100}
101
103{
104 EnterCriticalSection( &m_CriticalSection );
105 m_Threads.PushFront( a_pThread );
106 LeaveCriticalSection( &m_CriticalSection );
107}
108
110{
111 InitializeCriticalSection( &m_CriticalSection );
112}
113
115{
116 DeleteCriticalSection( &m_CriticalSection );
117}
118
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
uint8_t TUINT8
Definition Typedefs.h:17
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
unsigned long __stdcall ThreadEntry(void *userParam)
unsigned long m_iThreadID
Definition TThread_Win.h:50
virtual void Main()=0
TBOOL Create(size_t a_iStackSize, PRIORITY a_ePriority, TUINT8 a_eFlags)
static void Exit(TThread *a_pThread)
static TBOOL SetPriority(void *a_hThreadHnd, PRIORITY a_ePriority)
TINT PRIORITY
Definition TThread_Win.h:22
TBOOL Destroy()
static TBOOL GetPriority(void *a_hThreadHnd, PRIORITY &a_ePriority)
void * m_hThreadHnd
Definition TThread_Win.h:49
friend class TThread
Definition TThread_Win.h:64
void RemoveThread(TThread *a_pThread)
void InsertThread(TThread *a_pThread)
static TFORCEINLINE TThreadManager * GetSingletonSafe()
Definition TSingleton.h:37