VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/thread-win.cpp@ 37154

Last change on this file since 37154 was 37154, checked in by vboxsync, 14 years ago

RTThread[SG]etAffinity cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.6 KB
Line 
1/* $Id: thread-win.cpp 37154 2011-05-19 12:54:32Z vboxsync $ */
2/** @file
3 * IPRT - Threads, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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 <Windows.h>
33
34#include <errno.h>
35#include <process.h>
36
37#include <iprt/thread.h>
38#include <iprt/log.h>
39#include <iprt/assert.h>
40#include <iprt/alloc.h>
41#include <iprt/asm-amd64-x86.h>
42#include <iprt/err.h>
43#include "internal/thread.h"
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49/** The TLS index allocated for storing the RTTHREADINT pointer. */
50static DWORD g_dwSelfTLS = TLS_OUT_OF_INDEXES;
51
52
53/*******************************************************************************
54* Internal Functions *
55*******************************************************************************/
56static unsigned __stdcall rtThreadNativeMain(void *pvArgs);
57
58
59DECLHIDDEN(int) rtThreadNativeInit(void)
60{
61 g_dwSelfTLS = TlsAlloc();
62 if (g_dwSelfTLS == TLS_OUT_OF_INDEXES)
63 return VERR_NO_TLS_FOR_SELF;
64 return VINF_SUCCESS;
65}
66
67
68DECLHIDDEN(void) rtThreadNativeDetach(void)
69{
70 /*
71 * Deal with alien threads.
72 */
73 PRTTHREADINT pThread = (PRTTHREADINT)TlsGetValue(g_dwSelfTLS);
74 if ( pThread
75 && (pThread->fIntFlags & RTTHREADINT_FLAGS_ALIEN))
76 {
77 rtThreadTerminate(pThread, 0);
78 TlsSetValue(g_dwSelfTLS, NULL);
79 }
80}
81
82
83DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
84{
85 if (pThread == (PRTTHREADINT)TlsGetValue(g_dwSelfTLS))
86 TlsSetValue(g_dwSelfTLS, NULL);
87}
88
89
90DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
91{
92 if (!TlsSetValue(g_dwSelfTLS, pThread))
93 return VERR_FAILED_TO_SET_SELF_TLS;
94 return VINF_SUCCESS;
95}
96
97
98/**
99 * Bitch about dangling COM and OLE references, dispose of them
100 * afterwards so we don't end up deadlocked somewhere below
101 * OLE32!DllMain.
102 */
103static void rtThreadNativeUninitComAndOle(void)
104{
105#if 1 /* experimental code */
106 /*
107 * Read the counters.
108 */
109 struct MySOleTlsData
110 {
111 void *apvReserved0[2]; /**< x86=0x00 W7/64=0x00 */
112 DWORD adwReserved0[3]; /**< x86=0x08 W7/64=0x10 */
113 void *apvReserved1[1]; /**< x86=0x14 W7/64=0x20 */
114 DWORD cComInits; /**< x86=0x18 W7/64=0x28 */
115 DWORD cOleInits; /**< x86=0x1c W7/64=0x2c */
116 DWORD dwReserved1; /**< x86=0x20 W7/64=0x30 */
117 void *apvReserved2[4]; /**< x86=0x24 W7/64=0x38 */
118 DWORD adwReserved2[1]; /**< x86=0x34 W7/64=0x58 */
119 void *pvCurrentCtx; /**< x86=0x38 W7/64=0x60 */
120 IUnknown *pCallState; /**< x86=0x3c W7/64=0x68 */
121 } *pOleTlsData = NULL; /* outside the try/except for debugging */
122 DWORD cComInits = 0;
123 DWORD cOleInits = 0;
124 __try
125 {
126 void *pvTeb = NtCurrentTeb();
127# ifdef RT_ARCH_AMD64
128 pOleTlsData = *(struct MySOleTlsData **)((uintptr_t)pvTeb + 0x1758); /*TEB.ReservedForOle*/
129# elif RT_ARCH_X86
130 pOleTlsData = *(struct MySOleTlsData **)((uintptr_t)pvTeb + 0x0f80); /*TEB.ReservedForOle*/
131# else
132# error "Port me!"
133# endif
134 if (pOleTlsData)
135 {
136 cComInits = pOleTlsData->cComInits;
137 cOleInits = pOleTlsData->cOleInits;
138 }
139 }
140 __except(EXCEPTION_EXECUTE_HANDLER)
141 {
142 AssertFailedReturnVoid();
143 }
144
145 /*
146 * Assert sanity. If any of these breaks, the structure layout above is
147 * probably not correct any longer.
148 */
149 AssertMsgReturnVoid(cComInits < 1000, ("%u (%#x)\n", cComInits, cComInits));
150 AssertMsgReturnVoid(cOleInits < 1000, ("%u (%#x)\n", cOleInits, cOleInits));
151 AssertMsgReturnVoid(cComInits >= cOleInits, ("cComInits=%#x cOleInits=%#x\n", cComInits, cOleInits));
152
153 /*
154 * Do the uninitializing.
155 */
156 if (cComInits)
157 {
158 AssertMsgFailed(("cComInits=%u (%#x) cOleInits=%u (%#x) - dangling COM/OLE inits!\n",
159 cComInits, cComInits, cOleInits, cOleInits));
160
161 HMODULE hOle32 = GetModuleHandle("OLE32");
162 AssertReturnVoid(hOle32 != NULL);
163
164 typedef void (WINAPI *PFNOLEUNINITIALIZE)(void);
165 PFNOLEUNINITIALIZE pfnOleUninitialize = (PFNOLEUNINITIALIZE)GetProcAddress(hOle32, "OleUninitialize");
166 AssertReturnVoid(pfnOleUninitialize);
167
168 typedef void (WINAPI *PFNCOUNINITIALIZE)(void);
169 PFNCOUNINITIALIZE pfnCoUninitialize = (PFNCOUNINITIALIZE)GetProcAddress(hOle32, "CoUninitialize");
170 AssertReturnVoid(pfnCoUninitialize);
171
172 while (cOleInits-- > 0)
173 {
174 pfnOleUninitialize();
175 cComInits--;
176 }
177
178 while (cComInits-- > 0)
179 pfnCoUninitialize();
180 }
181#endif
182}
183
184
185/**
186 * Wrapper which unpacks the param stuff and calls thread function.
187 */
188static unsigned __stdcall rtThreadNativeMain(void *pvArgs)
189{
190 DWORD dwThreadId = GetCurrentThreadId();
191 PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
192
193 if (!TlsSetValue(g_dwSelfTLS, pThread))
194 AssertReleaseMsgFailed(("failed to set self TLS. lasterr=%d thread '%s'\n", GetLastError(), pThread->szName));
195
196 int rc = rtThreadMain(pThread, dwThreadId, &pThread->szName[0]);
197
198 TlsSetValue(g_dwSelfTLS, NULL);
199 rtThreadNativeUninitComAndOle();
200 _endthreadex(rc);
201 return rc;
202}
203
204
205DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
206{
207 AssertReturn(pThread->cbStack < ~(unsigned)0, VERR_INVALID_PARAMETER);
208
209 /*
210 * Create the thread.
211 */
212 pThread->hThread = (uintptr_t)INVALID_HANDLE_VALUE;
213 unsigned uThreadId = 0;
214 uintptr_t hThread = _beginthreadex(NULL, (unsigned)pThread->cbStack, rtThreadNativeMain, pThread, 0, &uThreadId);
215 if (hThread != 0 && hThread != ~0U)
216 {
217 pThread->hThread = hThread;
218 *pNativeThread = uThreadId;
219 return VINF_SUCCESS;
220 }
221 return RTErrConvertFromErrno(errno);
222}
223
224
225RTDECL(RTTHREAD) RTThreadSelf(void)
226{
227 PRTTHREADINT pThread = (PRTTHREADINT)TlsGetValue(g_dwSelfTLS);
228 /** @todo import alien threads ? */
229 return pThread;
230}
231
232
233RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
234{
235 return (RTNATIVETHREAD)GetCurrentThreadId();
236}
237
238
239RTR3DECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
240{
241 LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
242 Sleep(cMillies);
243 LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
244 return VINF_SUCCESS;
245}
246
247
248RTR3DECL(bool) RTThreadYield(void)
249{
250 uint64_t u64TS = ASMReadTSC();
251 Sleep(0);
252 u64TS = ASMReadTSC() - u64TS;
253 bool fRc = u64TS > 1500;
254 LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
255 return fRc;
256}
257
258
259#if 0 /* noone is using this ... */
260/**
261 * Returns the processor number the current thread was running on during this call
262 *
263 * @returns processor nr
264 */
265static int rtThreadGetCurrentProcessorNumber(void)
266{
267 static bool fInitialized = false;
268 static DWORD (WINAPI *pfnGetCurrentProcessorNumber)(void) = NULL;
269 if (!fInitialized)
270 {
271 HMODULE hmodKernel32 = GetModuleHandle("KERNEL32.DLL");
272 if (hmodKernel32)
273 pfnGetCurrentProcessorNumber = (DWORD (WINAPI*)(void))GetProcAddress(hmodKernel32, "GetCurrentProcessorNumber");
274 fInitialized = true;
275 }
276 if (pfnGetCurrentProcessorNumber)
277 return pfnGetCurrentProcessorNumber();
278 return -1;
279}
280#endif
281
282
283RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet)
284{
285 DWORD_PTR fNewMask = pCpuSet ? RTCpuSetToU64(pCpuSet) : ~(DWORD_PTR)0;
286 DWORD_PTR dwRet = SetThreadAffinityMask(GetCurrentThread(), fNewMask);
287 if (dwRet)
288 return VINF_SUCCESS;
289
290 int iLastError = GetLastError();
291 AssertMsgFailed(("SetThreadAffinityMask failed, LastError=%d\n", iLastError));
292 return RTErrConvertFromWin32(iLastError);
293}
294
295
296RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet)
297{
298 /*
299 * Haven't found no query api, but the set api returns the old mask, so let's use that.
300 */
301 DWORD_PTR dwIgnored;
302 DWORD_PTR dwProcAff = 0;
303 if (GetProcessAffinityMask(GetCurrentProcess(), &dwProcAff, &dwIgnored))
304 {
305 HANDLE hThread = GetCurrentThread();
306 DWORD_PTR dwRet = SetThreadAffinityMask(hThread, dwProcAff);
307 if (dwRet)
308 {
309 DWORD_PTR dwSet = SetThreadAffinityMask(hThread, dwRet);
310 Assert(dwSet == dwProcAff); NOREF(dwRet);
311
312 RTCpuSetFromU64(pCpuSet, (uint64_t)dwSet);
313 return VINF_SUCCESS;
314 }
315 }
316
317 int iLastError = GetLastError();
318 AssertMsgFailed(("SetThreadAffinityMask or GetProcessAffinityMask failed, LastError=%d\n", iLastError));
319 return RTErrConvertFromWin32(iLastError);
320}
321
322
323RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
324{
325 uint64_t u64CreationTime, u64ExitTime, u64KernelTime, u64UserTime;
326
327 if (GetThreadTimes(GetCurrentThread(), (LPFILETIME)&u64CreationTime, (LPFILETIME)&u64ExitTime, (LPFILETIME)&u64KernelTime, (LPFILETIME)&u64UserTime))
328 {
329 *pKernelTime = u64KernelTime / 10000; /* GetThreadTimes returns time in 100 ns units */
330 *pUserTime = u64UserTime / 10000; /* GetThreadTimes returns time in 100 ns units */
331 return VINF_SUCCESS;
332 }
333
334 int iLastError = GetLastError();
335 AssertMsgFailed(("GetThreadTimes failed, LastError=%d\n", iLastError));
336 return RTErrConvertFromWin32(iLastError);
337}
338
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette