OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/task_scheduler/scheduler_thread_pool_impl.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <algorithm> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/bind_helpers.h" | |
14 #include "base/lazy_instance.h" | |
15 #include "base/memory/ptr_util.h" | |
16 #include "base/sequenced_task_runner.h" | |
17 #include "base/single_thread_task_runner.h" | |
18 #include "base/strings/stringprintf.h" | |
19 #include "base/task_scheduler/delayed_task_manager.h" | |
20 #include "base/task_scheduler/task_tracker.h" | |
21 #include "base/threading/platform_thread.h" | |
22 #include "base/threading/thread_local.h" | |
23 #include "base/threading/thread_restrictions.h" | |
24 | |
25 namespace base { | |
26 namespace internal { | |
27 | |
28 namespace { | |
29 | |
30 // SchedulerThreadPool that owns the current thread, if any. | |
31 LazyInstance<ThreadLocalPointer<const SchedulerThreadPool>>::Leaky | |
32 tls_current_thread_pool = LAZY_INSTANCE_INITIALIZER; | |
33 | |
34 // SchedulerWorkerThread that owns the current thread, if any. | |
35 LazyInstance<ThreadLocalPointer<const SchedulerWorkerThread>>::Leaky | |
36 tls_current_worker_thread = LAZY_INSTANCE_INITIALIZER; | |
37 | |
38 // A task runner that runs tasks with the PARALLEL ExecutionMode. | |
39 class SchedulerParallelTaskRunner : public TaskRunner { | |
40 public: | |
41 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so | |
42 // long as |thread_pool| is alive. | |
43 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. | |
44 SchedulerParallelTaskRunner(const TaskTraits& traits, | |
45 SchedulerThreadPool* thread_pool) | |
46 : traits_(traits), thread_pool_(thread_pool) {} | |
47 | |
48 // TaskRunner: | |
49 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
50 const Closure& closure, | |
51 TimeDelta delay) override { | |
52 // Post the task as part of a one-off single-task Sequence. | |
53 return thread_pool_->PostTaskWithSequence( | |
54 WrapUnique(new Task(from_here, closure, traits_, delay)), | |
55 make_scoped_refptr(new Sequence), nullptr); | |
56 } | |
57 | |
58 bool RunsTasksOnCurrentThread() const override { | |
59 return tls_current_thread_pool.Get().Get() == thread_pool_; | |
60 } | |
61 | |
62 private: | |
63 ~SchedulerParallelTaskRunner() override = default; | |
64 | |
65 const TaskTraits traits_; | |
66 SchedulerThreadPool* const thread_pool_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(SchedulerParallelTaskRunner); | |
69 }; | |
70 | |
71 // A task runner that runs tasks with the SEQUENCED ExecutionMode. | |
72 class SchedulerSequencedTaskRunner : public SequencedTaskRunner { | |
73 public: | |
74 // Constructs a SchedulerSequencedTaskRunner which can be used to post tasks | |
75 // so long as |thread_pool| is alive. | |
76 // TODO(robliao): Find a concrete way to manage |thread_pool|'s memory. | |
77 SchedulerSequencedTaskRunner(const TaskTraits& traits, | |
78 SchedulerThreadPool* thread_pool) | |
79 : traits_(traits), thread_pool_(thread_pool) {} | |
80 | |
81 // SequencedTaskRunner: | |
82 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
83 const Closure& closure, | |
84 TimeDelta delay) override { | |
85 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); | |
86 task->sequenced_task_runner_ref = this; | |
87 | |
88 // Post the task as part of |sequence_|. | |
89 return thread_pool_->PostTaskWithSequence(std::move(task), sequence_, | |
90 nullptr); | |
91 } | |
92 | |
93 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
94 const Closure& closure, | |
95 base::TimeDelta delay) override { | |
96 // Tasks are never nested within the task scheduler. | |
97 return PostDelayedTask(from_here, closure, delay); | |
98 } | |
99 | |
100 bool RunsTasksOnCurrentThread() const override { | |
101 return tls_current_thread_pool.Get().Get() == thread_pool_; | |
102 } | |
103 | |
104 private: | |
105 ~SchedulerSequencedTaskRunner() override = default; | |
106 | |
107 // Sequence for all Tasks posted through this TaskRunner. | |
108 const scoped_refptr<Sequence> sequence_ = new Sequence; | |
109 | |
110 const TaskTraits traits_; | |
111 SchedulerThreadPool* const thread_pool_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(SchedulerSequencedTaskRunner); | |
114 }; | |
115 | |
116 // A task runner that runs tasks with the SINGLE_THREADED ExecutionMode. | |
117 class SchedulerSingleThreadTaskRunner : public SingleThreadTaskRunner { | |
118 public: | |
119 // Constructs a SchedulerSingleThreadTaskRunner which can be used to post | |
120 // tasks so long as |thread_pool| and |worker_thread| are alive. | |
121 // TODO(robliao): Find a concrete way to manage the memory of |thread_pool| | |
122 // and |worker_thread|. | |
123 SchedulerSingleThreadTaskRunner(const TaskTraits& traits, | |
124 SchedulerThreadPool* thread_pool, | |
125 SchedulerWorkerThread* worker_thread) | |
126 : traits_(traits), | |
127 thread_pool_(thread_pool), | |
128 worker_thread_(worker_thread) {} | |
129 | |
130 // SingleThreadTaskRunner: | |
131 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
132 const Closure& closure, | |
133 TimeDelta delay) override { | |
134 std::unique_ptr<Task> task(new Task(from_here, closure, traits_, delay)); | |
135 task->single_thread_task_runner_ref = this; | |
136 | |
137 // Post the task to be executed by |worker_thread_| as part of |sequence_|. | |
138 return thread_pool_->PostTaskWithSequence(std::move(task), sequence_, | |
139 worker_thread_); | |
140 } | |
141 | |
142 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
143 const Closure& closure, | |
144 base::TimeDelta delay) override { | |
145 // Tasks are never nested within the task scheduler. | |
146 return PostDelayedTask(from_here, closure, delay); | |
147 } | |
148 | |
149 bool RunsTasksOnCurrentThread() const override { | |
150 return tls_current_worker_thread.Get().Get() == worker_thread_; | |
151 } | |
152 | |
153 private: | |
154 ~SchedulerSingleThreadTaskRunner() override = default; | |
155 | |
156 // Sequence for all Tasks posted through this TaskRunner. | |
157 const scoped_refptr<Sequence> sequence_ = new Sequence; | |
158 | |
159 const TaskTraits traits_; | |
160 SchedulerThreadPool* const thread_pool_; | |
161 SchedulerWorkerThread* const worker_thread_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(SchedulerSingleThreadTaskRunner); | |
164 }; | |
165 | |
166 // Only used in DCHECKs. | |
167 bool ContainsWorkerThread( | |
168 const std::vector<std::unique_ptr<SchedulerWorkerThread>>& worker_threads, | |
169 const SchedulerWorkerThread* worker_thread) { | |
170 auto it = std::find_if( | |
171 worker_threads.begin(), worker_threads.end(), | |
172 [worker_thread](const std::unique_ptr<SchedulerWorkerThread>& i) { | |
173 return i.get() == worker_thread; | |
174 }); | |
175 return it != worker_threads.end(); | |
176 } | |
177 | |
178 } // namespace | |
179 | |
180 class SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl | |
181 : public SchedulerWorkerThread::Delegate { | |
182 public: | |
183 // |outer| owns the worker thread for which this delegate is constructed. | |
184 // |re_enqueue_sequence_callback| is invoked when ReEnqueueSequence() is | |
185 // called with a non-single-threaded Sequence. |shared_priority_queue| is a | |
186 // PriorityQueue whose transactions may overlap with the worker thread's | |
187 // single-threaded PriorityQueue's transactions. |index| will be appended to | |
188 // this thread's name to uniquely identify it. | |
189 SchedulerWorkerThreadDelegateImpl( | |
190 SchedulerThreadPoolImpl* outer, | |
191 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | |
192 const PriorityQueue* shared_priority_queue, | |
193 int index); | |
194 ~SchedulerWorkerThreadDelegateImpl() override; | |
195 | |
196 PriorityQueue* single_threaded_priority_queue() { | |
197 return &single_threaded_priority_queue_; | |
198 } | |
199 | |
200 // SchedulerWorkerThread::Delegate: | |
201 void OnMainEntry(SchedulerWorkerThread* worker_thread) override; | |
202 scoped_refptr<Sequence> GetWork( | |
203 SchedulerWorkerThread* worker_thread) override; | |
204 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override; | |
205 TimeDelta GetSleepTimeout() override; | |
206 | |
207 private: | |
208 SchedulerThreadPoolImpl* outer_; | |
209 const ReEnqueueSequenceCallback re_enqueue_sequence_callback_; | |
210 | |
211 // Single-threaded PriorityQueue for the worker thread. | |
212 PriorityQueue single_threaded_priority_queue_; | |
213 | |
214 // True if the last Sequence returned by GetWork() was extracted from | |
215 // |single_threaded_priority_queue_|. | |
216 bool last_sequence_is_single_threaded_ = false; | |
217 | |
218 const int index_; | |
219 | |
220 DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerThreadDelegateImpl); | |
221 }; | |
222 | |
223 SchedulerThreadPoolImpl::~SchedulerThreadPoolImpl() { | |
224 // SchedulerThreadPool should never be deleted in production unless its | |
225 // initialization failed. | |
226 DCHECK(join_for_testing_returned_.IsSignaled() || worker_threads_.empty()); | |
227 } | |
228 | |
229 // static | |
230 std::unique_ptr<SchedulerThreadPoolImpl> SchedulerThreadPoolImpl::Create( | |
231 StringPiece name, | |
232 ThreadPriority thread_priority, | |
233 size_t max_threads, | |
234 IORestriction io_restriction, | |
235 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | |
236 TaskTracker* task_tracker, | |
237 DelayedTaskManager* delayed_task_manager) { | |
238 std::unique_ptr<SchedulerThreadPoolImpl> thread_pool( | |
239 new SchedulerThreadPoolImpl(name, io_restriction, task_tracker, | |
240 delayed_task_manager)); | |
241 if (thread_pool->Initialize(thread_priority, max_threads, | |
242 re_enqueue_sequence_callback)) { | |
243 return thread_pool; | |
244 } | |
245 return nullptr; | |
246 } | |
247 | |
248 void SchedulerThreadPoolImpl::WaitForAllWorkerThreadsIdleForTesting() { | |
249 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
250 while (idle_worker_threads_stack_.Size() < worker_threads_.size()) | |
251 idle_worker_threads_stack_cv_for_testing_->Wait(); | |
252 } | |
253 | |
254 void SchedulerThreadPoolImpl::JoinForTesting() { | |
255 for (const auto& worker_thread : worker_threads_) | |
256 worker_thread->JoinForTesting(); | |
257 | |
258 DCHECK(!join_for_testing_returned_.IsSignaled()); | |
259 join_for_testing_returned_.Signal(); | |
260 } | |
261 | |
262 scoped_refptr<TaskRunner> SchedulerThreadPoolImpl::CreateTaskRunnerWithTraits( | |
263 const TaskTraits& traits, | |
264 ExecutionMode execution_mode) { | |
265 switch (execution_mode) { | |
266 case ExecutionMode::PARALLEL: | |
267 return make_scoped_refptr(new SchedulerParallelTaskRunner(traits, this)); | |
268 | |
269 case ExecutionMode::SEQUENCED: | |
270 return make_scoped_refptr(new SchedulerSequencedTaskRunner(traits, this)); | |
271 | |
272 case ExecutionMode::SINGLE_THREADED: { | |
273 // TODO(fdoray): Find a way to take load into account when assigning a | |
274 // SchedulerWorkerThread to a SingleThreadTaskRunner. Also, this code | |
275 // assumes that all SchedulerWorkerThreads are alive. Eventually, we might | |
276 // decide to tear down threads that haven't run tasks for a long time. | |
277 size_t worker_thread_index; | |
278 { | |
279 AutoSchedulerLock auto_lock(next_worker_thread_index_lock_); | |
280 worker_thread_index = next_worker_thread_index_; | |
281 next_worker_thread_index_ = | |
282 (next_worker_thread_index_ + 1) % worker_threads_.size(); | |
283 } | |
284 return make_scoped_refptr(new SchedulerSingleThreadTaskRunner( | |
285 traits, this, worker_threads_[worker_thread_index].get())); | |
286 } | |
287 } | |
288 | |
289 NOTREACHED(); | |
290 return nullptr; | |
291 } | |
292 | |
293 void SchedulerThreadPoolImpl::ReEnqueueSequence( | |
294 scoped_refptr<Sequence> sequence, | |
295 const SequenceSortKey& sequence_sort_key) { | |
296 shared_priority_queue_.BeginTransaction()->Push(std::move(sequence), | |
297 sequence_sort_key); | |
298 | |
299 // The thread calling this method just ran a Task from |sequence| and will | |
300 // soon try to get another Sequence from which to run a Task. If the thread | |
301 // belongs to this pool, it will get that Sequence from | |
302 // |shared_priority_queue_|. When that's the case, there is no need to wake up | |
303 // another thread after |sequence| is inserted in |shared_priority_queue_|. If | |
304 // we did wake up another thread, we would waste resources by having more | |
305 // threads trying to get a Sequence from |shared_priority_queue_| than the | |
306 // number of Sequences in it. | |
307 if (tls_current_thread_pool.Get().Get() != this) | |
308 WakeUpOneThread(); | |
309 } | |
310 | |
311 bool SchedulerThreadPoolImpl::PostTaskWithSequence( | |
312 std::unique_ptr<Task> task, | |
313 scoped_refptr<Sequence> sequence, | |
314 SchedulerWorkerThread* worker_thread) { | |
315 DCHECK(task); | |
316 DCHECK(sequence); | |
317 DCHECK(!worker_thread || | |
318 ContainsWorkerThread(worker_threads_, worker_thread)); | |
319 | |
320 if (!task_tracker_->WillPostTask(task.get())) | |
321 return false; | |
322 | |
323 if (task->delayed_run_time.is_null()) { | |
324 PostTaskWithSequenceNow(std::move(task), std::move(sequence), | |
325 worker_thread); | |
326 } else { | |
327 delayed_task_manager_->AddDelayedTask(std::move(task), std::move(sequence), | |
328 worker_thread, this); | |
329 } | |
330 | |
331 return true; | |
332 } | |
333 | |
334 void SchedulerThreadPoolImpl::PostTaskWithSequenceNow( | |
335 std::unique_ptr<Task> task, | |
336 scoped_refptr<Sequence> sequence, | |
337 SchedulerWorkerThread* worker_thread) { | |
338 DCHECK(task); | |
339 DCHECK(sequence); | |
340 DCHECK(!worker_thread || | |
341 ContainsWorkerThread(worker_threads_, worker_thread)); | |
342 | |
343 // Confirm that |task| is ready to run (its delayed run time is either null or | |
344 // in the past). | |
345 DCHECK_LE(task->delayed_run_time, delayed_task_manager_->Now()); | |
346 | |
347 // Because |worker_thread| belongs to this thread pool, we know that the type | |
348 // of its delegate is SchedulerWorkerThreadDelegateImpl. | |
349 PriorityQueue* const priority_queue = | |
350 worker_thread | |
351 ? static_cast<SchedulerWorkerThreadDelegateImpl*>( | |
352 worker_thread->delegate()) | |
353 ->single_threaded_priority_queue() | |
354 : &shared_priority_queue_; | |
355 DCHECK(priority_queue); | |
356 | |
357 const bool sequence_was_empty = sequence->PushTask(std::move(task)); | |
358 if (sequence_was_empty) { | |
359 // Insert |sequence| in |priority_queue| if it was empty before |task| was | |
360 // inserted into it. Otherwise, one of these must be true: | |
361 // - |sequence| is already in a PriorityQueue (not necessarily | |
362 // |shared_priority_queue_|), or, | |
363 // - A worker thread is running a Task from |sequence|. It will insert | |
364 // |sequence| in a PriorityQueue once it's done running the Task. | |
365 const auto sequence_sort_key = sequence->GetSortKey(); | |
366 priority_queue->BeginTransaction()->Push(std::move(sequence), | |
367 sequence_sort_key); | |
368 | |
369 // Wake up a worker thread to process |sequence|. | |
370 if (worker_thread) | |
371 worker_thread->WakeUp(); | |
372 else | |
373 WakeUpOneThread(); | |
374 } | |
375 } | |
376 | |
377 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | |
378 SchedulerWorkerThreadDelegateImpl( | |
379 SchedulerThreadPoolImpl* outer, | |
380 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback, | |
381 const PriorityQueue* shared_priority_queue, | |
382 int index) | |
383 : outer_(outer), | |
384 re_enqueue_sequence_callback_(re_enqueue_sequence_callback), | |
385 single_threaded_priority_queue_(shared_priority_queue), | |
386 index_(index) {} | |
387 | |
388 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | |
389 ~SchedulerWorkerThreadDelegateImpl() = default; | |
390 | |
391 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::OnMainEntry( | |
392 SchedulerWorkerThread* worker_thread) { | |
393 #if DCHECK_IS_ON() | |
394 // Wait for |outer_->threads_created_| to avoid traversing | |
395 // |outer_->worker_threads_| while it is being filled by Initialize(). | |
396 outer_->threads_created_.Wait(); | |
397 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); | |
398 #endif | |
399 | |
400 PlatformThread::SetName( | |
401 StringPrintf("%sWorker%d", outer_->name_.c_str(), index_)); | |
402 | |
403 DCHECK(!tls_current_worker_thread.Get().Get()); | |
404 DCHECK(!tls_current_thread_pool.Get().Get()); | |
405 tls_current_worker_thread.Get().Set(worker_thread); | |
406 tls_current_thread_pool.Get().Set(outer_); | |
407 | |
408 ThreadRestrictions::SetIOAllowed(outer_->io_restriction_ == | |
409 IORestriction::ALLOWED); | |
410 } | |
411 | |
412 scoped_refptr<Sequence> | |
413 SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork( | |
414 SchedulerWorkerThread* worker_thread) { | |
415 DCHECK(ContainsWorkerThread(outer_->worker_threads_, worker_thread)); | |
416 | |
417 scoped_refptr<Sequence> sequence; | |
418 { | |
419 std::unique_ptr<PriorityQueue::Transaction> shared_transaction( | |
420 outer_->shared_priority_queue_.BeginTransaction()); | |
421 std::unique_ptr<PriorityQueue::Transaction> single_threaded_transaction( | |
422 single_threaded_priority_queue_.BeginTransaction()); | |
423 | |
424 if (shared_transaction->IsEmpty() && | |
425 single_threaded_transaction->IsEmpty()) { | |
426 single_threaded_transaction.reset(); | |
427 | |
428 // |shared_transaction| is kept alive while |worker_thread| is added to | |
429 // |idle_worker_threads_stack_| to avoid this race: | |
430 // 1. This thread creates a Transaction, finds |shared_priority_queue_| | |
431 // empty and ends the Transaction. | |
432 // 2. Other thread creates a Transaction, inserts a Sequence into | |
433 // |shared_priority_queue_| and ends the Transaction. This can't happen | |
434 // if the Transaction of step 1 is still active because because there | |
435 // can only be one active Transaction per PriorityQueue at a time. | |
436 // 3. Other thread calls WakeUpOneThread(). No thread is woken up because | |
437 // |idle_worker_threads_stack_| is empty. | |
438 // 4. This thread adds itself to |idle_worker_threads_stack_| and goes to | |
439 // sleep. No thread runs the Sequence inserted in step 2. | |
440 outer_->AddToIdleWorkerThreadsStack(worker_thread); | |
441 return nullptr; | |
442 } | |
443 | |
444 // True if both PriorityQueues have Sequences and the Sequence at the top of | |
445 // the shared PriorityQueue is more important. | |
446 const bool shared_sequence_is_more_important = | |
447 !shared_transaction->IsEmpty() && | |
448 !single_threaded_transaction->IsEmpty() && | |
449 shared_transaction->PeekSortKey() > | |
450 single_threaded_transaction->PeekSortKey(); | |
451 | |
452 if (single_threaded_transaction->IsEmpty() || | |
453 shared_sequence_is_more_important) { | |
454 sequence = shared_transaction->PopSequence(); | |
455 last_sequence_is_single_threaded_ = false; | |
456 } else { | |
457 DCHECK(!single_threaded_transaction->IsEmpty()); | |
458 sequence = single_threaded_transaction->PopSequence(); | |
459 last_sequence_is_single_threaded_ = true; | |
460 } | |
461 } | |
462 DCHECK(sequence); | |
463 | |
464 outer_->RemoveFromIdleWorkerThreadsStack(worker_thread); | |
465 return sequence; | |
466 } | |
467 | |
468 void SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | |
469 ReEnqueueSequence(scoped_refptr<Sequence> sequence) { | |
470 if (last_sequence_is_single_threaded_) { | |
471 // A single-threaded Sequence is always re-enqueued in the single-threaded | |
472 // PriorityQueue from which it was extracted. | |
473 const SequenceSortKey sequence_sort_key = sequence->GetSortKey(); | |
474 single_threaded_priority_queue_.BeginTransaction()->Push( | |
475 std::move(sequence), sequence_sort_key); | |
476 } else { | |
477 // |re_enqueue_sequence_callback_| will determine in which PriorityQueue | |
478 // |sequence| must be enqueued. | |
479 re_enqueue_sequence_callback_.Run(std::move(sequence)); | |
480 } | |
481 } | |
482 | |
483 TimeDelta SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl:: | |
484 GetSleepTimeout() { | |
485 return TimeDelta::Max(); | |
486 } | |
487 | |
488 SchedulerThreadPoolImpl::SchedulerThreadPoolImpl( | |
489 StringPiece name, | |
490 IORestriction io_restriction, | |
491 TaskTracker* task_tracker, | |
492 DelayedTaskManager* delayed_task_manager) | |
493 : name_(name.as_string()), | |
494 io_restriction_(io_restriction), | |
495 idle_worker_threads_stack_lock_(shared_priority_queue_.container_lock()), | |
496 idle_worker_threads_stack_cv_for_testing_( | |
497 idle_worker_threads_stack_lock_.CreateConditionVariable()), | |
498 join_for_testing_returned_(WaitableEvent::ResetPolicy::MANUAL, | |
499 WaitableEvent::InitialState::NOT_SIGNALED), | |
500 #if DCHECK_IS_ON() | |
501 threads_created_(WaitableEvent::ResetPolicy::MANUAL, | |
502 WaitableEvent::InitialState::NOT_SIGNALED), | |
503 #endif | |
504 task_tracker_(task_tracker), | |
505 delayed_task_manager_(delayed_task_manager) { | |
506 DCHECK(task_tracker_); | |
507 DCHECK(delayed_task_manager_); | |
508 } | |
509 | |
510 bool SchedulerThreadPoolImpl::Initialize( | |
511 ThreadPriority thread_priority, | |
512 size_t max_threads, | |
513 const ReEnqueueSequenceCallback& re_enqueue_sequence_callback) { | |
514 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
515 | |
516 DCHECK(worker_threads_.empty()); | |
517 | |
518 for (size_t i = 0; i < max_threads; ++i) { | |
519 std::unique_ptr<SchedulerWorkerThread> worker_thread = | |
520 SchedulerWorkerThread::Create( | |
521 thread_priority, WrapUnique(new SchedulerWorkerThreadDelegateImpl( | |
522 this, re_enqueue_sequence_callback, | |
523 &shared_priority_queue_, static_cast<int>(i))), | |
524 task_tracker_); | |
525 if (!worker_thread) | |
526 break; | |
527 idle_worker_threads_stack_.Push(worker_thread.get()); | |
528 worker_threads_.push_back(std::move(worker_thread)); | |
529 } | |
530 | |
531 #if DCHECK_IS_ON() | |
532 threads_created_.Signal(); | |
533 #endif | |
534 | |
535 return !worker_threads_.empty(); | |
536 } | |
537 | |
538 void SchedulerThreadPoolImpl::WakeUpOneThread() { | |
539 SchedulerWorkerThread* worker_thread; | |
540 { | |
541 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
542 worker_thread = idle_worker_threads_stack_.Pop(); | |
543 } | |
544 if (worker_thread) | |
545 worker_thread->WakeUp(); | |
546 } | |
547 | |
548 void SchedulerThreadPoolImpl::AddToIdleWorkerThreadsStack( | |
549 SchedulerWorkerThread* worker_thread) { | |
550 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
551 idle_worker_threads_stack_.Push(worker_thread); | |
552 DCHECK_LE(idle_worker_threads_stack_.Size(), worker_threads_.size()); | |
553 | |
554 if (idle_worker_threads_stack_.Size() == worker_threads_.size()) | |
555 idle_worker_threads_stack_cv_for_testing_->Broadcast(); | |
556 } | |
557 | |
558 void SchedulerThreadPoolImpl::RemoveFromIdleWorkerThreadsStack( | |
559 SchedulerWorkerThread* worker_thread) { | |
560 AutoSchedulerLock auto_lock(idle_worker_threads_stack_lock_); | |
561 idle_worker_threads_stack_.Remove(worker_thread); | |
562 } | |
563 | |
564 } // namespace internal | |
565 } // namespace base | |
OLD | NEW |