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 if (!item.has_valid_io_context || item.context->handler) { |
|
rvargas (doing something else)
2012/08/13 18:54:12
This deserves a comment given that the first condi
alexeypa (please no reviews)
2012/08/13 20:02:23
Done, though both conditions are not redundant. Or
rvargas (doing something else)
2012/08/13 21:20:10
Thanks. What I meant is that (ignoring dereferenci
| |
| 550 if (filter && item.handler != filter) { | 565 if (filter && item.handler != filter) { |
| 551 // Save this item for later | 566 // Save this item for later |
| 552 completed_io_.push_back(item); | 567 completed_io_.push_back(item); |
| 553 } else { | 568 } else { |
| 554 DCHECK_EQ(item.context->handler, item.handler); | 569 DCHECK(!item.has_valid_io_context || |
| 570 (item.context->handler == item.handler)); | |
| 555 WillProcessIOEvent(); | 571 WillProcessIOEvent(); |
| 556 item.handler->OnIOCompleted(item.context, item.bytes_transfered, | 572 item.handler->OnIOCompleted(item.context, item.bytes_transfered, |
| 557 item.error); | 573 item.error); |
| 558 DidProcessIOEvent(); | 574 DidProcessIOEvent(); |
| 559 } | 575 } |
| 560 } else { | 576 } else { |
| 561 // The handler must be gone by now, just cleanup the mess. | 577 // The handler must be gone by now, just cleanup the mess. |
| 562 delete item.context; | 578 delete item.context; |
| 563 } | 579 } |
| 564 return true; | 580 return true; |
| 565 } | 581 } |
| 566 | 582 |
| 567 // Asks the OS for another IO completion result. | 583 // Asks the OS for another IO completion result. |
| 568 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { | 584 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { |
| 569 memset(item, 0, sizeof(*item)); | 585 memset(item, 0, sizeof(*item)); |
| 570 ULONG_PTR key = NULL; | 586 ULONG_PTR key = NULL; |
| 571 OVERLAPPED* overlapped = NULL; | 587 OVERLAPPED* overlapped = NULL; |
| 572 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, | 588 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, |
| 573 &overlapped, timeout)) { | 589 &overlapped, timeout)) { |
| 574 if (!overlapped) | 590 if (!overlapped) |
| 575 return false; // Nothing in the queue. | 591 return false; // Nothing in the queue. |
| 576 item->error = GetLastError(); | 592 item->error = GetLastError(); |
| 577 item->bytes_transfered = 0; | 593 item->bytes_transfered = 0; |
| 578 } | 594 } |
| 579 | 595 |
| 580 item->handler = reinterpret_cast<IOHandler*>(key); | 596 item->handler = KeyToHandler(key, &item->has_valid_io_context); |
| 581 item->context = reinterpret_cast<IOContext*>(overlapped); | 597 item->context = reinterpret_cast<IOContext*>(overlapped); |
| 582 return true; | 598 return true; |
| 583 } | 599 } |
| 584 | 600 |
| 585 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) { | 601 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) { |
| 586 if (this == reinterpret_cast<MessagePumpForIO*>(item.context) && | 602 if (this == reinterpret_cast<MessagePumpForIO*>(item.context) && |
| 587 this == reinterpret_cast<MessagePumpForIO*>(item.handler)) { | 603 this == reinterpret_cast<MessagePumpForIO*>(item.handler)) { |
| 588 // This is our internal completion. | 604 // This is our internal completion. |
| 589 DCHECK(!item.bytes_transfered); | 605 DCHECK(!item.bytes_transfered); |
| 590 InterlockedExchange(&have_work_, 0); | 606 InterlockedExchange(&have_work_, 0); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 616 } | 632 } |
| 617 | 633 |
| 618 void MessagePumpForIO::WillProcessIOEvent() { | 634 void MessagePumpForIO::WillProcessIOEvent() { |
| 619 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 635 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
| 620 } | 636 } |
| 621 | 637 |
| 622 void MessagePumpForIO::DidProcessIOEvent() { | 638 void MessagePumpForIO::DidProcessIOEvent() { |
| 623 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 639 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
| 624 } | 640 } |
| 625 | 641 |
| 642 // static | |
| 643 ULONG_PTR MessagePumpForIO::HandlerToKey(IOHandler* handler, | |
| 644 bool has_valid_io_context) { | |
| 645 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); | |
| 646 | |
| 647 // |IOHandler| has virtual methods, so it is at least pointer-size aligned. | |
| 648 DCHECK((key & 1) == 0); | |
| 649 | |
| 650 // Mark the completion key as context-less. | |
| 651 if (!has_valid_io_context) { | |
| 652 key = key | 1; | |
|
rvargas (doing something else)
2012/08/13 18:54:12
ouch
alexeypa (please no reviews)
2012/08/13 20:02:23
I think introducing a constant instead on the flag
rvargas (doing something else)
2012/08/13 21:20:10
yeah... I was just showing pain, not suggesting th
| |
| 653 } | |
|
rvargas (doing something else)
2012/08/13 18:54:12
nit: single line if without {}
alexeypa (please no reviews)
2012/08/13 20:02:23
Done.
| |
| 654 return key; | |
| 655 } | |
| 656 | |
| 657 // static | |
| 658 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( | |
| 659 ULONG_PTR key, | |
| 660 bool* has_valid_io_context_out) { | |
|
rvargas (doing something else)
2012/08/13 18:54:12
nit: naming an out argument _out is highly atypica
alexeypa (please no reviews)
2012/08/13 20:02:23
Done.
| |
| 661 *has_valid_io_context_out = !(key & 1); | |
| 662 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); | |
| 663 } | |
| 664 | |
| 626 } // namespace base | 665 } // namespace base |
| OLD | NEW |