Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_pump_win.h" | 5 #include "base/message_pump_win.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 | 468 |
| 469 void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { | 469 void MessagePumpForIO::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
| 470 // We know that we can't be blocked right now since this method can only be | 470 // We know that we can't be blocked right now since this method can only be |
| 471 // called on the same thread as Run, so we only need to update our record of | 471 // called on the same thread as Run, so we only need to update our record of |
| 472 // how long to sleep when we do sleep. | 472 // how long to sleep when we do sleep. |
| 473 delayed_work_time_ = delayed_work_time; | 473 delayed_work_time_ = delayed_work_time; |
| 474 } | 474 } |
| 475 | 475 |
| 476 void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle, | 476 void MessagePumpForIO::RegisterIOHandler(HANDLE file_handle, |
| 477 IOHandler* handler) { | 477 IOHandler* handler) { |
| 478 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); | 478 ULONG_PTR key = HandlerToKey(handler, true); |
| 479 HANDLE port = CreateIoCompletionPort(file_handle, port_, key, 1); | 479 HANDLE port = CreateIoCompletionPort(file_handle, port_, key, 1); |
| 480 DPCHECK(port); | 480 DPCHECK(port); |
| 481 } | 481 } |
| 482 | 482 |
| 483 bool MessagePumpForIO::RegisterJobObject(HANDLE job_handle, | |
| 484 IOHandler* handler) { | |
| 485 // Job object notifications use the OVERLAPPED pointer to carry the message | |
| 486 // data. Mark the completion key correspondingly, so we will not try to | |
| 487 // convert OVERLAPPED* to IOContext*. | |
| 488 ULONG_PTR key = HandlerToKey(handler, false); | |
| 489 JOBOBJECT_ASSOCIATE_COMPLETION_PORT info; | |
| 490 info.CompletionKey = reinterpret_cast<void*>(key); | |
| 491 info.CompletionPort = port_; | |
| 492 return SetInformationJobObject(job_handle, | |
| 493 JobObjectAssociateCompletionPortInformation, | |
| 494 &info, | |
| 495 sizeof(info)) != FALSE; | |
| 496 } | |
| 497 | |
| 483 //----------------------------------------------------------------------------- | 498 //----------------------------------------------------------------------------- |
| 484 // MessagePumpForIO private: | 499 // MessagePumpForIO private: |
| 485 | 500 |
| 486 void MessagePumpForIO::DoRunLoop() { | 501 void MessagePumpForIO::DoRunLoop() { |
| 487 for (;;) { | 502 for (;;) { |
| 488 // If we do any work, we may create more messages etc., and more work may | 503 // If we do any work, we may create more messages etc., and more work may |
| 489 // possibly be waiting in another task group. When we (for example) | 504 // possibly be waiting in another task group. When we (for example) |
| 490 // WaitForIOCompletion(), there is a good chance there are still more | 505 // WaitForIOCompletion(), there is a good chance there are still more |
| 491 // messages waiting. On the other hand, when any of these methods return | 506 // messages waiting. On the other hand, when any of these methods return |
| 492 // having done no work, then it is pretty unlikely that calling them | 507 // having done no work, then it is pretty unlikely that calling them |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 539 IOItem item; | 554 IOItem item; |
| 540 if (completed_io_.empty() || !MatchCompletedIOItem(filter, &item)) { | 555 if (completed_io_.empty() || !MatchCompletedIOItem(filter, &item)) { |
| 541 // We have to ask the system for another IO completion. | 556 // We have to ask the system for another IO completion. |
| 542 if (!GetIOItem(timeout, &item)) | 557 if (!GetIOItem(timeout, &item)) |
| 543 return false; | 558 return false; |
| 544 | 559 |
| 545 if (ProcessInternalIOItem(item)) | 560 if (ProcessInternalIOItem(item)) |
| 546 return true; | 561 return true; |
| 547 } | 562 } |
| 548 | 563 |
| 549 if (item.context->handler) { | 564 // Avoid dereferencing |item.context| if the context hasn't been allocated by |
| 565 // operator new(), i.e. when |item.has_valid_io_context| is false. | |
| 566 // |item.context| is not a valid pointer in this case, though it can be | |
| 567 // a valid non-pointer value. | |
| 568 if (!item.has_valid_io_context || item.context->handler) { | |
| 550 if (filter && item.handler != filter) { | 569 if (filter && item.handler != filter) { |
| 551 // Save this item for later | 570 // Save this item for later |
| 552 completed_io_.push_back(item); | 571 completed_io_.push_back(item); |
| 553 } else { | 572 } else { |
| 554 DCHECK_EQ(item.context->handler, item.handler); | 573 DCHECK(!item.has_valid_io_context || |
| 574 (item.context->handler == item.handler)); | |
| 555 WillProcessIOEvent(); | 575 WillProcessIOEvent(); |
| 556 item.handler->OnIOCompleted(item.context, item.bytes_transfered, | 576 item.handler->OnIOCompleted(item.context, item.bytes_transfered, |
| 557 item.error); | 577 item.error); |
| 558 DidProcessIOEvent(); | 578 DidProcessIOEvent(); |
| 559 } | 579 } |
| 560 } else { | 580 } else { |
| 561 // The handler must be gone by now, just cleanup the mess. | 581 // The handler must be gone by now, just cleanup the mess. |
| 562 delete item.context; | 582 delete item.context; |
| 563 } | 583 } |
| 564 return true; | 584 return true; |
| 565 } | 585 } |
| 566 | 586 |
| 567 // Asks the OS for another IO completion result. | 587 // Asks the OS for another IO completion result. |
| 568 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { | 588 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { |
| 569 memset(item, 0, sizeof(*item)); | 589 memset(item, 0, sizeof(*item)); |
| 570 ULONG_PTR key = NULL; | 590 ULONG_PTR key = NULL; |
| 571 OVERLAPPED* overlapped = NULL; | 591 OVERLAPPED* overlapped = NULL; |
| 572 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, | 592 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, |
| 573 &overlapped, timeout)) { | 593 &overlapped, timeout)) { |
| 574 if (!overlapped) | 594 if (!overlapped) |
| 575 return false; // Nothing in the queue. | 595 return false; // Nothing in the queue. |
| 576 item->error = GetLastError(); | 596 item->error = GetLastError(); |
| 577 item->bytes_transfered = 0; | 597 item->bytes_transfered = 0; |
| 578 } | 598 } |
| 579 | 599 |
| 580 item->handler = reinterpret_cast<IOHandler*>(key); | 600 item->handler = KeyToHandler(key, &item->has_valid_io_context); |
| 581 item->context = reinterpret_cast<IOContext*>(overlapped); | 601 item->context = reinterpret_cast<IOContext*>(overlapped); |
| 582 return true; | 602 return true; |
| 583 } | 603 } |
| 584 | 604 |
| 585 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) { | 605 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) { |
| 586 if (this == reinterpret_cast<MessagePumpForIO*>(item.context) && | 606 if (this == reinterpret_cast<MessagePumpForIO*>(item.context) && |
| 587 this == reinterpret_cast<MessagePumpForIO*>(item.handler)) { | 607 this == reinterpret_cast<MessagePumpForIO*>(item.handler)) { |
| 588 // This is our internal completion. | 608 // This is our internal completion. |
| 589 DCHECK(!item.bytes_transfered); | 609 DCHECK(!item.bytes_transfered); |
| 590 InterlockedExchange(&have_work_, 0); | 610 InterlockedExchange(&have_work_, 0); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 616 } | 636 } |
| 617 | 637 |
| 618 void MessagePumpForIO::WillProcessIOEvent() { | 638 void MessagePumpForIO::WillProcessIOEvent() { |
| 619 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 639 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
| 620 } | 640 } |
| 621 | 641 |
| 622 void MessagePumpForIO::DidProcessIOEvent() { | 642 void MessagePumpForIO::DidProcessIOEvent() { |
| 623 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 643 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 624 } | 644 } |
| 625 | 645 |
| 646 // static | |
| 647 ULONG_PTR MessagePumpForIO::HandlerToKey(IOHandler* handler, | |
| 648 bool has_valid_io_context) { | |
| 649 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); | |
| 650 | |
| 651 // |IOHandler| has virtual methods, so it is at least pointer-size aligned. | |
| 652 DCHECK((key & 1) == 0); | |
|
Wez
2012/08/14 01:08:59
nit: This check is for even (16-bit word) alignmen
alexeypa (please no reviews)
2012/08/14 05:43:22
Done.
| |
| 653 | |
| 654 // Mark the completion key as context-less. | |
| 655 if (!has_valid_io_context) | |
| 656 key = key | 1; | |
| 657 return key; | |
| 658 } | |
| 659 | |
| 660 // static | |
| 661 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | |
| 662 ULONG_PTR key, | |
| 663 bool* has_valid_io_context) { | |
| 664 *has_valid_io_context = !(key & 1); | |
|
Wez
2012/08/14 01:08:59
nit: (key & 1) == 0
alexeypa (please no reviews)
2012/08/14 05:43:22
Done.
| |
| 665 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | |
| 666 } | |
| 667 | |
| 626 } // namespace base | 668 } // namespace base |
| OLD | NEW |