1 | /* $Id: thread-win.cpp 75882 2018-12-02 19:04:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
32 | #include <iprt/nt/nt-and-windows.h>
|
---|
33 |
|
---|
34 | #include <errno.h>
|
---|
35 | #include <process.h>
|
---|
36 |
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include "internal/iprt.h"
|
---|
39 |
|
---|
40 | #include <iprt/asm-amd64-x86.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/cpuset.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/log.h>
|
---|
45 | #include <iprt/mem.h>
|
---|
46 | #include "internal/thread.h"
|
---|
47 | #include "internal-r3-win.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Defined Constants And Macros *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** The TLS index allocated for storing the RTTHREADINT pointer. */
|
---|
54 | static DWORD g_dwSelfTLS = TLS_OUT_OF_INDEXES;
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Internal Functions *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 | static unsigned __stdcall rtThreadNativeMain(void *pvArgs);
|
---|
61 | static void rtThreadWinTellDebuggerThreadName(uint32_t idThread, const char *pszName);
|
---|
62 |
|
---|
63 |
|
---|
64 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
65 | {
|
---|
66 | g_dwSelfTLS = TlsAlloc();
|
---|
67 | if (g_dwSelfTLS == TLS_OUT_OF_INDEXES)
|
---|
68 | return VERR_NO_TLS_FOR_SELF;
|
---|
69 | return VINF_SUCCESS;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void)
|
---|
74 | {
|
---|
75 | /* nothing to do here. */
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | DECLHIDDEN(void) rtThreadNativeDetach(void)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Deal with alien threads.
|
---|
83 | */
|
---|
84 | PRTTHREADINT pThread = (PRTTHREADINT)TlsGetValue(g_dwSelfTLS);
|
---|
85 | if ( pThread
|
---|
86 | && (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN))
|
---|
87 | {
|
---|
88 | rtThreadTerminate(pThread, 0);
|
---|
89 | TlsSetValue(g_dwSelfTLS, NULL);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
95 | {
|
---|
96 | if (pThread == (PRTTHREADINT)TlsGetValue(g_dwSelfTLS))
|
---|
97 | TlsSetValue(g_dwSelfTLS, NULL);
|
---|
98 |
|
---|
99 | if ((HANDLE)pThread->hThread != INVALID_HANDLE_VALUE)
|
---|
100 | {
|
---|
101 | CloseHandle((HANDLE)pThread->hThread);
|
---|
102 | pThread->hThread = (uintptr_t)INVALID_HANDLE_VALUE;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
108 | {
|
---|
109 | if (!TlsSetValue(g_dwSelfTLS, pThread))
|
---|
110 | return VERR_FAILED_TO_SET_SELF_TLS;
|
---|
111 | if (IsDebuggerPresent())
|
---|
112 | rtThreadWinTellDebuggerThreadName(GetCurrentThreadId(), pThread->szName);
|
---|
113 | return VINF_SUCCESS;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread)
|
---|
118 | {
|
---|
119 | rtThreadWinTellDebuggerThreadName((uint32_t)(uintptr_t)pThread->Core.Key, pThread->szName);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Communicates the thread name to the debugger, if we're begin debugged that
|
---|
125 | * is.
|
---|
126 | *
|
---|
127 | * See http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for debugger
|
---|
128 | * interface details.
|
---|
129 | *
|
---|
130 | * @param idThread The thread ID. UINT32_MAX for current thread.
|
---|
131 | * @param pszName The name.
|
---|
132 | */
|
---|
133 | static void rtThreadWinTellDebuggerThreadName(uint32_t idThread, const char *pszName)
|
---|
134 | {
|
---|
135 | struct
|
---|
136 | {
|
---|
137 | uint32_t uType;
|
---|
138 | const char *pszName;
|
---|
139 | uint32_t idThread;
|
---|
140 | uint32_t fFlags;
|
---|
141 | } Pkg = { 0x1000, pszName, idThread, 0 };
|
---|
142 | __try
|
---|
143 | {
|
---|
144 | RaiseException(0x406d1388, 0, sizeof(Pkg)/sizeof(ULONG_PTR), (ULONG_PTR *)&Pkg);
|
---|
145 | }
|
---|
146 | __except(EXCEPTION_CONTINUE_EXECUTION)
|
---|
147 | {
|
---|
148 |
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Bitch about dangling COM and OLE references, dispose of them
|
---|
155 | * afterwards so we don't end up deadlocked somewhere below
|
---|
156 | * OLE32!DllMain.
|
---|
157 | */
|
---|
158 | static void rtThreadNativeUninitComAndOle(void)
|
---|
159 | {
|
---|
160 | #if 1 /* experimental code */
|
---|
161 | /*
|
---|
162 | * Read the counters.
|
---|
163 | */
|
---|
164 | struct MySOleTlsData
|
---|
165 | {
|
---|
166 | void *apvReserved0[2]; /**< x86=0x00 W7/64=0x00 */
|
---|
167 | DWORD adwReserved0[3]; /**< x86=0x08 W7/64=0x10 */
|
---|
168 | void *apvReserved1[1]; /**< x86=0x14 W7/64=0x20 */
|
---|
169 | DWORD cComInits; /**< x86=0x18 W7/64=0x28 */
|
---|
170 | DWORD cOleInits; /**< x86=0x1c W7/64=0x2c */
|
---|
171 | DWORD dwReserved1; /**< x86=0x20 W7/64=0x30 */
|
---|
172 | void *apvReserved2[4]; /**< x86=0x24 W7/64=0x38 */
|
---|
173 | DWORD adwReserved2[1]; /**< x86=0x34 W7/64=0x58 */
|
---|
174 | void *pvCurrentCtx; /**< x86=0x38 W7/64=0x60 */
|
---|
175 | IUnknown *pCallState; /**< x86=0x3c W7/64=0x68 */
|
---|
176 | } *pOleTlsData = NULL; /* outside the try/except for debugging */
|
---|
177 | DWORD cComInits = 0;
|
---|
178 | DWORD cOleInits = 0;
|
---|
179 | __try
|
---|
180 | {
|
---|
181 | void *pvTeb = NtCurrentTeb();
|
---|
182 | # ifdef RT_ARCH_AMD64
|
---|
183 | pOleTlsData = *(struct MySOleTlsData **)((uintptr_t)pvTeb + 0x1758); /*TEB.ReservedForOle*/
|
---|
184 | # elif RT_ARCH_X86
|
---|
185 | pOleTlsData = *(struct MySOleTlsData **)((uintptr_t)pvTeb + 0x0f80); /*TEB.ReservedForOle*/
|
---|
186 | # else
|
---|
187 | # error "Port me!"
|
---|
188 | # endif
|
---|
189 | if (pOleTlsData)
|
---|
190 | {
|
---|
191 | cComInits = pOleTlsData->cComInits;
|
---|
192 | cOleInits = pOleTlsData->cOleInits;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
196 | {
|
---|
197 | AssertFailedReturnVoid();
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Assert sanity. If any of these breaks, the structure layout above is
|
---|
202 | * probably not correct any longer.
|
---|
203 | */
|
---|
204 | AssertMsgReturnVoid(cComInits < 1000, ("%u (%#x)\n", cComInits, cComInits));
|
---|
205 | AssertMsgReturnVoid(cOleInits < 1000, ("%u (%#x)\n", cOleInits, cOleInits));
|
---|
206 | AssertMsgReturnVoid(cComInits >= cOleInits, ("cComInits=%#x cOleInits=%#x\n", cComInits, cOleInits));
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Do the uninitializing.
|
---|
210 | */
|
---|
211 | if (cComInits)
|
---|
212 | {
|
---|
213 | AssertMsgFailed(("cComInits=%u (%#x) cOleInits=%u (%#x) - dangling COM/OLE inits!\n",
|
---|
214 | cComInits, cComInits, cOleInits, cOleInits));
|
---|
215 |
|
---|
216 | HMODULE hOle32 = GetModuleHandle("ole32.dll");
|
---|
217 | AssertReturnVoid(hOle32 != NULL);
|
---|
218 |
|
---|
219 | typedef void (WINAPI *PFNOLEUNINITIALIZE)(void);
|
---|
220 | PFNOLEUNINITIALIZE pfnOleUninitialize = (PFNOLEUNINITIALIZE)GetProcAddress(hOle32, "OleUninitialize");
|
---|
221 | AssertReturnVoid(pfnOleUninitialize);
|
---|
222 |
|
---|
223 | typedef void (WINAPI *PFNCOUNINITIALIZE)(void);
|
---|
224 | PFNCOUNINITIALIZE pfnCoUninitialize = (PFNCOUNINITIALIZE)GetProcAddress(hOle32, "CoUninitialize");
|
---|
225 | AssertReturnVoid(pfnCoUninitialize);
|
---|
226 |
|
---|
227 | while (cOleInits-- > 0)
|
---|
228 | {
|
---|
229 | pfnOleUninitialize();
|
---|
230 | cComInits--;
|
---|
231 | }
|
---|
232 |
|
---|
233 | while (cComInits-- > 0)
|
---|
234 | pfnCoUninitialize();
|
---|
235 | }
|
---|
236 | #endif
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Wrapper which unpacks the param stuff and calls thread function.
|
---|
242 | */
|
---|
243 | static unsigned __stdcall rtThreadNativeMain(void *pvArgs)
|
---|
244 | {
|
---|
245 | DWORD dwThreadId = GetCurrentThreadId();
|
---|
246 | PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
|
---|
247 |
|
---|
248 | if (!TlsSetValue(g_dwSelfTLS, pThread))
|
---|
249 | AssertReleaseMsgFailed(("failed to set self TLS. lasterr=%d thread '%s'\n", GetLastError(), pThread->szName));
|
---|
250 | if (IsDebuggerPresent())
|
---|
251 | rtThreadWinTellDebuggerThreadName(dwThreadId, &pThread->szName[0]);
|
---|
252 |
|
---|
253 | int rc = rtThreadMain(pThread, dwThreadId, &pThread->szName[0]);
|
---|
254 |
|
---|
255 | TlsSetValue(g_dwSelfTLS, NULL);
|
---|
256 | rtThreadNativeUninitComAndOle();
|
---|
257 | _endthreadex(rc);
|
---|
258 | return rc;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
|
---|
263 | {
|
---|
264 | AssertReturn(pThread->cbStack < ~(unsigned)0, VERR_INVALID_PARAMETER);
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Create the thread.
|
---|
268 | */
|
---|
269 | pThread->hThread = (uintptr_t)INVALID_HANDLE_VALUE;
|
---|
270 | unsigned uThreadId = 0;
|
---|
271 | uintptr_t hThread = _beginthreadex(NULL, (unsigned)pThread->cbStack, rtThreadNativeMain, pThread, 0, &uThreadId);
|
---|
272 | if (hThread != 0 && hThread != ~0U)
|
---|
273 | {
|
---|
274 | pThread->hThread = hThread;
|
---|
275 | *pNativeThread = uThreadId;
|
---|
276 | return VINF_SUCCESS;
|
---|
277 | }
|
---|
278 | return RTErrConvertFromErrno(errno);
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | DECLHIDDEN(bool) rtThreadNativeIsAliveKludge(PRTTHREADINT pThread)
|
---|
283 | {
|
---|
284 | PPEB_COMMON pPeb = NtCurrentPeb();
|
---|
285 | if (!pPeb || !pPeb->Ldr || !pPeb->Ldr->ShutdownInProgress)
|
---|
286 | return true;
|
---|
287 | DWORD rcWait = WaitForSingleObject((HANDLE)pThread->hThread, 0);
|
---|
288 | return rcWait != WAIT_OBJECT_0;
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
293 | {
|
---|
294 | PRTTHREADINT pThread = (PRTTHREADINT)TlsGetValue(g_dwSelfTLS);
|
---|
295 | /** @todo import alien threads ? */
|
---|
296 | return pThread;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | #if 0 /* noone is using this ... */
|
---|
301 | /**
|
---|
302 | * Returns the processor number the current thread was running on during this call
|
---|
303 | *
|
---|
304 | * @returns processor nr
|
---|
305 | */
|
---|
306 | static int rtThreadGetCurrentProcessorNumber(void)
|
---|
307 | {
|
---|
308 | static bool fInitialized = false;
|
---|
309 | static DWORD (WINAPI *pfnGetCurrentProcessorNumber)(void) = NULL;
|
---|
310 | if (!fInitialized)
|
---|
311 | {
|
---|
312 | HMODULE hmodKernel32 = GetModuleHandle("kernel32.dll");
|
---|
313 | if (hmodKernel32)
|
---|
314 | pfnGetCurrentProcessorNumber = (DWORD (WINAPI*)(void))GetProcAddress(hmodKernel32, "GetCurrentProcessorNumber");
|
---|
315 | fInitialized = true;
|
---|
316 | }
|
---|
317 | if (pfnGetCurrentProcessorNumber)
|
---|
318 | return pfnGetCurrentProcessorNumber();
|
---|
319 | return -1;
|
---|
320 | }
|
---|
321 | #endif
|
---|
322 |
|
---|
323 |
|
---|
324 | RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet)
|
---|
325 | {
|
---|
326 | DWORD_PTR fNewMask = pCpuSet ? RTCpuSetToU64(pCpuSet) : ~(DWORD_PTR)0;
|
---|
327 | DWORD_PTR dwRet = SetThreadAffinityMask(GetCurrentThread(), fNewMask);
|
---|
328 | if (dwRet)
|
---|
329 | return VINF_SUCCESS;
|
---|
330 |
|
---|
331 | int iLastError = GetLastError();
|
---|
332 | AssertMsgFailed(("SetThreadAffinityMask failed, LastError=%d\n", iLastError));
|
---|
333 | return RTErrConvertFromWin32(iLastError);
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet)
|
---|
338 | {
|
---|
339 | /*
|
---|
340 | * Haven't found no query api, but the set api returns the old mask, so let's use that.
|
---|
341 | */
|
---|
342 | DWORD_PTR dwIgnored;
|
---|
343 | DWORD_PTR dwProcAff = 0;
|
---|
344 | if (GetProcessAffinityMask(GetCurrentProcess(), &dwProcAff, &dwIgnored))
|
---|
345 | {
|
---|
346 | HANDLE hThread = GetCurrentThread();
|
---|
347 | DWORD_PTR dwRet = SetThreadAffinityMask(hThread, dwProcAff);
|
---|
348 | if (dwRet)
|
---|
349 | {
|
---|
350 | DWORD_PTR dwSet = SetThreadAffinityMask(hThread, dwRet);
|
---|
351 | Assert(dwSet == dwProcAff); NOREF(dwRet);
|
---|
352 |
|
---|
353 | RTCpuSetFromU64(pCpuSet, (uint64_t)dwSet);
|
---|
354 | return VINF_SUCCESS;
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | int iLastError = GetLastError();
|
---|
359 | AssertMsgFailed(("SetThreadAffinityMask or GetProcessAffinityMask failed, LastError=%d\n", iLastError));
|
---|
360 | return RTErrConvertFromWin32(iLastError);
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
|
---|
365 | {
|
---|
366 | uint64_t u64CreationTime, u64ExitTime, u64KernelTime, u64UserTime;
|
---|
367 |
|
---|
368 | if (GetThreadTimes(GetCurrentThread(), (LPFILETIME)&u64CreationTime, (LPFILETIME)&u64ExitTime, (LPFILETIME)&u64KernelTime, (LPFILETIME)&u64UserTime))
|
---|
369 | {
|
---|
370 | *pKernelTime = u64KernelTime / 10000; /* GetThreadTimes returns time in 100 ns units */
|
---|
371 | *pUserTime = u64UserTime / 10000; /* GetThreadTimes returns time in 100 ns units */
|
---|
372 | return VINF_SUCCESS;
|
---|
373 | }
|
---|
374 |
|
---|
375 | int iLastError = GetLastError();
|
---|
376 | AssertMsgFailed(("GetThreadTimes failed, LastError=%d\n", iLastError));
|
---|
377 | return RTErrConvertFromWin32(iLastError);
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Gets the native thread handle for a IPRT thread.
|
---|
383 | *
|
---|
384 | * @returns The thread handle. INVALID_HANDLE_VALUE on failure.
|
---|
385 | * @param hThread The IPRT thread handle.
|
---|
386 | *
|
---|
387 | * @note Windows only.
|
---|
388 | * @note Only valid after parent returns from the thread creation call.
|
---|
389 | */
|
---|
390 | RTDECL(uintptr_t) RTThreadGetNativeHandle(RTTHREAD hThread)
|
---|
391 | {
|
---|
392 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
393 | if (pThread)
|
---|
394 | {
|
---|
395 | uintptr_t hHandle = pThread->hThread;
|
---|
396 | rtThreadRelease(pThread);
|
---|
397 | return hHandle;
|
---|
398 | }
|
---|
399 | return (uintptr_t)INVALID_HANDLE_VALUE;
|
---|
400 | }
|
---|
401 | RT_EXPORT_SYMBOL(RTThreadGetNativeHandle);
|
---|
402 |
|
---|
403 |
|
---|
404 | RTDECL(int) RTThreadPoke(RTTHREAD hThread)
|
---|
405 | {
|
---|
406 | AssertReturn(hThread != RTThreadSelf(), VERR_INVALID_PARAMETER);
|
---|
407 | if (g_pfnNtAlertThread)
|
---|
408 | {
|
---|
409 | PRTTHREADINT pThread = rtThreadGet(hThread);
|
---|
410 | AssertReturn(pThread, VERR_INVALID_HANDLE);
|
---|
411 |
|
---|
412 | NTSTATUS rcNt = g_pfnNtAlertThread((HANDLE)pThread->hThread);
|
---|
413 |
|
---|
414 | rtThreadRelease(pThread);
|
---|
415 | if (NT_SUCCESS(rcNt))
|
---|
416 | return VINF_SUCCESS;
|
---|
417 | return RTErrConvertFromNtStatus(rcNt);
|
---|
418 | }
|
---|
419 | return VERR_NOT_IMPLEMENTED;
|
---|
420 | }
|
---|
421 |
|
---|