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 21 matching lines...) Expand all Loading... | |
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 UpdateThreadPriority(GetDesiredThreadPriority()); | |
44 | |
42 // Get the sequence containing the next task to execute. | 45 // Get the sequence containing the next task to execute. |
43 scoped_refptr<Sequence> sequence = outer_->delegate_->GetWork(outer_); | 46 scoped_refptr<Sequence> sequence = outer_->delegate_->GetWork(outer_); |
44 if (!sequence) { | 47 if (!sequence) { |
45 if (outer_->delegate_->CanDetach(outer_)) { | 48 if (outer_->delegate_->CanDetach(outer_)) { |
46 detached_thread = outer_->Detach(); | 49 detached_thread = outer_->Detach(); |
47 if (detached_thread) { | 50 if (detached_thread) { |
48 DCHECK_EQ(detached_thread.get(), this); | 51 DCHECK_EQ(detached_thread.get(), this); |
49 PlatformThread::Detach(thread_handle_); | 52 PlatformThread::Detach(thread_handle_); |
50 outer_ = nullptr; | 53 outer_ = nullptr; |
51 break; | 54 break; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 } | 87 } |
85 | 88 |
86 void Join() { PlatformThread::Join(thread_handle_); } | 89 void Join() { PlatformThread::Join(thread_handle_); } |
87 | 90 |
88 void WakeUp() { wake_up_event_.Signal(); } | 91 void WakeUp() { wake_up_event_.Signal(); } |
89 | 92 |
90 bool IsWakeUpPending() { return wake_up_event_.IsSignaled(); } | 93 bool IsWakeUpPending() { return wake_up_event_.IsSignaled(); } |
91 | 94 |
92 private: | 95 private: |
93 Thread(SchedulerWorker* outer) | 96 Thread(SchedulerWorker* outer) |
94 : outer_(outer), | 97 : outer_(outer), |
95 wake_up_event_(WaitableEvent::ResetPolicy::MANUAL, | 98 wake_up_event_(WaitableEvent::ResetPolicy::MANUAL, |
96 WaitableEvent::InitialState::NOT_SIGNALED) { | 99 WaitableEvent::InitialState::NOT_SIGNALED), |
100 current_thread_priority_(GetDesiredThreadPriority()) { | |
97 DCHECK(outer_); | 101 DCHECK(outer_); |
98 } | 102 } |
99 | 103 |
100 void Initialize() { | 104 void Initialize() { |
101 constexpr size_t kDefaultStackSize = 0; | 105 constexpr size_t kDefaultStackSize = 0; |
102 PlatformThread::CreateWithPriority(kDefaultStackSize, this, | 106 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, |
103 &thread_handle_, | 107 current_thread_priority_); |
104 outer_->thread_priority_); | |
105 } | 108 } |
106 | 109 |
107 void WaitForWork() { | 110 void WaitForWork() { |
108 DCHECK(outer_); | 111 DCHECK(outer_); |
109 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout(); | 112 const TimeDelta sleep_time = outer_->delegate_->GetSleepTimeout(); |
110 if (sleep_time.is_max()) { | 113 if (sleep_time.is_max()) { |
111 // Calling TimedWait with TimeDelta::Max is not recommended per | 114 // Calling TimedWait with TimeDelta::Max is not recommended per |
112 // http://crbug.com/465948. | 115 // http://crbug.com/465948. |
113 wake_up_event_.Wait(); | 116 wake_up_event_.Wait(); |
114 } else { | 117 } else { |
115 wake_up_event_.TimedWait(sleep_time); | 118 wake_up_event_.TimedWait(sleep_time); |
116 } | 119 } |
117 wake_up_event_.Reset(); | 120 wake_up_event_.Reset(); |
118 } | 121 } |
119 | 122 |
123 // Returns the desired thread priority based on the worker priority and the | |
124 // current shutdown state. | |
125 ThreadPriority GetDesiredThreadPriority() { | |
126 DCHECK(outer_); | |
127 | |
128 if (outer_->task_tracker_->HasShutdownStarted() && | |
129 static_cast<int>(outer_->thread_priority_) < | |
130 static_cast<int>(ThreadPriority::NORMAL)) { | |
131 return ThreadPriority::NORMAL; | |
132 } | |
133 return outer_->thread_priority_; | |
134 } | |
135 | |
136 void UpdateThreadPriority(ThreadPriority desired_thread_priority) { | |
robliao
2016/07/21 17:05:51
Nit: Use SetThreadPriority.
fdoray
2016/07/21 17:54:53
Done.
gab
2016/07/21 21:29:51
I prefered Update* as it didn't clash with underly
fdoray
2016/07/22 12:56:32
Done. I prefer Update* too.
| |
137 if (desired_thread_priority != current_thread_priority_) { | |
138 PlatformThread::SetCurrentThreadPriority(desired_thread_priority); | |
139 current_thread_priority_ = desired_thread_priority; | |
140 } | |
141 } | |
142 | |
120 PlatformThreadHandle thread_handle_; | 143 PlatformThreadHandle thread_handle_; |
121 | 144 |
122 SchedulerWorker* outer_; | 145 SchedulerWorker* outer_; |
123 | 146 |
124 // Event signaled to wake up this thread. | 147 // Event signaled to wake up this thread. |
125 WaitableEvent wake_up_event_; | 148 WaitableEvent wake_up_event_; |
126 | 149 |
150 // Current priority of this thread. May be different from | |
151 // |outer_->thread_priority_| during shutdown. | |
152 ThreadPriority current_thread_priority_; | |
153 | |
127 DISALLOW_COPY_AND_ASSIGN(Thread); | 154 DISALLOW_COPY_AND_ASSIGN(Thread); |
128 }; | 155 }; |
129 | 156 |
130 std::unique_ptr<SchedulerWorker> SchedulerWorker::Create( | 157 std::unique_ptr<SchedulerWorker> SchedulerWorker::Create( |
131 ThreadPriority thread_priority, | 158 ThreadPriority thread_priority, |
132 std::unique_ptr<Delegate> delegate, | 159 std::unique_ptr<Delegate> delegate, |
133 TaskTracker* task_tracker, | 160 TaskTracker* task_tracker, |
134 InitialState initial_state) { | 161 InitialState initial_state) { |
135 std::unique_ptr<SchedulerWorker> worker( | 162 std::unique_ptr<SchedulerWorker> worker( |
136 new SchedulerWorker(thread_priority, std::move(delegate), task_tracker)); | 163 new SchedulerWorker(thread_priority, std::move(delegate), task_tracker)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 CreateThread(); | 240 CreateThread(); |
214 } | 241 } |
215 | 242 |
216 bool SchedulerWorker::ShouldExitForTesting() const { | 243 bool SchedulerWorker::ShouldExitForTesting() const { |
217 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | 244 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); |
218 return should_exit_for_testing_; | 245 return should_exit_for_testing_; |
219 } | 246 } |
220 | 247 |
221 } // namespace internal | 248 } // namespace internal |
222 } // namespace base | 249 } // namespace base |
OLD | NEW |