| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/task_scheduler/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <unordered_set> | 10 #include <unordered_set> |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 WaitableEvent task_waiter(WaitableEvent::ResetPolicy::AUTOMATIC, | 478 WaitableEvent task_waiter(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 479 WaitableEvent::InitialState::NOT_SIGNALED); | 479 WaitableEvent::InitialState::NOT_SIGNALED); |
| 480 single_thread_task_runner->PostTask( | 480 single_thread_task_runner->PostTask( |
| 481 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&task_waiter))); | 481 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&task_waiter))); |
| 482 task_waiter.Wait(); | 482 task_waiter.Wait(); |
| 483 worker_pool_->WaitForAllWorkersIdleForTesting(); | 483 worker_pool_->WaitForAllWorkersIdleForTesting(); |
| 484 | 484 |
| 485 // Give the worker pool a chance to reclaim its threads. | 485 // Give the worker pool a chance to reclaim its threads. |
| 486 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 486 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); |
| 487 | 487 |
| 488 worker_pool_->DisallowWorkerDetachmentForTesting(); | |
| 489 | |
| 490 single_thread_task_runner->PostTask( | 488 single_thread_task_runner->PostTask( |
| 491 FROM_HERE, | 489 FROM_HERE, |
| 492 Bind(&TaskSchedulerWorkerPoolSingleThreadedTest::CheckValidThread, | 490 Bind(&TaskSchedulerWorkerPoolSingleThreadedTest::CheckValidThread, |
| 493 Unretained(this))); | 491 Unretained(this))); |
| 494 single_thread_task_runner->PostTask( | 492 single_thread_task_runner->PostTask( |
| 495 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&task_waiter))); | 493 FROM_HERE, Bind(&WaitableEvent::Signal, Unretained(&task_waiter))); |
| 496 task_waiter.Wait(); | 494 task_waiter.Wait(); |
| 497 } | 495 } |
| 498 | 496 |
| 499 namespace { | 497 namespace { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 // Release tasks waiting on |waiter_|. | 554 // Release tasks waiting on |waiter_|. |
| 557 waiter_.Signal(); | 555 waiter_.Signal(); |
| 558 worker_pool_->WaitForAllWorkersIdleForTesting(); | 556 worker_pool_->WaitForAllWorkersIdleForTesting(); |
| 559 | 557 |
| 560 // All threads should be done running by now, so reset for the next phase. | 558 // All threads should be done running by now, so reset for the next phase. |
| 561 waiter_.Reset(); | 559 waiter_.Reset(); |
| 562 | 560 |
| 563 // Give the worker pool a chance to detach its threads. | 561 // Give the worker pool a chance to detach its threads. |
| 564 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 562 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); |
| 565 | 563 |
| 566 worker_pool_->DisallowWorkerDetachmentForTesting(); | |
| 567 | |
| 568 // Saturate and count the threads that do not have the magic TLS value. If the | 564 // Saturate and count the threads that do not have the magic TLS value. If the |
| 569 // value is not there, that means we're at a new thread. | 565 // value is not there, that means we're at a new thread. |
| 570 std::vector<std::unique_ptr<WaitableEvent>> count_waiters; | 566 std::vector<std::unique_ptr<WaitableEvent>> count_waiters; |
| 571 for (auto& factory : factories) { | 567 for (auto& factory : factories) { |
| 572 count_waiters.push_back(WrapUnique(new WaitableEvent( | 568 count_waiters.push_back(WrapUnique(new WaitableEvent( |
| 573 WaitableEvent::ResetPolicy::MANUAL, | 569 WaitableEvent::ResetPolicy::MANUAL, |
| 574 WaitableEvent::InitialState::NOT_SIGNALED))); | 570 WaitableEvent::InitialState::NOT_SIGNALED))); |
| 575 ASSERT_TRUE(factory->PostTask( | 571 ASSERT_TRUE(factory->PostTask( |
| 576 PostNestedTask::NO, | 572 PostNestedTask::NO, |
| 577 Bind(&TaskSchedulerWorkerPoolCheckTlsReuse::CountZeroTlsValuesAndWait, | 573 Bind(&TaskSchedulerWorkerPoolCheckTlsReuse::CountZeroTlsValuesAndWait, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 // the sleep timeout expires. | 704 // the sleep timeout expires. |
| 709 EXPECT_GE(histogram->SnapshotSamples()->GetCount(0), 1); | 705 EXPECT_GE(histogram->SnapshotSamples()->GetCount(0), 1); |
| 710 // - The "1" bucket has a count of |kNumWorkersInWorkerPool| because each | 706 // - The "1" bucket has a count of |kNumWorkersInWorkerPool| because each |
| 711 // SchedulerWorker ran a task before waiting on its WaitableEvent at the | 707 // SchedulerWorker ran a task before waiting on its WaitableEvent at the |
| 712 // beginning of the test. | 708 // beginning of the test. |
| 713 EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool), | 709 EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool), |
| 714 histogram->SnapshotSamples()->GetCount(1)); | 710 histogram->SnapshotSamples()->GetCount(1)); |
| 715 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 711 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); |
| 716 | 712 |
| 717 tasks_can_exit_event.Signal(); | 713 tasks_can_exit_event.Signal(); |
| 714 |
| 715 // Do not let |tasks_can_exit_event| go out of scope before all workers are |
| 716 // idle. |
| 718 worker_pool_->WaitForAllWorkersIdleForTesting(); | 717 worker_pool_->WaitForAllWorkersIdleForTesting(); |
| 719 worker_pool_->DisallowWorkerDetachmentForTesting(); | |
| 720 } | 718 } |
| 721 | 719 |
| 722 namespace { | 720 namespace { |
| 723 | 721 |
| 724 void NotReachedReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | 722 void NotReachedReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { |
| 725 ADD_FAILURE() | 723 ADD_FAILURE() |
| 726 << "Unexpected invocation of NotReachedReEnqueueSequenceCallback."; | 724 << "Unexpected invocation of NotReachedReEnqueueSequenceCallback."; |
| 727 } | 725 } |
| 728 | 726 |
| 729 void CaptureThreadId(PlatformThreadId* thread_id) { | 727 void CaptureThreadId(PlatformThreadId* thread_id) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 757 // To allow the SchedulerWorker associated with |task_runner| to detach: | 755 // To allow the SchedulerWorker associated with |task_runner| to detach: |
| 758 // - Make sure it isn't on top of the idle stack by waking up another | 756 // - Make sure it isn't on top of the idle stack by waking up another |
| 759 // SchedulerWorker and waiting until it goes back to sleep. | 757 // SchedulerWorker and waiting until it goes back to sleep. |
| 760 // - Release |task_runner|. | 758 // - Release |task_runner|. |
| 761 other_task_runner->PostTask(FROM_HERE, Bind(&VerifyThreadIdIsNot, thread_id)); | 759 other_task_runner->PostTask(FROM_HERE, Bind(&VerifyThreadIdIsNot, thread_id)); |
| 762 worker_pool_->WaitForAllWorkersIdleForTesting(); | 760 worker_pool_->WaitForAllWorkersIdleForTesting(); |
| 763 task_runner = nullptr; | 761 task_runner = nullptr; |
| 764 | 762 |
| 765 // Allow the SchedulerWorker that was associated with |task_runner| to detach. | 763 // Allow the SchedulerWorker that was associated with |task_runner| to detach. |
| 766 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 764 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); |
| 767 worker_pool_->DisallowWorkerDetachmentForTesting(); | |
| 768 | 765 |
| 769 // Verify that counts were recorded to the histogram as expected. | 766 // Verify that counts were recorded to the histogram as expected. |
| 770 const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); | 767 const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); |
| 771 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 768 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); |
| 772 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 769 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); |
| 773 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 770 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); |
| 774 } | 771 } |
| 775 | 772 |
| 776 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitLazy) { | 773 TEST(TaskSchedulerWorkerPoolStandbyPolicyTest, InitLazy) { |
| 777 TaskTracker task_tracker; | 774 TaskTracker task_tracker; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 797 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), | 794 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), |
| 798 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 795 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, |
| 799 &delayed_task_manager); | 796 &delayed_task_manager); |
| 800 ASSERT_TRUE(worker_pool); | 797 ASSERT_TRUE(worker_pool); |
| 801 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); | 798 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); |
| 802 worker_pool->JoinForTesting(); | 799 worker_pool->JoinForTesting(); |
| 803 } | 800 } |
| 804 | 801 |
| 805 } // namespace internal | 802 } // namespace internal |
| 806 } // namespace base | 803 } // namespace base |
| OLD | NEW |