VirtualBox

Changeset 27549 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Mar 19, 2010 6:26:29 PM (15 years ago)
Author:
vboxsync
Message:

iprt/socket.cpp: relaxed the locking for accept and shutdown.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/socket.cpp

    r27541 r27549  
    124124     *  handle concurrently. */
    125125    uint32_t volatile   cUsers;
    126 #ifdef RT_OS_WINDOWS
    127126    /** The native socket handle. */
    128     SOCKET              hNative;
     127    RTSOCKETNATIVE      hNative;
     128#ifdef RT_OS_WINDOWS
    129129    /** The event semaphore we've associated with the socket handle.
    130130     * This is WSA_INVALID_EVENT if not done. */
     
    138138     * This is ZERO if we're currently not subscribing to anything. */
    139139    uint32_t            fSubscribedEvts;
    140 #else
    141     /** The native socket handle. */
    142     int                 hNative;
    143 #endif
     140#endif /* RT_OS_WINDOWS */
    144141} RTSOCKETINT;
    145142
     
    246243 * @param   hNative         The native handle.
    247244 */
    248 int rtSocketCreateForNative(RTSOCKETINT **ppSocket,
    249 #ifdef RT_OS_WINDOWS
    250                             SOCKET hNative
    251 #else
    252                             int hNative
    253 #endif
    254                             )
     245int rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative)
    255246{
    256247    RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemAlloc(sizeof(*pThis));
     
    286277     * Create the socket.
    287278     */
    288 #ifdef RT_OS_WINDOWS
    289     SOCKET  hNative = socket(iDomain, iType, iProtocol);
    290     if (hNative == INVALID_SOCKET)
     279    RTSOCKETNATIVE hNative = socket(iDomain, iType, iProtocol);
     280    if (hNative == NIL_RTSOCKETNATIVE)
    291281        return rtSocketError();
    292 #else
    293     int     hNative = socket(iDomain, iType, iProtocol);
    294     if (hNative == -1)
    295         return rtSocketError();
    296 #endif
    297282
    298283    /*
     
    333318        pThis->hEvent = WSA_INVALID_EVENT;
    334319    }
    335 
    336     if (pThis->hNative != INVALID_SOCKET)
    337     {
    338         rc = closesocket(pThis->hNative);
    339         if (!rc)
    340             rc = VINF_SUCCESS;
    341         else
     320#endif
     321
     322    if (pThis->hNative != NIL_RTSOCKETNATIVE)
     323    {
     324#ifdef RT_OS_WINDOWS
     325        if (closesocket(pThis->hNative))
     326#else
     327        if (close(pThis->hNative))
     328#endif
    342329        {
    343330            rc = rtSocketError();
    344             AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", pThis->hNative, rc));
     331#ifdef RT_OS_WINDOWS
     332            AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", (uintptr_t)pThis->hNative, rc));
     333#else
     334            AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", pThis->hNative, rc));
     335#endif
    345336        }
    346         pThis->hNative = INVALID_SOCKET;
    347     }
    348 
    349 #else
    350     if (pThis->hNative != -1)
    351     {
    352         if (close(pThis->hNative))
    353         {
    354             rc = rtSocketError();
    355             AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", pThis->hNative, rc));
    356         }
    357         pThis->hNative = -1;
    358     }
    359 #endif
     337        pThis->hNative = NIL_RTSOCKETNATIVE;
     338    }
    360339
    361340    return rc;
     
    569548{
    570549    /*
    571      * Validate input.
     550     * Validate input, don't lock it because we might want to interrupt a call
     551     * active on a different thread.
    572552     */
    573553    RTSOCKETINT *pThis = hSocket;
     
    575555    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
    576556    AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
    577     //AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
    578557
    579558    /*
     
    591570        rc = rtSocketError();
    592571
    593     rtSocketUnlock(pThis);
    594572    return rc;
    595573}
     
    770748    /*
    771749     * Validate input.
    772      */
    773     RTSOCKETINT *pThis = hSocket;
    774     AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    775     AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
     750     * Only lock the socket temporarily while we get the native handle, so that
     751     * we can safely shutdown and destroy the socket from a different thread.
     752     */
     753    RTSOCKETINT *pThis = hSocket;
     754    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     755    AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
     756
    776757    AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
     758
     759    rtSocketUnlock(pThis);
     760
    777761
    778762    /*
     
    783767#ifdef RT_OS_WINDOWS
    784768    int         cbAddr  = (int)*pcbAddr;
    785     SOCKET      hNative = accept(pThis->hNative, pAddr, &cbAddr);
    786     if (hNative != INVALID_SOCKET)
    787769#else
    788770    socklen_t   cbAddr  = *pcbAddr;
    789     int         hNative = accept(pThis->hNative, pAddr, &cbAddr);
    790     if (hNative != -1)
    791 #endif
     771#endif
     772    RTSOCKETNATIVE hNative = accept(pThis->hNative, pAddr, &cbAddr);
     773    if (hNative != NIL_RTSOCKETNATIVE)
    792774    {
    793775        *pcbAddr = cbAddr;
     
    808790    else
    809791        rc = rtSocketError();
    810 
    811     rtSocketUnlock(pThis);
    812792    return rc;
    813793}
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