/
ThreadImpl.cpp
192 lines (160 loc) · 4.77 KB
/
ThreadImpl.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (C) 2005-2011 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include <windows.h>
void CThread::Create(bool bAutoDelete, unsigned stacksize)
{
if (m_ThreadId != 0)
{
if (logger) logger->Log(LOGERROR, "%s - fatal error creating thread- old thread id not null", __FUNCTION__);
exit(1);
}
m_iLastTime = XbmcThreads::SystemClockMillis() * 10000;
m_iLastUsage = 0;
m_fLastUsage = 0.0f;
m_bAutoDelete = bAutoDelete;
m_bStop = false;
m_StopEvent.Reset();
m_TermEvent.Reset();
m_StartEvent.Reset();
m_ThreadOpaque.handle = CreateThread(NULL,stacksize, (LPTHREAD_START_ROUTINE)&staticThread, this, 0, &m_ThreadId);
if (m_ThreadOpaque.handle == NULL)
{
if (logger) logger->Log(LOGERROR, "%s - fatal error creating thread", __FUNCTION__);
}
}
void CThread::TermHandler()
{
CloseHandle(m_ThreadOpaque.handle);
m_ThreadOpaque.handle = NULL;
}
void CThread::SetThreadInfo()
{
const unsigned int MS_VC_EXCEPTION = 0x406d1388;
struct THREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in same addr space)
DWORD dwThreadID; // thread ID (-1 caller thread)
DWORD dwFlags; // reserved for future use, most be zero
} info;
info.dwType = 0x1000;
info.szName = m_ThreadName.c_str();
info.dwThreadID = m_ThreadId;
info.dwFlags = 0;
try
{
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
}
catch(...)
{
}
}
ThreadIdentifier CThread::GetCurrentThreadId()
{
return ::GetCurrentThreadId();
}
bool CThread::IsCurrentThread(const ThreadIdentifier tid)
{
return (::GetCurrentThreadId() == tid);
}
int CThread::GetMinPriority(void)
{
return(THREAD_PRIORITY_IDLE);
}
int CThread::GetMaxPriority(void)
{
return(THREAD_PRIORITY_HIGHEST);
}
int CThread::GetNormalPriority(void)
{
return(THREAD_PRIORITY_NORMAL);
}
int CThread::GetSchedRRPriority(void)
{
return GetNormalPriority();
}
bool CThread::SetPriority(const int iPriority)
{
bool bReturn = false;
CSingleLock lock(m_CriticalSection);
if (m_ThreadOpaque.handle)
{
bReturn = SetThreadPriority(m_ThreadOpaque.handle, iPriority) == TRUE;
}
return bReturn;
}
int CThread::GetPriority()
{
CSingleLock lock(m_CriticalSection);
int iReturn = THREAD_PRIORITY_NORMAL;
if (m_ThreadOpaque.handle)
{
iReturn = GetThreadPriority(m_ThreadOpaque.handle);
}
return iReturn;
}
bool CThread::WaitForThreadExit(unsigned int milliseconds)
{
bool bReturn = true;
CSingleLock lock(m_CriticalSection);
if (m_ThreadId && m_ThreadOpaque.handle != NULL)
{
// boost priority of thread we are waiting on to same as caller
int callee = GetThreadPriority(m_ThreadOpaque.handle);
int caller = GetThreadPriority(GetCurrentThread());
if(caller > callee)
SetThreadPriority(m_ThreadOpaque.handle, caller);
lock.Leave();
bReturn = m_TermEvent.WaitMSec(milliseconds);
lock.Enter();
// restore thread priority if thread hasn't exited
if(caller > callee && m_ThreadOpaque.handle)
SetThreadPriority(m_ThreadOpaque.handle, callee);
}
return bReturn;
}
int64_t CThread::GetAbsoluteUsage()
{
CSingleLock lock(m_CriticalSection);
if (!m_ThreadOpaque.handle)
return 0;
uint64_t time = 0;
FILETIME CreationTime, ExitTime, UserTime, KernelTime;
if( GetThreadTimes(m_ThreadOpaque.handle, &CreationTime, &ExitTime, &KernelTime, &UserTime ) )
{
time = (((uint64_t)UserTime.dwHighDateTime) << 32) + ((uint64_t)UserTime.dwLowDateTime);
time += (((uint64_t)KernelTime.dwHighDateTime) << 32) + ((uint64_t)KernelTime.dwLowDateTime);
}
return time;
}
float CThread::GetRelativeUsage()
{
unsigned int iTime = XbmcThreads::SystemClockMillis();
iTime *= 10000; // convert into 100ns tics
// only update every 1 second
if( iTime < m_iLastTime + 1000*10000 ) return m_fLastUsage;
int64_t iUsage = GetAbsoluteUsage();
if (m_iLastUsage > 0 && m_iLastTime > 0)
m_fLastUsage = (float)( iUsage - m_iLastUsage ) / (float)( iTime - m_iLastTime );
m_iLastUsage = iUsage;
m_iLastTime = iTime;
return m_fLastUsage;
}