thread-win.cpp revision bff8ec545df4050ffb0e6a52885ec2ecab9a8a02
/* $Id$ */
/** @file
* IPRT - Threads, Windows.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_THREAD
#include <Windows.h>
#include <errno.h>
#include <process.h>
#include <iprt/asm-amd64-x86.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The TLS index allocated for storing the RTTHREADINT pointer. */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
DECLHIDDEN(int) rtThreadNativeInit(void)
{
g_dwSelfTLS = TlsAlloc();
if (g_dwSelfTLS == TLS_OUT_OF_INDEXES)
return VERR_NO_TLS_FOR_SELF;
return VINF_SUCCESS;
}
DECLHIDDEN(void) rtThreadNativeDetach(void)
{
/*
* Deal with alien threads.
*/
if ( pThread
{
rtThreadTerminate(pThread, 0);
}
}
{
}
{
return VERR_FAILED_TO_SET_SELF_TLS;
return VINF_SUCCESS;
}
/**
* Bitch about dangling COM and OLE references, dispose of them
* afterwards so we don't end up deadlocked somewhere below
* OLE32!DllMain.
*/
static void rtThreadNativeUninitComAndOle(void)
{
#if 1 /* experimental code */
/*
* Read the counters.
*/
struct MySOleTlsData
{
void *pvCurrentCtx; /**< x86=0x38 W7/64=0x60 */
{
void *pvTeb = NtCurrentTeb();
# ifdef RT_ARCH_AMD64
# else
# error "Port me!"
# endif
if (pOleTlsData)
{
}
}
{
}
/*
* Assert sanity. If any of these breaks, the structure layout above is
* probably not correct any longer.
*/
AssertMsgReturnVoid(cComInits >= cOleInits, ("cComInits=%#x cOleInits=%#x\n", cComInits, cOleInits));
/*
* Do the uninitializing.
*/
if (cComInits)
{
AssertMsgFailed(("cComInits=%u (%#x) cOleInits=%u (%#x) - dangling COM/OLE inits!\n",
typedef void (WINAPI *PFNOLEUNINITIALIZE)(void);
PFNOLEUNINITIALIZE pfnOleUninitialize = (PFNOLEUNINITIALIZE)GetProcAddress(hOle32, "OleUninitialize");
typedef void (WINAPI *PFNCOUNINITIALIZE)(void);
while (cOleInits-- > 0)
{
cComInits--;
}
while (cComInits-- > 0)
}
#endif
}
/**
* Wrapper which unpacks the param stuff and calls thread function.
*/
{
AssertReleaseMsgFailed(("failed to set self TLS. lasterr=%d thread '%s'\n", GetLastError(), pThread->szName));
return rc;
}
{
/*
* Create the thread.
*/
unsigned uThreadId = 0;
uintptr_t hThread = _beginthreadex(NULL, (unsigned)pThread->cbStack, rtThreadNativeMain, pThread, 0, &uThreadId);
{
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
{
/** @todo import alien threads ? */
return pThread;
}
{
return (RTNATIVETHREAD)GetCurrentThreadId();
}
{
return VINF_SUCCESS;
}
RTR3DECL(bool) RTThreadYield(void)
{
Sleep(0);
return fRc;
}
#if 0 /* noone is using this ... */
/**
* Returns the processor number the current thread was running on during this call
*
* @returns processor nr
*/
static int rtThreadGetCurrentProcessorNumber(void)
{
static bool fInitialized = false;
if (!fInitialized)
{
if (hmodKernel32)
pfnGetCurrentProcessorNumber = (DWORD (WINAPI*)(void))GetProcAddress(hmodKernel32, "GetCurrentProcessorNumber");
fInitialized = true;
}
return pfnGetCurrentProcessorNumber();
return -1;
}
#endif
{
if (dwRet)
return VINF_SUCCESS;
int iLastError = GetLastError();
return RTErrConvertFromWin32(iLastError);
}
{
/*
* Haven't found no query api, but the set api returns the old mask, so let's use that.
*/
{
if (dwRet)
{
return VINF_SUCCESS;
}
}
int iLastError = GetLastError();
AssertMsgFailed(("SetThreadAffinityMask or GetProcessAffinityMask failed, LastError=%d\n", iLastError));
return RTErrConvertFromWin32(iLastError);
}
{
if (GetThreadTimes(GetCurrentThread(), (LPFILETIME)&u64CreationTime, (LPFILETIME)&u64ExitTime, (LPFILETIME)&u64KernelTime, (LPFILETIME)&u64UserTime))
{
return VINF_SUCCESS;
}
int iLastError = GetLastError();
return RTErrConvertFromWin32(iLastError);
}