VirtualBox

Changeset 89425 in vbox for trunk/src/VBox/Devices/Audio


Ignore:
Timestamp:
Jun 1, 2021 10:22:23 AM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
144778
Message:

DrvHostAudioAlsaStubs: Use RTOnce to make the init thread-safe and all. bugref:9890

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Audio/DrvHostAudioAlsaStubs.cpp

    r88966 r89425  
    1818#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
    1919#include <iprt/assert.h>
     20#include <iprt/err.h>
    2021#include <iprt/ldr.h>
    2122#include <VBox/log.h>
    22 #include <iprt/err.h>
     23#include <iprt/once.h>
    2324
    2425#include <alsa/asoundlib.h>
     
    239240#undef ELEMENT
    240241
    241 /**
    242  * Try to dynamically load the ALSA libraries.  This function is not
    243  * thread-safe, and should be called before attempting to use any of the
    244  * ALSA functions.
    245  *
    246  * @returns iprt status code
    247  */
    248 int audioLoadAlsaLib(void)
    249 {
    250     static enum { NO = 0, YES, FAIL } isLibLoaded = NO;
    251     RTLDRMOD hLib;
    252 
     242/** Init once. */
     243static RTONCE g_AlsaLibInitOnce = RTONCE_INITIALIZER;
     244
     245
     246/** @callback_method_impl{FNRTONCE} */
     247static DECLCALLBACK(int32_t) drvHostAudioAlsaLibInitOnce(void *pvUser)
     248{
     249    RT_NOREF(pvUser);
    253250    LogFlowFunc(("\n"));
    254     /* If this is not NO then the function has obviously been called twice,
    255        which is likely to be a bug. */
    256     if (NO != isLibLoaded)
     251
     252    RTLDRMOD hMod = NIL_RTLDRMOD;
     253    int rc = RTLdrLoadSystemEx(VBOX_ALSA_LIB, RTLDRLOAD_FLAGS_NO_UNLOAD, &hMod);
     254    if (RT_SUCCESS(rc))
    257255    {
    258         AssertMsgFailed(("isLibLoaded == %s\n", YES == isLibLoaded ? "YES" : "NO"));
    259         return YES == isLibLoaded ? VINF_SUCCESS : VERR_NOT_SUPPORTED;
    260     }
    261     isLibLoaded = FAIL;
    262     int rc = RTLdrLoad(VBOX_ALSA_LIB, &hLib);
    263     if (RT_FAILURE(rc))
    264     {
    265         LogRelFunc(("Failed to load library %s (%Rrc)\n", VBOX_ALSA_LIB, rc));
    266         return rc;
    267     }
    268     for (uintptr_t i = 0; i < RT_ELEMENTS(SharedFuncs); i++)
    269     {
    270         rc = RTLdrGetSymbol(hLib, SharedFuncs[i].name, (void **)SharedFuncs[i].pfn);
    271         if (RT_SUCCESS(rc))
    272         { /* likely */ }
    273         else if (SharedFuncs[i].pfnFallback && rc == VERR_SYMBOL_NOT_FOUND)
    274             *SharedFuncs[i].pfn = SharedFuncs[i].pfnFallback;
    275         else
     256        for (uintptr_t i = 0; i < RT_ELEMENTS(SharedFuncs); i++)
    276257        {
    277             LogRelFunc(("Failed to load library %s: Getting symbol %s failed: %Rrc\n", VBOX_ALSA_LIB, SharedFuncs[i].name, rc));
    278             return rc;
     258            rc = RTLdrGetSymbol(hMod, SharedFuncs[i].name, (void **)SharedFuncs[i].pfn);
     259            if (RT_SUCCESS(rc))
     260            { /* likely */ }
     261            else if (SharedFuncs[i].pfnFallback && rc == VERR_SYMBOL_NOT_FOUND)
     262                *SharedFuncs[i].pfn = SharedFuncs[i].pfnFallback;
     263            else
     264            {
     265                LogRelFunc(("Failed to load library %s: Getting symbol %s failed: %Rrc\n", VBOX_ALSA_LIB, SharedFuncs[i].name, rc));
     266                return rc;
     267            }
    279268        }
    280269    }
    281     isLibLoaded = YES;
     270    else
     271        LogRelFunc(("Failed to load library %s (%Rrc)\n", VBOX_ALSA_LIB, rc));
    282272    return rc;
    283273}
    284274
     275
     276/**
     277 * Try to dynamically load the ALSA libraries.
     278 *
     279 * @returns VBox status code.
     280 */
     281int audioLoadAlsaLib(void)
     282{
     283    LogFlowFunc(("\n"));
     284    return RTOnce(&g_AlsaLibInitOnce, drvHostAudioAlsaLibInitOnce, NULL);
     285}
     286
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