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
|
#include "UnityPrefix.h"
#if SUPPORT_THREADS
#ifndef THREAD_API_WINAPI
#define THREAD_API_WINAPI (UNITY_WIN || UNITY_XENON || UNITY_WINRT)
#endif
#endif // SUPPORT_THREADS
#if THREAD_API_WINAPI
#include "PlatformThread.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Threads/ThreadHelper.h"
#include "Runtime/Utilities/Word.h"
// module@TODO : Move this to PlatformThread.h
#if UNITY_WINRT
#include "PlatformDependent/MetroPlayer/Win32Threads.h"
#endif
PlatformThread::PlatformThread()
: m_Thread(NULL)
#if !UNITY_WINRT
, m_ThreadId(0)
#endif
{
}
PlatformThread::~PlatformThread()
{
AssertMsg(m_Thread == NULL, "***Thread was not cleaned up!***");
}
void PlatformThread::Create(const Thread* thread, const UInt32 stackSize, const int processor)
{
#if UNITY_WINRT
m_Thread = win32::CreateThread(Thread::RunThreadWrapper, (LPVOID) thread);
#else // UNITY_WINRT
DWORD creationFlags = 0;
#if UNITY_XENON
if (processor != DEFAULT_UNITY_THREAD_PROCESSOR)
creationFlags = CREATE_SUSPENDED;
#endif
m_Thread = ::CreateThread(NULL, stackSize, Thread::RunThreadWrapper, (LPVOID) thread, creationFlags, &m_ThreadId);
Assert(NULL != m_Thread);
#if UNITY_XENON
if (processor != DEFAULT_UNITY_THREAD_PROCESSOR)
{
ThreadHelper::SetThreadProcessor(thread, processor);
ResumeThread(m_Thread);
}
#endif
#endif // UNITY_WINRT
}
void PlatformThread::Enter(const Thread* thread)
{
if (thread->m_Priority != kNormalPriority)
UpdatePriority(thread);
}
void PlatformThread::Exit(const Thread* thread, void* result)
{
}
void PlatformThread::Join(const Thread* thread)
{
#if !UNITY_WINRT // Why doesn't WINRT store the thread ID ?
if (Thread::EqualsCurrentThreadID(m_ThreadId))
{
ErrorStringMsg("***Thread '%s' tried to join itself!***", thread->m_Name);
}
#endif
if (thread->m_Running)
{
DWORD waitResult = WaitForSingleObjectEx(m_Thread, INFINITE, FALSE);
Assert(WAIT_OBJECT_0 == waitResult);
}
if (m_Thread != NULL)
{
BOOL closeResult = CloseHandle(m_Thread);
Assert(FALSE != closeResult);
}
m_Thread = NULL;
}
void PlatformThread::UpdatePriority(const Thread* thread) const
{
ThreadPriority p = thread->m_Priority;
#if UNITY_WINRT
#pragma message("todo: implement") // ?!-
#else
int iPriority;
switch (p)
{
case kLowPriority:
iPriority = THREAD_PRIORITY_LOWEST;
break;
case kBelowNormalPriority:
iPriority = THREAD_PRIORITY_BELOW_NORMAL;
break;
case kNormalPriority:
iPriority = THREAD_PRIORITY_NORMAL;
break;
case kHighPriority:
iPriority = THREAD_PRIORITY_HIGHEST;
break;
default:
AssertString("Undefined thread priority");
}
int res = SetThreadPriority(m_Thread, iPriority);
AssertIf(res == 0);
#endif
}
PlatformThread::ThreadID PlatformThread::GetCurrentThreadID()
{
return GetCurrentThreadId();
}
#endif // THREAD_API_PTHREAD
|