VirtualBox

Changeset 25908 in vbox


Ignore:
Timestamp:
Jan 18, 2010 10:07:28 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
56726
Message:

RTSemRWIsReadOwner: For assertion in main.

Location:
trunk
Files:
7 edited

Legend:

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

    r25748 r25908  
    718718
    719719/**
     720 * Checks if the specified thread is one of the owners.
     721 *
     722 * @returns true if it is, false if not.
     723 *
     724 * @param   pRec                The validator record.
     725 * @param   hThread             The thread handle of the owner.  NIL_RTTHREAD is
     726 *                              an alias for the current thread.
     727 */
     728RTDECL(bool) RTLockValidatorRecSharedIsOwner(PRTLOCKVALRECSHRD pRec, RTTHREAD hThread);
     729
     730/**
    720731 * Check the exit order and release (unset) the shared ownership.
    721732 *
  • trunk/include/iprt/semaphore.h

    r25759 r25908  
    850850 */
    851851RTDECL(bool)  RTSemRWIsWriteOwner(RTSEMRW hRWSem);
     852
     853/**
     854 * Checks if the caller is one of the read owners of the sempahore.
     855 *
     856 * @note    !CAUTION!  This API doesn't work reliably if lock validation isn't
     857 *          enabled. Meaning, the answer is not trustworhty unless
     858 *          RT_LOCK_STRICT or RTSEMRW_STRICT was defined at build time.  Also,
     859 *          make sure you do not use RTSEMRW_FLAGS_NO_LOCK_VAL when creating
     860 *          the semaphore.  And finally, if you used a locking class, don't
     861 *          disable deadlock detection by setting cMsMinDeadlock to
     862 *          RT_INDEFINITE_WAIT.
     863 *
     864 *          In short, only use this for assertions.
     865 *
     866 * @returns true if reader, false if not.
     867 * @param   hRWSem              Handle to the read/write semaphore.
     868 * @param   fWannaHear          What you'd like to hear when lock validation is
     869 *                              not available.  (For avoiding asserting all over
     870 *                              the place.)
     871 */
     872RTDECL(bool)  RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear);
    852873
    853874/**
  • trunk/src/VBox/Runtime/common/misc/lockvalidator.cpp

    r25844 r25908  
    40184018    if (!pRec->fEnabled)
    40194019        return;
    4020     AssertReturnVoid(hThread != NIL_RTTHREAD);
     4020    if (hThread == NIL_RTTHREAD)
     4021    {
     4022        hThread = RTThreadSelfAutoAdopt();
     4023        AssertReturnVoid(hThread != NIL_RTTHREAD);
     4024    }
    40214025    AssertReturnVoid(hThread->u32Magic == RTTHREADINT_MAGIC);
    40224026
     
    40434047}
    40444048RT_EXPORT_SYMBOL(RTLockValidatorRecSharedRemoveOwner);
     4049
     4050
     4051RTDECL(bool) RTLockValidatorRecSharedIsOwner(PRTLOCKVALRECSHRD pRec, RTTHREAD hThread)
     4052{
     4053    /* Validate and resolve input. */
     4054    AssertReturn(pRec->Core.u32Magic == RTLOCKVALRECSHRD_MAGIC, false);
     4055    if (!pRec->fEnabled)
     4056        return false;
     4057    if (hThread == NIL_RTTHREAD)
     4058    {
     4059        hThread = RTThreadSelfAutoAdopt();
     4060        AssertReturn(hThread != NIL_RTTHREAD, false);
     4061    }
     4062    AssertReturn(hThread->u32Magic == RTTHREADINT_MAGIC, false);
     4063
     4064    /* Do the job. */
     4065    PRTLOCKVALRECUNION pEntry = rtLockValidatorRecSharedFindOwner(pRec, hThread, NULL);
     4066    return pEntry != NULL;
     4067}
     4068RT_EXPORT_SYMBOL(RTLockValidatorRecSharedIsOwner);
    40454069
    40464070
  • trunk/src/VBox/Runtime/generic/RTFileReadAll-generic.cpp

    r21337 r25908  
    3939RTDECL(int) RTFileReadAll(const char *pszFilename, void **ppvFile, size_t *pcbFile)
    4040{
    41     return RTFileReadAllEx(pszFilename, 0, RTFOFF_MAX, 0, ppvFile, pcbFile);
     41    return RTFileReadAllEx(pszFilename, 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_WRITE, ppvFile, pcbFile);
    4242}
    4343RT_EXPORT_SYMBOL(RTFileReadAll);
  • trunk/src/VBox/Runtime/generic/semrw-generic.cpp

    r25831 r25908  
    869869RTDECL(bool) RTSemRWIsWriteOwner(RTSEMRW hRWSem)
    870870{
     871    /*
     872     * Validate handle.
     873     */
    871874    struct RTSEMRWINTERNAL *pThis = hRWSem;
    872 
    873     /*
    874      * Validate handle.
    875      */
    876875    AssertPtrReturn(pThis, false);
    877876    AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
     
    886885}
    887886RT_EXPORT_SYMBOL(RTSemRWIsWriteOwner);
     887
     888
     889RTDECL(bool)  RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
     890{
     891    /*
     892     * Validate handle.
     893     */
     894    struct RTSEMRWINTERNAL *pThis = hRWSem;
     895    AssertPtrReturn(pThis, false);
     896    AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
     897
     898    /*
     899     * Check write ownership.  The writer is also a valid reader.
     900     */
     901    RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
     902    RTNATIVETHREAD hWriter;
     903    ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
     904    if (hWriter == hNativeSelf)
     905        return true;
     906    if (hWriter != NIL_RTNATIVETHREAD)
     907        return false;
     908
     909#ifdef RTSEMRW_STRICT
     910    /*
     911     * Ask the lock validator.
     912     */
     913    return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
     914#else
     915    /*
     916     * If there are no reads we cannot be one of them... But if there are we
     917     * cannot know and can only return what the caller want to hear.
     918     */
     919    if (pThis->cReads == 0)
     920        return false;
     921    return fWannaHear;
     922#endif
     923}
     924RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
    888925
    889926
  • trunk/src/VBox/Runtime/generic/semrw-lockless-generic.cpp

    r25831 r25908  
    876876
    877877
     878RTDECL(bool)  RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
     879{
     880    /*
     881     * Validate handle.
     882     */
     883    struct RTSEMRWINTERNAL *pThis = hRWSem;
     884    AssertPtrReturn(pThis, false);
     885    AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
     886
     887    /*
     888     * Inspect the state.
     889     */
     890    uint64_t u64State = ASMAtomicReadU64(&pThis->u64State);
     891    if ((u64State & RTSEMRW_DIR_MASK) == (RTSEMRW_DIR_WRITE << RTSEMRW_DIR_SHIFT))
     892    {
     893        /*
     894         * It's in write mode, so we can only be a reader if we're also the
     895         * current writer.
     896         */
     897        RTNATIVETHREAD hNativeSelf = RTThreadNativeSelf();
     898        RTNATIVETHREAD hWriter;
     899        ASMAtomicUoReadHandle(&pThis->hWriter, &hWriter);
     900        return hWriter == hNativeSelf;
     901    }
     902
     903    /*
     904     * Read mode.  If there are no current readers, then we cannot be a reader.
     905     */
     906    if (!(u64State & RTSEMRW_CNT_RD_MASK))
     907        return false;
     908
     909#ifdef RTSEMRW_STRICT
     910    /*
     911     * Ask the lock validator.
     912     */
     913    return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
     914#else
     915    /*
     916     * Ok, we don't know, just tell the caller what he want to hear.
     917     */
     918    return fWannaHear;
     919#endif
     920}
     921RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
     922
     923
    878924RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
    879925{
  • trunk/src/VBox/Runtime/r3/posix/semrw-posix.cpp

    r25831 r25908  
    638638
    639639
     640RTDECL(bool)  RTSemRWIsReadOwner(RTSEMRW hRWSem, bool fWannaHear)
     641{
     642    /*
     643     * Validate handle.
     644     */
     645    struct RTSEMRWINTERNAL *pThis = hRWSem;
     646    AssertPtrReturn(pThis, false);
     647    AssertReturn(pThis->u32Magic == RTSEMRW_MAGIC, false);
     648
     649    /*
     650     * Check write ownership.  The writer is also a valid reader.
     651     */
     652    pthread_t Self = pthread_self();
     653    pthread_t Writer;
     654    ATOMIC_GET_PTHREAD_T(&pThis->Writer, &Writer);
     655    if (Writer == Self)
     656        return true;
     657    if (Writer != (pthread_t)-1)
     658        return false;
     659
     660    /*
     661     * If there are no readers, we cannot be one of them, can we?
     662     */
     663    if (ASMAtomicReadU32(&pThis->cReaders) == 0)
     664        return false;
     665
     666#ifdef RTSEMRW_STRICT
     667    /*
     668     * Ask the lock validator.
     669     */
     670    return RTLockValidatorRecSharedIsOwner(&pThis->ValidatorRead, NIL_RTTHREAD);
     671#else
     672    /*
     673     * Just tell the caller what he want to hear.
     674     */
     675    return fWannaHear;
     676#endif
     677}
     678RT_EXPORT_SYMBOL(RTSemRWIsReadOwner);
     679
     680
    640681RTDECL(uint32_t) RTSemRWGetWriteRecursion(RTSEMRW hRWSem)
    641682{
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