VirtualBox

Changeset 22131 in vbox for trunk/src/VBox/Runtime/r0drv


Ignore:
Timestamp:
Aug 10, 2009 1:07:43 PM (15 years ago)
Author:
vboxsync
Message:

spinlock-r0drv-[solaris|linux].c: More preemption assertions.

Location:
trunk/src/VBox/Runtime/r0drv
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r0drv/linux/spinlock-r0drv-linux.c

    r22130 r22131  
    5858    /** The linux spinlock structure. */
    5959    spinlock_t          Spinlock;
    60 #if !defined(CONFIG_SMP) || defined(RT_ARCH_AMD64)
     60#ifdef RT_MORE_STRICT
     61    /** The idAssertCpu variable before acquring the lock for asserting after
     62     *  releasing the spinlock. */
     63    RTCPUID volatile    idAssertCpu;
     64    /** The CPU that owns the lock. */
     65    RTCPUID volatile    idCpuOwner;
     66#elif !defined(CONFIG_SMP) || defined(RT_ARCH_AMD64)
    6167    /** A placeholder on Uni systems so we won't piss off RTMemAlloc(). */
    6268    void                *pvUniDummy;
     
    7177     * Allocate.
    7278     */
    73     PRTSPINLOCKINTERNAL pSpinlockInt;
     79    PRTSPINLOCKINTERNAL pThis;
    7480    Assert(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
    75     pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
    76     if (!pSpinlockInt)
     81    pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
     82    if (!pThis)
    7783        return VERR_NO_MEMORY;
    7884    /*
    7985     * Initialize and return.
    8086     */
    81     pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
    82     spin_lock_init(&pSpinlockInt->Spinlock);
     87    pThis->u32Magic    = RTSPINLOCK_MAGIC;
     88#ifdef RT_MORE_STRICT
     89    pThis->idCpuOwner  = NIL_RTCPUID;
     90    pThis->idAssertCpu = NIL_RTCPUID;
     91#endif
     92    spin_lock_init(&pThis->Spinlock);
    8393
    84     *pSpinlock = pSpinlockInt;
     94    *pSpinlock = pThis;
    8595    return VINF_SUCCESS;
    8696}
     
    93103     * Validate input.
    94104     */
    95     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    96     if (!pSpinlockInt)
     105    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     106    if (!pThis)
    97107        return VERR_INVALID_PARAMETER;
    98     if (pSpinlockInt->u32Magic != RTSPINLOCK_MAGIC)
     108    if (pThis->u32Magic != RTSPINLOCK_MAGIC)
    99109    {
    100         AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic));
     110        AssertMsgFailed(("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic));
    101111        return VERR_INVALID_PARAMETER;
    102112    }
    103113
    104     ASMAtomicIncU32(&pSpinlockInt->u32Magic);
    105     RTMemFree(pSpinlockInt);
     114    ASMAtomicIncU32(&pThis->u32Magic);
     115    RTMemFree(pThis);
    106116    return VINF_SUCCESS;
    107117}
     
    111121RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    112122{
    113     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    114     AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
    115               ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
    116     NOREF(pSpinlockInt);
     123    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     124    RT_ASSERT_PREEMPT_CPUID_VAR();
     125    AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
     126              ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
    117127
    118     spin_lock_irqsave(&pSpinlockInt->Spinlock, pTmp->flFlags);
     128    spin_lock_irqsave(&pThis->Spinlock, pTmp->flFlags);
     129
     130    RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
    119131}
    120132RT_EXPORT_SYMBOL(RTSpinlockAcquireNoInts);
     
    123135RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    124136{
    125     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    126     AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
    127               ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
    128     NOREF(pSpinlockInt);
     137    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     138    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
    129139
    130     spin_unlock_irqrestore(&pSpinlockInt->Spinlock, pTmp->flFlags);
     140    AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
     141              ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
     142    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
     143    NOREF(pTmp);
     144
     145    spin_unlock_irqrestore(&pThis->Spinlock, pTmp->flFlags);
     146
     147    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASED();
    131148}
    132149RT_EXPORT_SYMBOL(RTSpinlockReleaseNoInts);
     
    135152RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    136153{
    137     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
     154    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
    138155    RT_ASSERT_PREEMPT_CPUID_VAR();
    139     AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
    140               ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
    141     NOREF(pSpinlockInt); NOREF(pTmp);
     156    AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
     157              ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
     158    NOREF(pTmp);
    142159
    143     spin_lock(&pSpinlockInt->Spinlock);
     160    spin_lock(&pThis->Spinlock);
    144161
    145 #ifdef RT_MORE_STRICT
    146     {
    147         RTCPUID const idAssertCpuNow = RTMpCpuId(); /* Spinlocks are not preemptible, so we cannot be rescheduled. */
    148         AssertMsg(idAssertCpu == idAssertCpuNow || idAssertCpu == NIL_RTCPUID,  ("%#x, %#x\n", idAssertCpu, idAssertCpuNow));
    149         pTmp->flFlags = idAssertCpuNow;
    150     }
    151 #endif
     162    RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED(pThis);
    152163}
    153164RT_EXPORT_SYMBOL(RTSpinlockAcquire);
     
    156167RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    157168{
    158     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    159 #ifdef RT_MORE_STRICT
    160     RTCPUID const idAssertCpu = pTmp->flFlags;
    161     pTmp->flFlags = 0;
    162     RT_ASSERT_PREEMPT_CPUID();
    163 #endif
    164     AssertMsg(pSpinlockInt && pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
    165               ("pSpinlockInt=%p u32Magic=%08x\n", pSpinlockInt, pSpinlockInt ? (int)pSpinlockInt->u32Magic : 0));
    166     NOREF(pSpinlockInt); NOREF(pTmp);
     169    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     170    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
     171    AssertMsg(pThis && pThis->u32Magic == RTSPINLOCK_MAGIC,
     172              ("pThis=%p u32Magic=%08x\n", pThis, pThis ? (int)pThis->u32Magic : 0));
     173    NOREF(pTmp);
     174    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
    167175
    168     spin_unlock(&pSpinlockInt->Spinlock);
     176    spin_unlock(&pThis->Spinlock);
    169177
    170 #ifdef RT_MORE_STRICT
    171     if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
    172         RT_ASSERT_PREEMPT_CPUID();
    173 #endif
     178    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASED();
    174179}
    175180RT_EXPORT_SYMBOL(RTSpinlockRelease);
  • trunk/src/VBox/Runtime/r0drv/solaris/spinlock-r0drv-solaris.c

    r22130 r22131  
    5858    /** A Solaris spinlock. */
    5959    kmutex_t            Mtx;
     60#ifdef RT_MORE_STRICT
     61    /** The idAssertCpu variable before acquring the lock for asserting after
     62     *  releasing the spinlock. */
     63    RTCPUID volatile    idAssertCpu;
     64    /** The CPU that owns the lock. */
     65    RTCPUID volatile    idCpuOwner;
     66#endif
    6067} RTSPINLOCKINTERNAL, *PRTSPINLOCKINTERNAL;
    6168
     
    6976    RT_ASSERT_PREEMPTIBLE();
    7077    AssertCompile(sizeof(RTSPINLOCKINTERNAL) > sizeof(void *));
    71     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pSpinlockInt));
    72     if (!pSpinlockInt)
     78    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)RTMemAlloc(sizeof(*pThis));
     79    if (!pThis)
    7380        return VERR_NO_MEMORY;
    7481
     
    7683     * Initialize & return.
    7784     */
    78     pSpinlockInt->u32Magic = RTSPINLOCK_MAGIC;
    79     mutex_init(&pSpinlockInt->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
    80     *pSpinlock = pSpinlockInt;
     85    pThis->u32Magic = RTSPINLOCK_MAGIC;
     86    mutex_init(&pThis->Mtx, "IPRT Spinlock", MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
     87    *pSpinlock = pThis;
    8188    return VINF_SUCCESS;
    8289}
     
    8996     */
    9097    RT_ASSERT_INTS_ON();
    91     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    92     if (!pSpinlockInt)
     98    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     99    if (!pThis)
    93100        return VERR_INVALID_PARAMETER;
    94     AssertMsgReturn(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC,
    95                     ("Invalid spinlock %p magic=%#x\n", pSpinlockInt, pSpinlockInt->u32Magic),
     101    AssertMsgReturn(pThis->u32Magic == RTSPINLOCK_MAGIC,
     102                    ("Invalid spinlock %p magic=%#x\n", pThis, pThis->u32Magic),
    96103                    VERR_INVALID_PARAMETER);
    97104
     
    99106     * Make the lock invalid and release the memory.
    100107     */
    101     ASMAtomicIncU32(&pSpinlockInt->u32Magic);
    102     mutex_destroy(&pSpinlockInt->Mtx);
    103     RTMemFree(pSpinlockInt);
     108    ASMAtomicIncU32(&pThis->u32Magic);
     109    mutex_destroy(&pThis->Mtx);
     110    RTMemFree(pThis);
    104111    return VINF_SUCCESS;
    105112}
     
    108115RTDECL(void) RTSpinlockAcquireNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    109116{
    110     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    111     AssertPtr(pSpinlockInt);
    112     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
     117    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     118    RT_ASSERT_PREEMPT_CPUID_VAR();
     119
     120    AssertPtr(pThis);
     121    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
    113122
    114123    pTmp->uFlags = ASMIntDisableFlags();
    115     {
    116         RT_ASSERT_PREEMPT_CPUID_VAR();
    117         mutex_enter(&pSpinlockInt->Mtx);
    118         RT_ASSERT_PREEMPT_CPUID();
    119     }
     124    mutex_enter(&pThis->Mtx);
     125
     126    RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED()
    120127}
    121128
     
    123130RTDECL(void) RTSpinlockReleaseNoInts(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    124131{
    125     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    126     AssertPtr(pSpinlockInt);
    127     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
     132    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     133    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
    128134
    129     mutex_exit(&pSpinlockInt->Mtx);
     135    AssertPtr(pThis);
     136    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
     137    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
     138    NOREF(pTmp);
     139
     140    mutex_exit(&pThis->Mtx);
    130141    ASMSetFlags(pTmp->uFlags);
     142
     143    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASED();
    131144}
    132145
     
    134147RTDECL(void) RTSpinlockAcquire(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    135148{
    136     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
     149    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
    137150    RT_ASSERT_PREEMPT_CPUID_VAR();
    138     AssertPtr(pSpinlockInt);
    139     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
     151    AssertPtr(pThis);
     152    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
    140153    NOREF(pTmp);
    141154
    142     mutex_enter(&pSpinlockInt->Mtx);
     155    mutex_enter(&pThis->Mtx);
    143156
    144 #ifdef RT_MORE_STRICT
    145     {
    146         RTCPUID const idAssertCpuNow = RTMpCpuId(); /* Spinlocks are not preemptible, so we cannot be rescheduled. */
    147         AssertMsg(idAssertCpu == idAssertCpuNow || idAssertCpu == NIL_RTCPUID,  ("%#x, %#x\n", idAssertCpu, idAssertCpuNow));
    148         pTmp->uFlags = idAssertCpuNow;
    149     }
    150 #endif
     157    RT_ASSERT_PREEMPT_CPUID_SPIN_ACQUIRED()
    151158}
    152159
     
    154161RTDECL(void) RTSpinlockRelease(RTSPINLOCK Spinlock, PRTSPINLOCKTMP pTmp)
    155162{
    156     PRTSPINLOCKINTERNAL pSpinlockInt = (PRTSPINLOCKINTERNAL)Spinlock;
    157 #ifdef RT_MORE_STRICT
    158     RTCPUID const idAssertCpu = pTmp->uFlags;
    159     pTmp->uFlags = 0;
    160     RT_ASSERT_PREEMPT_CPUID();
    161 #endif
    162     AssertPtr(pSpinlockInt);
    163     Assert(pSpinlockInt->u32Magic == RTSPINLOCK_MAGIC);
     163    PRTSPINLOCKINTERNAL pThis = (PRTSPINLOCKINTERNAL)Spinlock;
     164    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE_VARS();
     165
     166    AssertPtr(pThis);
     167    Assert(pThis->u32Magic == RTSPINLOCK_MAGIC);
     168    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASE(pThis);
    164169    NOREF(pTmp);
    165170
    166     mutex_exit(&pSpinlockInt->Mtx);
     171    mutex_exit(&pThis->Mtx);
    167172
    168 #ifdef RT_MORE_STRICT
    169     if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
    170         RT_ASSERT_PREEMPT_CPUID();
    171 #endif
     173    RT_ASSERT_PREEMPT_CPUID_SPIN_RELEASED();
    172174}
    173175
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