VirtualBox

Ignore:
Timestamp:
Apr 6, 2012 9:05:19 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
77321
Message:

RTSpinlock: Redid the interface, eliminating NoInts and Tmp. Whether a spinlock is interrupt safe or not is now defined at creation time, preventing stupid bugs arrising from calling the wrong acquire and/or release methods somewhere. The saved flags are stored in the spinlock strucutre, eliminating the annoying Tmp variable. Needs testing on each platform before fixing the build burn.

Location:
trunk/src/VBox/Additions/common/VBoxGuest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest-os2.cpp

    r33540 r40806  
    164164                 * Initialize the session hash table.
    165165                 */
    166                 rc = RTSpinlockCreate(&g_Spinlock);
     166                rc = RTSpinlockCreate(&g_Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestOS2");
    167167                if (RT_SUCCESS(rc))
    168168                {
     
    370370         */
    371371        unsigned iHash = SESSION_HASH(sfn);
    372         RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    373         RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
     372        RTSpinlockAcquire(g_Spinlock);
    374373        pSession->pNextHash = g_apSessionHashTab[iHash];
    375374        g_apSessionHashTab[iHash] = pSession;
    376         RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
     375        RTSpinlockReleaseNoInts(g_Spinlock);
    377376    }
    378377
     
    392391    const RTPROCESS     Process = RTProcSelf();
    393392    const unsigned      iHash = SESSION_HASH(sfn);
    394     RTSPINLOCKTMP       Tmp = RTSPINLOCKTMP_INITIALIZER;
    395     RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
     393    RTSpinlockAcquire(g_Spinlock);
    396394
    397395    pSession = g_apSessionHashTab[iHash];
     
    424422        }
    425423    }
    426     RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
     424    RTSpinlockReleaseNoInts(g_Spinlock);
    427425    if (!pSession)
    428426    {
     
    444442     * Find the session.
    445443     */
    446     RTSPINLOCKTMP       Tmp = RTSPINLOCKTMP_INITIALIZER;
    447444    const RTPROCESS     Process = RTProcSelf();
    448445    const unsigned      iHash = SESSION_HASH(sfn);
    449446    PVBOXGUESTSESSION   pSession;
    450447
    451     RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
     448    RTSpinlockAcquire(g_Spinlock);
    452449    pSession = g_apSessionHashTab[iHash];
    453450    if (pSession && pSession->Process != Process)
     
    458455                     || pSession->Process != Process));
    459456    }
    460     RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
     457    RTSpinlockReleaseNoInts(g_Spinlock);
    461458    if (RT_UNLIKELY(!pSession))
    462459    {
     
    536533     * Find the session.
    537534     */
    538     RTSPINLOCKTMP       Tmp = RTSPINLOCKTMP_INITIALIZER;
    539535    const RTPROCESS     Process = RTProcSelf();
    540536    const unsigned      iHash = SESSION_HASH(sfn);
    541537    PVBOXGUESTSESSION   pSession;
    542538
    543     RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
     539    RTSpinlockAcquire(g_Spinlock);
    544540    pSession = g_apSessionHashTab[iHash];
    545541    if (pSession && pSession->Process != Process)
     
    550546                     || pSession->Process != Process));
    551547    }
    552     RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
     548    RTSpinlockReleaseNoInts(g_Spinlock);
    553549    if (!pSession)
    554550    {
  • trunk/src/VBox/Additions/common/VBoxGuest/VBoxGuest.cpp

    r40483 r40806  
    747747     * Create the wait and session spinlocks as well as the ballooning mutex.
    748748     */
    749     rc = RTSpinlockCreate(&pDevExt->EventSpinlock);
     749    rc = RTSpinlockCreate(&pDevExt->EventSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestEvent");
    750750    if (RT_SUCCESS(rc))
    751         rc = RTSpinlockCreate(&pDevExt->SessionSpinlock);
     751        rc = RTSpinlockCreate(&pDevExt->SessionSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestSession");
    752752    if (RT_FAILURE(rc))
    753753    {
     
    10111011    if (pWait)
    10121012    {
    1013         RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    1014         RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1013        RTSpinlockAcquire(pDevExt->EventSpinlock);
    10151014
    10161015        pWait = RTListGetFirst(&pDevExt->FreeList, VBOXGUESTWAIT, ListNode);
     
    10181017            RTListNodeRemove(&pWait->ListNode);
    10191018
    1020         RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1019        RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    10211020    }
    10221021    if (!pWait)
     
    11011100static void VBoxGuestWaitFreeUnlocked(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTWAIT pWait)
    11021101{
    1103     RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    1104     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1102    RTSpinlockAcquire(pDevExt->EventSpinlock);
    11051103    VBoxGuestWaitFreeLocked(pDevExt, pWait);
    1106     RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1104    RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    11071105}
    11081106
     
    11211119    if (!RTListIsEmpty(&pDevExt->WakeUpList))
    11221120    {
    1123         RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    1124         RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1121        RTSpinlockAcquire(pDevExt->EventSpinlock);
    11251122        for (;;)
    11261123        {
     
    11301127                break;
    11311128            pWait->fPendingWakeUp = true;
    1132             RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1129            RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    11331130
    11341131            rc = RTSemEventMultiSignal(pWait->Event);
    11351132            AssertRC(rc);
    11361133
    1137             RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1134            RTSpinlockAcquire(pDevExt->EventSpinlock);
    11381135            pWait->fPendingWakeUp = false;
    11391136            if (!pWait->fFreeMe)
     
    11481145            }
    11491146        }
    1150         RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1147        RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    11511148    }
    11521149}
     
    12341231 */
    12351232DECLINLINE(int) WaitEventCheckCondition(PVBOXGUESTDEVEXT pDevExt, VBoxGuestWaitEventInfo *pInfo,
    1236                                         int iEvent, const uint32_t fReqEvents, PRTSPINLOCKTMP pTmp)
     1233                                        int iEvent, const uint32_t fReqEvents)
    12371234{
    12381235    uint32_t fMatches = pDevExt->f32PendingEvents & fReqEvents;
     
    12401237    {
    12411238        ASMAtomicAndU32(&pDevExt->f32PendingEvents, ~fMatches);
    1242         RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, pTmp);
     1239        RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    12431240
    12441241        pInfo->u32EventFlagsOut = fMatches;
     
    12501247        return VINF_SUCCESS;
    12511248    }
    1252     RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, pTmp);
     1249    RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    12531250    return VERR_TIMEOUT;
    12541251}
     
    12581255                                          VBoxGuestWaitEventInfo *pInfo,  size_t *pcbDataReturned, bool fInterruptible)
    12591256{
    1260     RTSPINLOCKTMP   Tmp = RTSPINLOCKTMP_INITIALIZER;
    12611257    const uint32_t  fReqEvents = pInfo->u32EventMaskIn;
    12621258    uint32_t        fResEvents;
     
    12831279     * Check the condition up front, before doing the wait-for-event allocations.
    12841280     */
    1285     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
    1286     rc = WaitEventCheckCondition(pDevExt, pInfo, iEvent, fReqEvents, &Tmp);
     1281    RTSpinlockAcquire(pDevExt->EventSpinlock);
     1282    rc = WaitEventCheckCondition(pDevExt, pInfo, iEvent, fReqEvents);
    12871283    if (rc == VINF_SUCCESS)
    12881284        return rc;
     
    13051301     * Otherwise enter into the list and go to sleep waiting for the ISR to signal us.
    13061302     */
    1307     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1303    RTSpinlockAcquire(pDevExt->EventSpinlock);
    13081304    RTListAppend(&pDevExt->WaitList, &pWait->ListNode);
    1309     rc = WaitEventCheckCondition(pDevExt, pInfo, iEvent, fReqEvents, &Tmp);
     1305    rc = WaitEventCheckCondition(pDevExt, pInfo, iEvent, fReqEvents);
    13101306    if (rc == VINF_SUCCESS)
    13111307    {
     
    13321328     * Unlink the wait item and dispose of it.
    13331329     */
    1334     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1330    RTSpinlockAcquire(pDevExt->EventSpinlock);
    13351331    fResEvents = pWait->fResEvents;
    13361332    VBoxGuestWaitFreeLocked(pDevExt, pWait);
    1337     RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1333    RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    13381334
    13391335    /*
     
    13821378static int VBoxGuestCommonIOCtl_CancelAllWaitEvents(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession)
    13831379{
    1384     RTSPINLOCKTMP           Tmp   = RTSPINLOCKTMP_INITIALIZER;
    13851380    PVBOXGUESTWAIT          pWait;
    13861381    PVBOXGUESTWAIT          pSafe;
     
    13921387     * Walk the event list and wake up anyone with a matching session.
    13931388     */
    1394     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1389    RTSpinlockAcquire(pDevExt->EventSpinlock);
    13951390    RTListForEachSafe(&pDevExt->WaitList, pWait, pSafe, VBOXGUESTWAIT, ListNode)
    13961391    {
     
    14071402        }
    14081403    }
    1409     RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1404    RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    14101405    Assert(rc == 0);
    14111406
     
    16921687     * us returning too early.
    16931688     */
    1694     RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    16951689    PVBOXGUESTWAIT pWait;
    16961690    for (;;)
    16971691    {
    1698         RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1692        RTSpinlockAcquire(pDevExt->EventSpinlock);
    16991693        if ((pHdr->fu32Flags & VBOX_HGCM_REQ_DONE) != 0)
    17001694        {
    1701             RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1695            RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    17021696            return VINF_SUCCESS;
    17031697        }
    1704         RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1698        RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    17051699
    17061700        pWait = VBoxGuestWaitAlloc(pDevExt, NULL);
     
    17191713     * Otherwise link us into the HGCM wait list and go to sleep.
    17201714     */
    1721     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     1715    RTSpinlockAcquire(pDevExt->EventSpinlock);
    17221716    RTListAppend(&pDevExt->HGCMWaitList, &pWait->ListNode);
    17231717    if ((pHdr->fu32Flags & VBOX_HGCM_REQ_DONE) != 0)
    17241718    {
    17251719        VBoxGuestWaitFreeLocked(pDevExt, pWait);
    1726         RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1720        RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    17271721        return VINF_SUCCESS;
    17281722    }
    1729     RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     1723    RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    17301724
    17311725    if (fInterruptible)
     
    18101804             */
    18111805            unsigned i;
    1812             RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    1813             RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
     1806            RTSpinlockAcquire(pDevExt->SessionSpinlock);
    18141807            for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
    18151808                if (!pSession->aHGCMClientIds[i])
     
    18181811                    break;
    18191812                }
    1820             RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
     1813            RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock);
    18211814            if (i >= RT_ELEMENTS(pSession->aHGCMClientIds))
    18221815            {
     
    18491842    const uint32_t  u32ClientId = pInfo->u32ClientID;
    18501843    unsigned        i;
    1851     RTSPINLOCKTMP   Tmp = RTSPINLOCKTMP_INITIALIZER;
    1852     RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
     1844    RTSpinlockAcquire(pDevExt->SessionSpinlock);
    18531845    for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
    18541846        if (pSession->aHGCMClientIds[i] == u32ClientId)
     
    18571849            break;
    18581850        }
    1859     RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
     1851    RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock);
    18601852    if (i >= RT_ELEMENTS(pSession->aHGCMClientIds))
    18611853    {
     
    18811873
    18821874    /* Update the client id array according to the result. */
    1883     RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
     1875    RTSpinlockAcquire(pDevExt->SessionSpinlock);
    18841876    if (pSession->aHGCMClientIds[i] == UINT32_MAX)
    18851877        pSession->aHGCMClientIds[i] = RT_SUCCESS(rc) && RT_SUCCESS(pInfo->result) ? 0 : u32ClientId;
    1886     RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
     1878    RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock);
    18871879
    18881880    return rc;
     
    18971889{
    18981890    const uint32_t  u32ClientId = pInfo->u32ClientID;
    1899     RTSPINLOCKTMP   Tmp = RTSPINLOCKTMP_INITIALIZER;
    19001891    uint32_t        fFlags;
    19011892    size_t          cbActual;
     
    19291920     * Validate the client id.
    19301921     */
    1931     RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
     1922    RTSpinlockAcquire(pDevExt->SessionSpinlock);
    19321923    for (i = 0; i < RT_ELEMENTS(pSession->aHGCMClientIds); i++)
    19331924        if (pSession->aHGCMClientIds[i] == u32ClientId)
    19341925            break;
    1935     RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
     1926    RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock);
    19361927    if (RT_UNLIKELY(i >= RT_ELEMENTS(pSession->aHGCMClientIds)))
    19371928    {
     
    22222213static int VBoxGuestCommonIOCtl_SetMouseStatus(PVBOXGUESTDEVEXT pDevExt, PVBOXGUESTSESSION pSession, uint32_t fFeatures)
    22232214{
    2224     unsigned i;
    2225     RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
    2226     uint32_t fNewDevExtStatus = 0;
    2227     int rc;
     2215    uint32_t    fNewDevExtStatus = 0;
     2216    unsigned    i;
     2217    int         rc;
    22282218    /* Exit early if nothing has changed - hack to work around the
    22292219     * Windows Additions not using the common code. */
    2230     bool fNoAction;
    2231 
    2232     RTSpinlockAcquireNoInts(pDevExt->SessionSpinlock, &Tmp);
    2233     for (i = 0; i < sizeof(fFeatures) * 8; ++i)
     2220    bool        fNoAction;
     2221
     2222    RTSpinlockAcquire(pDevExt->SessionSpinlock);
     2223
     2224    for (i = 0; i < sizeof(fFeatures) * 8; i++)
    22342225    {
    22352226        if (RT_BIT_32(i) & VMMDEV_MOUSE_GUEST_MASK)
     
    22372228            if (   (RT_BIT_32(i) & fFeatures)
    22382229                && !(RT_BIT_32(i) & pSession->fMouseStatus))
    2239                 ++pDevExt->cMouseFeatureUsage[i];
     2230                pDevExt->cMouseFeatureUsage[i]++;
    22402231            else if (   !(RT_BIT_32(i) & fFeatures)
    22412232                     && (RT_BIT_32(i) & pSession->fMouseStatus))
    2242                 --pDevExt->cMouseFeatureUsage[i];
     2233                pDevExt->cMouseFeatureUsage[i]--;
    22432234        }
    22442235        if (pDevExt->cMouseFeatureUsage[i] > 0)
    22452236            fNewDevExtStatus |= RT_BIT_32(i);
    22462237    }
     2238
    22472239    pSession->fMouseStatus = fFeatures & VMMDEV_MOUSE_GUEST_MASK;
    22482240    fNoAction = (pDevExt->fMouseStatus == fNewDevExtStatus);
    22492241    pDevExt->fMouseStatus = fNewDevExtStatus;
    2250     RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock, &Tmp);
     2242
     2243    RTSpinlockReleaseNoInts(pDevExt->SessionSpinlock);
    22512244    if (fNoAction)
    22522245        return VINF_SUCCESS;
     2246
    22532247    do
    22542248    {
    22552249        fNewDevExtStatus = pDevExt->fMouseStatus;
    22562250        rc = vboxguestcommonSetMouseStatus(fNewDevExtStatus);
    2257     } while(RT_SUCCESS(rc) && fNewDevExtStatus != pDevExt->fMouseStatus);
     2251    } while (   RT_SUCCESS(rc)
     2252             && fNewDevExtStatus != pDevExt->fMouseStatus);
     2253
    22582254    return rc;
    22592255}
     
    22712267
    22722268    g_test_fSetMouseStatus = true;
    2273     rc = RTSpinlockCreate(&Spinlock);
     2269    rc = RTSpinlockCreate(&Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxGuestTest");
    22742270    AssertRCReturnVoid(rc);
    22752271    {
     
    25962592{
    25972593    bool                    fMousePositionChanged = false;
    2598     RTSPINLOCKTMP           Tmp                   = RTSPINLOCKTMP_INITIALIZER;
    25992594    VMMDevEvents volatile  *pReq                  = pDevExt->pIrqAckEvents;
    26002595    int                     rc                    = 0;
     
    26102605     * Enter the spinlock and check if it's our IRQ or not.
    26112606     */
    2612     RTSpinlockAcquireNoInts(pDevExt->EventSpinlock, &Tmp);
     2607    RTSpinlockAcquire(pDevExt->EventSpinlock);
    26132608    fOurIrq = pDevExt->pVMMDevMemory->V.V1_04.fHaveEvents;
    26142609    if (fOurIrq)
     
    26952690        LogFlow(("VBoxGuestCommonISR: not ours\n"));
    26962691
    2697     RTSpinlockReleaseNoInts(pDevExt->EventSpinlock, &Tmp);
     2692    RTSpinlockReleaseNoInts(pDevExt->EventSpinlock);
    26982693
    26992694#if defined(VBOXGUEST_USE_DEFERRED_WAKE_UP) && !defined(RT_OS_WINDOWS)
Note: See TracChangeset for help on using the changeset viewer.

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