VirtualBox

Ignore:
Timestamp:
Dec 10, 2019 1:46:40 AM (5 years ago)
Author:
vboxsync
Message:

SharedClipboardSvc,Vbgl: Worked over the host messages, taking locks when needed, not using C++ lists and a bunch of other simplifications. bugref:9437

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp

    r82526 r82527  
    8989}
    9090
     91#if 0 /* currently not used */
    9192/**
    9293 * Creates (initializes) an event.
     
    9495 * @returns VBox status code.
    9596 * @param   pEvent              Event to initialize.
    96  * @param   uID                 Event ID to use.
    97  */
    98 int ShClEventCreate(PSHCLEVENT pEvent, SHCLEVENTID uID)
     97 * @param   idEvent             Event ID to use.
     98 */
     99static int shClEventInit(PSHCLEVENT pEvent, SHCLEVENTID idEvent)
    99100{
    100101    AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
    101102
    102     LogFlowFunc(("Event %RU32\n", uID));
    103 
    104     int rc = RTSemEventCreate(&pEvent->hEventSem);
     103    LogFlowFunc(("Event %RU32\n", idEvent));
     104
     105    int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
    105106    if (RT_SUCCESS(rc))
    106107    {
    107         pEvent->uID      = uID;
     108        pEvent->idEvent  = idEvent;
    108109        pEvent->pPayload = NULL;
    109110    }
     
    111112    return rc;
    112113}
    113 
    114 /**
    115  * Destroys an event.
     114#endif
     115
     116/**
     117 * Destroys an event, but doesn't free the memory.
    116118 *
    117119 * @param   pEvent              Event to destroy.
    118120 */
    119 void ShClEventDestroy(PSHCLEVENT pEvent)
     121static void shClEventTerm(PSHCLEVENT pEvent)
    120122{
    121123    if (!pEvent)
    122124        return;
    123125
    124     LogFlowFunc(("Event %RU32\n", pEvent->uID));
    125 
    126     if (pEvent->hEventSem != NIL_RTSEMEVENT)
    127     {
    128         RTSemEventDestroy(pEvent->hEventSem);
    129         pEvent->hEventSem = NIL_RTSEMEVENT;
     126    LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
     127
     128    if (pEvent->hEvtMulSem != NIL_RTSEMEVENT)
     129    {
     130        RTSemEventMultiDestroy(pEvent->hEvtMulSem);
     131        pEvent->hEvtMulSem = NIL_RTSEMEVENT;
    130132    }
    131133
    132134    ShClPayloadFree(pEvent->pPayload);
    133135
    134     pEvent->uID = 0;
     136    pEvent->idEvent = 0;
    135137}
    136138
     
    151153    pSource->uID          = uID;
    152154    /* Choose a random event ID starting point. */
    153     pSource->uEventIDNext = RTRandU32() % VBOX_SHCL_MAX_EVENTS;
     155    pSource->idNextEvent  = RTRandU32Ex(1, VBOX_SHCL_MAX_EVENTS - 1);
    154156
    155157    LogFlowFuncLeaveRC(VINF_SUCCESS);
     
    172174
    173175    pSource->uID          = UINT16_MAX;
    174     pSource->uEventIDNext = UINT32_MAX;
     176    pSource->idNextEvent = UINT32_MAX;
    175177}
    176178
     
    190192        RTListNodeRemove(&pEvIt->Node);
    191193
    192         ShClEventDestroy(pEvIt);
     194        shClEventTerm(pEvIt);
    193195
    194196        RTMemFree(pEvIt);
    195197        pEvIt = NULL;
    196198    }
    197 }
    198 
    199 /**
    200  * Generates a new event ID for a specific event source.
    201  *
    202  * @returns New event ID generated, or 0 on error.
    203  * @param   pSource             Event source to generate event for.
    204  */
    205 SHCLEVENTID ShClEventIDGenerate(PSHCLEVENTSOURCE pSource)
    206 {
    207     AssertPtrReturn(pSource, 0);
    208 
    209     LogFlowFunc(("uSource=%RU16: New event: %RU32\n", pSource->uID, pSource->uEventIDNext));
    210 
    211     pSource->uEventIDNext++;
    212     if (pSource->uEventIDNext == VBOX_SHCL_MAX_EVENTS)
    213         pSource->uEventIDNext = 0;
    214 
    215     return pSource->uEventIDNext;
    216199}
    217200
     
    223206 * @param   uID                 Event ID to get.
    224207 */
    225 inline PSHCLEVENT shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID uID)
    226 {
    227     PSHCLEVENT pEvIt;
    228     RTListForEach(&pSource->lstEvents, pEvIt, SHCLEVENT, Node)
    229     {
    230         if (pEvIt->uID == uID)
    231             return pEvIt;
     208DECLINLINE(PSHCLEVENT) shclEventGet(PSHCLEVENTSOURCE pSource, SHCLEVENTID idEvent)
     209{
     210    PSHCLEVENT pEvent;
     211    RTListForEach(&pSource->lstEvents, pEvent, SHCLEVENT, Node)
     212    {
     213        if (pEvent->idEvent == idEvent)
     214            return pEvent;
    232215    }
    233216
    234217    return NULL;
     218}
     219
     220/**
     221 * Generates a new event ID for a specific event source.
     222 *
     223 * @returns New event ID generated, or 0 on error.
     224 * @param   pSource             Event source to generate event for.
     225 * @deprecated as this does not deal with duplicates.
     226 */
     227SHCLEVENTID ShClEventIDGenerate(PSHCLEVENTSOURCE pSource)
     228{
     229    AssertPtrReturn(pSource, 0);
     230
     231    SHCLEVENTID idEvent = ++pSource->idNextEvent;
     232    if (idEvent >= VBOX_SHCL_MAX_EVENTS)
     233        pSource->idNextEvent = idEvent = 1;  /* zero == error, remember! */
     234
     235    LogFlowFunc(("uSource=%RU16: New event: %RU32\n", pSource->uID, idEvent));
     236    return idEvent;
     237}
     238
     239/**
     240 * Generates a new event ID for a specific event source and registers it.
     241 *
     242 * @returns New event ID generated, or 0 on error.
     243 * @param   pSource             Event source to generate event for.
     244 */
     245SHCLEVENTID ShClEventIdGenerateAndRegister(PSHCLEVENTSOURCE pSource)
     246{
     247    AssertPtrReturn(pSource, 0);
     248
     249    /*
     250     * Allocate an event.
     251     */
     252    PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
     253    AssertReturn(pEvent, 0);
     254    int rc = RTSemEventMultiCreate(&pEvent->hEvtMulSem);
     255    AssertRCReturnStmt(rc, RTMemFree(pEvent), 0);
     256
     257    /*
     258     * Allocate an unique event ID.
     259     */
     260    for (uint32_t cTries = 0;; cTries++)
     261    {
     262        SHCLEVENTID idEvent = ++pSource->idNextEvent;
     263        if (idEvent < VBOX_SHCL_MAX_EVENTS)
     264        { /* likely */ }
     265        else
     266            pSource->idNextEvent = idEvent = 1; /* zero == error, remember! */
     267
     268        if (shclEventGet(pSource, idEvent) == NULL)
     269        {
     270
     271            pEvent->idEvent = idEvent;
     272            RTListAppend(&pSource->lstEvents, &pEvent->Node);
     273
     274            LogFlowFunc(("uSource=%RU16: New event: %#x\n", pSource->uID, idEvent));
     275            return idEvent;
     276        }
     277
     278        AssertBreak(cTries < 4096);
     279    }
     280
     281    RTMemFree(pEvent);
     282    return 0;
    235283}
    236284
     
    246294    PSHCLEVENT pEvent = RTListGetLast(&pSource->lstEvents, SHCLEVENT, Node);
    247295    if (pEvent)
    248         return pEvent->uID;
     296        return pEvent->idEvent;
    249297
    250298    return 0;
     
    266314}
    267315
     316#if 0 /** @todo fix later */
    268317/**
    269318 * Registers an event.
     
    283332    if (shclEventGet(pSource, uID) == NULL)
    284333    {
    285         PSHCLEVENT pEvent
    286             = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
     334        PSHCLEVENT pEvent = (PSHCLEVENT)RTMemAllocZ(sizeof(SHCLEVENT));
    287335        if (pEvent)
    288336        {
    289             rc = ShClEventCreate(pEvent, uID);
     337            rc = shClEventInit(pEvent, uID);
    290338            if (RT_SUCCESS(rc))
    291339            {
     
    308356    return rc;
    309357}
     358#endif /* later */
    310359
    311360/**
     
    327376    if (pEvent)
    328377    {
    329         LogFlowFunc(("Event %RU32\n", pEvent->uID));
     378        LogFlowFunc(("Event %RU32\n", pEvent->idEvent));
    330379
    331380        RTListNodeRemove(&pEvent->Node);
    332381
    333         ShClEventDestroy(pEvent);
     382        shClEventTerm(pEvent);
    334383
    335384        RTMemFree(pEvent);
     
    368417    if (pEvent)
    369418    {
    370         rc = RTSemEventWait(pEvent->hEventSem, uTimeoutMs);
     419        rc = RTSemEventMultiWait(pEvent->hEvtMulSem, uTimeoutMs);
    371420        if (RT_SUCCESS(rc))
    372421        {
     
    411460        pEvent->pPayload = pPayload;
    412461
    413         rc = RTSemEventSignal(pEvent->hEventSem);
     462        rc = RTSemEventMultiSignal(pEvent->hEvtMulSem);
    414463    }
    415464    else
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