| 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 <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 // PlatformThread::Delegate. | 36 // PlatformThread::Delegate. |
| 37 void ThreadMain() override { | 37 void ThreadMain() override { |
| 38 // Set if this thread was detached. | 38 // Set if this thread was detached. |
| 39 std::unique_ptr<Thread> detached_thread; | 39 std::unique_ptr<Thread> detached_thread; |
| 40 | 40 |
| 41 outer_->delegate_->OnMainEntry(outer_.get()); | 41 outer_->delegate_->OnMainEntry(outer_.get()); |
| 42 | 42 |
| 43 // A SchedulerWorker starts out waiting for work. | 43 // A SchedulerWorker starts out waiting for work. |
| 44 WaitForWork(); | 44 outer_->delegate_->WaitForWork(&wake_up_event_); |
| 45 | 45 |
| 46 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
| 47 std::unique_ptr<win::ScopedCOMInitializer> com_initializer; | 47 std::unique_ptr<win::ScopedCOMInitializer> com_initializer; |
| 48 if (outer_->backward_compatibility_ == | 48 if (outer_->backward_compatibility_ == |
| 49 SchedulerBackwardCompatibility::INIT_COM_STA) { | 49 SchedulerBackwardCompatibility::INIT_COM_STA) { |
| 50 com_initializer = MakeUnique<win::ScopedCOMInitializer>(); | 50 com_initializer = MakeUnique<win::ScopedCOMInitializer>(); |
| 51 } | 51 } |
| 52 #endif | 52 #endif |
| 53 | 53 |
| 54 while (!outer_->ShouldExit()) { | 54 while (!outer_->ShouldExit()) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 65 outer_->delegate_->GetWork(outer_.get()); | 65 outer_->delegate_->GetWork(outer_.get()); |
| 66 if (!sequence) { | 66 if (!sequence) { |
| 67 if (outer_->delegate_->CanDetach(outer_.get())) { | 67 if (outer_->delegate_->CanDetach(outer_.get())) { |
| 68 detached_thread = outer_->DetachThreadObject(DetachNotify::DELEGATE); | 68 detached_thread = outer_->DetachThreadObject(DetachNotify::DELEGATE); |
| 69 if (detached_thread) { | 69 if (detached_thread) { |
| 70 DCHECK_EQ(detached_thread.get(), this); | 70 DCHECK_EQ(detached_thread.get(), this); |
| 71 PlatformThread::Detach(thread_handle_); | 71 PlatformThread::Detach(thread_handle_); |
| 72 break; | 72 break; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 WaitForWork(); | 75 outer_->delegate_->WaitForWork(&wake_up_event_); |
| 76 continue; | 76 continue; |
| 77 } | 77 } |
| 78 | 78 |
| 79 if (outer_->task_tracker_->RunTask(sequence->TakeTask(), | 79 if (outer_->task_tracker_->RunTask(sequence->TakeTask(), |
| 80 sequence->token())) { | 80 sequence->token())) { |
| 81 outer_->delegate_->DidRunTask(); | 81 outer_->delegate_->DidRunTask(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 const bool sequence_became_empty = sequence->Pop(); | 84 const bool sequence_became_empty = sequence->Pop(); |
| 85 | 85 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 current_thread_priority_(GetDesiredThreadPriority()) { | 136 current_thread_priority_(GetDesiredThreadPriority()) { |
| 137 DCHECK(outer_); | 137 DCHECK(outer_); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void Initialize() { | 140 void Initialize() { |
| 141 constexpr size_t kDefaultStackSize = 0; | 141 constexpr size_t kDefaultStackSize = 0; |
| 142 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | 142 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, |
| 143 current_thread_priority_); | 143 current_thread_priority_); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void WaitForWork() { | |
| 147 DCHECK(outer_); | |
| 148 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout(); | |
| 149 if (sleep_time.is_max()) { | |
| 150 // Calling TimedWait with TimeDelta::Max is not recommended per | |
| 151 // http://crbug.com/465948. | |
| 152 wake_up_event_.Wait(); | |
| 153 } else { | |
| 154 wake_up_event_.TimedWait(sleep_time); | |
| 155 } | |
| 156 wake_up_event_.Reset(); | |
| 157 } | |
| 158 | |
| 159 // Returns the priority for which the thread should be set based on the | 146 // Returns the priority for which the thread should be set based on the |
| 160 // priority hint, current shutdown state, and platform capabilities. | 147 // priority hint, current shutdown state, and platform capabilities. |
| 161 ThreadPriority GetDesiredThreadPriority() { | 148 ThreadPriority GetDesiredThreadPriority() { |
| 162 DCHECK(outer_); | 149 DCHECK(outer_); |
| 163 | 150 |
| 164 // All threads have a NORMAL priority when Lock doesn't handle multiple | 151 // All threads have a NORMAL priority when Lock doesn't handle multiple |
| 165 // thread priorities. | 152 // thread priorities. |
| 166 if (!Lock::HandlesMultipleThreadPriorities()) | 153 if (!Lock::HandlesMultipleThreadPriorities()) |
| 167 return ThreadPriority::NORMAL; | 154 return ThreadPriority::NORMAL; |
| 168 | 155 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 194 // Event signaled to wake up this thread. | 181 // Event signaled to wake up this thread. |
| 195 WaitableEvent wake_up_event_; | 182 WaitableEvent wake_up_event_; |
| 196 | 183 |
| 197 // Current priority of this thread. May be different from | 184 // Current priority of this thread. May be different from |
| 198 // |outer_->priority_hint_|. | 185 // |outer_->priority_hint_|. |
| 199 ThreadPriority current_thread_priority_; | 186 ThreadPriority current_thread_priority_; |
| 200 | 187 |
| 201 DISALLOW_COPY_AND_ASSIGN(Thread); | 188 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 202 }; | 189 }; |
| 203 | 190 |
| 191 void SchedulerWorker::Delegate::WaitForWork(WaitableEvent* wake_up_event) { |
| 192 DCHECK(wake_up_event); |
| 193 const TimeDelta sleep_time = GetSleepTimeout(); |
| 194 if (sleep_time.is_max()) { |
| 195 // Calling TimedWait with TimeDelta::Max is not recommended per |
| 196 // http://crbug.com/465948. |
| 197 wake_up_event->Wait(); |
| 198 } else { |
| 199 wake_up_event->TimedWait(sleep_time); |
| 200 } |
| 201 wake_up_event->Reset(); |
| 202 } |
| 203 |
| 204 scoped_refptr<SchedulerWorker> SchedulerWorker::Create( | 204 scoped_refptr<SchedulerWorker> SchedulerWorker::Create( |
| 205 ThreadPriority priority_hint, | 205 ThreadPriority priority_hint, |
| 206 std::unique_ptr<Delegate> delegate, | 206 std::unique_ptr<Delegate> delegate, |
| 207 TaskTracker* task_tracker, | 207 TaskTracker* task_tracker, |
| 208 InitialState initial_state, | 208 InitialState initial_state, |
| 209 SchedulerBackwardCompatibility backward_compatibility) { | 209 SchedulerBackwardCompatibility backward_compatibility) { |
| 210 scoped_refptr<SchedulerWorker> worker( | 210 scoped_refptr<SchedulerWorker> worker( |
| 211 new SchedulerWorker(priority_hint, std::move(delegate), task_tracker, | 211 new SchedulerWorker(priority_hint, std::move(delegate), task_tracker, |
| 212 backward_compatibility)); | 212 backward_compatibility)); |
| 213 // Creation happens before any other thread can reference this one, so no | 213 // Creation happens before any other thread can reference this one, so no |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 CreateThread(); | 330 CreateThread(); |
| 331 } | 331 } |
| 332 | 332 |
| 333 bool SchedulerWorker::ShouldExit() { | 333 bool SchedulerWorker::ShouldExit() { |
| 334 return task_tracker_->IsShutdownComplete() || | 334 return task_tracker_->IsShutdownComplete() || |
| 335 join_called_for_testing_.IsSet() || should_exit_.IsSet(); | 335 join_called_for_testing_.IsSet() || should_exit_.IsSet(); |
| 336 } | 336 } |
| 337 | 337 |
| 338 } // namespace internal | 338 } // namespace internal |
| 339 } // namespace base | 339 } // namespace base |
| OLD | NEW |