Debian Codegraph @

nsprpub/pr/src/md/windows/w95thred.c

raw
1
2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "primpl.h"
8 #include <process.h> /* for _beginthreadex() */
9
10 #if defined(_MSC_VER) && _MSC_VER <= 1200
11 /*
12 * VC++ 6.0 doesn't have DWORD_PTR.
13 */
14
15 typedef DWORD DWORD_PTR;
16 #endif /* _MSC_VER <= 1200 */
17
18 /* --- globals ------------------------------------------------ */
19 #ifdef _PR_USE_STATIC_TLS
20 __declspec(thread) struct PRThread* _pr_thread_last_run;
21 __declspec(thread) struct PRThread* _pr_currentThread;
22 __declspec(thread) struct _PRCPU* _pr_currentCPU;
23 #else
24 DWORD _pr_currentThreadIndex;
25 DWORD _pr_lastThreadIndex;
26 DWORD _pr_currentCPUIndex;
27 #endif
28 int _pr_intsOff = 0;
29 _PRInterruptTable _pr_interruptTable[] = {{0}};
30
31 typedef HRESULT(WINAPI* SETTHREADDESCRIPTION)(HANDLE, PCWSTR);
32 static SETTHREADDESCRIPTION sSetThreadDescription = NULL;
33
34 void _PR_MD_EARLY_INIT() {
35 HMODULE hModule;
36
37 #ifndef _PR_USE_STATIC_TLS
38 _pr_currentThreadIndex = TlsAlloc();
39 _pr_lastThreadIndex = TlsAlloc();
40 _pr_currentCPUIndex = TlsAlloc();
41 #endif
42
43 #if defined(_WIN64) && defined(WIN95)
44 _fd_waiting_for_overlapped_done_lock = PR_NewLock();
45 #endif
46
47 // SetThreadDescription is Windows 10 build 1607+
48 hModule = GetModuleHandleW(L"kernel32.dll");
49 if (hModule) {
50 sSetThreadDescription =
51 (SETTHREADDESCRIPTION)GetProcAddress(hModule, "SetThreadDescription");
52 }
53 }
54
55 void _PR_MD_CLEANUP_BEFORE_EXIT(void) {
56 _PR_NT_FreeSids();
57
58 _PR_MD_CleanupSockets();
59
60 WSACleanup();
61
62 #ifndef _PR_USE_STATIC_TLS
63 TlsFree(_pr_currentThreadIndex);
64 TlsFree(_pr_lastThreadIndex);
65 TlsFree(_pr_currentCPUIndex);
66 #endif
67
68 #if defined(_WIN64) && defined(WIN95)
69 // For each iteration check if TFO overlapped IOs are down.
70 if (_fd_waiting_for_overlapped_done_lock) {
71 PRIntervalTime delay = PR_MillisecondsToInterval(1000);
72 PRFileDescList* cur;
73 do {
74 CheckOverlappedPendingSocketsAreDone();
75
76 PR_Lock(_fd_waiting_for_overlapped_done_lock);
77 cur = _fd_waiting_for_overlapped_done;
78 PR_Unlock(_fd_waiting_for_overlapped_done_lock);
79 # if defined(DO_NOT_WAIT_FOR_CONNECT_OVERLAPPED_OPERATIONS)
80 cur = NULL;
81 # endif
82 if (cur) {
83 PR_Sleep(delay); // wait another 1s.
84 }
85 } while (cur);
86
87 PR_DestroyLock(_fd_waiting_for_overlapped_done_lock);
88 }
89 #endif
90 }
91
92 PRStatus _PR_MD_INIT_THREAD(PRThread* thread) {
93 if (thread->flags & (_PR_PRIMORDIAL | _PR_ATTACHED)) {
94 /*
95 ** Warning:
96 ** --------
97 ** NSPR requires a real handle to every thread.
98 ** GetCurrentThread() returns a pseudo-handle which
99 ** is not suitable for some thread operations (e.g.,
100 ** suspending). Therefore, get a real handle from
101 ** the pseudo handle via DuplicateHandle(...)
102 */
103 BOOL ok =
104 DuplicateHandle(GetCurrentProcess(), /* Process of source handle */
105 GetCurrentThread(), /* Pseudo Handle to dup */
106 GetCurrentProcess(), /* Process of handle */
107 &(thread->md.handle), /* resulting handle */
108 0L, /* access flags */
109 FALSE, /* Inheritable */
110 DUPLICATE_SAME_ACCESS); /* Options */
111 if (!ok) {
112 return PR_FAILURE;
113 }
114 thread->id = GetCurrentThreadId();
115 thread->md.id = thread->id;
116 }
117
118 /* Create the blocking IO semaphore */
119 thread->md.blocked_sema = CreateSemaphore(NULL, 0, 1, NULL);
120 if (thread->md.blocked_sema == NULL) {
121 return PR_FAILURE;
122 } else {
123 return PR_SUCCESS;
124 }
125 }
126
127 static unsigned __stdcall pr_root(void* arg) {
128 PRThread* thread = (PRThread*)arg;
129 thread->md.start(thread);
130 return 0;
131 }
132
133 PRStatus _PR_MD_CREATE_THREAD(PRThread* thread, void (*start)(void*),
134 PRThreadPriority priority, PRThreadScope scope,
135 PRThreadState state, PRUint32 stackSize) {
136 thread->md.start = start;
137 thread->md.handle = (HANDLE)_beginthreadex(
138 NULL, thread->stack->stackSize, pr_root, (void*)thread,
139 CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, &(thread->id));
140 if (!thread->md.handle) {
141 return PR_FAILURE;
142 }
143
144 thread->md.id = thread->id;
145 /*
146 * On windows, a thread is created with a thread priority of
147 * THREAD_PRIORITY_NORMAL.
148 */
149 if (priority != PR_PRIORITY_NORMAL) {
150 _PR_MD_SET_PRIORITY(&(thread->md), priority);
151 }
152
153 /* Activate the thread */
154 if (ResumeThread(thread->md.handle) != -1) {
155 return PR_SUCCESS;
156 }
157
158 return PR_FAILURE;
159 }
160
161 void _PR_MD_YIELD(void) {
162 /* Can NT really yield at all? */
163 Sleep(0);
164 }
165
166 void _PR_MD_SET_PRIORITY(_MDThread* thread, PRThreadPriority newPri) {
167 int nativePri;
168 BOOL rv;
169
170 if (newPri < PR_PRIORITY_FIRST) {
171 newPri = PR_PRIORITY_FIRST;
172 } else if (newPri > PR_PRIORITY_LAST) {
173 newPri = PR_PRIORITY_LAST;
174 }
175 switch (newPri) {
176 case PR_PRIORITY_LOW:
177 nativePri = THREAD_PRIORITY_BELOW_NORMAL;
178 break;
179 case PR_PRIORITY_NORMAL:
180 nativePri = THREAD_PRIORITY_NORMAL;
181 break;
182 case PR_PRIORITY_HIGH:
183 nativePri = THREAD_PRIORITY_ABOVE_NORMAL;
184 break;
185 case PR_PRIORITY_URGENT:
186 nativePri = THREAD_PRIORITY_HIGHEST;
187 }
188 rv = SetThreadPriority(thread->handle, nativePri);
189 PR_ASSERT(rv);
190 if (!rv) {
191 PR_LOG(_pr_thread_lm, PR_LOG_MIN,
192 ("PR_SetThreadPriority: can't set thread priority\n"));
193 }
194 return;
195 }
196
197 const DWORD MS_VC_EXCEPTION = 0x406D1388;
198
199 #pragma pack(push, 8)
200 typedef struct tagTHREADNAME_INFO {
201 DWORD dwType; // Must be 0x1000.
202 LPCSTR szName; // Pointer to name (in user addr space).
203 DWORD dwThreadID; // Thread ID (-1=caller thread).
204 DWORD dwFlags; // Reserved for future use, must be zero.
205 } THREADNAME_INFO;
206 #pragma pack(pop)
207
208 void _PR_MD_SET_CURRENT_THREAD_NAME(const char* name) {
209 #ifdef _MSC_VER
210 THREADNAME_INFO info;
211 #endif
212
213 if (sSetThreadDescription) {
214 WCHAR wideName[MAX_PATH];
215 if (MultiByteToWideChar(CP_ACP, 0, name, -1, wideName, MAX_PATH)) {
216 sSetThreadDescription(GetCurrentThread(), wideName);
217 }
218 }
219
220 #ifdef _MSC_VER
221 if (!IsDebuggerPresent()) {
222 return;
223 }
224
225 info.dwType = 0x1000;
226 info.szName = (char*)name;
227 info.dwThreadID = -1;
228 info.dwFlags = 0;
229
230 __try {
231 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR),
232 (ULONG_PTR*)&info);
233 } __except (EXCEPTION_CONTINUE_EXECUTION) {
234 }
235 #endif
236 }
237
238 void _PR_MD_CLEAN_THREAD(PRThread* thread) {
239 BOOL rv;
240
241 if (thread->md.blocked_sema) {
242 rv = CloseHandle(thread->md.blocked_sema);
243 PR_ASSERT(rv);
244 thread->md.blocked_sema = 0;
245 }
246
247 if (thread->md.handle) {
248 rv = CloseHandle(thread->md.handle);
249 PR_ASSERT(rv);
250 thread->md.handle = 0;
251 }
252 }
253
254 void _PR_MD_EXIT_THREAD(PRThread* thread) {
255 _PR_MD_CLEAN_THREAD(thread);
256 _PR_MD_SET_CURRENT_THREAD(NULL);
257 }
258
259 void _PR_MD_EXIT(PRIntn status) { _exit(status); }
260
261 PRInt32 _PR_MD_SETTHREADAFFINITYMASK(PRThread* thread, PRUint32 mask) {
262 #ifdef WINCE
263 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
264 return -1;
265 #else
266 DWORD_PTR rv;
267
268 rv = SetThreadAffinityMask(thread->md.handle, mask);
269
270 return rv ? 0 : -1;
271 #endif
272 }
273
274 PRInt32 _PR_MD_GETTHREADAFFINITYMASK(PRThread* thread, PRUint32* mask) {
275 #ifdef WINCE
276 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
277 return -1;
278 #else
279 BOOL rv;
280 DWORD_PTR process_mask;
281 DWORD_PTR system_mask;
282
283 rv = GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask);
284 if (rv) {
285 *mask = (PRUint32)process_mask;
286 }
287
288 return rv ? 0 : -1;
289 #endif
290 }
291
292 void _PR_MD_SUSPEND_CPU(_PRCPU* cpu) { _PR_MD_SUSPEND_THREAD(cpu->thread); }
293
294 void _PR_MD_RESUME_CPU(_PRCPU* cpu) { _PR_MD_RESUME_THREAD(cpu->thread); }
295
296 void _PR_MD_SUSPEND_THREAD(PRThread* thread) {
297 if (_PR_IS_NATIVE_THREAD(thread)) {
298 DWORD previousSuspendCount;
299 /* XXXMB - SuspendThread() is not a blocking call; how do we
300 * know when the thread is *REALLY* suspended?
301 */
302 previousSuspendCount = SuspendThread(thread->md.handle);
303 PR_ASSERT(previousSuspendCount == 0);
304 }
305 }
306
307 void _PR_MD_RESUME_THREAD(PRThread* thread) {
308 if (_PR_IS_NATIVE_THREAD(thread)) {
309 DWORD previousSuspendCount;
310 previousSuspendCount = ResumeThread(thread->md.handle);
311 PR_ASSERT(previousSuspendCount == 1);
312 }
313 }
314
315 PRThread* _MD_CURRENT_THREAD(void) {
316 PRThread* thread;
317
318 thread = _MD_GET_ATTACHED_THREAD();
319
320 if (NULL == thread) {
321 thread = _PRI_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
322 }
323 PR_ASSERT(thread != NULL);
324 return thread;
325 }