1 | /* $Id: thread-win.cpp 34507 2010-11-30 13:14:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads, Win32.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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. */
|
---|
50 | static DWORD g_dwSelfTLS = TLS_OUT_OF_INDEXES;
|
---|
51 |
|
---|
52 |
|
---|
53 | /*******************************************************************************
|
---|
54 | * Internal Functions *
|
---|
55 | *******************************************************************************/
|
---|
56 | static unsigned __stdcall rtThreadNativeMain(void *pvArgs);
|
---|
57 |
|
---|
58 |
|
---|
59 | 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 |
|
---|
68 | 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 |
|
---|
83 | void rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
84 | {
|
---|
85 | if (pThread == (PRTTHREADINT)TlsGetValue(g_dwSelfTLS))
|
---|
86 | TlsSetValue(g_dwSelfTLS, NULL);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | 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 | * Wrapper which unpacks the param stuff and calls thread function.
|
---|
100 | */
|
---|
101 | static unsigned __stdcall rtThreadNativeMain(void *pvArgs)
|
---|
102 | {
|
---|
103 | DWORD dwThreadId = GetCurrentThreadId();
|
---|
104 | PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
|
---|
105 |
|
---|
106 | if (!TlsSetValue(g_dwSelfTLS, pThread))
|
---|
107 | AssertReleaseMsgFailed(("failed to set self TLS. lasterr=%d thread '%s'\n", GetLastError(), pThread->szName));
|
---|
108 |
|
---|
109 | int rc = rtThreadMain(pThread, dwThreadId, &pThread->szName[0]);
|
---|
110 |
|
---|
111 | TlsSetValue(g_dwSelfTLS, NULL);
|
---|
112 | _endthreadex(rc);
|
---|
113 | return rc;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
|
---|
118 | {
|
---|
119 | AssertReturn(pThread->cbStack < ~(unsigned)0, VERR_INVALID_PARAMETER);
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Create the thread.
|
---|
123 | */
|
---|
124 | pThread->hThread = (uintptr_t)INVALID_HANDLE_VALUE;
|
---|
125 | unsigned uThreadId = 0;
|
---|
126 | uintptr_t hThread = _beginthreadex(NULL, (unsigned)pThread->cbStack, rtThreadNativeMain, pThread, 0, &uThreadId);
|
---|
127 | if (hThread != 0 && hThread != ~0U)
|
---|
128 | {
|
---|
129 | pThread->hThread = hThread;
|
---|
130 | *pNativeThread = uThreadId;
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 | return RTErrConvertFromErrno(errno);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
138 | {
|
---|
139 | PRTTHREADINT pThread = (PRTTHREADINT)TlsGetValue(g_dwSelfTLS);
|
---|
140 | /** @todo import alien threads ? */
|
---|
141 | return pThread;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
146 | {
|
---|
147 | return (RTNATIVETHREAD)GetCurrentThreadId();
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | RTR3DECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
152 | {
|
---|
153 | LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
|
---|
154 | Sleep(cMillies);
|
---|
155 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTR3DECL(bool) RTThreadYield(void)
|
---|
161 | {
|
---|
162 | uint64_t u64TS = ASMReadTSC();
|
---|
163 | Sleep(0);
|
---|
164 | u64TS = ASMReadTSC() - u64TS;
|
---|
165 | bool fRc = u64TS > 1500;
|
---|
166 | LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
|
---|
167 | return fRc;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | #if 0 /* noone is using this ... */
|
---|
172 | /**
|
---|
173 | * Returns the processor number the current thread was running on during this call
|
---|
174 | *
|
---|
175 | * @returns processor nr
|
---|
176 | */
|
---|
177 | static int rtThreadGetCurrentProcessorNumber(void)
|
---|
178 | {
|
---|
179 | static bool fInitialized = false;
|
---|
180 | static DWORD (WINAPI *pfnGetCurrentProcessorNumber)(void) = NULL;
|
---|
181 | if (!fInitialized)
|
---|
182 | {
|
---|
183 | HMODULE hmodKernel32 = GetModuleHandle("KERNEL32.DLL");
|
---|
184 | if (hmodKernel32)
|
---|
185 | pfnGetCurrentProcessorNumber = (DWORD (WINAPI*)(void))GetProcAddress(hmodKernel32, "GetCurrentProcessorNumber");
|
---|
186 | fInitialized = true;
|
---|
187 | }
|
---|
188 | if (pfnGetCurrentProcessorNumber)
|
---|
189 | return pfnGetCurrentProcessorNumber();
|
---|
190 | return -1;
|
---|
191 | }
|
---|
192 | #endif
|
---|
193 |
|
---|
194 |
|
---|
195 | RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
|
---|
196 | {
|
---|
197 | Assert((DWORD_PTR)u64Mask == u64Mask || u64Mask == ~(uint64_t)0);
|
---|
198 | DWORD_PTR dwRet = SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR)u64Mask);
|
---|
199 | if (dwRet)
|
---|
200 | return VINF_SUCCESS;
|
---|
201 |
|
---|
202 | int iLastError = GetLastError();
|
---|
203 | AssertMsgFailed(("SetThreadAffinityMask failed, LastError=%d\n", iLastError));
|
---|
204 | return RTErrConvertFromWin32(iLastError);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | RTR3DECL(uint64_t) RTThreadGetAffinity(void)
|
---|
209 | {
|
---|
210 | /*
|
---|
211 | * Haven't found no query api, but the set api returns the old mask, so let's use that.
|
---|
212 | */
|
---|
213 | DWORD_PTR dwIgnored;
|
---|
214 | DWORD_PTR dwProcAff = 0;
|
---|
215 | if (GetProcessAffinityMask(GetCurrentProcess(), &dwProcAff, &dwIgnored))
|
---|
216 | {
|
---|
217 | HANDLE hThread = GetCurrentThread();
|
---|
218 | DWORD_PTR dwRet = SetThreadAffinityMask(hThread, dwProcAff);
|
---|
219 | if (dwRet)
|
---|
220 | {
|
---|
221 | DWORD_PTR dwSet = SetThreadAffinityMask(hThread, dwRet);
|
---|
222 | Assert(dwSet == dwProcAff); NOREF(dwRet);
|
---|
223 | return dwRet;
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | int iLastError = GetLastError();
|
---|
228 | AssertMsgFailed(("SetThreadAffinityMask or GetProcessAffinityMask failed, LastError=%d\n", iLastError));
|
---|
229 | return RTErrConvertFromWin32(iLastError);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime)
|
---|
234 | {
|
---|
235 | uint64_t u64CreationTime, u64ExitTime, u64KernelTime, u64UserTime;
|
---|
236 |
|
---|
237 | if (GetThreadTimes(GetCurrentThread(), (LPFILETIME)&u64CreationTime, (LPFILETIME)&u64ExitTime, (LPFILETIME)&u64KernelTime, (LPFILETIME)&u64UserTime))
|
---|
238 | {
|
---|
239 | *pKernelTime = u64KernelTime / 10000; /* GetThreadTimes returns time in 100 ns units */
|
---|
240 | *pUserTime = u64UserTime / 10000; /* GetThreadTimes returns time in 100 ns units */
|
---|
241 | return VINF_SUCCESS;
|
---|
242 | }
|
---|
243 |
|
---|
244 | int iLastError = GetLastError();
|
---|
245 | AssertMsgFailed(("GetThreadTimes failed, LastError=%d\n", iLastError));
|
---|
246 | return RTErrConvertFromWin32(iLastError);
|
---|
247 | }
|
---|