VirtualBox

Ignore:
Timestamp:
Jan 11, 2010 1:24:09 PM (15 years ago)
Author:
vboxsync
Message:

iprt: Added RTSemEventCreateEx and did some cleanups of the RTSemEventDestroy behavior wrt NIL handles.

File:
1 edited

Legend:

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

    r24956 r25717  
    6262
    6363
    64 RTDECL(int)  RTSemEventCreate(PRTSEMEVENT pEventSem)
    65 {
    66     PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pEventInt));
    67     if (pEventInt)
    68     {
    69         pEventInt->u32Magic = RTSEMEVENT_MAGIC;
    70         pEventInt->fState   = 0;
    71         init_waitqueue_head(&pEventInt->Head);
    72         *pEventSem = pEventInt;
     64RTDECL(int)  RTSemEventCreate(PRTSEMEVENT phEventSem)
     65{
     66    return RTSemEventCreateEx(phEventSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, NULL);
     67}
     68
     69
     70RTDECL(int)  RTSemEventCreateEx(PRTSEMEVENT phEventSem, uint32_t fFlags, RTLOCKVALCLASS hClass, const char *pszNameFmt, ...)
     71{
     72    PRTSEMEVENTINTERNAL pThis;
     73
     74    AssertReturn(!(fFlags & ~RTSEMEVENT_FLAGS_NO_LOCK_VAL), VERR_INVALID_PARAMETER);
     75    pThis = (PRTSEMEVENTINTERNAL)RTMemAlloc(sizeof(*pThis));
     76    if (!pThis)
     77        return VERR_NO_MEMORY;
     78
     79    pThis->u32Magic = RTSEMEVENT_MAGIC;
     80    pThis->fState   = 0;
     81    init_waitqueue_head(&pThis->Head);
     82
     83    *phEventSem = pThis;
     84    return VINF_SUCCESS;
     85}
     86RT_EXPORT_SYMBOL(RTSemEventCreate);
     87
     88
     89RTDECL(int)  RTSemEventDestroy(RTSEMEVENT hEventSem)
     90{
     91    /*
     92     * Validate input.
     93     */
     94    PRTSEMEVENTINTERNAL pThis = hEventSem;
     95    if (pThis == NIL_RTSEMEVENT)
    7396        return VINF_SUCCESS;
    74     }
    75     return VERR_NO_MEMORY;
    76 }
    77 RT_EXPORT_SYMBOL(RTSemEventCreate);
    78 
    79 
    80 RTDECL(int)  RTSemEventDestroy(RTSEMEVENT EventSem)
     97    AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
     98
     99    /*
     100     * Invalidate it and signal the object just in case.
     101     */
     102    ASMAtomicWriteU32(&pThis->u32Magic, ~RTSEMEVENT_MAGIC);
     103    ASMAtomicWriteU32(&pThis->fState, 0);
     104    Assert(!waitqueue_active(&pThis->Head));
     105    wake_up_all(&pThis->Head);
     106    RTMemFree(pThis);
     107    return VINF_SUCCESS;
     108}
     109RT_EXPORT_SYMBOL(RTSemEventDestroy);
     110
     111
     112RTDECL(int)  RTSemEventSignal(RTSEMEVENT hEventSem)
    81113{
    82114    /*
    83115     * Validate input.
    84116     */
    85     PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
    86     if (!pEventInt)
    87         return VERR_INVALID_PARAMETER;
    88     if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
    89     {
    90         AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt->u32Magic, pEventInt));
    91         return VERR_INVALID_PARAMETER;
    92     }
    93 
    94     /*
    95      * Invalidate it and signal the object just in case.
    96      */
    97     ASMAtomicWriteU32(&pEventInt->u32Magic, ~RTSEMEVENT_MAGIC);
    98     ASMAtomicWriteU32(&pEventInt->fState, 0);
    99     Assert(!waitqueue_active(&pEventInt->Head));
    100     wake_up_all(&pEventInt->Head);
    101     RTMemFree(pEventInt);
    102     return VINF_SUCCESS;
    103 }
    104 RT_EXPORT_SYMBOL(RTSemEventDestroy);
    105 
    106 
    107 RTDECL(int)  RTSemEventSignal(RTSEMEVENT EventSem)
    108 {
    109     /*
    110      * Validate input.
    111      */
    112     PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
    113     if (!pEventInt)
    114         return VERR_INVALID_PARAMETER;
    115     if (    !pEventInt
    116         ||  pEventInt->u32Magic != RTSEMEVENT_MAGIC)
    117     {
    118         AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
    119         return VERR_INVALID_PARAMETER;
    120     }
     117    PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
     118    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     119    AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
    121120
    122121    /*
    123122     * Signal the event object.
    124123     */
    125     ASMAtomicWriteU32(&pEventInt->fState, 1);
    126     wake_up(&pEventInt->Head);
     124    ASMAtomicWriteU32(&pThis->fState, 1);
     125    wake_up(&pThis->Head);
    127126
    128127    return VINF_SUCCESS;
     
    135134 *
    136135 * @returns VBox status code.
    137  * @param   pEventInt           The event semaphore.
     136 * @param   pThis           The event semaphore.
    138137 * @param   cMillies            The number of milliseconds to wait.
    139138 * @param   fInterruptible      Whether it's an interruptible wait or not.
    140139 */
    141 static int rtSemEventWait(PRTSEMEVENTINTERNAL pEventInt, unsigned cMillies, bool fInterruptible)
     140static int rtSemEventWait(PRTSEMEVENTINTERNAL pThis, unsigned cMillies, bool fInterruptible)
    142141{
    143142    /*
     
    148147    long    lTimeout = cMillies == RT_INDEFINITE_WAIT ? MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(cMillies);
    149148#ifdef IPRT_DEBUG_SEMS
    150     snprintf(current->comm, TASK_COMM_LEN, "e%lx", IPRT_DEBUG_SEMS_ADDRESS(pEventInt));
     149    snprintf(current->comm, TASK_COMM_LEN, "e%lx", IPRT_DEBUG_SEMS_ADDRESS(pThis));
    151150#endif
    152151    for (;;)
    153152    {
    154153        /* make everything thru schedule_timeout() atomic scheduling wise. */
    155         prepare_to_wait(&pEventInt->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
     154        prepare_to_wait(&pThis->Head, &Wait, fInterruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
    156155
    157156        /* check the condition. */
    158         if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
     157        if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
    159158            break;
    160159
     
    172171
    173172        /* Check if someone destroyed the semaphore while we were waiting. */
    174         if (pEventInt->u32Magic != RTSEMEVENT_MAGIC)
     173        if (pThis->u32Magic != RTSEMEVENT_MAGIC)
    175174        {
    176175            rc = VERR_SEM_DESTROYED;
     
    186185    }
    187186
    188     finish_wait(&pEventInt->Head, &Wait);
     187    finish_wait(&pThis->Head, &Wait);
    189188#ifdef IPRT_DEBUG_SEMS
    190     snprintf(current->comm, TASK_COMM_LEN, "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pEventInt), rc);
     189    snprintf(current->comm, TASK_COMM_LEN, "e%lx:%d", IPRT_DEBUG_SEMS_ADDRESS(pThis), rc);
    191190#endif
    192191    return rc;
     
    194193
    195194
    196 RTDECL(int) RTSemEventWait(RTSEMEVENT EventSem, unsigned cMillies)
    197 {
    198     PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
    199     if (!pEventInt)
    200         return VERR_INVALID_PARAMETER;
    201     if (    !pEventInt
    202         ||  pEventInt->u32Magic != RTSEMEVENT_MAGIC)
    203     {
    204         AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
    205         return VERR_INVALID_PARAMETER;
    206     }
    207 
    208     if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
     195RTDECL(int) RTSemEventWait(RTSEMEVENT hEventSem, unsigned cMillies)
     196{
     197    PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
     198    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     199    AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
     200
     201    if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
    209202        return VINF_SUCCESS;
    210     return rtSemEventWait(pEventInt, cMillies, false /* fInterruptible */);
     203    return rtSemEventWait(pThis, cMillies, false /* fInterruptible */);
    211204}
    212205RT_EXPORT_SYMBOL(RTSemEventWait);
    213206
    214207
    215 RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT EventSem, unsigned cMillies)
    216 {
    217     PRTSEMEVENTINTERNAL pEventInt = (PRTSEMEVENTINTERNAL)EventSem;
    218     if (!pEventInt)
    219         return VERR_INVALID_PARAMETER;
    220     if (    !pEventInt
    221         ||  pEventInt->u32Magic != RTSEMEVENT_MAGIC)
    222     {
    223         AssertMsgFailed(("pEventInt->u32Magic=%RX32 pEventInt=%p\n", pEventInt ? pEventInt->u32Magic : 0, pEventInt));
    224         return VERR_INVALID_PARAMETER;
    225     }
    226 
    227     if (ASMAtomicCmpXchgU32(&pEventInt->fState, 0, 1))
     208RTDECL(int) RTSemEventWaitNoResume(RTSEMEVENT hEventSem, unsigned cMillies)
     209{
     210    PRTSEMEVENTINTERNAL pThis = (PRTSEMEVENTINTERNAL)hEventSem;
     211    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     212    AssertMsgReturn(pThis->u32Magic == RTSEMEVENT_MAGIC, ("pThis->u32Magic=%RX32 pThis=%p\n", pThis->u32Magic, pThis), VERR_INVALID_HANDLE);
     213
     214    if (ASMAtomicCmpXchgU32(&pThis->fState, 0, 1))
    228215        return VINF_SUCCESS;
    229     return rtSemEventWait(pEventInt, cMillies, true /* fInterruptible */);
     216    return rtSemEventWait(pThis, cMillies, true /* fInterruptible */);
    230217}
    231218RT_EXPORT_SYMBOL(RTSemEventWaitNoResume);
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