- Timestamp:
- Mar 29, 2010 9:42:20 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/socket.cpp
r27755 r27772 121 121 /** Magic number (RTTCPSOCKET_MAGIC). */ 122 122 uint32_t u32Magic; 123 /** Usage bit. This is used to prevent two threads from accessing the123 /** Usage count. This is used to prevent two threads from accessing the 124 124 * handle concurrently. */ 125 125 uint32_t volatile cUsers; 126 /* Number of references to this object */127 int32_t volatile cRefs;128 126 /** The native socket handle. */ 129 127 RTSOCKETNATIVE hNative; … … 214 212 215 213 /** 216 * Increase reference counter of socket. This is orthogonal to usage bit,217 * as some users of socket may not really modify its state (such as RTSocketSelectOne())218 * or be allowed to perform operation disregarding if socket is in use (such as RTSocketShutdown()).219 *220 * Call rtSocketRelease() when done with socket.221 *222 * @returns @c true if object valid, @c false otherwise.223 * @param pThis The socket structure.224 */225 DECLINLINE(bool) rtSocketAddRef(RTSOCKETINT *pThis)226 {227 if (ASMAtomicReadS32(&pThis->cRefs) < 0)228 return false;229 230 ASMAtomicAddS32(&pThis->cRefs, 1);231 return true;232 }233 234 /**235 * Decrease reference counter of socket.236 *237 * @returns @c true if object valid, @c false otherwise.238 * @param pThis The socket structure.239 */240 DECLINLINE(bool) rtSocketRelease(RTSOCKETINT *pThis)241 {242 if (ASMAtomicReadS32(&pThis->cRefs) <= 0)243 return false;244 245 ASMAtomicSubS32(&pThis->cRefs, 1);246 return true;247 }248 249 /**250 * Declare socket dead.251 *252 * @param pThis The socket structure.253 */254 DECLINLINE(void) rtSocketNoMore(RTSOCKETINT *pThis)255 {256 ASMAtomicWriteS32(&pThis->cRefs, -1);257 }258 259 260 /**261 214 * Tries to lock the socket for exclusive usage by the calling thread. 262 215 * … … 268 221 DECLINLINE(bool) rtSocketTryLock(RTSOCKETINT *pThis) 269 222 { 270 bool rv = ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0); 271 if (rv) 272 { 273 rv = rtSocketAddRef(pThis); 274 AssertReturn(rv, false); 275 } 276 277 return rv; 223 return ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0); 278 224 } 279 225 … … 286 232 DECLINLINE(void) rtSocketUnlock(RTSOCKETINT *pThis) 287 233 { 288 bool rv = ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1); 289 Assert(rv); 290 291 if (rv) 292 { 293 rv = rtSocketRelease(pThis); 294 Assert(rv); 295 } 296 } 234 ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1); 235 } 236 297 237 298 238 /** … … 397 337 pThis->hNative = NIL_RTSOCKETNATIVE; 398 338 } 399 400 rtSocketNoMore(pThis);401 339 402 340 return rc; … … 574 512 AssertPtrReturn(pThis, VERR_INVALID_HANDLE); 575 513 AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE); 576 AssertReturn(rtSocket AddRef(pThis), VERR_INVALID_HANDLE);514 AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS); 577 515 578 516 /* … … 602 540 rc = rtSocketError(); 603 541 604 rtSocket Release(pThis);542 rtSocketUnlock(pThis); 605 543 return rc; 606 544 } … … 617 555 AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE); 618 556 AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER); 619 AssertReturn(rtSocketAddRef(pThis), VERR_INVALID_HANDLE);620 557 621 558 /* … … 633 570 rc = rtSocketError(); 634 571 635 rtSocketRelease(pThis);636 572 return rc; 637 573 }
Note:
See TracChangeset
for help on using the changeset viewer.