OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/test/channel_transport/udp_socket2_manager_win.h" | 11 #include "webrtc/test/channel_transport/udp_socket2_manager_win.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 | 15 |
| 16 #include "webrtc/base/atomicops.h" |
16 #include "webrtc/system_wrappers/interface/aligned_malloc.h" | 17 #include "webrtc/system_wrappers/interface/aligned_malloc.h" |
17 #include "webrtc/test/channel_transport/udp_socket2_win.h" | 18 #include "webrtc/test/channel_transport/udp_socket2_win.h" |
18 | 19 |
19 namespace webrtc { | 20 namespace webrtc { |
20 namespace test { | 21 namespace test { |
21 | 22 |
22 uint32_t UdpSocket2ManagerWindows::_numOfActiveManagers = 0; | 23 uint32_t UdpSocket2ManagerWindows::_numOfActiveManagers = 0; |
23 bool UdpSocket2ManagerWindows::_wsaInit = false; | 24 bool UdpSocket2ManagerWindows::_wsaInit = false; |
24 | 25 |
25 UdpSocket2ManagerWindows::UdpSocket2ManagerWindows() | 26 UdpSocket2ManagerWindows::UdpSocket2ManagerWindows() |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 : _pListHead(NULL), | 412 : _pListHead(NULL), |
412 _init(false), | 413 _init(false), |
413 _size(0), | 414 _size(0), |
414 _inUse(0) | 415 _inUse(0) |
415 { | 416 { |
416 } | 417 } |
417 | 418 |
418 IoContextPool::~IoContextPool() | 419 IoContextPool::~IoContextPool() |
419 { | 420 { |
420 Free(); | 421 Free(); |
421 assert(_size.Value() == 0); | 422 assert(rtc::AtomicOps::AcquireLoad(&_size) == 0); |
422 AlignedFree(_pListHead); | 423 AlignedFree(_pListHead); |
423 } | 424 } |
424 | 425 |
425 int32_t IoContextPool::Init(uint32_t /*increaseSize*/) | 426 int32_t IoContextPool::Init(uint32_t /*increaseSize*/) |
426 { | 427 { |
427 if(_init) | 428 if(_init) |
428 { | 429 { |
429 return 0; | 430 return 0; |
430 } | 431 } |
431 | 432 |
(...skipping 22 matching lines...) Expand all Loading... |
454 AlignedMalloc( | 455 AlignedMalloc( |
455 sizeof(IoContextPoolItem), | 456 sizeof(IoContextPoolItem), |
456 MEMORY_ALLOCATION_ALIGNMENT); | 457 MEMORY_ALLOCATION_ALIGNMENT); |
457 if(item == NULL) | 458 if(item == NULL) |
458 { | 459 { |
459 return NULL; | 460 return NULL; |
460 } | 461 } |
461 memset(&item->payload.ioContext,0,sizeof(PerIoContext)); | 462 memset(&item->payload.ioContext,0,sizeof(PerIoContext)); |
462 item->payload.base = item; | 463 item->payload.base = item; |
463 pListEntry = &(item->itemEntry); | 464 pListEntry = &(item->itemEntry); |
464 ++_size; | 465 rtc::AtomicOps::Increment(&_size); |
465 } | 466 } |
466 ++_inUse; | 467 rtc::AtomicOps::Increment(&_inUse); |
467 return &((IoContextPoolItem*)pListEntry)->payload.ioContext; | 468 return &((IoContextPoolItem*)pListEntry)->payload.ioContext; |
468 } | 469 } |
469 | 470 |
470 int32_t IoContextPool::PushIoContext(PerIoContext* pIoContext) | 471 int32_t IoContextPool::PushIoContext(PerIoContext* pIoContext) |
471 { | 472 { |
472 // TODO (hellner): Overlapped IO should be completed at this point. Perhaps | 473 // TODO (hellner): Overlapped IO should be completed at this point. Perhaps |
473 // add an assert? | 474 // add an assert? |
474 const bool overlappedIOCompleted = HasOverlappedIoCompleted( | 475 const bool overlappedIOCompleted = HasOverlappedIoCompleted( |
475 (LPOVERLAPPED)pIoContext); | 476 (LPOVERLAPPED)pIoContext); |
476 | 477 |
477 IoContextPoolItem* item = ((IoContextPoolItemPayload*)pIoContext)->base; | 478 IoContextPoolItem* item = ((IoContextPoolItemPayload*)pIoContext)->base; |
478 | 479 |
479 const int32_t usedItems = --_inUse; | 480 const int32_t usedItems = rtc::AtomicOps::Decrement(&_inUse); |
480 const int32_t totalItems = _size.Value(); | 481 const int32_t totalItems = rtc::AtomicOps::AcquireLoad(&_size); |
481 const int32_t freeItems = totalItems - usedItems; | 482 const int32_t freeItems = totalItems - usedItems; |
482 if(freeItems < 0) | 483 if(freeItems < 0) |
483 { | 484 { |
484 assert(false); | 485 assert(false); |
485 AlignedFree(item); | 486 AlignedFree(item); |
486 return -1; | 487 return -1; |
487 } | 488 } |
488 if((freeItems >= totalItems>>1) && | 489 if((freeItems >= totalItems>>1) && |
489 overlappedIOCompleted) | 490 overlappedIOCompleted) |
490 { | 491 { |
491 AlignedFree(item); | 492 AlignedFree(item); |
492 --_size; | 493 rtc::AtomicOps::Decrement(&_size); |
493 return 0; | 494 return 0; |
494 } | 495 } |
495 InterlockedPushEntrySList(_pListHead, &(item->itemEntry)); | 496 InterlockedPushEntrySList(_pListHead, &(item->itemEntry)); |
496 return 0; | 497 return 0; |
497 } | 498 } |
498 | 499 |
499 int32_t IoContextPool::Free() | 500 int32_t IoContextPool::Free() |
500 { | 501 { |
501 if(!_init) | 502 if(!_init) |
502 { | 503 { |
503 return 0; | 504 return 0; |
504 } | 505 } |
505 | 506 |
506 int32_t itemsFreed = 0; | 507 int32_t itemsFreed = 0; |
507 PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(_pListHead); | 508 PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(_pListHead); |
508 while(pListEntry != NULL) | 509 while(pListEntry != NULL) |
509 { | 510 { |
510 IoContextPoolItem* item = ((IoContextPoolItem*)pListEntry); | 511 IoContextPoolItem* item = ((IoContextPoolItem*)pListEntry); |
511 AlignedFree(item); | 512 AlignedFree(item); |
512 --_size; | 513 rtc::AtomicOps::Decrement(&_size); |
513 itemsFreed++; | 514 itemsFreed++; |
514 pListEntry = InterlockedPopEntrySList(_pListHead); | 515 pListEntry = InterlockedPopEntrySList(_pListHead); |
515 } | 516 } |
516 return itemsFreed; | 517 return itemsFreed; |
517 } | 518 } |
518 | 519 |
519 int32_t UdpSocket2WorkerWindows::_numOfWorkers = 0; | 520 int32_t UdpSocket2WorkerWindows::_numOfWorkers = 0; |
520 | 521 |
521 UdpSocket2WorkerWindows::UdpSocket2WorkerWindows(HANDLE ioCompletionHandle) | 522 UdpSocket2WorkerWindows::UdpSocket2WorkerWindows(HANDLE ioCompletionHandle) |
522 : _ioCompletionHandle(ioCompletionHandle), | 523 : _ioCompletionHandle(ioCompletionHandle), |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 _workerNumber); | 605 _workerNumber); |
605 return true; | 606 return true; |
606 } | 607 } |
607 pIOContext = (PerIoContext*)pOverlapped; | 608 pIOContext = (PerIoContext*)pOverlapped; |
608 pSocket->IOCompleted(pIOContext,ioSize,error); | 609 pSocket->IOCompleted(pIOContext,ioSize,error); |
609 return true; | 610 return true; |
610 } | 611 } |
611 | 612 |
612 } // namespace test | 613 } // namespace test |
613 } // namespace webrtc | 614 } // namespace webrtc |
OLD | NEW |