VirtualBox

Changeset 199 in vbox


Ignore:
Timestamp:
Jan 20, 2007 2:09:02 AM (18 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
17675
Message:

fast mutex sems.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r0drv/darwin/semaphore-r0drv-darwin.cpp

    r1 r199  
    3636*   Structures and Typedefs                                                    *
    3737*******************************************************************************/
    38 #if 0/* GRRR */
    3938#if 0 /** @todo */
    4039/**
     
    5352#endif
    5453
    55 
     54#if 0 /** @todo */
    5655/**
    5756 * Darwin mutex semaphore.
     
    6766/** Magic for the Darwin mutex semaphore structure. (Douglas Adams) */
    6867#define RTSEMMUTEX_MAGIC 0x19520311
     68#endif
    6969
    7070
     
    197197
    198198
     199#if 0 /* need proper timeout lock function! */
    199200RTDECL(int)  RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
    200201{
     
    320321}
    321322
     323#endif /* later */
     324
    322325
    323326
     
    325328RTDECL(int)  RTSemFastMutexCreate(PRTSEMFASTMUTEX pMutexSem)
    326329{
    327     /*
    328      * Allocate.
    329      */
    330     PRTSEMFASTMUTEXINTERNAL pFastInt;
    331     Assert(sizeof(*pFastInt) > sizeof(void *));
    332     pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
    333     if (!pFastInt)
    334         return VERR_NO_MEMORY;
    335 
    336     /*
    337      * Initialize.
    338      */
    339     pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
    340     ExInitializeFastMutex(&pFastInt->Mutex);
    341     *pMutexSem = pFastInt;
    342     return VINF_SUCCESS;
     330    AssertCompile(sizeof(RTSEMFASTMUTEXINTERNAL) > sizeof(void *));
     331    AssertPtrReturn(VALID_PTR(pMutexSem), VERR_INVALID_POINTER);
     332
     333    PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)RTMemAlloc(sizeof(*pFastInt));
     334    if (pFastInt)
     335    {
     336        pFastInt->u32Magic = RTSEMFASTMUTEX_MAGIC;
     337        Assert(g_pDarwinLockGroup);
     338        pFastInt->pMtx = lck_mtx_alloc_init(g_pDarwinLockGroup, LCK_ATTR_NULL);
     339        if (pFastInt->pMtx)
     340        {
     341            *pMutexSem = pFastInt;
     342            return VINF_SUCCESS;
     343        }
     344
     345        RTMemFree(pFastInt);
     346    }
     347    return VERR_NO_MEMORY;
    343348}
    344349
     
    346351RTDECL(int)  RTSemFastMutexDestroy(RTSEMFASTMUTEX MutexSem)
    347352{
    348     /*
    349      * Validate.
    350      */
     353    if (MutexSem == NIL_RTSEMFASTMUTEX) /* don't bitch */
     354        return VERR_INVALID_PARAMETER;
    351355    PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
    352     if (!pFastInt)
    353         return VERR_INVALID_PARAMETER;
    354     if (pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
    355     {
    356         AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt->u32Magic, pFastInt));
    357         return VERR_INVALID_PARAMETER;
    358     }
    359 
    360     ASMAtomicIncU32(&pFastInt->u32Magic);
     356    AssertPtrReturn(VALID_PTR(pFastInt), VERR_INVALID_POINTER);
     357    AssertMsgReturn(pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
     358                    ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", pFastInt->u32Magic, pFastInt),
     359                    VERR_INVALID_PARAMETER);
     360
     361    ASMAtomicIncU32(&pFastInt->u32Magic); /* make the handle invalid. */
     362    Assert(g_pDarwinLockGroup);
     363    lck_mtx_free(pFastInt->pMtx, g_pDarwinLockGroup);
     364    pFastInt->pMtx = NULL;
    361365    RTMemFree(pFastInt);
     366
    362367    return VINF_SUCCESS;
    363368}
     
    366371RTDECL(int)  RTSemFastMutexRequest(RTSEMFASTMUTEX MutexSem)
    367372{
    368     /*
    369      * Validate.
    370      */
    371373    PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
    372     if (    !pFastInt
    373         ||  pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
    374     {
    375         AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt ? pFastInt->u32Magic : 0, pFastInt));
    376         return VERR_INVALID_PARAMETER;
    377     }
    378 
    379     ExAcquireFastMutex(&pFastInt->Mutex);
     374    AssertMsgReturn(VALID_PTR(pFastInt) && pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
     375                    ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", VALID_PTR(pFastInt) ? pFastInt->u32Magic : 0, pFastInt),
     376                    VERR_INVALID_PARAMETER);
     377    lck_mtx_lock(pFastInt->pMtx);
    380378    return VINF_SUCCESS;
    381379}
     
    384382RTDECL(int)  RTSemFastMutexRelease(RTSEMFASTMUTEX MutexSem)
    385383{
    386     /*
    387      * Validate.
    388      */
    389384    PRTSEMFASTMUTEXINTERNAL pFastInt = (PRTSEMFASTMUTEXINTERNAL)MutexSem;
    390     if (    !pFastInt
    391         ||  pFastInt->u32Magic != RTSEMFASTMUTEX_MAGIC)
    392     {
    393         AssertMsgFailed(("pFastInt->u32Magic=%RX32 pMutexInt=%p\n", pFastInt ? pFastInt->u32Magic : 0, pFastInt));
    394         return VERR_INVALID_PARAMETER;
    395     }
    396 
    397     ExReleaseFastMutex(&pFastInt->Mutex);
    398     return VINF_SUCCESS;
    399 }
    400 
    401 
    402 #endif
     385    AssertMsgReturn(VALID_PTR(pFastInt) && pFastInt->u32Magic == RTSEMFASTMUTEX_MAGIC,
     386                    ("pFastInt->u32Magic=%RX32 pFastInt=%p\n", VALID_PTR(pFastInt) ? pFastInt->u32Magic : 0, pFastInt),
     387                    VERR_INVALID_PARAMETER);
     388    lck_mtx_unlock(pFastInt->pMtx);
     389    return VINF_SUCCESS;
     390}
     391
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