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
193
194
195
196
197
|
/*
* Copyright (c) 2005, Eric Crahen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "ThreadOps.h"
#include "zthread/Runnable.h"
#include <process.h>
namespace ZThread {
const ThreadOps ThreadOps::INVALID(0);
/**
* Detect OS at runtime and attempt to locate the SwitchToThread
* function, which will assist in making the spin lock implementation
* which use ThreadOps::yield() a bit fairer.
*/
class YieldOps {
typedef BOOL (*Yield)(void);
Yield _fnYield;
public:
YieldOps() : _fnYield(NULL) {
OSVERSIONINFO v;
v.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(::GetVersionEx(&v) && (v.dwPlatformId == VER_PLATFORM_WIN32_NT)) {
// Uses GetModuleHandle() so the reference count on the dll is
// not affected. There is a warning about race conditions involving
// this function being called as FreeLibrary() completes; however
// nearly all win32 applications load this particular and will keep it
// in memory until the process exits.
HINSTANCE hInst = ::GetModuleHandle("Kernel32.dll");
if(hInst != NULL)
_fnYield = (Yield)::GetProcAddress(hInst, "SwitchToThread");
// REMIND: possibly need to use _T() macro for these strings
}
}
bool operator()() {
// Attempt to yield using the best function available
if(!_fnYield || !_fnYield())
::Sleep(0);
return true;
}
};
bool ThreadOps::join(ThreadOps* ops) {
assert(ops);
assert(ops->_tid != 0);
assert(ops->_hThread != NULL);
if(::WaitForSingleObjectEx(ops->_hThread, INFINITE, FALSE) != WAIT_OBJECT_0)
return false;
::CloseHandle(ops->_hThread);
ops->_hThread = NULL;
return true;
}
bool ThreadOps::yield() {
static YieldOps yielder;
yielder();
return true;
}
bool ThreadOps::setPriority(ThreadOps* impl, Priority p) {
assert(impl);
#if !defined(ZTHREAD_DISABLE_PRIORITY)
bool result;
// Convert
int n;
switch(p) {
case Low:
n = THREAD_PRIORITY_BELOW_NORMAL;
break;
case High:
n = THREAD_PRIORITY_ABOVE_NORMAL;
break;
case Medium:
default:
n = THREAD_PRIORITY_NORMAL;
}
result = (::SetThreadPriority(impl->_hThread, n) != THREAD_PRIORITY_ERROR_RETURN);
return result;
#else
return true;
#endif
}
bool ThreadOps::getPriority(ThreadOps* impl, Priority& p) {
assert(impl);
bool result = true;
#if !defined(ZTHREAD_DISABLE_PRIORITY)
// Convert to one of the PRIORITY values
switch(::GetThreadPriority(impl->_hThread)) {
case THREAD_PRIORITY_ERROR_RETURN:
result = false;
case THREAD_PRIORITY_BELOW_NORMAL:
p = Low;
break;
case THREAD_PRIORITY_ABOVE_NORMAL:
p = High;
break;
case THREAD_PRIORITY_NORMAL:
default:
p = Medium;
}
#endif
return result;
}
bool ThreadOps::spawn(Runnable* task) {
// Start the thread.
#if defined(HAVE_BEGINTHREADEX)
_hThread = (HANDLE)::_beginthreadex(0, 0, &_dispatch, task, 0, (unsigned int*)&_tid);
#else
_hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&_dispatch, task, 0, (DWORD*)&_tid);
#endif
return _hThread != NULL;
}
unsigned int __stdcall ThreadOps::_dispatch(void *arg) {
Runnable* task = reinterpret_cast<Runnable*>(arg);
assert(task);
// Run the task from the correct context
task->run();
// Exit the thread
#if defined(HAVE_BEGINTHREADEX)
::_endthreadex(0);
#else
ExitThread(0);
#endif
return 0;
}
}
|