thread-win.cpp revision 7c3df511b65c587226c53ce0d72c3644bc803ef9
/* $Id$ */
/** @file
* innotek Portable Runtime - Threads, Win32.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_THREAD
#include <Windows.h>
#include <errno.h>
#include <process.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The TLS index allocated for storing the RTTHREADINT pointer. */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
int rtThreadNativeInit(void)
{
g_dwSelfTLS = TlsAlloc();
if (g_dwSelfTLS == TLS_OUT_OF_INDEXES)
return VERR_NO_TLS_FOR_SELF;
return VINF_SUCCESS;
}
void rtThreadNativeDetach(void)
{
/*
* Deal with alien threads.
*/
if ( pThread
{
rtThreadTerminate(pThread, 0);
}
}
{
return VERR_FAILED_TO_SET_SELF_TLS;
return VINF_SUCCESS;
}
/**
* 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 dwRet;
}
}
int iLastError = GetLastError();
AssertMsgFailed(("SetThreadAffinityMask or GetProcessAffinityMask failed, LastError=%d\n", iLastError));
return RTErrConvertFromWin32(iLastError);
}