VirtualBox

Changeset 43879 in vbox for trunk/include


Ignore:
Timestamp:
Nov 15, 2012 2:49:23 PM (12 years ago)
Author:
vboxsync
Message:

Extended RTOnce with termination cleanups. (Changes existing structures and functions.)

Location:
trunk/include/iprt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/once.h

    r37316 r43879  
    44
    55/*
    6  * Copyright (C) 2006-2007 Oracle Corporation
     6 * Copyright (C) 2006-2012 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3131#include <iprt/asm.h>
    3232#include <iprt/err.h>
     33#include <iprt/list.h>
    3334
    3435RT_C_DECLS_BEGIN
     
    4041
    4142/**
     43 * Callback that gets executed once.
     44 *
     45 * @returns IPRT style status code, RTOnce returns this.
     46 *
     47 * @param   pvUser          The user parameter.
     48 */
     49typedef DECLCALLBACK(int32_t) FNRTONCE(void *pvUser);
     50/** Pointer to a FNRTONCE. */
     51typedef FNRTONCE *PFNRTONCE;
     52
     53/**
     54 * Callback that gets executed on IPRT/process termination.
     55 *
     56 * @param   pvUser          The user parameter.
     57 * @param   fLazyCleanUpOk  Indicates whether lazy clean-up is OK (see
     58 *                          initterm.h).
     59 */
     60typedef DECLCALLBACK(void) FNRTONCECLEANUP(void *pvUser, bool fLazyCleanUpOk);
     61/** Pointer to a FNRTONCE. */
     62typedef FNRTONCECLEANUP *PFNRTONCECLEANUP;
     63
     64/**
    4265 * Execute once structure.
    4366 *
     
    4871{
    4972    /** Event semaphore that the other guys are blocking on. */
    50     RTSEMEVENTMULTI volatile hEventMulti;
     73    RTSEMEVENTMULTI volatile    hEventMulti;
    5174    /** Reference counter for hEventMulti. */
    52     int32_t volatile cEventRefs;
    53     /** -1 when uninitialized, 1 when initializing (busy) and 2 when done. */
    54     int32_t volatile iState;
     75    int32_t volatile            cEventRefs;
     76    /** See RTONCESTATE. */
     77    int32_t volatile            iState;
    5578    /** The return code of pfnOnce. */
    56     int32_t volatile rc;
     79    int32_t volatile            rc;
     80
     81    /** Pointer to the clean-up function. */
     82    PFNRTONCECLEANUP            pfnCleanUp;
     83    /** Argument to hand to the clean-up function. */
     84    void                       *pvUser;
     85    /** Clean-up list entry. */
     86    RTLISTNODE                  CleanUpNode;
    5787} RTONCE;
    5888/** Pointer to a execute once struct. */
     
    94124
    95125/** Static initializer for RTONCE variables. */
    96 #define RTONCE_INITIALIZER      { NIL_RTSEMEVENTMULTI, 0, RTONCESTATE_UNINITIALIZED, VERR_INTERNAL_ERROR }
    97 
    98 
    99 /**
    100  * Callback that gets executed once.
    101  *
    102  * @returns IPRT style status code, RTOnce returns this.
    103  *
    104  * @param   pvUser1         The first user parameter.
    105  * @param   pvUser2         The second user parameter.
    106  */
    107 typedef DECLCALLBACK(int32_t) FNRTONCE(void *pvUser1, void *pvUser2);
    108 /** Pointer to a FNRTONCE. */
    109 typedef FNRTONCE *PFNRTONCE;
     126#define RTONCE_INITIALIZER \
     127    { NIL_RTSEMEVENTMULTI, 0, RTONCESTATE_UNINITIALIZED, VERR_INTERNAL_ERROR, NULL, NULL, { NULL, NULL } }
     128
    110129
    111130/**
     
    118137 * @param   pOnce           Pointer to the execute once variable.
    119138 * @param   pfnOnce         The function to executed once.
    120  * @param   pvUser1         The first user parameter for pfnOnce.
    121  * @param   pvUser2         The second user parameter for pfnOnce.
    122  */
    123 RTDECL(int) RTOnceSlow(PRTONCE pOnce, PFNRTONCE pfnOnce, void *pvUser1, void *pvUser2);
     139 * @param   pfnCleanUp      The function that will be doing the cleaning up.
     140 *                          Optional.
     141 * @param   pvUser          The user parameter for pfnOnce.
     142 */
     143RTDECL(int) RTOnceSlow(PRTONCE pOnce, PFNRTONCE pfnOnce, FNRTONCECLEANUP pfnCleanUp, void *pvUser);
    124144
    125145/**
     
    132152 * @param   pOnce           Pointer to the execute once variable.
    133153 * @param   pfnOnce         The function to executed once.
    134  * @param   pvUser1         The first user parameter for pfnOnce.
    135  * @param   pvUser2         The second user parameter for pfnOnce.
    136  */
    137 DECLINLINE(int) RTOnce(PRTONCE pOnce, PFNRTONCE pfnOnce, void *pvUser1, void *pvUser2)
     154 * @param   pvUser          The user parameter for pfnOnce.
     155 */
     156DECLINLINE(int) RTOnce(PRTONCE pOnce, PFNRTONCE pfnOnce, void *pvUser)
    138157{
    139158    int32_t iState = ASMAtomicUoReadS32(&pOnce->iState);
     
    142161                  || iState == RTONCESTATE_DONE_HAVE_SEM ))
    143162        return ASMAtomicUoReadS32(&pOnce->rc);
    144     return RTOnceSlow(pOnce, pfnOnce, pvUser1, pvUser2);
     163    return RTOnceSlow(pOnce, pfnOnce, NULL, pvUser);
    145164}
     165
     166/**
     167 * Execute pfnOnce once and register a termination clean-up callback.
     168 *
     169 * Serializes execution of the pfnOnce function, making sure it's
     170 * executed exactly once and that nobody returns from RTOnce before
     171 * it has executed successfully.
     172 *
     173 * @returns IPRT like status code returned by pfnOnce.
     174 *
     175 * @param   pOnce           Pointer to the execute once variable.
     176 * @param   pfnOnce         The function to executed once.
     177 * @param   pfnCleanUp      The function that will be doing the cleaning up.
     178 * @param   pvUser          The user parameter for pfnOnce.
     179 */
     180DECLINLINE(int) RTOnceEx(PRTONCE pOnce, PFNRTONCE pfnOnce, PFNRTONCECLEANUP pfnCleanUp, void *pvUser)
     181{
     182    int32_t iState = ASMAtomicUoReadS32(&pOnce->iState);
     183    if (RT_LIKELY(   iState == RTONCESTATE_DONE
     184                  || iState == RTONCESTATE_DONE_CREATING_SEM
     185                  || iState == RTONCESTATE_DONE_HAVE_SEM ))
     186        return ASMAtomicUoReadS32(&pOnce->rc);
     187    return RTOnceSlow(pOnce, pfnOnce, pfnCleanUp, pvUser);
     188}
     189
    146190
    147191/**
  • trunk/include/iprt/runtime-loader.h

    r36511 r43879  
    44
    55/*
    6  * Copyright (C) 2008-2010 Oracle Corporation
     6 * Copyright (C) 2008-2012 Oracle Corporation
    77 *
    88 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    116116 * serialised for thread safety.
    117117 */
    118 static DECLCALLBACK(int) rtldrLoadOnce(void *, void *)
     118static DECLCALLBACK(int) rtldrLoadOnce(void *)
    119119{
    120120    RTLDRMOD    hLib;
     
    145145
    146146    LogFlowFunc(("\n"));
    147     rc = RTOnce(&s_Once, rtldrLoadOnce, NULL, NULL);
     147    rc = RTOnce(&s_Once, rtldrLoadOnce, NULL);
    148148    LogFlowFunc(("rc = %Rrc\n", rc));
    149149
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