thread-posix.cpp revision a4d7fc6f54717c342281099fe14f5666be9b7921
/* $Id$ */
/** @file
* innotek Portable Runtime - Threads, POSIX.
*/
/*
* 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_THREAD
#include <errno.h>
#include <pthread.h>
#include <signal.h>
/*******************************************************************************
* Header Files *
*******************************************************************************/
/** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
static pthread_key_t g_SelfKey;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void *rtThreadNativeMain(void *pvArgs);
static void rtThreadKeyDestruct(void *pvValue);
int rtThreadNativeInit(void)
{
/*
* Allocate the TLS (key in posix terms) where we store the pointer to
* a threads RTTHREADINT structure.
*/
if (!rc)
return VINF_SUCCESS;
return VERR_NO_TLS_FOR_SELF;
}
/**
* Destructor called when a thread terminates.
* @param pvValue The key value. PRTTHREAD in our case.
*/
static void rtThreadKeyDestruct(void *pvValue)
{
/*
* Deal with alien threads.
*/
{
rtThreadTerminate(pThread, 0);
}
}
/**
* Adopts a thread, this is called immediately after allocating the
* thread structure.
*
* @param pThread Pointer to the thread structure.
*/
{
/*
* Block SIGALRM - required for timer-posix.cpp.
* This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
* It will not help much if someone creates threads directly using pthread_create. :/
*/
if (!rc)
return VINF_SUCCESS;
return VERR_FAILED_TO_SET_SELF_TLS;
}
/**
* Wrapper which unpacks the params and calls thread function.
*/
static void *rtThreadNativeMain(void *pvArgs)
{
/*
* Block SIGALRM - required for timer-posix.cpp.
* This is done to limit harm done by OSes which doesn't do special SIGALRM scheduling.
* It will not help much if someone creates threads directly using pthread_create. :/
*/
/*
* Call common main.
*/
pthread_exit((void *)rc);
return (void *)rc;
}
{
/*
* Set the default stack size.
*/
/*
* Setup thread attributes.
*/
if (!rc)
{
if (!rc)
{
if (!rc)
{
/*
* Create the thread.
*/
if (!rc)
{
return VINF_SUCCESS;
}
}
}
}
return RTErrConvertFromErrno(rc);
}
{
/** @todo import alien threads? */
return pThread;
}
{
return (RTNATIVETHREAD)pthread_self();
}
{
if (!cMillies)
{
/* pthread_yield() isn't part of SuS, thus this fun. */
#ifdef RT_OS_DARWIN
#else
if (!pthread_yield())
#endif
{
return VINF_SUCCESS;
}
}
else
{
{
return VINF_SUCCESS;
}
}
return rc;
}
RTDECL(bool) RTThreadYield(void)
{
#ifdef RT_OS_DARWIN
#else
#endif
return fRc;
}
{
return 1;
}
{
if (u64Mask != 1)
return VERR_INVALID_PARAMETER;
return VINF_SUCCESS;
}