-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCThread.cpp
More file actions
134 lines (110 loc) · 2.43 KB
/
Copy pathCThread.cpp
File metadata and controls
134 lines (110 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*********************************************************
*
* Multi Theft Auto: San Andreas - Deathmatch
*
* ml_base, External lua add-on module
*
* Copyright © 2003-2008 MTA. All Rights Reserved.
*
* Grand Theft Auto is © 2002-2003 Rockstar North
*
* THE FOLLOWING SOURCES ARE PART OF THE MULTI THEFT
* AUTO SOFTWARE DEVELOPMENT KIT AND ARE RELEASED AS
* OPEN SOURCE FILES. THESE FILES MAY BE USED AS LONG
* AS THE DEVELOPER AGREES TO THE LICENSE THAT IS
* PROVIDED WITH THIS PACKAGE.
*
*********************************************************/
#include "CThread.h"
CThread::CThread ( void )
{
m_pThreadData = NULL;
m_pArg = NULL;
#ifdef WIN32 // Win32 threads
m_hThread = NULL;
#endif
}
CThread::~CThread ( void )
{
Stop ();
}
bool CThread::Start ( CThreadData *pData )
{
if ( pData == NULL ) // pData HAS to be valid
return false;
Stop ();
Arg ( pData );
#ifdef WIN32 // Win32 threads
m_hThread = CreateThread ( NULL, 0, &CThread::EntryPoint, this, 0, NULL );
return m_hThread != NULL;
#else // POSIX threads
if ( pthread_create ( &m_hThread, NULL, CThread::EntryPoint, this ) )
{
// something bad happened
return false;
}
#endif
return true;
}
void CThread::Stop ( void )
{
if ( m_hThread )
{
// TerminateThread ( m_hThread, 0 );
if ( m_pThreadData != NULL )
m_pThreadData->bAbortThread = true;
}
}
bool CThread::TryLock ( ThreadMutex * Mutex )
{
#ifdef WIN32
if ( TryEnterCriticalSection ( Mutex ) != 0 )
return true;
#else
if ( pthread_mutex_trylock ( Mutex ) == 0 )
return true;
#endif
return false;
}
void CThread::Lock ( ThreadMutex * Mutex )
{
#ifdef WIN32 // Win32 threads
EnterCriticalSection ( Mutex );
#else // POSIX threads
pthread_mutex_lock ( Mutex );
#endif
}
void CThread::Unlock ( ThreadMutex * Mutex )
{
#ifdef WIN32 // Win32 threads
LeaveCriticalSection ( Mutex );
#else // POSIX threads
pthread_mutex_unlock ( Mutex );
#endif
}
int CThread::Run ( CThreadData* arg )
{
return Execute ( arg );
}
#ifdef WIN32 // Win32 threads
DWORD CThread::EntryPoint ( void* pThis )
{
CThread* pt = (CThread*)pThis;
return pt->Run ( pt->Arg () );
}
#else // POSIX threads
void* CThread::EntryPoint ( void* pThis )
{
CThread* pt = (CThread*)pThis;
pt->Run ( pt->Arg () );
return NULL;
}
#endif
CThreadData* CThread::Arg ( void ) const
{
return m_pThreadData;
}
void CThread::Arg ( CThreadData* pData )
{
m_pThreadData = pData;
}