| 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_thread.h" | 5 #include "base/task_scheduler/scheduler_worker_thread.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/synchronization/condition_variable.h" | 16 #include "base/synchronization/condition_variable.h" |
| 16 #include "base/task_scheduler/scheduler_lock.h" | 17 #include "base/task_scheduler/scheduler_lock.h" |
| 17 #include "base/task_scheduler/sequence.h" | 18 #include "base/task_scheduler/sequence.h" |
| 18 #include "base/task_scheduler/task.h" | 19 #include "base/task_scheduler/task.h" |
| 19 #include "base/task_scheduler/task_tracker.h" | 20 #include "base/task_scheduler/task_tracker.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 namespace internal { | 24 namespace internal { |
| 24 namespace { | 25 namespace { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 AutoSchedulerLock auto_lock(lock_); | 92 AutoSchedulerLock auto_lock(lock_); |
| 92 EXPECT_EQ(0U, num_sequences_to_create_); | 93 EXPECT_EQ(0U, num_sequences_to_create_); |
| 93 num_sequences_to_create_ = num_sequences_to_create; | 94 num_sequences_to_create_ = num_sequences_to_create; |
| 94 } | 95 } |
| 95 | 96 |
| 96 size_t NumGetWorkCallback() const { | 97 size_t NumGetWorkCallback() const { |
| 97 AutoSchedulerLock auto_lock(lock_); | 98 AutoSchedulerLock auto_lock(lock_); |
| 98 return num_get_work_callback_; | 99 return num_get_work_callback_; |
| 99 } | 100 } |
| 100 | 101 |
| 101 scoped_ptr<SchedulerWorkerThread> worker_thread_; | 102 std::unique_ptr<SchedulerWorkerThread> worker_thread_; |
| 102 | 103 |
| 103 private: | 104 private: |
| 104 void MainEntryCallback() { | 105 void MainEntryCallback() { |
| 105 AutoSchedulerLock auto_lock(lock_); | 106 AutoSchedulerLock auto_lock(lock_); |
| 106 ++num_main_entry_callback_; | 107 ++num_main_entry_callback_; |
| 107 num_main_entry_callback_cv_->Signal(); | 108 num_main_entry_callback_cv_->Signal(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 // Returns a Sequence that contains 1 Task if |num_sequences_to_create_| is | 111 // Returns a Sequence that contains 1 Task if |num_sequences_to_create_| is |
| 111 // greater than 0. | 112 // greater than 0. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 123 // Check if a Sequence should be returned. | 124 // Check if a Sequence should be returned. |
| 124 if (num_sequences_to_create_ == 0) | 125 if (num_sequences_to_create_ == 0) |
| 125 return nullptr; | 126 return nullptr; |
| 126 --num_sequences_to_create_; | 127 --num_sequences_to_create_; |
| 127 } | 128 } |
| 128 | 129 |
| 129 // Create a Sequence that contains 1 Task. | 130 // Create a Sequence that contains 1 Task. |
| 130 scoped_refptr<Sequence> sequence(new Sequence); | 131 scoped_refptr<Sequence> sequence(new Sequence); |
| 131 task_tracker_.PostTask( | 132 task_tracker_.PostTask( |
| 132 Bind(IgnoreResult(&Sequence::PushTask), Unretained(sequence.get())), | 133 Bind(IgnoreResult(&Sequence::PushTask), Unretained(sequence.get())), |
| 133 make_scoped_ptr(new Task( | 134 base::WrapUnique(new Task( |
| 134 FROM_HERE, Bind(&TaskSchedulerWorkerThreadTest::RunTaskCallback, | 135 FROM_HERE, Bind(&TaskSchedulerWorkerThreadTest::RunTaskCallback, |
| 135 Unretained(this)), | 136 Unretained(this)), |
| 136 TaskTraits()))); | 137 TaskTraits()))); |
| 137 | 138 |
| 138 { | 139 { |
| 139 // Add the Sequence to the vector of created Sequences. | 140 // Add the Sequence to the vector of created Sequences. |
| 140 AutoSchedulerLock auto_lock(lock_); | 141 AutoSchedulerLock auto_lock(lock_); |
| 141 created_sequences_.push_back(sequence); | 142 created_sequences_.push_back(sequence); |
| 142 } | 143 } |
| 143 | 144 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 160 | 161 |
| 161 TaskTracker task_tracker_; | 162 TaskTracker task_tracker_; |
| 162 | 163 |
| 163 // Synchronizes access to all members below. | 164 // Synchronizes access to all members below. |
| 164 mutable SchedulerLock lock_; | 165 mutable SchedulerLock lock_; |
| 165 | 166 |
| 166 // Number of times that MainEntryCallback() has been called. | 167 // Number of times that MainEntryCallback() has been called. |
| 167 size_t num_main_entry_callback_ = 0; | 168 size_t num_main_entry_callback_ = 0; |
| 168 | 169 |
| 169 // Condition variable signaled when |num_main_entry_callback_| is incremented. | 170 // Condition variable signaled when |num_main_entry_callback_| is incremented. |
| 170 scoped_ptr<ConditionVariable> num_main_entry_callback_cv_; | 171 std::unique_ptr<ConditionVariable> num_main_entry_callback_cv_; |
| 171 | 172 |
| 172 // Number of Sequences that should be created by GetWorkCallback(). When this | 173 // Number of Sequences that should be created by GetWorkCallback(). When this |
| 173 // is 0, GetWorkCallback() returns nullptr. | 174 // is 0, GetWorkCallback() returns nullptr. |
| 174 size_t num_sequences_to_create_ = 0; | 175 size_t num_sequences_to_create_ = 0; |
| 175 | 176 |
| 176 // Number of times that GetWorkCallback() has been called. | 177 // Number of times that GetWorkCallback() has been called. |
| 177 size_t num_get_work_callback_ = 0; | 178 size_t num_get_work_callback_ = 0; |
| 178 | 179 |
| 179 // Condition variable signaled when |num_get_work_callback_| is incremented. | 180 // Condition variable signaled when |num_get_work_callback_| is incremented. |
| 180 scoped_ptr<ConditionVariable> num_get_work_callback_cv_; | 181 std::unique_ptr<ConditionVariable> num_get_work_callback_cv_; |
| 181 | 182 |
| 182 // Sequences created by GetWorkCallback(). | 183 // Sequences created by GetWorkCallback(). |
| 183 std::vector<scoped_refptr<Sequence>> created_sequences_; | 184 std::vector<scoped_refptr<Sequence>> created_sequences_; |
| 184 | 185 |
| 185 // Sequences passed to RanTaskFromSequenceCallback(). | 186 // Sequences passed to RanTaskFromSequenceCallback(). |
| 186 std::vector<scoped_refptr<Sequence>> run_sequences_; | 187 std::vector<scoped_refptr<Sequence>> run_sequences_; |
| 187 | 188 |
| 188 // Condition variable signaled when a Sequence is added to |run_sequences_|. | 189 // Condition variable signaled when a Sequence is added to |run_sequences_|. |
| 189 scoped_ptr<ConditionVariable> run_sequences_cv_; | 190 std::unique_ptr<ConditionVariable> run_sequences_cv_; |
| 190 | 191 |
| 191 // Number of times that RunTaskCallback() has been called. | 192 // Number of times that RunTaskCallback() has been called. |
| 192 size_t num_run_tasks_ = 0; | 193 size_t num_run_tasks_ = 0; |
| 193 | 194 |
| 194 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); | 195 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerThreadTest); |
| 195 }; | 196 }; |
| 196 | 197 |
| 197 // Verify that when GetWorkCallback() continuously returns Sequences, all Tasks | 198 // Verify that when GetWorkCallback() continuously returns Sequences, all Tasks |
| 198 // in these Sequences run successfully. The SchedulerWorkerThread is woken up | 199 // in these Sequences run successfully. The SchedulerWorkerThread is woken up |
| 199 // once. | 200 // once. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // |i| calls in which it returned nullptr. | 233 // |i| calls in which it returned nullptr. |
| 233 const size_t expected_num_get_work_callback = 2 * (i + 1); | 234 const size_t expected_num_get_work_callback = 2 * (i + 1); |
| 234 WaitForNumGetWorkCallback(expected_num_get_work_callback); | 235 WaitForNumGetWorkCallback(expected_num_get_work_callback); |
| 235 EXPECT_EQ(expected_num_get_work_callback, NumGetWorkCallback()); | 236 EXPECT_EQ(expected_num_get_work_callback, NumGetWorkCallback()); |
| 236 } | 237 } |
| 237 } | 238 } |
| 238 | 239 |
| 239 } // namespace | 240 } // namespace |
| 240 } // namespace internal | 241 } // namespace internal |
| 241 } // namespace base | 242 } // namespace base |
| OLD | NEW |