VirtualBox

Changeset 25624 in vbox for trunk/src/VBox/Runtime/r3/win


Ignore:
Timestamp:
Jan 3, 2010 3:23:27 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
56315
Message:

iprt/semmutex*: Added RTSemMutexIsOwned. Rewrote mutex recursion handling for windows (semmutex-win.cpp).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/win/semmutex-win.cpp

    r25618 r25624  
    5656{
    5757    /** Magic value (RTSEMMUTEX_MAGIC). */
    58     uint32_t            u32Magic;
     58    uint32_t                u32Magic;
     59    /** Recursion count. */
     60    uint32_t volatile       cRecursions;
     61    /** The owner thread. */
     62    RTNATIVETHREAD volatile hNativeOwner;
    5963    /** The mutex handle. */
    60     HANDLE              hMtx;
     64    HANDLE                  hMtx;
    6165#ifdef RTSEMMUTEX_STRICT
    6266    /** Lock validator record associated with this mutex. */
    63     RTLOCKVALRECEXCL    ValidatorRec;
     67    RTLOCKVALRECEXCL        ValidatorRec;
    6468#endif
    6569};
     
    8488        if (pThis)
    8589        {
    86             pThis->u32Magic = RTSEMMUTEX_MAGIC;
    87             pThis->hMtx = hMtx;
     90            pThis->u32Magic     = RTSEMMUTEX_MAGIC;
     91            pThis->hMtx         = hMtx;
     92            pThis->hNativeOwner = NIL_RTNATIVETHREAD;
     93            pThis->cRecursions  = 0;
    8894#ifdef RTSEMMUTEX_STRICT
    8995            RTLockValidatorRecExclInit(&pThis->ValidatorRec,  NIL_RTLOCKVALIDATORCLASS, RTLOCKVALIDATOR_SUB_CLASS_NONE, "RTSemMutex", pThis);
     
    152158
    153159    /*
     160     * Check for recursive entry.
     161     */
     162    RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
     163    RTNATIVETHREAD hNativeOwner;
     164    ASMAtomicReadHandle(&pThis->hNativeOwner, hNativeOwner);
     165    if (hNativeOwner == hNativeSelf)
     166    {
     167#ifdef RTSEMMUTEX_STRICT
     168        int rc9 = RTLockValidatorRecExclRecursion(&pThis->ValidatorRec, pSrcPos);
     169        if (RT_FAILURE(rc9))
     170            return rc9;
     171#endif
     172        ASMAtomicIncU32(&pThis->cRecursions);
     173        return VINF_SUCCESS;
     174    }
     175
     176    /*
    154177     * Lock mutex semaphore.
    155178     */
    156     RTTHREAD hThreadSelf = NIL_RTTHREAD;
     179    RTTHREAD        hThreadSelf = NIL_RTTHREAD;
    157180    if (cMillies > 0)
    158181    {
     
    175198        case WAIT_OBJECT_0:
    176199#ifdef RTSEMMUTEX_STRICT
    177             RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, false /* we don't know */);
    178 #endif
    179 /** @todo record who owns this thing and avoid kernel calls during recursion. */
     200            RTLockValidatorRecExclSetOwner(&pThis->ValidatorRec, hThreadSelf, pSrcPos, true);
     201#endif
     202            ASMAtomicWriteHandle(&pThis->hNativeOwner, hNativeSelf);
     203            ASMAtomicWriteU32(&pThis->cRecursions, 1);
    180204            return VINF_SUCCESS;
    181205
     
    224248    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
    225249
     250    /*
     251     * Check ownership and recursions.
     252     */
     253    RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
     254    RTNATIVETHREAD hNativeOwner;
     255    ASMAtomicReadHandle(&pThis->hNativeOwner, hNativeOwner);
     256    if (RT_UNLIKELY(hNativeOwner != hNativeSelf))
     257    {
     258        AssertMsgFailed(("Not owner of mutex %p!! hNativeSelf=%RTntrd Owner=%RTntrd cRecursions=%d\n",
     259                         pThis, hNativeSelf, hNativeOwner, pThis->cRecursions));
     260        return VERR_NOT_OWNER;
     261    }
     262    if (pThis->cRecursions > 1)
     263    {
     264#ifdef RTSEMMUTEX_STRICT
     265        int rc9 = RTLockValidatorRecExclUnwind(&pThis->ValidatorRec);
     266        if (RT_FAILURE(rc9))
     267            return rc9;
     268#endif
     269        ASMAtomicDecU32(&pThis->cRecursions);
     270        return VINF_SUCCESS;
     271    }
     272
     273    /*
     274     * Unlock mutex semaphore.
     275     */
    226276#ifdef RTSEMMUTEX_STRICT
    227277    int rc9 = RTLockValidatorRecExclReleaseOwner(&pThis->ValidatorRec, false);
     
    229279        return rc9;
    230280#endif
    231 
    232     /*
    233      * Unlock mutex semaphore.
    234      */
     281    ASMAtomicWriteU32(&pThis->cRecursions, 0);
     282    ASMAtomicWriteHandle(&pThis->hNativeOwner, NIL_RTNATIVETHREAD);
     283
    235284    if (ReleaseMutex(pThis->hMtx))
    236285        return VINF_SUCCESS;
     286
    237287    int rc = RTErrConvertFromWin32(GetLastError());
    238288    AssertMsgFailed(("%p/%p, rc=%Rrc lasterr=%d\n", pThis, pThis->hMtx, rc, GetLastError()));
     
    240290}
    241291
     292
     293RTDECL(bool) RTSemMutexIsOwned(RTSEMMUTEX hMutex)
     294{
     295    /*
     296     * Validate.
     297     */
     298    RTSEMMUTEXINTERNAL *pThis = hMutex;
     299    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     300    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
     301
     302    RTNATIVETHREAD hNativeOwner;
     303    ASMAtomicReadHandle(&pThis->hNativeOwner, hNativeOwner);
     304    return hNativeOwner == NIL_RTNATIVETHREAD;
     305}
     306
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