VirtualBox

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

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

build fix

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

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