VirtualBox

Changeset 25714 in vbox


Ignore:
Timestamp:
Jan 11, 2010 12:40:15 PM (15 years ago)
Author:
vboxsync
Message:

iprt: Adjuested the remaining RTSemMutex implementations.

Location:
trunk/src/VBox/Runtime
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/Makefile.kmk

    r25666 r25714  
    12961296        r0drv/linux/semeventmulti-r0drv-linux.c \
    12971297        r0drv/linux/semfastmutex-r0drv-linux.c \
     1298        r0drv/linux/semmutex-r0drv-linux.c \
    12981299        r0drv/linux/spinlock-r0drv-linux.c \
    12991300        r0drv/linux/thread-r0drv-linux.c \
  • trunk/src/VBox/Runtime/r0drv/linux/semmutex-r0drv-linux.c

    r25378 r25714  
    6363
    6464
    65 /* Undefine debug mappings. */
    66 #undef RTSemMutexRequest
    67 #undef RTSemMutexRequestNoResume
    68 
    69 
    70 RTDECL(int)  RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
    71 {
    72     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
    73     if (pMutexInt)
    74     {
    75         pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
    76         init_waitqueue_head(&pMutexInt->Head);
    77         *pMutexSem = pMutexInt;
     65
     66RTDECL(int)  RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
     67{
     68    return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
     69}
     70
     71
     72RTDECL(int)  RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
     73                                RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
     74{
     75    PRTSEMMUTEXINTERNAL pThis;
     76
     77    AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
     78
     79    pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
     80    if (pThis)
     81    {
     82        pThis->u32Magic   = RTSEMMUTEX_MAGIC;
     83        pThis->cRecursion = 0;
     84        pThis->pOwner     = NULL;
     85        init_waitqueue_head(&pThis->Head);
     86
     87        *phMutexSem = pThis;
    7888AssertReleaseMsgFailed(("This mutex implementation is buggy, fix it!\n"));
    7989        return VINF_SUCCESS;
     
    8999     * Validate input.
    90100     */
    91     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
    92     if (!pMutexInt)
    93         return VERR_INVALID_PARAMETER;
    94     if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
    95     {
    96         AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt->u32Magic, pMutexInt));
    97         return VERR_INVALID_PARAMETER;
    98     }
     101    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
     102    if (pThis == NIL_RTSEMMUTEX)
     103        return VINF_SUCCESS;
     104    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     105    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
    99106
    100107    /*
    101108     * Invalidate it and signal the object just in case.
    102109     */
    103     ASMAtomicIncU32(&pMutexInt->u32Magic);
    104     ASMAtomicXchgU32(&pMutexInt->cRecursion, 0);
    105     Assert(!waitqueue_active(&pMutexInt->Head));
    106     wake_up_all(&pMutexInt->Head);
    107     RTMemFree(pMutexInt);
     110    AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
     111    ASMAtomicWriteU32(&pThis->cRecursion, 0);
     112    Assert(!waitqueue_active(&pThis->Head));
     113    wake_up_all(&pThis->Head);
     114
     115    RTMemFree(pThis);
    108116    return VINF_SUCCESS;
    109117}
     
    111119
    112120
     121#undef RTSemMutexRequest
    113122RTDECL(int)  RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
    114123{
     124    int                 rc    = VINF_SUCCESS;
     125    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
     126
    115127    /*
    116128     * Validate input.
    117129     */
    118     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
    119     if (!pMutexInt)
    120         return VERR_INVALID_PARAMETER;
    121     if (    !pMutexInt
    122         ||  pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
    123     {
    124         AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
    125         return VERR_INVALID_PARAMETER;
    126     }
     130    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     131    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
    127132
    128133    /*
    129134     * Check for recursive request.
    130135     */
    131     if (pMutexInt->pOwner == current)
    132     {
    133         Assert(pMutexInt->cRecursion < 1000);
    134         ASMAtomicIncU32(&pMutexInt->cRecursion);
     136    if (pThis->pOwner == current)
     137    {
     138        Assert(pThis->cRecursion < 1000);
     139        ASMAtomicIncU32(&pThis->cRecursion);
    135140        return VINF_SUCCESS;
    136141    }
     
    139144     * Try aquire it.
    140145     */
    141     if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
    142     {
    143         ASMAtomicXchgPtr(&pMutexInt->pOwner, current);
    144         return VINF_SUCCESS;
    145     }
     146    if (ASMAtomicCmpXchgU32(&pThis->cRecursion, 1, 0))
     147        ASMAtomicWritePtr(&pThis->pOwner, current);
    146148    else
    147149    {
     
    150152         */
    151153        DEFINE_WAIT(Wait);
    152         int     rc       = VINF_SUCCESS;
    153154        long    lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
    154155        for (;;)
    155156        {
    156157            /* make everything thru schedule() atomic scheduling wise. */
    157             prepare_to_wait(&pMutexInt->Head, &Wait, TASK_INTERRUPTIBLE);
     158            prepare_to_wait(&pThis->Head, &Wait, TASK_INTERRUPTIBLE);
    158159
    159160            /* check the condition. */
    160             if (ASMAtomicCmpXchgU32(&pMutexInt->cRecursion, 1, 0))
    161             {
    162                 ASMAtomicXchgPtr(&pMutexInt->pOwner, current);
     161            if (ASMAtomicCmpXchgU32(&pThis->cRecursion, 1, 0))
     162            {
     163                ASMAtomicWritePtr(&pThis->pOwner, current);
    163164                break;
    164165            }
     
    177178
    178179            /* Check if someone destroyed the semaphore while we was waiting. */
    179             if (pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
     180            if (pThis->u32Magic != RTSEMMUTEX_MAGIC)
    180181            {
    181182                rc = VERR_SEM_DESTROYED;
     
    190191            }
    191192        }
    192         finish_wait(&pMutexInt->Head, &Wait);
    193         return rc;
    194     }
     193        finish_wait(&pThis->Head, &Wait);
     194    }
     195    return rc;
     196}
     197RT_EXPORT_SYMBOL(RTSemMutexRequest);
     198
     199
     200RTDECL(int)  RTSemMutexRelease(RTSEMMUTEX MutexSem)
     201{
     202    /*
     203     * Validate input.
     204     */
     205    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
     206    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     207    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
     208
     209    AssertReturn(pThis->pOwner == current, VERR_NOT_OWNER);
     210
     211    /*
     212     * Release the mutex.
     213     */
     214    if (pThis->cRecursion == 1)
     215    {
     216        ASMAtomicWritePtr(&pThis->pOwner, NULL);
     217        ASMAtomicWriteU32(&pThis->cRecursion, 0);
     218        wake_up(&pThis->Head);
     219    }
     220    else
     221        ASMAtomicDecU32(&pThis->cRecursion);
     222
    195223    return VINF_SUCCESS;
    196224}
    197 RT_EXPORT_SYMBOL(RTSemMutexRequest);
    198 
    199 
    200 RTDECL(int)  RTSemMutexRelease(RTSEMMUTEX MutexSem)
    201 {
    202     /*
    203      * Validate input.
    204      */
    205     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
    206     if (!pMutexInt)
    207         return VERR_INVALID_PARAMETER;
    208     if (    !pMutexInt
    209         ||  pMutexInt->u32Magic != RTSEMMUTEX_MAGIC)
    210     {
    211         AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p\n", pMutexInt ? pMutexInt->u32Magic : 0, pMutexInt));
    212         return VERR_INVALID_PARAMETER;
    213     }
    214     if (pMutexInt->pOwner != current)
    215     {
    216         AssertMsgFailed(("Not owner, pOwner=%p current=%p\n", (void *)pMutexInt->pOwner, (void *)current));
    217         return VERR_NOT_OWNER;
    218     }
    219 
    220     /*
    221      * Release the mutex.
    222      */
    223     if (pMutexInt->cRecursion == 1)
    224     {
    225         ASMAtomicXchgPtr(&pMutexInt->pOwner, NULL);
    226         ASMAtomicXchgU32(&pMutexInt->cRecursion, 0);
    227     }
    228     else
    229         ASMAtomicDecU32(&pMutexInt->cRecursion);
    230 
    231     return VINF_SUCCESS;
    232 }
    233225RT_EXPORT_SYMBOL(RTSemMutexRelease);
    234226
  • trunk/src/VBox/Runtime/r0drv/nt/semmutex-r0drv-nt.cpp

    r25433 r25714  
    55
    66/*
    7  * Copyright (C) 2006-2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    6464
    6565
    66 /* Undefine debug mappings. */
    67 #undef RTSemMutexRequest
    68 #undef RTSemMutexRequestNoResume
    69 
    70 
    71 RTDECL(int) RTSemMutexCreate(PRTSEMMUTEX pMutexSem)
    72 {
    73     Assert(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
    74     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pMutexInt));
    75     if (pMutexInt)
    76     {
    77         pMutexInt->u32Magic = RTSEMMUTEX_MAGIC;
    78 #ifdef RT_USE_FAST_MUTEX
    79         ExInitializeFastMutex(&pMutexInt->Mutex);
     66
     67RTDECL(int)  RTSemMutexCreate(PRTSEMMUTEX phMutexSem)
     68{
     69    return RTSemMutexCreateEx(phMutexSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE, NULL);
     70}
     71
     72
     73RTDECL(int) RTSemMutexCreateEx(PRTSEMMUTEX phMutexSem, uint32_t fFlags,
     74                               RTLOCKVALCLASS hClass, uint32_t uSubClass, const char *pszNameFmt, ...)
     75{
     76    AssertReturn(!(fFlags & ~RTSEMMUTEX_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
     77
     78    AssertCompile(sizeof(RTSEMMUTEXINTERNAL) > sizeof(void *));
     79    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)RTMemAlloc(sizeof(*pThis));
     80    if (!pThis)
     81        return VERR_NO_MEMORY;
     82
     83    pThis->u32Magic = RTSEMMUTEX_MAGIC;
     84#ifdef RT_USE_FAST_MUTEX
     85    ExInitializeFastMutex(&pThis->Mutex);
    8086#else
    81         KeInitializeMutex(&pMutexInt->Mutex, 0);
     87    KeInitializeMutex(&pThis->Mutex, 0);
    8288#endif
    83         *pMutexSem = pMutexInt;
     89
     90    *phMutexSem = pThis;
     91    return VINF_SUCCESS;
     92}
     93
     94
     95RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
     96{
     97    /*
     98     * Validate input.
     99     */
     100    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
     101    if (pThis == NIL_RTSEMMUTEX)
    84102        return VINF_SUCCESS;
    85     }
    86     return VERR_NO_MEMORY;
    87 }
    88 
    89 
    90 RTDECL(int) RTSemMutexDestroy(RTSEMMUTEX MutexSem)
    91 {
    92     /*
    93      * Validate input.
    94      */
    95     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
    96     if (!pMutexInt)
    97         return VERR_INVALID_PARAMETER;
    98     AssertReturn(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
     103    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     104    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
    99105
    100106    /*
    101107     * Invalidate it and signal the object just in case.
    102108     */
    103     AssertReturn(ASMAtomicCmpXchgU32(&pMutexInt->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
    104     RTMemFree(pMutexInt);
     109    AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSEMMUTEX_MAGIC_DEAD, RTSEMMUTEX_MAGIC), VERR_INVALID_HANDLE);
     110    RTMemFree(pThis);
    105111    return VINF_SUCCESS;
    106112}
     
    122128     * Validate input.
    123129     */
    124     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
    125     if (!pMutexInt)
    126         return VERR_INVALID_PARAMETER;
    127     AssertReturn(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
     130    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
     131    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     132    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
    128133
    129134    /*
     
    132137#ifdef RT_USE_FAST_MUTEX
    133138    AssertMsg(cMillies == RT_INDEFINITE_WAIT, ("timeouts are not supported when using fast mutexes!\n"));
    134     ExAcquireFastMutex(&pMutexInt->Mutex);
     139    ExAcquireFastMutex(&pThis->Mutex);
    135140#else  /* !RT_USE_FAST_MUTEX */
    136141    NTSTATUS rcNt;
    137142    if (cMillies == RT_INDEFINITE_WAIT)
    138         rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, fInterruptible, NULL);
     143        rcNt = KeWaitForSingleObject(&pThis->Mutex, Executive, KernelMode, fInterruptible, NULL);
    139144    else
    140145    {
    141146        LARGE_INTEGER Timeout;
    142147        Timeout.QuadPart = -(int64_t)cMillies * 10000;
    143         rcNt = KeWaitForSingleObject(&pMutexInt->Mutex, Executive, KernelMode, fInterruptible, &Timeout);
     148        rcNt = KeWaitForSingleObject(&pThis->Mutex, Executive, KernelMode, fInterruptible, &Timeout);
    144149    }
    145150    switch (rcNt)
    146151    {
    147152        case STATUS_SUCCESS:
    148             if (pMutexInt->u32Magic == RTSEMMUTEX_MAGIC)
     153            if (pThis->u32Magic == RTSEMMUTEX_MAGIC)
    149154                return VINF_SUCCESS;
    150155            return VERR_SEM_DESTROYED;
     
    159164
    160165        default:
    161             AssertMsgFailed(("pMutexInt->u32Magic=%RX32 pMutexInt=%p: wait returned %lx!\n",
    162                              pMutexInt->u32Magic, pMutexInt, (long)rcNt));
     166            AssertMsgFailed(("pThis->u32Magic=%RX32 pThis=%p: wait returned %lx!\n",
     167                             pThis->u32Magic, pThis, (long)rcNt));
    163168            return VERR_INTERNAL_ERROR;
    164169    }
     
    168173
    169174
     175#undef RTSemMutexRequest
    170176RTDECL(int) RTSemMutexRequest(RTSEMMUTEX MutexSem, unsigned cMillies)
    171177{
     
    180186
    181187
     188#undef RTSemMutexRequestNoResume
    182189RTDECL(int) RTSemMutexRequestNoResume(RTSEMMUTEX MutexSem, unsigned cMillies)
    183190{
     
    197204     * Validate input.
    198205     */
    199     PRTSEMMUTEXINTERNAL pMutexInt = (PRTSEMMUTEXINTERNAL)MutexSem;
    200     if (!pMutexInt)
    201         return VERR_INVALID_PARAMETER;
    202     AssertReturn(pMutexInt->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
     206    PRTSEMMUTEXINTERNAL pThis = (PRTSEMMUTEXINTERNAL)MutexSem;
     207    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     208    AssertReturn(pThis->u32Magic == RTSEMMUTEX_MAGIC, VERR_INVALID_HANDLE);
    203209
    204210    /*
     
    206212     */
    207213#ifdef RT_USE_FAST_MUTEX
    208     ExReleaseFastMutex(&pMutexInt->Mutex);
     214    ExReleaseFastMutex(&pThis->Mutex);
    209215#else
    210     KeReleaseMutex(&pMutexInt->Mutex, FALSE /*Wait*/);
     216    KeReleaseMutex(&pThis->Mutex, FALSE /*Wait*/);
    211217#endif
    212218    return VINF_SUCCESS;
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