VirtualBox

Changeset 5501 in vbox for trunk/src/VBox/Main/glue


Ignore:
Timestamp:
Oct 25, 2007 10:37:58 AM (17 years ago)
Author:
vboxsync
Message:

Main/XPCOM: Sorted out the XPCOM glue library usage: the standalone glue library is not used anymore (and should never be used when linking to the shared XPCOM library directly). This finally solved XPCOM symbol conflicts & mixups.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/initterm.cpp

    r4862 r5501  
    2323#include <stdlib.h>
    2424
     25/* XPCOM_GLUE is defined when the client uses the standalone glue
     26 * (i.e. dynamically picks up the existing XPCOM shared library installation).
     27 * This is not the case for VirtualBox XPCOM clients (they are always
     28 * distrubuted with the self-built XPCOM library, and therefore have a binary
     29 * dependency on it) but left here for clarity.
     30 */
     31#if defined (XPCOM_GLUE)
    2532#include <nsXPCOMGlue.h>
     33#endif
     34
    2635#include <nsIComponentRegistrar.h>
    2736#include <nsIServiceManager.h>
     
    4049#include <iprt/string.h>
    4150#include <iprt/env.h>
     51#include <iprt/asm.h>
    4252
    4353#include <VBox/err.h>
     
    164174}
    165175
     176/**
     177 *  Global XPCOM initialization flag (we maintain it ourselves since XPCOM
     178 *  doesn't provide such functionality)
     179 */
     180static bool gIsXPCOMInitialized = false;
     181
     182/**
     183 *  Number of Initialize() calls on the main thread.
     184 */
     185static unsigned int gXPCOMInitCount = 0;
     186
    166187#endif /* defined (VBOX_WITH_XPCOM) */
     188
    167189
    168190HRESULT Initialize()
     
    253275#else /* !defined (VBOX_WITH_XPCOM) */
    254276
     277    if (ASMAtomicXchgBool (&gIsXPCOMInitialized, true) == true)
     278    {
     279        /* XPCOM is already initialized on the main thread, no special
     280         * initialization is necessary on additional threads. Just increase
     281         * the init counter if it's a main thread again (to correctly support
     282         * nested calls to Initialize()/Shutdown() for compatibility with
     283         * Win32). */
     284
     285        nsCOMPtr <nsIEventQueue> eventQ;
     286        rc = NS_GetMainEventQ (getter_AddRefs (eventQ));
     287
     288        if (NS_SUCCEEDED (rc))
     289        {
     290            PRBool isOnMainThread = PR_FALSE;
     291            rc = eventQ->IsOnCurrentThread (&isOnMainThread);
     292            if (NS_SUCCEEDED (rc) && isOnMainThread)
     293                ++ gXPCOMInitCount;
     294        }
     295
     296        AssertComRC (rc);
     297        return rc;
     298    }
     299
     300    /* this is the first initialization */
     301    gXPCOMInitCount = 1;
     302
    255303    /* Set VBOX_XPCOM_HOME if not present */
    256304    if (!RTEnvExist ("VBOX_XPCOM_HOME"))
     
    272320    }
    273321
    274     nsCOMPtr <nsIEventQueue> eventQ;
    275     rc = NS_GetMainEventQ (getter_AddRefs (eventQ));
    276     if (rc == NS_ERROR_NOT_INITIALIZED)
    277     {
    278         XPCOMGlueStartup (nsnull);
    279 
    280         nsCOMPtr <DirectoryServiceProvider> dsProv;
    281 
    282         /* prepare paths for registry files */
    283         char homeDir [RTPATH_MAX];
    284         char privateArchDir [RTPATH_MAX];
    285         int vrc = GetVBoxUserHomeDirectory (homeDir, sizeof (homeDir));
    286         if (RT_SUCCESS (vrc))
    287             vrc = RTPathAppPrivateArch (privateArchDir, sizeof (privateArchDir));
    288         if (RT_SUCCESS (vrc))
    289         {
    290             char compReg [RTPATH_MAX];
    291             char xptiDat [RTPATH_MAX];
    292             char compDir [RTPATH_MAX];
    293 
    294             RTStrPrintf (compReg, sizeof (compReg), "%s%c%s",
    295                          homeDir, RTPATH_DELIMITER, "compreg.dat");
    296             RTStrPrintf (xptiDat, sizeof (xptiDat), "%s%c%s",
    297                          homeDir, RTPATH_DELIMITER, "xpti.dat");
    298             RTStrPrintf (compDir, sizeof (compDir), "%s%c/components",
    299                          privateArchDir, RTPATH_DELIMITER);
    300 
    301             dsProv = new DirectoryServiceProvider();
    302             if (dsProv)
    303                 rc = dsProv->init (compReg, xptiDat, compDir, privateArchDir);
    304             else
    305                 rc = NS_ERROR_OUT_OF_MEMORY;
    306         }
     322#if defined (XPCOM_GLUE)
     323    XPCOMGlueStartup (nsnull);
     324#endif
     325
     326    nsCOMPtr <DirectoryServiceProvider> dsProv;
     327
     328    /* prepare paths for registry files */
     329    char homeDir [RTPATH_MAX];
     330    char privateArchDir [RTPATH_MAX];
     331    int vrc = GetVBoxUserHomeDirectory (homeDir, sizeof (homeDir));
     332    if (RT_SUCCESS (vrc))
     333        vrc = RTPathAppPrivateArch (privateArchDir, sizeof (privateArchDir));
     334    if (RT_SUCCESS (vrc))
     335    {
     336        char compReg [RTPATH_MAX];
     337        char xptiDat [RTPATH_MAX];
     338        char compDir [RTPATH_MAX];
     339
     340        RTStrPrintf (compReg, sizeof (compReg), "%s%c%s",
     341                     homeDir, RTPATH_DELIMITER, "compreg.dat");
     342        RTStrPrintf (xptiDat, sizeof (xptiDat), "%s%c%s",
     343                     homeDir, RTPATH_DELIMITER, "xpti.dat");
     344        RTStrPrintf (compDir, sizeof (compDir), "%s%c/components",
     345                     privateArchDir, RTPATH_DELIMITER);
     346
     347        LogFlowFunc (("component registry  : \"%s\"\n", compReg));
     348        LogFlowFunc (("XPTI data file      : \"%s\"\n", xptiDat));
     349        LogFlowFunc (("component directory : \"%s\"\n", compDir));
     350
     351        dsProv = new DirectoryServiceProvider();
     352        if (dsProv)
     353            rc = dsProv->init (compReg, xptiDat, compDir, privateArchDir);
    307354        else
    308             rc = NS_ERROR_FAILURE;
    309 
     355            rc = NS_ERROR_OUT_OF_MEMORY;
     356    }
     357    else
     358        rc = NS_ERROR_FAILURE;
     359
     360    if (NS_SUCCEEDED (rc))
     361    {
    310362        /* get the path to the executable */
    311363        nsCOMPtr <nsIFile> appDir;
     
    395447         * StopAcceptingEvents() on the main event queue). */
    396448
    397         BOOL isOnMainThread = FALSE;
     449        PRBool isOnMainThread = PR_FALSE;
    398450        if (NS_SUCCEEDED (rc))
    399451        {
    400452            rc = eventQ->IsOnCurrentThread (&isOnMainThread);
    401             eventQ = nsnull; /* early release */
     453            eventQ = nsnull; /* early release before shutdown */
    402454        }
    403455        else
    404456        {
    405             isOnMainThread = TRUE;
     457            isOnMainThread = PR_TRUE;
    406458            rc = NS_OK;
    407459        }
     
    409461        if (NS_SUCCEEDED (rc) && isOnMainThread)
    410462        {
    411             /* only the main thread needs to uninitialize XPCOM */
    412             rc = NS_ShutdownXPCOM (nsnull);
    413             XPCOMGlueShutdown();
     463            /* only the main thread needs to uninitialize XPCOM and only if
     464             * init counter drops to zero */
     465            if (-- gXPCOMInitCount == 0)
     466            {
     467                rc = NS_ShutdownXPCOM (nsnull);
     468
     469                /* This is a thread initialized XPCOM and set gIsXPCOMInitialized to
     470                 * true. Reset it back to false. */
     471                bool wasInited = ASMAtomicXchgBool (&gIsXPCOMInitialized, false);
     472                Assert (wasInited == true);
     473                NOREF (wasInited);
     474
     475#if defined (XPCOM_GLUE)
     476                XPCOMGlueShutdown();
     477#endif
     478            }
    414479        }
    415480    }
Note: See TracChangeset for help on using the changeset viewer.

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