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 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // Wait until all workers are idle to be sure that no task accesses | 310 // Wait until all workers are idle to be sure that no task accesses |
311 // its TestTaskFactory after it is destroyed. | 311 // its TestTaskFactory after it is destroyed. |
312 worker_pool_->WaitForAllWorkersIdleForTesting(); | 312 worker_pool_->WaitForAllWorkersIdleForTesting(); |
313 } | 313 } |
314 | 314 |
315 // Verify that a Task can't be posted after shutdown. | 315 // Verify that a Task can't be posted after shutdown. |
316 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { | 316 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { |
317 auto task_runner = | 317 auto task_runner = |
318 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); | 318 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); |
319 task_tracker_.Shutdown(); | 319 task_tracker_.Shutdown(); |
320 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRun))); | 320 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, BindOnce(&ShouldNotRun))); |
321 } | 321 } |
322 | 322 |
323 // Verify that a Task runs shortly after its delay expires. | 323 // Verify that a Task runs shortly after its delay expires. |
324 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTask) { | 324 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTask) { |
325 TimeTicks start_time = TimeTicks::Now(); | 325 TimeTicks start_time = TimeTicks::Now(); |
326 | 326 |
327 // Post a task with a short delay. | 327 // Post a task with a short delay. |
328 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 328 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
329 WaitableEvent::InitialState::NOT_SIGNALED); | 329 WaitableEvent::InitialState::NOT_SIGNALED); |
330 EXPECT_TRUE(CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()) | 330 EXPECT_TRUE(CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()) |
331 ->PostDelayedTask(FROM_HERE, Bind(&WaitableEvent::Signal, | 331 ->PostDelayedTask( |
332 Unretained(&task_ran)), | 332 FROM_HERE, |
333 TestTimeouts::tiny_timeout())); | 333 BindOnce(&WaitableEvent::Signal, Unretained(&task_ran)), |
| 334 TestTimeouts::tiny_timeout())); |
334 | 335 |
335 // Wait until the task runs. | 336 // Wait until the task runs. |
336 task_ran.Wait(); | 337 task_ran.Wait(); |
337 | 338 |
338 // Expect the task to run after its delay expires, but not more than 250 ms | 339 // Expect the task to run after its delay expires, but not more than 250 ms |
339 // after that. | 340 // after that. |
340 const TimeDelta actual_delay = TimeTicks::Now() - start_time; | 341 const TimeDelta actual_delay = TimeTicks::Now() - start_time; |
341 EXPECT_GE(actual_delay, TestTimeouts::tiny_timeout()); | 342 EXPECT_GE(actual_delay, TestTimeouts::tiny_timeout()); |
342 EXPECT_LT(actual_delay, | 343 EXPECT_LT(actual_delay, |
343 TimeDelta::FromMilliseconds(250) + TestTimeouts::tiny_timeout()); | 344 TimeDelta::FromMilliseconds(250) + TestTimeouts::tiny_timeout()); |
344 } | 345 } |
345 | 346 |
346 // Verify that the RunsTasksOnCurrentThread() method of a SEQUENCED TaskRunner | 347 // Verify that the RunsTasksOnCurrentThread() method of a SEQUENCED TaskRunner |
347 // returns false when called from a task that isn't part of the sequence. Note: | 348 // returns false when called from a task that isn't part of the sequence. Note: |
348 // Tests that use TestTaskFactory already verify that RunsTasksOnCurrentThread() | 349 // Tests that use TestTaskFactory already verify that RunsTasksOnCurrentThread() |
349 // returns true when appropriate so this method complements it to get full | 350 // returns true when appropriate so this method complements it to get full |
350 // coverage of that method. | 351 // coverage of that method. |
351 TEST_P(TaskSchedulerWorkerPoolImplTest, SequencedRunsTasksOnCurrentThread) { | 352 TEST_P(TaskSchedulerWorkerPoolImplTest, SequencedRunsTasksOnCurrentThread) { |
352 auto task_runner = | 353 auto task_runner = |
353 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); | 354 CreateTaskRunnerWithExecutionMode(worker_pool_.get(), GetParam()); |
354 auto sequenced_task_runner = | 355 auto sequenced_task_runner = |
355 worker_pool_->CreateSequencedTaskRunnerWithTraits(TaskTraits()); | 356 worker_pool_->CreateSequencedTaskRunnerWithTraits(TaskTraits()); |
356 | 357 |
357 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, | 358 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, |
358 WaitableEvent::InitialState::NOT_SIGNALED); | 359 WaitableEvent::InitialState::NOT_SIGNALED); |
359 task_runner->PostTask( | 360 task_runner->PostTask( |
360 FROM_HERE, | 361 FROM_HERE, |
361 Bind( | 362 BindOnce( |
362 [](scoped_refptr<TaskRunner> sequenced_task_runner, | 363 [](scoped_refptr<TaskRunner> sequenced_task_runner, |
363 WaitableEvent* task_ran) { | 364 WaitableEvent* task_ran) { |
364 EXPECT_FALSE(sequenced_task_runner->RunsTasksOnCurrentThread()); | 365 EXPECT_FALSE(sequenced_task_runner->RunsTasksOnCurrentThread()); |
365 task_ran->Signal(); | 366 task_ran->Signal(); |
366 }, | 367 }, |
367 sequenced_task_runner, Unretained(&task_ran))); | 368 sequenced_task_runner, Unretained(&task_ran))); |
368 task_ran.Wait(); | 369 task_ran.Wait(); |
369 } | 370 } |
370 | 371 |
371 INSTANTIATE_TEST_CASE_P(Parallel, | 372 INSTANTIATE_TEST_CASE_P(Parallel, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 | 493 |
493 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) { | 494 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) { |
494 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 495 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
495 WaitableEvent::InitialState::NOT_SIGNALED); | 496 WaitableEvent::InitialState::NOT_SIGNALED); |
496 InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); | 497 InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); |
497 auto task_runner = worker_pool_->CreateSequencedTaskRunnerWithTraits( | 498 auto task_runner = worker_pool_->CreateSequencedTaskRunnerWithTraits( |
498 TaskTraits().WithBaseSyncPrimitives()); | 499 TaskTraits().WithBaseSyncPrimitives()); |
499 | 500 |
500 // Post a task. | 501 // Post a task. |
501 task_runner->PostTask(FROM_HERE, | 502 task_runner->PostTask(FROM_HERE, |
502 Bind(&WaitableEvent::Wait, Unretained(&event))); | 503 BindOnce(&WaitableEvent::Wait, Unretained(&event))); |
503 | 504 |
504 // Post 2 more tasks while the first task hasn't completed its execution. It | 505 // Post 2 more tasks while the first task hasn't completed its execution. It |
505 // is guaranteed that these tasks will run immediately after the first task, | 506 // is guaranteed that these tasks will run immediately after the first task, |
506 // without allowing the worker to sleep. | 507 // without allowing the worker to sleep. |
507 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 508 task_runner->PostTask(FROM_HERE, BindOnce(&DoNothing)); |
508 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 509 task_runner->PostTask(FROM_HERE, BindOnce(&DoNothing)); |
509 | 510 |
510 // Allow tasks to run and wait until the SchedulerWorker is idle. | 511 // Allow tasks to run and wait until the SchedulerWorker is idle. |
511 event.Signal(); | 512 event.Signal(); |
512 worker_pool_->WaitForAllWorkersIdleForTesting(); | 513 worker_pool_->WaitForAllWorkersIdleForTesting(); |
513 | 514 |
514 // Wake up the SchedulerWorker that just became idle by posting a task and | 515 // Wake up the SchedulerWorker that just became idle by posting a task and |
515 // wait until it becomes idle again. The SchedulerWorker should record the | 516 // wait until it becomes idle again. The SchedulerWorker should record the |
516 // TaskScheduler.NumTasksBetweenWaits.* histogram on wake up. | 517 // TaskScheduler.NumTasksBetweenWaits.* histogram on wake up. |
517 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 518 task_runner->PostTask(FROM_HERE, BindOnce(&DoNothing)); |
518 worker_pool_->WaitForAllWorkersIdleForTesting(); | 519 worker_pool_->WaitForAllWorkersIdleForTesting(); |
519 | 520 |
520 // Verify that counts were recorded to the histogram as expected. | 521 // Verify that counts were recorded to the histogram as expected. |
521 const auto* histogram = worker_pool_->num_tasks_between_waits_histogram(); | 522 const auto* histogram = worker_pool_->num_tasks_between_waits_histogram(); |
522 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 523 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); |
523 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 524 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); |
524 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 525 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); |
525 } | 526 } |
526 | 527 |
527 namespace { | 528 namespace { |
(...skipping 12 matching lines...) Expand all Loading... |
540 InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool); | 541 InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool); |
541 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( | 542 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( |
542 TaskTraits().WithBaseSyncPrimitives()); | 543 TaskTraits().WithBaseSyncPrimitives()); |
543 | 544 |
544 // Post tasks to saturate the pool. | 545 // Post tasks to saturate the pool. |
545 std::vector<std::unique_ptr<WaitableEvent>> task_started_events; | 546 std::vector<std::unique_ptr<WaitableEvent>> task_started_events; |
546 for (size_t i = 0; i < kNumWorkersInWorkerPool; ++i) { | 547 for (size_t i = 0; i < kNumWorkersInWorkerPool; ++i) { |
547 task_started_events.push_back( | 548 task_started_events.push_back( |
548 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::MANUAL, | 549 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::MANUAL, |
549 WaitableEvent::InitialState::NOT_SIGNALED)); | 550 WaitableEvent::InitialState::NOT_SIGNALED)); |
550 task_runner->PostTask( | 551 task_runner->PostTask(FROM_HERE, |
551 FROM_HERE, | 552 BindOnce(&SignalAndWaitEvent, |
552 Bind(&SignalAndWaitEvent, Unretained(task_started_events.back().get()), | 553 Unretained(task_started_events.back().get()), |
553 Unretained(&tasks_can_exit_event))); | 554 Unretained(&tasks_can_exit_event))); |
554 } | 555 } |
555 for (const auto& task_started_event : task_started_events) | 556 for (const auto& task_started_event : task_started_events) |
556 task_started_event->Wait(); | 557 task_started_event->Wait(); |
557 | 558 |
558 // Allow tasks to complete their execution and wait to allow workers to | 559 // Allow tasks to complete their execution and wait to allow workers to |
559 // detach. | 560 // detach. |
560 tasks_can_exit_event.Signal(); | 561 tasks_can_exit_event.Signal(); |
561 worker_pool_->WaitForAllWorkersIdleForTesting(); | 562 worker_pool_->WaitForAllWorkersIdleForTesting(); |
562 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 563 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); |
563 | 564 |
564 // Wake up SchedulerWorkers by posting tasks. They should record the | 565 // Wake up SchedulerWorkers by posting tasks. They should record the |
565 // TaskScheduler.NumTasksBetweenWaits.* histogram on wake up. | 566 // TaskScheduler.NumTasksBetweenWaits.* histogram on wake up. |
566 tasks_can_exit_event.Reset(); | 567 tasks_can_exit_event.Reset(); |
567 task_started_events.clear(); | 568 task_started_events.clear(); |
568 for (size_t i = 0; i < kNumWorkersInWorkerPool; ++i) { | 569 for (size_t i = 0; i < kNumWorkersInWorkerPool; ++i) { |
569 task_started_events.push_back( | 570 task_started_events.push_back( |
570 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::MANUAL, | 571 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::MANUAL, |
571 WaitableEvent::InitialState::NOT_SIGNALED)); | 572 WaitableEvent::InitialState::NOT_SIGNALED)); |
572 task_runner->PostTask( | 573 task_runner->PostTask(FROM_HERE, |
573 FROM_HERE, | 574 BindOnce(&SignalAndWaitEvent, |
574 Bind(&SignalAndWaitEvent, Unretained(task_started_events.back().get()), | 575 Unretained(task_started_events.back().get()), |
575 Unretained(&tasks_can_exit_event))); | 576 Unretained(&tasks_can_exit_event))); |
576 } | 577 } |
577 for (const auto& task_started_event : task_started_events) | 578 for (const auto& task_started_event : task_started_events) |
578 task_started_event->Wait(); | 579 task_started_event->Wait(); |
579 | 580 |
580 const auto* histogram = worker_pool_->num_tasks_between_waits_histogram(); | 581 const auto* histogram = worker_pool_->num_tasks_between_waits_histogram(); |
581 | 582 |
582 // Verify that counts were recorded to the histogram as expected. | 583 // Verify that counts were recorded to the histogram as expected. |
583 // - The "0" bucket has a count of at least 1 because the SchedulerWorker on | 584 // - The "0" bucket has a count of at least 1 because the SchedulerWorker on |
584 // top of the idle stack isn't allowed to detach when its sleep timeout | 585 // top of the idle stack isn't allowed to detach when its sleep timeout |
585 // expires. Instead, it waits on its WaitableEvent again without running a | 586 // expires. Instead, it waits on its WaitableEvent again without running a |
(...skipping 17 matching lines...) Expand all Loading... |
603 | 604 |
604 auto histogrammed_thread_task_runner = | 605 auto histogrammed_thread_task_runner = |
605 worker_pool_->CreateSequencedTaskRunnerWithTraits( | 606 worker_pool_->CreateSequencedTaskRunnerWithTraits( |
606 TaskTraits().WithBaseSyncPrimitives()); | 607 TaskTraits().WithBaseSyncPrimitives()); |
607 | 608 |
608 // Post 3 tasks and hold the thread for idle thread stack ordering. | 609 // Post 3 tasks and hold the thread for idle thread stack ordering. |
609 // This test assumes |histogrammed_thread_task_runner| gets assigned the same | 610 // This test assumes |histogrammed_thread_task_runner| gets assigned the same |
610 // thread for each of its tasks. | 611 // thread for each of its tasks. |
611 PlatformThreadRef thread_ref; | 612 PlatformThreadRef thread_ref; |
612 histogrammed_thread_task_runner->PostTask( | 613 histogrammed_thread_task_runner->PostTask( |
613 FROM_HERE, Bind( | 614 FROM_HERE, BindOnce( |
614 [](PlatformThreadRef* thread_ref) { | 615 [](PlatformThreadRef* thread_ref) { |
615 ASSERT_TRUE(thread_ref); | 616 ASSERT_TRUE(thread_ref); |
616 *thread_ref = PlatformThread::CurrentRef(); | 617 *thread_ref = PlatformThread::CurrentRef(); |
617 }, | 618 }, |
618 Unretained(&thread_ref))); | 619 Unretained(&thread_ref))); |
619 histogrammed_thread_task_runner->PostTask( | 620 histogrammed_thread_task_runner->PostTask( |
620 FROM_HERE, Bind( | 621 FROM_HERE, BindOnce( |
621 [](PlatformThreadRef* thread_ref) { | 622 [](PlatformThreadRef* thread_ref) { |
622 ASSERT_FALSE(thread_ref->is_null()); | 623 ASSERT_FALSE(thread_ref->is_null()); |
623 EXPECT_EQ(*thread_ref, PlatformThread::CurrentRef()); | 624 EXPECT_EQ(*thread_ref, PlatformThread::CurrentRef()); |
624 }, | 625 }, |
625 Unretained(&thread_ref))); | 626 Unretained(&thread_ref))); |
626 | 627 |
627 WaitableEvent detach_thread_running( | 628 WaitableEvent detach_thread_running( |
628 WaitableEvent::ResetPolicy::MANUAL, | 629 WaitableEvent::ResetPolicy::MANUAL, |
629 WaitableEvent::InitialState::NOT_SIGNALED); | 630 WaitableEvent::InitialState::NOT_SIGNALED); |
630 WaitableEvent detach_thread_continue( | 631 WaitableEvent detach_thread_continue( |
631 WaitableEvent::ResetPolicy::MANUAL, | 632 WaitableEvent::ResetPolicy::MANUAL, |
632 WaitableEvent::InitialState::NOT_SIGNALED); | 633 WaitableEvent::InitialState::NOT_SIGNALED); |
633 histogrammed_thread_task_runner->PostTask( | 634 histogrammed_thread_task_runner->PostTask( |
634 FROM_HERE, | 635 FROM_HERE, |
635 Bind( | 636 BindOnce( |
636 [](PlatformThreadRef* thread_ref, | 637 [](PlatformThreadRef* thread_ref, |
637 WaitableEvent* detach_thread_running, | 638 WaitableEvent* detach_thread_running, |
638 WaitableEvent* detach_thread_continue) { | 639 WaitableEvent* detach_thread_continue) { |
639 ASSERT_FALSE(thread_ref->is_null()); | 640 ASSERT_FALSE(thread_ref->is_null()); |
640 EXPECT_EQ(*thread_ref, PlatformThread::CurrentRef()); | 641 EXPECT_EQ(*thread_ref, PlatformThread::CurrentRef()); |
641 detach_thread_running->Signal(); | 642 detach_thread_running->Signal(); |
642 detach_thread_continue->Wait(); | 643 detach_thread_continue->Wait(); |
643 }, | 644 }, |
644 Unretained(&thread_ref), Unretained(&detach_thread_running), | 645 Unretained(&thread_ref), Unretained(&detach_thread_running), |
645 Unretained(&detach_thread_continue))); | 646 Unretained(&detach_thread_continue))); |
(...skipping 10 matching lines...) Expand all Loading... |
656 WaitableEvent top_idle_thread_running( | 657 WaitableEvent top_idle_thread_running( |
657 WaitableEvent::ResetPolicy::MANUAL, | 658 WaitableEvent::ResetPolicy::MANUAL, |
658 WaitableEvent::InitialState::NOT_SIGNALED); | 659 WaitableEvent::InitialState::NOT_SIGNALED); |
659 WaitableEvent top_idle_thread_continue( | 660 WaitableEvent top_idle_thread_continue( |
660 WaitableEvent::ResetPolicy::MANUAL, | 661 WaitableEvent::ResetPolicy::MANUAL, |
661 WaitableEvent::InitialState::NOT_SIGNALED); | 662 WaitableEvent::InitialState::NOT_SIGNALED); |
662 auto task_runner_for_top_idle = | 663 auto task_runner_for_top_idle = |
663 worker_pool_->CreateSequencedTaskRunnerWithTraits( | 664 worker_pool_->CreateSequencedTaskRunnerWithTraits( |
664 TaskTraits().WithBaseSyncPrimitives()); | 665 TaskTraits().WithBaseSyncPrimitives()); |
665 task_runner_for_top_idle->PostTask( | 666 task_runner_for_top_idle->PostTask( |
666 FROM_HERE, Bind( | 667 FROM_HERE, BindOnce( |
667 [](PlatformThreadRef thread_ref, | 668 [](PlatformThreadRef thread_ref, |
668 WaitableEvent* top_idle_thread_running, | 669 WaitableEvent* top_idle_thread_running, |
669 WaitableEvent* top_idle_thread_continue) { | 670 WaitableEvent* top_idle_thread_continue) { |
670 ASSERT_FALSE(thread_ref.is_null()); | 671 ASSERT_FALSE(thread_ref.is_null()); |
671 EXPECT_NE(thread_ref, PlatformThread::CurrentRef()) | 672 EXPECT_NE(thread_ref, PlatformThread::CurrentRef()) |
672 << "Worker reused. Thread will not detach and the " | 673 << "Worker reused. Thread will not detach and the " |
673 "histogram value will be wrong."; | 674 "histogram value will be wrong."; |
674 top_idle_thread_running->Signal(); | 675 top_idle_thread_running->Signal(); |
675 top_idle_thread_continue->Wait(); | 676 top_idle_thread_continue->Wait(); |
676 }, | 677 }, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), | 735 StandbyThreadPolicy::ONE, 8U, TimeDelta::Max()), |
735 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, | 736 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, |
736 &delayed_task_manager); | 737 &delayed_task_manager); |
737 ASSERT_TRUE(worker_pool); | 738 ASSERT_TRUE(worker_pool); |
738 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); | 739 EXPECT_EQ(1U, worker_pool->NumberOfAliveWorkersForTesting()); |
739 worker_pool->JoinForTesting(); | 740 worker_pool->JoinForTesting(); |
740 } | 741 } |
741 | 742 |
742 } // namespace internal | 743 } // namespace internal |
743 } // namespace base | 744 } // namespace base |
OLD | NEW |