Chromium Code Reviews| 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.h" | 5 #include "base/task_scheduler/scheduler_worker.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/synchronization/condition_variable.h" | 16 #include "base/synchronization/condition_variable.h" |
| 17 #include "base/synchronization/waitable_event.h" | |
| 17 #include "base/task_scheduler/scheduler_lock.h" | 18 #include "base/task_scheduler/scheduler_lock.h" |
| 18 #include "base/task_scheduler/sequence.h" | 19 #include "base/task_scheduler/sequence.h" |
| 19 #include "base/task_scheduler/task.h" | 20 #include "base/task_scheduler/task.h" |
| 20 #include "base/task_scheduler/task_tracker.h" | 21 #include "base/task_scheduler/task_tracker.h" |
| 22 #include "base/threading/platform_thread.h" | |
| 21 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 25 |
| 24 namespace base { | 26 namespace base { |
| 25 namespace internal { | 27 namespace internal { |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 const size_t kNumSequencesPerTest = 150; | 30 const size_t kNumSequencesPerTest = 150; |
| 29 | 31 |
| 30 // The test parameter is the number of Tasks per Sequence returned by GetWork(). | 32 // The test parameter is the number of Tasks per Sequence returned by GetWork(). |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, | 416 ThreadPriority::NORMAL, WrapUnique(delegate), &task_tracker, |
| 415 SchedulerWorker::InitialState::DETACHED); | 417 SchedulerWorker::InitialState::DETACHED); |
| 416 ASSERT_FALSE(worker->ThreadAliveForTesting()); | 418 ASSERT_FALSE(worker->ThreadAliveForTesting()); |
| 417 worker->WakeUp(); | 419 worker->WakeUp(); |
| 418 delegate->WaitForWorkToRun(); | 420 delegate->WaitForWorkToRun(); |
| 419 delegate->WaitForDetachRequest(); | 421 delegate->WaitForDetachRequest(); |
| 420 ASSERT_TRUE(worker->ThreadAliveForTesting()); | 422 ASSERT_TRUE(worker->ThreadAliveForTesting()); |
| 421 worker->JoinForTesting(); | 423 worker->JoinForTesting(); |
| 422 } | 424 } |
| 423 | 425 |
| 426 namespace { | |
| 427 | |
| 428 class ExpectThreadPriorityDelegate : public SchedulerWorker::Delegate { | |
| 429 public: | |
| 430 ExpectThreadPriorityDelegate() | |
| 431 : priority_verified_in_get_work_event_( | |
| 432 WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 433 WaitableEvent::InitialState::NOT_SIGNALED), | |
| 434 expected_thread_priority_(ThreadPriority::BACKGROUND) {} | |
| 435 | |
| 436 void SetExpectedThreadPriority(ThreadPriority expected_thread_priority) { | |
| 437 expected_thread_priority_ = expected_thread_priority; | |
| 438 } | |
| 439 | |
| 440 void WaitForPriorityVerifiedInGetWork() { | |
| 441 priority_verified_in_get_work_event_.Wait(); | |
| 442 } | |
| 443 | |
| 444 // SchedulerWorker::Delegate: | |
| 445 void OnMainEntry(SchedulerWorker* worker) override { VerifyThreadPriority(); } | |
| 446 scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) override { | |
| 447 VerifyThreadPriority(); | |
| 448 priority_verified_in_get_work_event_.Signal(); | |
| 449 return nullptr; | |
| 450 } | |
| 451 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override {} | |
| 452 TimeDelta GetSleepTimeout() override { return TimeDelta::Max(); } | |
| 453 bool CanDetach(SchedulerWorker* worker) override { return false; } | |
| 454 | |
| 455 private: | |
| 456 void VerifyThreadPriority() { | |
| 457 AutoSchedulerLock auto_lock(expected_thread_priority_lock_); | |
| 458 EXPECT_EQ(expected_thread_priority_, | |
| 459 PlatformThread::GetCurrentThreadPriority()); | |
| 460 } | |
| 461 | |
| 462 // Signaled after GetWork() has verified the priority of the worker thread. | |
| 463 WaitableEvent priority_verified_in_get_work_event_; | |
| 464 | |
| 465 // Synchronizes access to |expected_thread_priority_|. | |
| 466 SchedulerLock expected_thread_priority_lock_; | |
| 467 | |
| 468 // Expected thread priority for the next call to OnMainEntry() or GetWork(). | |
| 469 ThreadPriority expected_thread_priority_; | |
| 470 | |
| 471 DISALLOW_COPY_AND_ASSIGN(ExpectThreadPriorityDelegate); | |
| 472 }; | |
| 473 | |
| 474 } // namespace | |
| 475 | |
| 476 TEST(TaskSchedulerWorkerTest, BumpPriorityOfAliveThreadDuringShutdown) { | |
| 477 TaskTracker task_tracker; | |
| 478 | |
| 479 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( | |
| 480 new ExpectThreadPriorityDelegate); | |
| 481 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); | |
| 482 delegate_raw->SetExpectedThreadPriority(ThreadPriority::BACKGROUND); | |
| 483 | |
| 484 // Create an ALIVE thread. | |
|
robliao
2016/07/20 22:31:48
Nit: Remove this comment.
fdoray
2016/07/21 13:21:56
Done.
| |
| 485 std::unique_ptr<SchedulerWorker> worker = SchedulerWorker::Create( | |
| 486 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker, | |
| 487 SchedulerWorker::InitialState::ALIVE); | |
| 488 | |
| 489 // Verify that the initial thread priority is BACKGROUND. | |
| 490 worker->WakeUp(); | |
| 491 delegate_raw->WaitForPriorityVerifiedInGetWork(); | |
| 492 | |
| 493 // Verify that the thread priority is bumped to NORMAL during shutdown. | |
| 494 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); | |
| 495 task_tracker.SetHasShutdownStartedForTesting(); | |
| 496 worker->WakeUp(); | |
| 497 delegate_raw->WaitForPriorityVerifiedInGetWork(); | |
| 498 | |
| 499 worker->JoinForTesting(); | |
| 500 } | |
| 501 | |
| 502 TEST(TaskSchedulerWorkerTest, BumpPriorityOfDetachedThreadDuringShutdown) { | |
| 503 TaskTracker task_tracker; | |
| 504 | |
| 505 std::unique_ptr<ExpectThreadPriorityDelegate> delegate( | |
| 506 new ExpectThreadPriorityDelegate); | |
| 507 ExpectThreadPriorityDelegate* delegate_raw = delegate.get(); | |
| 508 delegate_raw->SetExpectedThreadPriority(ThreadPriority::NORMAL); | |
| 509 | |
| 510 // Create a DETACHED thread. | |
| 511 std::unique_ptr<SchedulerWorker> worker = SchedulerWorker::Create( | |
| 512 ThreadPriority::BACKGROUND, std::move(delegate), &task_tracker, | |
| 513 SchedulerWorker::InitialState::DETACHED); | |
| 514 | |
| 515 // Pretend that shutdown has started. | |
| 516 task_tracker.SetHasShutdownStartedForTesting(); | |
| 517 | |
| 518 // Wake up the thread and verify that its priority is NORMAL when | |
| 519 // OnMainEntry() and GetWork() are called. | |
| 520 worker->WakeUp(); | |
| 521 delegate_raw->WaitForPriorityVerifiedInGetWork(); | |
| 522 | |
| 523 worker->JoinForTesting(); | |
| 524 } | |
| 525 | |
| 424 } // namespace | 526 } // namespace |
| 425 } // namespace internal | 527 } // namespace internal |
| 426 } // namespace base | 528 } // namespace base |
| OLD | NEW |