Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1011)

Side by Side Diff: base/task_scheduler/scheduler_worker.cc

Issue 2161213002: TaskScheduler: Bump thread priority during shutdown. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self-review Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 21 matching lines...) Expand all
32 std::unique_ptr<Thread> detached_thread; 32 std::unique_ptr<Thread> detached_thread;
33 33
34 outer_->delegate_->OnMainEntry(outer_); 34 outer_->delegate_->OnMainEntry(outer_);
35 35
36 // A SchedulerWorker starts out waiting for work. 36 // A SchedulerWorker starts out waiting for work.
37 WaitForWork(); 37 WaitForWork();
38 38
39 while (!outer_->task_tracker_->IsShutdownComplete() && 39 while (!outer_->task_tracker_->IsShutdownComplete() &&
40 !outer_->ShouldExitForTesting()) { 40 !outer_->ShouldExitForTesting()) {
41 DCHECK(outer_); 41 DCHECK(outer_);
42
43 const ThreadPriority desired_thread_priority = GetDesiredThreadPriority();
44 if (desired_thread_priority != actual_thread_priority_) {
45 PlatformThread::SetCurrentThreadPriority(desired_thread_priority);
46 actual_thread_priority_ = desired_thread_priority;
47 }
48
42 // Get the sequence containing the next task to execute. 49 // Get the sequence containing the next task to execute.
43 scoped_refptr<Sequence> sequence = outer_->delegate_->GetWork(outer_); 50 scoped_refptr<Sequence> sequence = outer_->delegate_->GetWork(outer_);
44 if (!sequence) { 51 if (!sequence) {
45 if (outer_->delegate_->CanDetach(outer_)) { 52 if (outer_->delegate_->CanDetach(outer_)) {
46 detached_thread = outer_->Detach(); 53 detached_thread = outer_->Detach();
47 if (detached_thread) { 54 if (detached_thread) {
48 DCHECK_EQ(detached_thread.get(), this); 55 DCHECK_EQ(detached_thread.get(), this);
49 PlatformThread::Detach(thread_handle_); 56 PlatformThread::Detach(thread_handle_);
50 outer_ = nullptr; 57 outer_ = nullptr;
51 break; 58 break;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 } 91 }
85 92
86 void Join() { PlatformThread::Join(thread_handle_); } 93 void Join() { PlatformThread::Join(thread_handle_); }
87 94
88 void WakeUp() { wake_up_event_.Signal(); } 95 void WakeUp() { wake_up_event_.Signal(); }
89 96
90 bool IsWakeUpPending() { return wake_up_event_.IsSignaled(); } 97 bool IsWakeUpPending() { return wake_up_event_.IsSignaled(); }
91 98
92 private: 99 private:
93 Thread(SchedulerWorker* outer) 100 Thread(SchedulerWorker* outer)
94 : outer_(outer), 101 : outer_(outer),
95 wake_up_event_(WaitableEvent::ResetPolicy::MANUAL, 102 wake_up_event_(WaitableEvent::ResetPolicy::MANUAL,
96 WaitableEvent::InitialState::NOT_SIGNALED) { 103 WaitableEvent::InitialState::NOT_SIGNALED),
104 actual_thread_priority_(GetDesiredThreadPriority()) {
97 DCHECK(outer_); 105 DCHECK(outer_);
98 } 106 }
99 107
100 void Initialize() { 108 void Initialize() {
101 constexpr size_t kDefaultStackSize = 0; 109 constexpr size_t kDefaultStackSize = 0;
102 PlatformThread::CreateWithPriority(kDefaultStackSize, this, 110 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_,
103 &thread_handle_, 111 actual_thread_priority_);
104 outer_->thread_priority_);
105 } 112 }
106 113
107 void WaitForWork() { 114 void WaitForWork() {
108 DCHECK(outer_); 115 DCHECK(outer_);
109 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout(); 116 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout();
110 if (sleep_time.is_max()) { 117 if (sleep_time.is_max()) {
111 // Calling TimedWait with TimeDelta::Max is not recommended per 118 // Calling TimedWait with TimeDelta::Max is not recommended per
112 // http://crbug.com/465948. 119 // http://crbug.com/465948.
113 wake_up_event_.Wait(); 120 wake_up_event_.Wait();
114 } else { 121 } else {
115 wake_up_event_.TimedWait(sleep_time); 122 wake_up_event_.TimedWait(sleep_time);
116 } 123 }
117 wake_up_event_.Reset(); 124 wake_up_event_.Reset();
118 } 125 }
119 126
127 // Returns the desired thread priority based on the worker priority and the
128 // current shutdown state.
129 ThreadPriority GetDesiredThreadPriority() {
130 DCHECK(outer_);
131
132 if (outer_->task_tracker_->HasShutdownStarted() &&
133 static_cast<int>(outer_->thread_priority_) <
134 static_cast<int>(ThreadPriority::NORMAL)) {
135 return ThreadPriority::NORMAL;
136 }
137 return outer_->thread_priority_;
138 }
139
120 PlatformThreadHandle thread_handle_; 140 PlatformThreadHandle thread_handle_;
121 141
122 SchedulerWorker* outer_; 142 SchedulerWorker* outer_;
123 143
124 // Event signaled to wake up this thread. 144 // Event signaled to wake up this thread.
125 WaitableEvent wake_up_event_; 145 WaitableEvent wake_up_event_;
126 146
147 // Actual priority of this thread. May be different from
148 // |outer_->thread_priority_| during shutdown.
149 ThreadPriority actual_thread_priority_;
robliao 2016/07/20 22:31:48 Use current_thread_priority since that's what it's
fdoray 2016/07/21 13:21:56 Done.
150
127 DISALLOW_COPY_AND_ASSIGN(Thread); 151 DISALLOW_COPY_AND_ASSIGN(Thread);
128 }; 152 };
129 153
130 std::unique_ptr<SchedulerWorker> SchedulerWorker::Create( 154 std::unique_ptr<SchedulerWorker> SchedulerWorker::Create(
131 ThreadPriority thread_priority, 155 ThreadPriority thread_priority,
132 std::unique_ptr<Delegate> delegate, 156 std::unique_ptr<Delegate> delegate,
133 TaskTracker* task_tracker, 157 TaskTracker* task_tracker,
134 InitialState initial_state) { 158 InitialState initial_state) {
135 std::unique_ptr<SchedulerWorker> worker( 159 std::unique_ptr<SchedulerWorker> worker(
136 new SchedulerWorker(thread_priority, std::move(delegate), task_tracker)); 160 new SchedulerWorker(thread_priority, std::move(delegate), task_tracker));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 CreateThread(); 237 CreateThread();
214 } 238 }
215 239
216 bool SchedulerWorker::ShouldExitForTesting() const { 240 bool SchedulerWorker::ShouldExitForTesting() const {
217 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); 241 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_);
218 return should_exit_for_testing_; 242 return should_exit_for_testing_;
219 } 243 }
220 244
221 } // namespace internal 245 } // namespace internal
222 } // namespace base 246 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/task_scheduler/scheduler_worker_unittest.cc » ('j') | base/task_scheduler/scheduler_worker_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698