| 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 <memory> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 num_get_work_cv_(lock_.CreateConditionVariable()) {} | 35 num_get_work_cv_(lock_.CreateConditionVariable()) {} |
| 36 | 36 |
| 37 void SetUp() override { | 37 void SetUp() override { |
| 38 worker_thread_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( | 38 worker_thread_ = SchedulerWorkerThread::CreateSchedulerWorkerThread( |
| 39 ThreadPriority::NORMAL, this, &task_tracker_); | 39 ThreadPriority::NORMAL, this, &task_tracker_); |
| 40 ASSERT_TRUE(worker_thread_); | 40 ASSERT_TRUE(worker_thread_); |
| 41 main_entry_called_.Wait(); | 41 main_entry_called_.Wait(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void TearDown() override { | 44 void TearDown() override { |
| 45 { | |
| 46 AutoSchedulerLock auto_lock(lock_); | |
| 47 EXPECT_FALSE(main_exit_called_); | |
| 48 } | |
| 49 | |
| 50 worker_thread_->JoinForTesting(); | 45 worker_thread_->JoinForTesting(); |
| 51 | |
| 52 AutoSchedulerLock auto_lock(lock_); | |
| 53 EXPECT_TRUE(main_exit_called_); | |
| 54 } | 46 } |
| 55 | 47 |
| 56 size_t TasksPerSequence() const { return GetParam(); } | 48 size_t TasksPerSequence() const { return GetParam(); } |
| 57 | 49 |
| 58 // Wait until GetWork() has been called |num_get_work| times. | 50 // Wait until GetWork() has been called |num_get_work| times. |
| 59 void WaitForNumGetWork(size_t num_get_work) { | 51 void WaitForNumGetWork(size_t num_get_work) { |
| 60 AutoSchedulerLock auto_lock(lock_); | 52 AutoSchedulerLock auto_lock(lock_); |
| 61 while (num_get_work_ < num_get_work) | 53 while (num_get_work_ < num_get_work) |
| 62 num_get_work_cv_->Wait(); | 54 num_get_work_cv_->Wait(); |
| 63 } | 55 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 93 private: | 85 private: |
| 94 // SchedulerWorkerThread::Delegate: | 86 // SchedulerWorkerThread::Delegate: |
| 95 void OnMainEntry() override { | 87 void OnMainEntry() override { |
| 96 // Without this |auto_lock|, OnMainEntry() could be called twice without | 88 // Without this |auto_lock|, OnMainEntry() could be called twice without |
| 97 // generating an error. | 89 // generating an error. |
| 98 AutoSchedulerLock auto_lock(lock_); | 90 AutoSchedulerLock auto_lock(lock_); |
| 99 EXPECT_FALSE(main_entry_called_.IsSignaled()); | 91 EXPECT_FALSE(main_entry_called_.IsSignaled()); |
| 100 main_entry_called_.Signal(); | 92 main_entry_called_.Signal(); |
| 101 } | 93 } |
| 102 | 94 |
| 103 void OnMainExit() override { | |
| 104 AutoSchedulerLock auto_lock(lock_); | |
| 105 EXPECT_FALSE(main_exit_called_); | |
| 106 main_exit_called_ = true; | |
| 107 } | |
| 108 | |
| 109 scoped_refptr<Sequence> GetWork( | 95 scoped_refptr<Sequence> GetWork( |
| 110 SchedulerWorkerThread* worker_thread) override { | 96 SchedulerWorkerThread* worker_thread) override { |
| 111 EXPECT_EQ(worker_thread_.get(), worker_thread); | 97 EXPECT_EQ(worker_thread_.get(), worker_thread); |
| 112 | 98 |
| 113 { | 99 { |
| 114 AutoSchedulerLock auto_lock(lock_); | 100 AutoSchedulerLock auto_lock(lock_); |
| 115 | 101 |
| 116 // Increment the number of times that this method has been called. | 102 // Increment the number of times that this method has been called. |
| 117 ++num_get_work_; | 103 ++num_get_work_; |
| 118 num_get_work_cv_->Signal(); | 104 num_get_work_cv_->Signal(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 159 } |
| 174 | 160 |
| 175 TaskTracker task_tracker_; | 161 TaskTracker task_tracker_; |
| 176 | 162 |
| 177 // Synchronizes access to all members below. | 163 // Synchronizes access to all members below. |
| 178 mutable SchedulerLock lock_; | 164 mutable SchedulerLock lock_; |
| 179 | 165 |
| 180 // Signaled once OnMainEntry() has been called. | 166 // Signaled once OnMainEntry() has been called. |
| 181 WaitableEvent main_entry_called_; | 167 WaitableEvent main_entry_called_; |
| 182 | 168 |
| 183 // True once OnMainExit() has been called. | |
| 184 bool main_exit_called_ = false; | |
| 185 | |
| 186 // Number of Sequences that should be created by GetWork(). When this | 169 // Number of Sequences that should be created by GetWork(). When this |
| 187 // is 0, GetWork() returns nullptr. | 170 // is 0, GetWork() returns nullptr. |
| 188 size_t num_sequences_to_create_ = 0; | 171 size_t num_sequences_to_create_ = 0; |
| 189 | 172 |
| 190 // Number of times that GetWork() has been called. | 173 // Number of times that GetWork() has been called. |
| 191 size_t num_get_work_ = 0; | 174 size_t num_get_work_ = 0; |
| 192 | 175 |
| 193 // Maximum number of times that GetWork() can be called. | 176 // Maximum number of times that GetWork() can be called. |
| 194 size_t max_get_work_ = 0; | 177 size_t max_get_work_ = 0; |
| 195 | 178 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 INSTANTIATE_TEST_CASE_P(OneTaskPerSequence, | 255 INSTANTIATE_TEST_CASE_P(OneTaskPerSequence, |
| 273 TaskSchedulerWorkerThreadTest, | 256 TaskSchedulerWorkerThreadTest, |
| 274 ::testing::Values(1)); | 257 ::testing::Values(1)); |
| 275 INSTANTIATE_TEST_CASE_P(TwoTasksPerSequence, | 258 INSTANTIATE_TEST_CASE_P(TwoTasksPerSequence, |
| 276 TaskSchedulerWorkerThreadTest, | 259 TaskSchedulerWorkerThreadTest, |
| 277 ::testing::Values(2)); | 260 ::testing::Values(2)); |
| 278 | 261 |
| 279 } // namespace | 262 } // namespace |
| 280 } // namespace internal | 263 } // namespace internal |
| 281 } // namespace base | 264 } // namespace base |
| OLD | NEW |