1 | /* $Id: thread-win.cpp 28800 2010-04-27 08:22:32Z 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.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 | int rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
84 | {
|
---|
85 | if (!TlsSetValue(g_dwSelfTLS, pThread))
|
---|
86 | return VERR_FAILED_TO_SET_SELF_TLS;
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Wrapper which unpacks the param stuff and calls thread function.
|
---|
93 | */
|
---|
94 | static unsigned __stdcall rtThreadNativeMain(void *pvArgs)
|
---|
95 | {
|
---|
96 | DWORD dwThreadId = GetCurrentThreadId();
|
---|
97 | PRTTHREADINT pThread = (PRTTHREADINT)pvArgs;
|
---|
98 |
|
---|
99 | if (!TlsSetValue(g_dwSelfTLS, pThread))
|
---|
100 | AssertReleaseMsgFailed(("failed to set self TLS. lasterr=%d thread '%s'\n", GetLastError(), pThread->szName));
|
---|
101 |
|
---|
102 | int rc = rtThreadMain(pThread, dwThreadId, &pThread->szName[0]);
|
---|
103 |
|
---|
104 | TlsSetValue(g_dwSelfTLS, NULL);
|
---|
105 | _endthreadex(rc);
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | int rtThreadNativeCreate(PRTTHREADINT pThread, PRTNATIVETHREAD pNativeThread)
|
---|
111 | {
|
---|
112 | AssertReturn(pThread->cbStack < ~(unsigned)0, VERR_INVALID_PARAMETER);
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Create the thread.
|
---|
116 | */
|
---|
117 | pThread->hThread = (uintptr_t)INVALID_HANDLE_VALUE;
|
---|
118 | unsigned uThreadId = 0;
|
---|
119 | uintptr_t hThread = _beginthreadex(NULL, (unsigned)pThread->cbStack, rtThreadNativeMain, pThread, 0, &uThreadId);
|
---|
120 | if (hThread != 0 && hThread != ~0U)
|
---|
121 | {
|
---|
122 | pThread->hThread = hThread;
|
---|
123 | *pNativeThread = uThreadId;
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 | return RTErrConvertFromErrno(errno);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
131 | {
|
---|
132 | PRTTHREADINT pThread = (PRTTHREADINT)TlsGetValue(g_dwSelfTLS);
|
---|
133 | /** @todo import alien threads ? */
|
---|
134 | return pThread;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
|
---|
139 | {
|
---|
140 | return (RTNATIVETHREAD)GetCurrentThreadId();
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTR3DECL(int) RTThreadSleep(RTMSINTERVAL cMillies)
|
---|
145 | {
|
---|
146 | LogFlow(("RTThreadSleep: cMillies=%d\n", cMillies));
|
---|
147 | Sleep(cMillies);
|
---|
148 | LogFlow(("RTThreadSleep: returning %Rrc (cMillies=%d)\n", VINF_SUCCESS, cMillies));
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | RTR3DECL(bool) RTThreadYield(void)
|
---|
154 | {
|
---|
155 | uint64_t u64TS = ASMReadTSC();
|
---|
156 | Sleep(0);
|
---|
157 | u64TS = ASMReadTSC() - u64TS;
|
---|
158 | bool fRc = u64TS > 1500;
|
---|
159 | LogFlow(("RTThreadYield: returning %d (%llu ticks)\n", fRc, u64TS));
|
---|
160 | return fRc;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | #if 0 /* noone is using this ... */
|
---|
165 | /**
|
---|
166 | * Returns the processor number the current thread was running on during this call
|
---|
167 | *
|
---|
168 | * @returns processor nr
|
---|
169 | */
|
---|
170 | static int rtThreadGetCurrentProcessorNumber(void)
|
---|
171 | {
|
---|
172 | static bool fInitialized = false;
|
---|
173 | static DWORD (WINAPI *pfnGetCurrentProcessorNumber)(void) = NULL;
|
---|
174 | if (!fInitialized)
|
---|
175 | {
|
---|
176 | HMODULE hmodKernel32 = GetModuleHandle("KERNEL32.DLL");
|
---|
177 | if (hmodKernel32)
|
---|
178 | pfnGetCurrentProcessorNumber = (DWORD (WINAPI*)(void))GetProcAddress(hmodKernel32, "GetCurrentProcessorNumber");
|
---|
179 | fInitialized = true;
|
---|
180 | }
|
---|
181 | if (pfnGetCurrentProcessorNumber)
|
---|
182 | return pfnGetCurrentProcessorNumber();
|
---|
183 | return -1;
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 |
|
---|
187 |
|
---|
188 | RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask)
|
---|
189 | {
|
---|
190 | Assert((DWORD_PTR)u64Mask == u64Mask || u64Mask == ~(uint64_t)0);
|
---|
191 | DWORD dwRet = SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR)u64Mask);
|
---|
192 | if (dwRet)
|
---|
193 | return VINF_SUCCESS;
|
---|
194 |
|
---|
195 | int iLastError = GetLastError();
|
---|
196 | AssertMsgFailed(("SetThreadAffinityMask failed, LastError=%d\n", iLastError));
|
---|
197 | return RTErrConvertFromWin32(iLastError);
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | RTR3DECL(uint64_t) RTThreadGetAffinity(void)
|
---|
202 | {
|
---|
203 | /*
|
---|
204 | * Haven't found no query api, but the set api returns the old mask, so let's use that.
|
---|
205 | */
|
---|
206 | DWORD_PTR dwIgnored;
|
---|
207 | DWORD_PTR dwProcAff = 0;
|
---|
208 | if (GetProcessAffinityMask(GetCurrentProcess(), &dwProcAff, &dwIgnored))
|
---|
209 | {
|
---|
210 | HANDLE hThread = GetCurrentThread();
|
---|
211 | DWORD dwRet = SetThreadAffinityMask(hThread, dwProcAff);
|
---|
212 | if (dwRet)
|
---|
213 | {
|
---|
214 | DWORD dwSet = SetThreadAffinityMask(hThread, dwRet);
|
---|
215 | Assert(dwSet == dwProcAff); NOREF(dwRet);
|
---|
216 | return dwRet;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | int iLastError = GetLastError();
|
---|
221 | AssertMsgFailed(("SetThreadAffinityMask or GetProcessAffinityMask failed, LastError=%d\n", iLastError));
|
---|
222 | return RTErrConvertFromWin32(iLastError);
|
---|
223 | }
|
---|
224 |
|
---|