VirtualBox

忽略:
時間撮記:
2009-4-17 下午03:22:25 (16 年 以前)
作者:
vboxsync
訊息:

IPRT: Redid r46081.

檔案:
修改 1 筆資料

圖例:

未更動
新增
刪除
  • trunk/src/VBox/Runtime/r3/init.cpp

    r18989 r18997  
    55
    66/*
    7  * Copyright (C) 2006-2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2828 * additional information or have any questions.
    2929 */
    30 
    3130
    3231
     
    3938#else
    4039# include <unistd.h>
     40# ifndef RT_OS_OS2
     41#  include <pthread.h>
     42# endif
     43#endif
     44#ifdef RT_OS_OS2
     45# include <InnoTekLIBC/fork.h>
    4146#endif
    4247#include <locale.h>
     
    5156#include <iprt/string.h>
    5257#include <iprt/param.h>
    53 #include <iprt/process.h>
    5458#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
    5559# include <iprt/file.h>
     
    101105 * The process identifier of the running process.
    102106 */
    103 RTPROCESS g_ProcessSelf = NIL_RTPROCESS;
     107RTPROCESS   g_ProcessSelf = NIL_RTPROCESS;
    104108
    105109/**
     
    107111 */
    108112RTPROCPRIORITY g_enmProcessPriority = RTPROCPRIORITY_DEFAULT;
     113
     114
     115#ifndef RT_OS_WINDOWS
     116/**
     117 * Fork callback, child context.
     118 */
     119static void rtR3ForkChildCallback(void)
     120{
     121    g_ProcessSelf = getpid();
     122}
     123#endif /* RT_OS_WINDOWS */
     124
     125#ifdef RT_OS_OS2
     126/** Low-level fork callback for OS/2.  */
     127int rtR3ForkOs2Child(__LIBC_PFORKHANDLE pForkHandle, __LIBC_FORKOP enmOperation)
     128{
     129    if (enmOperation == __LIBC_FORK_STAGE_COMPLETION_CHILD)
     130        rtR3ForkChildCallback();
     131    return 0;
     132}
     133
     134_FORK_CHILD1(0, rtR3ForkOs2Child);
     135#endif /* RT_OS_OS2 */
    109136
    110137
     
    145172}
    146173
    147 
    148 /**
    149  * Internal initialization worker.
    150  *
    151  * @returns IPRT status code.
    152  * @param   fInitSUPLib     Whether to call SUPR3Init.
    153  * @param   pszProgramPath  The program path, NULL if not specified.
    154  */
    155 static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
    156 {
    157     int rc = VINF_SUCCESS;
    158     /* no entry log flow, because prefixes and thread may freak out. */
    159 
    160     /*
    161      * Do reference counting, only initialize the first time around.
    162      *
    163      * We are ASSUMING that nobody will be able to race RTR3Init calls when the
    164      * first one, the real init, is running (second assertion).
    165      */
    166     int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
    167     if (cUsers != 1)
    168     {
    169         AssertMsg(cUsers > 1, ("%d\n", cUsers));
    170         Assert(!g_fInitializing);
    171 #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
    172         if (fInitSUPLib)
    173             SUPR3Init(NULL);
    174 #endif
    175         if (pszProgramPath)
    176             rc = rtR3InitProgramPath(pszProgramPath);
    177         return rc;
    178     }
    179     ASMAtomicWriteBool(&g_fInitializing, true);
     174/**
     175 * rtR3Init worker.
     176 */
     177static int rtR3InitBody(bool fInitSUPLib, const char *pszProgramPath)
     178{
     179    /*
     180     * The Process ID.
     181     */
     182#ifdef _MSC_VER
     183    g_ProcessSelf = _getpid(); /* crappy ansi compiler */
     184#else
     185    g_ProcessSelf = getpid();
     186#endif
    180187
    181188#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
     
    202209     * without having initialized TLS entries and suchlike.
    203210     */
    204     rc = rtThreadInit();
    205     if (RT_FAILURE(rc))
    206     {
    207         AssertMsgFailed(("Failed to initialize threads, rc=%Rrc!\n", rc));
    208         ASMAtomicWriteBool(&g_fInitializing, false);
    209         ASMAtomicDecS32(&g_cUsers);
    210         return rc;
    211     }
     211    int rc = rtThreadInit();
     212    AssertMsgRCReturn(rc, ("Failed to initialize threads, rc=%Rrc!\n", rc), rc);
    212213
    213214#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
     
    219220         */
    220221        rc = SUPR3Init(NULL);
    221         if (RT_FAILURE(rc))
    222         {
    223             AssertMsgFailed(("Failed to initializeble the support library, rc=%Rrc!\n", rc));
    224             ASMAtomicWriteBool(&g_fInitializing, false);
    225             ASMAtomicDecS32(&g_cUsers);
    226             return rc;
    227         }
    228     }
    229 #endif
    230 
    231     /*
    232      * The Process ID.
    233      */
    234     /* The first call to RTProcSelf lazily initialises the cached pid, and
    235      * on posix systems also sets a callback to update the cache on fork.
    236      * We just do a dummy call to it here rather than duplicating
    237      * initialisation code. */
    238     /** @todo since we do lazy initialisation anyway, do we really also need
    239      * to do it explicitly? */
    240     RTProcSelf();
     222        AssertMsgRCReturn(rc, ("Failed to initializeble the support library, rc=%Rrc!\n", rc), rc);
     223    }
     224#endif
    241225
    242226    /*
     
    244228     */
    245229    rc = rtR3InitProgramPath(pszProgramPath);
     230    AssertLogRelMsgRCReturn(rc, ("Failed to get executable directory path, rc=%Rrc!\n", rc), rc);
     231
     232#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
     233    /*
     234     * The threading is initialized we can safely sleep a bit if GIP
     235     * needs some time to update itself updating.
     236     */
     237    if (fInitSUPLib && g_pSUPGlobalInfoPage)
     238    {
     239        RTThreadSleep(20);
     240        RTTimeNanoTS();
     241    }
     242#endif
     243
     244    /*
     245     * Init the program start TSes.
     246     * Do that here to be sure that the GIP time was properly updated the 1st time.
     247     */
     248    g_u64ProgramStartNanoTS = RTTimeNanoTS();
     249    g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
     250    g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
     251
     252    /*
     253     * The remainder cannot easily be undone, so it has to go last.
     254     */
     255
     256    /* Init C runtime locale. */
     257    setlocale(LC_CTYPE, "");
     258
     259    /* Fork callbacks. */
     260#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
     261    rc = pthread_atfork(NULL, NULL, rtR3ForkChildCallback);
     262    AssertMsg(rc == 0, ("%d\n", rc));
     263#endif
     264
     265    return VINF_SUCCESS;
     266}
     267
     268
     269/**
     270 * Internal initialization worker.
     271 *
     272 * @returns IPRT status code.
     273 * @param   fInitSUPLib     Whether to call SUPR3Init.
     274 * @param   pszProgramPath  The program path, NULL if not specified.
     275 */
     276static int rtR3Init(bool fInitSUPLib, const char *pszProgramPath)
     277{
     278    /* no entry log flow, because prefixes and thread may freak out. */
     279
     280    /*
     281     * Do reference counting, only initialize the first time around.
     282     *
     283     * We are ASSUMING that nobody will be able to race RTR3Init calls when the
     284     * first one, the real init, is running (second assertion).
     285     */
     286    int32_t cUsers = ASMAtomicIncS32(&g_cUsers);
     287    if (cUsers != 1)
     288    {
     289        AssertMsg(cUsers > 1, ("%d\n", cUsers));
     290        Assert(!g_fInitializing);
     291#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
     292        if (fInitSUPLib)
     293            SUPR3Init(NULL);
     294#endif
     295        if (!pszProgramPath)
     296            return VINF_SUCCESS;
     297        return rtR3InitProgramPath(pszProgramPath);
     298    }
     299    ASMAtomicWriteBool(&g_fInitializing, true);
     300
     301    /*
     302     * Do the initialization.
     303     */
     304    int rc = rtR3InitBody(fInitSUPLib, pszProgramPath);
    246305    if (RT_FAILURE(rc))
    247306    {
    248         AssertLogRelMsgFailed(("Failed to get executable directory path, rc=%Rrc!\n", rc));
     307        /* failure */
    249308        ASMAtomicWriteBool(&g_fInitializing, false);
    250309        ASMAtomicDecS32(&g_cUsers);
     
    252311    }
    253312
    254 #if !defined(IN_GUEST) && !defined(RT_NO_GIP)
    255     /*
    256      * The threading is initialized we can safely sleep a bit if GIP
    257      * needs some time to update itself updating.
    258      */
    259     if (fInitSUPLib && g_pSUPGlobalInfoPage)
    260     {
    261         RTThreadSleep(20);
    262         RTTimeNanoTS();
    263     }
    264 #endif
    265 
    266     /*
    267      * Init the program start TSes.
    268      * Do that here to be sure that the GIP time was properly updated the 1st time.
    269      */
    270     g_u64ProgramStartNanoTS = RTTimeNanoTS();
    271     g_u64ProgramStartMicroTS = g_u64ProgramStartNanoTS / 1000;
    272     g_u64ProgramStartMilliTS = g_u64ProgramStartNanoTS / 1000000;
    273 
    274     /*
    275      * Init C runtime locale
    276      */
    277     setlocale(LC_CTYPE, "");
    278 
    279     /*
    280      * More stuff to come?
    281      */
    282 
     313    /* success */
    283314    LogFlow(("RTR3Init: returns VINF_SUCCESS\n"));
    284315    ASMAtomicWriteBool(&g_fInitializing, false);
注意: 瀏覽 TracChangeset 來幫助您使用更動檢視器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette