thread-posix.cpp revision 58caf30c5bb8eb2e621b3f2e0a6a4c8526f4742d
/* $Id$ */
/** @file
* IPRT - Threads, POSIX.
*/
/*
* Copyright (C) 2006-2012 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 <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>
# include <sys/resource.h>
#endif
#if defined(RT_OS_DARWIN)
# include <mach/thread_act.h>
# include <mach/thread_info.h>
# include <mach/host_info.h>
# include <mach/mach_init.h>
# include <mach/mach_host.h>
#endif
#ifdef IN_RT_STATIC
/* The pthread_setname_np trickery below assumes a working dl env which is
* not guaranteed in the context of a fully static executable. */
#else
|| defined(IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP)
# define IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP
# include <dlfcn.h>
# endif
#endif
#if defined(RT_OS_HAIKU)
# include <OS.h>
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#ifndef IN_GUEST
/** Includes RTThreadPoke. */
# define RTTHREAD_POSIX_WITH_POKE
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The pthread key in which we store the pointer to our own PRTTHREAD structure. */
static pthread_key_t g_SelfKey;
#ifdef RTTHREAD_POSIX_WITH_POKE
/** The signal we use for poking threads.
* This is set to -1 if no available signal was found. */
static int g_iSigPokeThread = -1;
#endif
# if defined(RT_OS_DARWIN)
/**
* The Mac OS X (10.6 and later) variant of pthread_setname_np.
*
* @returns errno.h
* @param pszName The new thread name.
*/
typedef int (*PFNPTHREADSETNAME)(const char *pszName);
# else
/**
* The variant of pthread_setname_np most other unix-like systems implement.
*
* @returns errno.h
* @param hThread The thread.
* @param pszName The new thread name.
*/
# endif
/** Pointer to pthread_setname_np if found. */
#endif /* IPRT_MAY_HAVE_PTHREAD_SET_NAME_NP */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void *rtThreadNativeMain(void *pvArgs);
static void rtThreadKeyDestruct(void *pvValue);
static void rtThreadPosixPokeSignal(int iSignal);
DECLHIDDEN(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;
#ifdef RTTHREAD_POSIX_WITH_POKE
/*
* Try register the dummy signal handler for RTThreadPoke.
* Avoid SIGRTMIN thru SIGRTMIN+2 because of LinuxThreads.
*/
static const int s_aiSigCandidates[] =
{
# ifdef SIGRTMAX
SIGRTMAX-3,
SIGRTMAX-2,
SIGRTMAX-1,
# endif
# ifndef RT_OS_SOLARIS
# endif
};
g_iSigPokeThread = -1;
{
{
{
/* ASSUMES no sigaction race... (lazy bird) */
{
break;
}
}
}
else
}
#endif /* RTTHREAD_POSIX_WITH_POKE */
if (RT_SUCCESS(rc))
#endif
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);
}
}
#ifdef RTTHREAD_POSIX_WITH_POKE
/**
* Dummy signal handler for the poke signal.
*
* @param iSignal The signal number.
*/
static void rtThreadPosixPokeSignal(int iSignal)
{
}
#endif
/**
* 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. :/
*/
#ifdef RTTHREAD_POSIX_WITH_POKE
if (g_iSigPokeThread != -1)
#endif
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. :/
*/
#ifdef RTTHREAD_POSIX_WITH_POKE
if (g_iSigPokeThread != -1)
#endif
/*
* Set the TLS entry and, if possible, the thread name.
*/
if (g_pfnThreadSetName)
# ifdef RT_OS_DARWIN
# else
# endif
#endif
/*
* Call common main.
*/
}
{
/*
* 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;
}
#ifdef RTTHREAD_POSIX_WITH_POKE
{
int rc;
if (g_iSigPokeThread != -1)
{
}
else
return rc;
}
#endif
/** @todo move this into platform specific files. */
{
#if defined(RT_OS_SOLARIS)
if (rc)
return RTErrConvertFromErrno(rc);
return VINF_SUCCESS;
/* on Linux, getrusage(RUSAGE_THREAD, ...) is available since 2.6.26 */
if (rc)
return RTErrConvertFromErrno(rc);
*pKernelTime = 0;
return VINF_SUCCESS;
#elif defined(RT_OS_DARWIN)
kern_return_t krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&ThreadInfo, &Count);
return VINF_SUCCESS;
#elif defined(RT_OS_HAIKU)
return VINF_SUCCESS;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}