thread-posix.cpp revision 0fcf82b2591711fa8980e8f5d9cad1b8f222d6d7
/* $Id$ */
/** @file
* IPRT - Threads, POSIX.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_THREAD
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#if defined(RT_OS_LINUX)
# include <unistd.h>
#endif
#if defined(RT_OS_SOLARIS)
# include <sched.h>
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The signal we're using for RTThreadPoke. */
#define RTTHREAD_POSIX_POKE_SIG SIGUSR2
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** 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);
static void rtThreadPosixPokeSignal(int iSignal);
int rtThreadNativeInit(void)
{
/*
* Allocate the TLS (key in posix terms) where we store the pointer to
* a threads RTTHREADINT structure.
*/
if (rc)
return VERR_NO_TLS_FOR_SELF;
/*
* Register the dummy signal handler for RTThreadPoke.
* (Assert may explode here, but at least we'll notice.)
*/
else
{
g_SelfKey = 0;
}
return rc;
}
/**
* 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);
}
}
/**
* Dummy signal handler for the poke signal.
*
* @param iSignal The signal number.
*/
static void rtThreadPosixPokeSignal(int iSignal)
{
}
/**
* 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)
{
#if defined(RT_OS_LINUX)
/*
* Set the TID.
*/
#endif
/*
* 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.
*/
#ifdef RT_OS_LINUX
#endif
/*
* 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
#elif defined(RT_OS_SOLARIS)
sched_yield();
#else
if (!pthread_yield())
#endif
{
return VINF_SUCCESS;
}
}
else
{
{
return VINF_SUCCESS;
}
}
return rc;
}
RTDECL(bool) RTThreadYield(void)
{
#ifdef RT_OS_DARWIN
#elif defined(RT_OS_SOLARIS)
sched_yield();
#else
#endif
return fRc;
}
{
return 1;
}
{
if (u64Mask != 1)
return VERR_INVALID_PARAMETER;
return VINF_SUCCESS;
}
{
return RTErrConvertFromErrno(rc);
}