| 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_thread_pool.h" | 5 #include "base/task_scheduler/scheduler_thread_pool.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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/synchronization/condition_variable.h" | 18 #include "base/synchronization/condition_variable.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
| 21 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
| 22 #include "base/task_scheduler/delayed_task_manager.h" |
| 22 #include "base/task_scheduler/sequence.h" | 23 #include "base/task_scheduler/sequence.h" |
| 23 #include "base/task_scheduler/sequence_sort_key.h" | 24 #include "base/task_scheduler/sequence_sort_key.h" |
| 24 #include "base/task_scheduler/task_tracker.h" | 25 #include "base/task_scheduler/task_tracker.h" |
| 25 #include "base/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
| 26 #include "base/threading/simple_thread.h" | 27 #include "base/threading/simple_thread.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 namespace base { | 30 namespace base { |
| 30 namespace internal { | 31 namespace internal { |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 const size_t kNumThreadsInThreadPool = 4; | 34 const size_t kNumThreadsInThreadPool = 4; |
| 34 const size_t kNumThreadsPostingTasks = 4; | 35 const size_t kNumThreadsPostingTasks = 4; |
| 35 const size_t kNumTasksPostedPerThread = 150; | 36 const size_t kNumTasksPostedPerThread = 150; |
| 36 | 37 |
| 37 class TaskSchedulerThreadPoolTest | 38 class TaskSchedulerThreadPoolTest |
| 38 : public testing::TestWithParam<ExecutionMode> { | 39 : public testing::TestWithParam<ExecutionMode> { |
| 39 protected: | 40 protected: |
| 40 TaskSchedulerThreadPoolTest() = default; | 41 TaskSchedulerThreadPoolTest() : delayed_task_manager_(Bind(&DoNothing)) {} |
| 41 | 42 |
| 42 void SetUp() override { | 43 void SetUp() override { |
| 43 thread_pool_ = SchedulerThreadPool::CreateThreadPool( | 44 thread_pool_ = SchedulerThreadPool::CreateThreadPool( |
| 44 ThreadPriority::NORMAL, kNumThreadsInThreadPool, | 45 ThreadPriority::NORMAL, kNumThreadsInThreadPool, |
| 45 Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback, | 46 Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback, |
| 46 Unretained(this)), | 47 Unretained(this)), |
| 47 &task_tracker_); | 48 &task_tracker_, &delayed_task_manager_); |
| 48 ASSERT_TRUE(thread_pool_); | 49 ASSERT_TRUE(thread_pool_); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void TearDown() override { | 52 void TearDown() override { |
| 52 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); | 53 thread_pool_->WaitForAllWorkerThreadsIdleForTesting(); |
| 53 thread_pool_->JoinForTesting(); | 54 thread_pool_->JoinForTesting(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 std::unique_ptr<SchedulerThreadPool> thread_pool_; | 57 std::unique_ptr<SchedulerThreadPool> thread_pool_; |
| 57 | 58 |
| 58 private: | 59 private: |
| 59 void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { | 60 void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { |
| 60 // In production code, this callback would be implemented by the | 61 // In production code, this callback would be implemented by the |
| 61 // TaskScheduler which would first determine which PriorityQueue the | 62 // TaskScheduler which would first determine which PriorityQueue the |
| 62 // sequence must be reinserted. | 63 // sequence must be reinserted. |
| 63 const SequenceSortKey sort_key(sequence->GetSortKey()); | 64 const SequenceSortKey sort_key(sequence->GetSortKey()); |
| 64 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); | 65 thread_pool_->EnqueueSequence(std::move(sequence), sort_key); |
| 65 } | 66 } |
| 66 | 67 |
| 67 TaskTracker task_tracker_; | 68 TaskTracker task_tracker_; |
| 69 DelayedTaskManager delayed_task_manager_; |
| 68 | 70 |
| 69 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); | 71 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest); |
| 70 }; | 72 }; |
| 71 | 73 |
| 72 class TaskFactory { | 74 class TaskFactory { |
| 73 public: | 75 public: |
| 74 // Constructs a TaskFactory that posts tasks with |execution_mode| to | 76 // Constructs a TaskFactory that posts tasks with |execution_mode| to |
| 75 // |thread_pool|. | 77 // |thread_pool|. |
| 76 TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) | 78 TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode) |
| 77 : cv_(&lock_), | 79 : cv_(&lock_), |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 INSTANTIATE_TEST_CASE_P(Parallel, | 328 INSTANTIATE_TEST_CASE_P(Parallel, |
| 327 TaskSchedulerThreadPoolTest, | 329 TaskSchedulerThreadPoolTest, |
| 328 ::testing::Values(ExecutionMode::PARALLEL)); | 330 ::testing::Values(ExecutionMode::PARALLEL)); |
| 329 INSTANTIATE_TEST_CASE_P(Sequenced, | 331 INSTANTIATE_TEST_CASE_P(Sequenced, |
| 330 TaskSchedulerThreadPoolTest, | 332 TaskSchedulerThreadPoolTest, |
| 331 ::testing::Values(ExecutionMode::SEQUENCED)); | 333 ::testing::Values(ExecutionMode::SEQUENCED)); |
| 332 | 334 |
| 333 } // namespace | 335 } // namespace |
| 334 } // namespace internal | 336 } // namespace internal |
| 335 } // namespace base | 337 } // namespace base |
| OLD | NEW |