VirtualBox

Changeset 22650 in vbox


Ignore:
Timestamp:
Sep 1, 2009 11:54:15 AM (15 years ago)
Author:
vboxsync
Message:

spinlock-r0drv-freebsd.c,spinlock.h: quick review w/ bugfixes.

Location:
trunk
Files:
2 edited

Legend:

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

    r22127 r22650  
    6666#  define RTSPINLOCKTMP_INITIALIZER { 0 }
    6767
    68 # elif defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
     68# elif defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) || defined(RT_OS_SOLARIS)
    6969    /** The saved [R|E]FLAGS. */
    7070    RTCCUINTREG     uFlags;
    7171#  define RTSPINLOCKTMP_INITIALIZER { 0 }
    7272
    73 # elif defined(RT_OS_OS2) || defined(RT_OS_FREEBSD) /** @todo r=bird: FreeBSD is probably doing the wrong thing here. */
     73# elif defined(RT_OS_OS2)
    7474    /** The saved [R|E]FLAGS. (dummy) */
    7575    RTCCUINTREG     uFlags;
  • trunk/src/VBox/Runtime/r0drv/freebsd/spinlock-r0drv-freebsd.c

    r22580 r22650  
    7777    RT_ASSERT_PREEMPTIBLE();
    7878    AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
    79     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pSpinlockInt));
    80     if (!pSpinlockInt)
     79    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAllocZ(sizeof(*pThis));
     80    if (!pThis)
    8181        return VERR_NO_MEMORY;
    8282
     
    8484     * Initialize & return.
    8585     */
    86     pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
    87     pSpinlockInt->fLocked  = 0;
    88     *pSpinlock = pSpinlockInt;
     86    pThis->u32Magic = RTSPINLOCK_MAGIC;
     87    pThis->fLocked  = 0;
     88    *pSpinlock = pThis;
    8989    return VINF_SUCCESS;
    9090}
     
    9797     */
    9898    RT_ASSERT_INTS_ON();
    99     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    100     if (!pSpinlockInt)
     99    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     100    if (!pThis)
    101101        return VERR_INVALID_PARAMETER;
    102     AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
    103                     ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
     102    AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
     103                    ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
    104104                    VERR_INVALID_PARAMETER);
    105105
     
    107107     * Make the lock invalid and release the memory.
    108108     */
    109     ASMAtomicIncU32(&pSpinlockInt->u32Magic);
    110     RTMemFree(pSpinlockInt);
     109    ASMAtomicIncU32(&pThis->u32Magic);
     110    RTMemFree(pThis);
    111111    return VINF_SUCCESS;
    112112}
     
    115115RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    116116{
    117     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    118     AssertPtr(pSpinlockInt);
    119     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
     117    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     118    AssertPtr(pThis);
     119    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
    120120    RT_ASSERT_PREEMPT_CPUID_VAR();
     121    Assert(pTmp->uFlags == 0);
    121122
    122123    for (;;)
    123124    {
    124         pTmp->uFlags = ASMGetFlags();
    125         ASMIntDisable();
     125        pTmp->uFlags = ASMIntDisableFlags();
    126126        critical_enter();
    127127
    128         for (int c = 50; c > 0; c--)
    129             if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
     128        int c = 50;
     129        for (;;)
     130        {
     131            if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
    130132            {
    131                 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
     133                RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
    132134                return;
    133135            }
    134             else
    135                 cpu_spinwait();
     136            if (--c <= 0)
     137                break;
     138            cpu_spinwait();
     139        }
    136140
    137141        /* Enable interrupts while we sleep. */
     
    145149RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    146150{
    147     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
     151    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
    148152    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
    149153
    150     AssertPtr(pSpinlockInt);
    151     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
    152     RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
    153     NOREF(pTmp);
    154 
    155     if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
    156         AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
     154    AssertPtr(pThis);
     155    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
     156    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
     157
     158    if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
     159        AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
     160
    157161    ASMSetFlags(pTmp->uFlags);
    158 
    159162    critical_exit();
     163    pTmp->uFlags = 0;
    160164}
    161165
     
    163167RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    164168{
    165     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
     169    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
    166170    RT_ASSERT_PREEMPT_CPUID_VAR();
    167     AssertPtr(pSpinlockInt);
    168     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
     171    AssertPtr(pThis);
     172    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
     173#ifdef RT_STRICT
     174    Assert(pTmp->uFlags == 0);
     175    pTmp->uFlags = 0;
     176#endif
    169177
    170178    NOREF(pTmp);
     
    173181    {
    174182        critical_enter();
    175         for (int c = 50; c > 0; c--)
    176             if (ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 1, 0))
     183
     184        int c = 50;
     185        for (;;)
     186        {
     187            if (ASMAtomicCmpXchgU32(&pThis->fLocked, 1, 0))
    177188            {
    178                 RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pSpinlockInt);
     189                RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
    179190                return;
    180191            }
    181             else
    182                 cpu_spinwait();
     192            if (--c <= 0)
     193                break;
     194            cpu_spinwait();
     195        }
    183196
    184197        critical_exit();
     
    190203RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    191204{
    192     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
     205    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
    193206    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
    194207
    195     AssertPtr(pSpinlockInt);
    196     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
    197     RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pSpinlockInt);
    198 
     208    AssertPtr(pThis);
     209    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
     210    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
     211#ifdef RT_STRICT
     212    Assert(pTmp->uFlags == 42);
     213    pTmp->uFlags = 0;
     214#endif
    199215    NOREF(pTmp);
    200216
    201     if (!ASMAtomicCmpXchgU32(&pSpinlockInt->fLocked, 0, 1))
    202         AssertMsgFailed(("Spinlock %p was not locked!\n", pSpinlockInt));
     217    if (!ASMAtomicCmpXchgU32(&pThis->fLocked, 0, 1))
     218        AssertMsgFailed(("Spinlock %p was not locked!\n", pThis));
    203219
    204220    critical_exit();
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