| 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_loop/message_pump_win.h" | 5 #include "base/message_loop/message_pump_win.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 // windows code. This ScheduleWork() may hurt performance a tiny bit when | 503 // windows code. This ScheduleWork() may hurt performance a tiny bit when |
| 504 // tasks appear very infrequently, but when the event queue is busy, the | 504 // tasks appear very infrequently, but when the event queue is busy, the |
| 505 // kMsgHaveWork events get (percentage wise) rarer and rarer. | 505 // kMsgHaveWork events get (percentage wise) rarer and rarer. |
| 506 ScheduleWork(); | 506 ScheduleWork(); |
| 507 return ProcessMessageHelper(msg); | 507 return ProcessMessageHelper(msg); |
| 508 } | 508 } |
| 509 | 509 |
| 510 //----------------------------------------------------------------------------- | 510 //----------------------------------------------------------------------------- |
| 511 // MessagePumpForGpu public: | 511 // MessagePumpForGpu public: |
| 512 | 512 |
| 513 MessagePumpForGpu::MessagePumpForGpu() | 513 MessagePumpForGpu::MessagePumpForGpu() { |
| 514 : event_(CreateEvent(nullptr, FALSE, FALSE, nullptr)) { | 514 event_.Set(CreateEvent(nullptr, FALSE, FALSE, nullptr)); |
| 515 InitUser32APIs(); | 515 InitUser32APIs(); |
| 516 } | 516 } |
| 517 | 517 |
| 518 MessagePumpForGpu::~MessagePumpForGpu() { | 518 MessagePumpForGpu::~MessagePumpForGpu() {} |
| 519 CloseHandle(event_); | |
| 520 } | |
| 521 | 519 |
| 522 // static | 520 // static |
| 523 void MessagePumpForGpu::InitFactory() { | 521 void MessagePumpForGpu::InitFactory() { |
| 524 bool init_result = MessageLoop::InitMessagePumpForUIFactory( | 522 bool init_result = MessageLoop::InitMessagePumpForUIFactory( |
| 525 &MessagePumpForGpu::CreateMessagePumpForGpu); | 523 &MessagePumpForGpu::CreateMessagePumpForGpu); |
| 526 DCHECK(init_result); | 524 DCHECK(init_result); |
| 527 } | 525 } |
| 528 | 526 |
| 529 // static | 527 // static |
| 530 std::unique_ptr<MessagePump> MessagePumpForGpu::CreateMessagePumpForGpu() { | 528 std::unique_ptr<MessagePump> MessagePumpForGpu::CreateMessagePumpForGpu() { |
| 531 return WrapUnique<MessagePump>(new MessagePumpForGpu); | 529 return WrapUnique<MessagePump>(new MessagePumpForGpu); |
| 532 } | 530 } |
| 533 | 531 |
| 534 void MessagePumpForGpu::ScheduleWork() { | 532 void MessagePumpForGpu::ScheduleWork() { |
| 535 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) | 533 if (InterlockedExchange(&work_state_, HAVE_WORK) != READY) |
| 536 return; // Someone else continued the pumping. | 534 return; // Someone else continued the pumping. |
| 537 | 535 |
| 538 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. | 536 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. |
| 539 // Remove this when the bug is fixed. | 537 // Remove this when the bug is fixed. |
| 540 last_set_event_timeticks_ = TimeTicks::Now(); | 538 last_set_event_timeticks_ = TimeTicks::Now(); |
| 541 | 539 |
| 542 // Make sure the MessagePump does some work for us. | 540 // Make sure the MessagePump does some work for us. |
| 543 SetEvent(event_); | 541 SetEvent(event_.Get()); |
| 544 } | 542 } |
| 545 | 543 |
| 546 void MessagePumpForGpu::ScheduleDelayedWork( | 544 void MessagePumpForGpu::ScheduleDelayedWork( |
| 547 const TimeTicks& delayed_work_time) { | 545 const TimeTicks& delayed_work_time) { |
| 548 // We know that we can't be blocked right now since this method can only be | 546 // We know that we can't be blocked right now since this method can only be |
| 549 // called on the same thread as Run, so we only need to update our record of | 547 // called on the same thread as Run, so we only need to update our record of |
| 550 // how long to sleep when we do sleep. | 548 // how long to sleep when we do sleep. |
| 551 delayed_work_time_ = delayed_work_time; | 549 delayed_work_time_ = delayed_work_time; |
| 552 } | 550 } |
| 553 | 551 |
| 554 bool MessagePumpForGpu::WasSignaled() { | 552 bool MessagePumpForGpu::WasSignaled() { |
| 555 // If |event_| was set this would reset it back to unset state. | 553 // If |event_| was set this would reset it back to unset state. |
| 556 return WaitForSingleObject(event_, 0) == WAIT_OBJECT_0; | 554 return WaitForSingleObject(event_.Get(), 0) == WAIT_OBJECT_0; |
| 557 } | 555 } |
| 558 | 556 |
| 559 //----------------------------------------------------------------------------- | 557 //----------------------------------------------------------------------------- |
| 560 // MessagePumpForGpu private: | 558 // MessagePumpForGpu private: |
| 561 | 559 |
| 562 void MessagePumpForGpu::DoRunLoop() { | 560 void MessagePumpForGpu::DoRunLoop() { |
| 563 while (!state_->should_quit) { | 561 while (!state_->should_quit) { |
| 564 // Indicate that the loop is handling the work. | 562 // Indicate that the loop is handling the work. |
| 565 // If there is a race condition between switching to WORKING state here and | 563 // If there is a race condition between switching to WORKING state here and |
| 566 // the producer thread setting the HAVE_WORK state after exiting the wait, | 564 // the producer thread setting the HAVE_WORK state after exiting the wait, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 while ((delay = GetCurrentDelay()) != 0) { | 612 while ((delay = GetCurrentDelay()) != 0) { |
| 615 if (delay < 0) // Negative value means no timers waiting. | 613 if (delay < 0) // Negative value means no timers waiting. |
| 616 delay = INFINITE; | 614 delay = INFINITE; |
| 617 | 615 |
| 618 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. | 616 // TODO(stanisc): crbug.com/596190: Preserve for crash dump analysis. |
| 619 // Remove this when the bug is fixed. | 617 // Remove this when the bug is fixed. |
| 620 TimeTicks wait_for_work_timeticks = TimeTicks::Now(); | 618 TimeTicks wait_for_work_timeticks = TimeTicks::Now(); |
| 621 debug::Alias(&wait_for_work_timeticks); | 619 debug::Alias(&wait_for_work_timeticks); |
| 622 debug::Alias(&delay); | 620 debug::Alias(&delay); |
| 623 | 621 |
| 622 HANDLE handle = event_.Get(); |
| 624 DWORD result = | 623 DWORD result = |
| 625 g_msg_wait_for_multiple_objects_ex(1, &event_, delay, QS_ALLINPUT, 0); | 624 g_msg_wait_for_multiple_objects_ex(1, &handle, delay, QS_ALLINPUT, 0); |
| 626 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); | 625 DCHECK_NE(WAIT_FAILED, result) << GetLastError(); |
| 627 if (result != WAIT_TIMEOUT) { | 626 if (result != WAIT_TIMEOUT) { |
| 628 // Either work or message available. | 627 // Either work or message available. |
| 629 return; | 628 return; |
| 630 } | 629 } |
| 631 } | 630 } |
| 632 } | 631 } |
| 633 | 632 |
| 634 bool MessagePumpForGpu::ProcessNextMessage() { | 633 bool MessagePumpForGpu::ProcessNextMessage() { |
| 635 MSG msg; | 634 MSG msg; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 if (!filter || it->handler == filter) { | 823 if (!filter || it->handler == filter) { |
| 825 *item = *it; | 824 *item = *it; |
| 826 completed_io_.erase(it); | 825 completed_io_.erase(it); |
| 827 return true; | 826 return true; |
| 828 } | 827 } |
| 829 } | 828 } |
| 830 return false; | 829 return false; |
| 831 } | 830 } |
| 832 | 831 |
| 833 } // namespace base | 832 } // namespace base |
| OLD | NEW |