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_context || item.context->handler) { |
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_context || (item.context->handler == item.handler)); |
555 WillProcessIOEvent(); | 570 WillProcessIOEvent(); |
556 item.handler->OnIOCompleted(item.context, item.bytes_transfered, | 571 item.handler->OnIOCompleted(item.context, item.bytes_transfered, |
557 item.error); | 572 item.error); |
558 DidProcessIOEvent(); | 573 DidProcessIOEvent(); |
559 } | 574 } |
560 } else { | 575 } else { |
561 // The handler must be gone by now, just cleanup the mess. | 576 // The handler must be gone by now, just cleanup the mess. |
562 delete item.context; | 577 delete item.context; |
563 } | 578 } |
564 return true; | 579 return true; |
565 } | 580 } |
566 | 581 |
567 // Asks the OS for another IO completion result. | 582 // Asks the OS for another IO completion result. |
568 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { | 583 bool MessagePumpForIO::GetIOItem(DWORD timeout, IOItem* item) { |
569 memset(item, 0, sizeof(*item)); | 584 memset(item, 0, sizeof(*item)); |
570 ULONG_PTR key = NULL; | 585 ULONG_PTR key = NULL; |
571 OVERLAPPED* overlapped = NULL; | 586 OVERLAPPED* overlapped = NULL; |
572 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, | 587 if (!GetQueuedCompletionStatus(port_.Get(), &item->bytes_transfered, &key, |
573 &overlapped, timeout)) { | 588 &overlapped, timeout)) { |
574 if (!overlapped) | 589 if (!overlapped) |
575 return false; // Nothing in the queue. | 590 return false; // Nothing in the queue. |
576 item->error = GetLastError(); | 591 item->error = GetLastError(); |
577 item->bytes_transfered = 0; | 592 item->bytes_transfered = 0; |
578 } | 593 } |
579 | 594 |
580 item->handler = reinterpret_cast<IOHandler*>(key); | 595 item->handler = KeyToHandler(key, &item->has_context); |
581 item->context = reinterpret_cast<IOContext*>(overlapped); | 596 item->context = reinterpret_cast<IOContext*>(overlapped); |
582 return true; | 597 return true; |
583 } | 598 } |
584 | 599 |
585 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) { | 600 bool MessagePumpForIO::ProcessInternalIOItem(const IOItem& item) { |
586 if (this == reinterpret_cast<MessagePumpForIO*>(item.context) && | 601 if (this == reinterpret_cast<MessagePumpForIO*>(item.context) && |
587 this == reinterpret_cast<MessagePumpForIO*>(item.handler)) { | 602 this == reinterpret_cast<MessagePumpForIO*>(item.handler)) { |
588 // This is our internal completion. | 603 // This is our internal completion. |
589 DCHECK(!item.bytes_transfered); | 604 DCHECK(!item.bytes_transfered); |
590 InterlockedExchange(&have_work_, 0); | 605 InterlockedExchange(&have_work_, 0); |
(...skipping 25 matching lines...) Expand all Loading... |
616 } | 631 } |
617 | 632 |
618 void MessagePumpForIO::WillProcessIOEvent() { | 633 void MessagePumpForIO::WillProcessIOEvent() { |
619 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); | 634 FOR_EACH_OBSERVER(IOObserver, io_observers_, WillProcessIOEvent()); |
620 } | 635 } |
621 | 636 |
622 void MessagePumpForIO::DidProcessIOEvent() { | 637 void MessagePumpForIO::DidProcessIOEvent() { |
623 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); | 638 FOR_EACH_OBSERVER(IOObserver, io_observers_, DidProcessIOEvent()); |
624 } | 639 } |
625 | 640 |
| 641 // static |
| 642 ULONG_PTR MessagePumpForIO::HandlerToKey(IOHandler* handler, bool has_context) { |
| 643 ULONG_PTR key = reinterpret_cast<ULONG_PTR>(handler); |
| 644 |
| 645 // |IOHandler| has virtual methods, so it is at least pointer-size aligned. |
| 646 DCHECK((key & 1) == 0); |
| 647 |
| 648 // Mark the completion key as context-less. |
| 649 if (!has_context) { |
| 650 key = key | 1; |
| 651 } |
| 652 return key; |
| 653 } |
| 654 |
| 655 // static |
| 656 MessagePumpForIO::IOHandler* MessagePumpForIO::KeyToHandler( |
| 657 ULONG_PTR key, |
| 658 bool* has_context_out) { |
| 659 *has_context_out = !(key & 1); |
| 660 return reinterpret_cast<IOHandler*>(key & ~static_cast<ULONG_PTR>(1)); |
| 661 } |
| 662 |
626 } // namespace base | 663 } // namespace base |
OLD | NEW |