Chromium Code Reviews| 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/worker_thread.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/task_scheduler/task_tracker.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace internal { | |
| 16 | |
| 17 scoped_ptr<WorkerThread> WorkerThread::CreateWorkerThread( | |
| 18 ThreadPriority thread_priority, | |
| 19 const GetWorkCallback& get_work_callback, | |
| 20 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
| 21 TaskTracker* task_tracker) { | |
| 22 scoped_ptr<WorkerThread> worker_thread( | |
| 23 new WorkerThread(thread_priority, get_work_callback, | |
| 24 ran_task_from_sequence_callback, task_tracker)); | |
| 25 | |
| 26 if (worker_thread->thread_handle_.is_null()) | |
| 27 return scoped_ptr<WorkerThread>(); | |
| 28 return worker_thread; | |
| 29 } | |
| 30 | |
| 31 WorkerThread::~WorkerThread() { | |
| 32 DCHECK(should_exit_for_testing()); | |
| 33 } | |
| 34 | |
| 35 void WorkerThread::WakeUp() { | |
| 36 wake_up_event_.Signal(); | |
| 37 } | |
| 38 | |
| 39 void WorkerThread::JoinForTesting() { | |
| 40 should_exit_for_testing_ = true; | |
| 41 base::subtle::MemoryBarrier(); | |
| 42 WakeUp(); | |
| 43 PlatformThread::Join(thread_handle_); | |
| 44 } | |
| 45 | |
| 46 WorkerThread::WorkerThread( | |
| 47 ThreadPriority thread_priority, | |
| 48 const GetWorkCallback& get_work_callback, | |
| 49 const RanTaskFromSequenceCallback& ran_task_from_sequence_callback, | |
| 50 TaskTracker* task_tracker) | |
| 51 : wake_up_event_(false, false), | |
| 52 get_work_callback_(get_work_callback), | |
| 53 ran_task_from_sequence_callback_(ran_task_from_sequence_callback), | |
| 54 task_tracker_(task_tracker) { | |
| 55 DCHECK(!get_work_callback_.is_null()); | |
| 56 DCHECK(!ran_task_from_sequence_callback_.is_null()); | |
| 57 DCHECK(task_tracker_); | |
| 58 | |
| 59 static const size_t kDefaultStackSize = 0; | |
| 60 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | |
| 61 thread_priority); | |
| 62 } | |
| 63 | |
| 64 void WorkerThread::ThreadMain() { | |
| 65 // Wait for a wake-up. | |
|
robliao
2016/03/30 19:38:26
Instead, consider commenting that all WorkerThread
fdoray
2016/03/30 19:56:37
Done.
| |
| 66 wake_up_event_.Wait(); | |
| 67 | |
| 68 while (!task_tracker_->shutdown_completed() && !should_exit_for_testing()) { | |
| 69 // Get the sequence containing the next task to execute. | |
| 70 scoped_refptr<Sequence> sequence = get_work_callback_.Run(this); | |
| 71 | |
| 72 if (!sequence) { | |
| 73 // Wait for a wake-up. | |
|
robliao
2016/03/30 19:38:26
Nit: Remove this comment.
fdoray
2016/03/30 19:56:37
Done.
| |
| 74 wake_up_event_.Wait(); | |
| 75 continue; | |
| 76 } | |
| 77 | |
| 78 // Peek the next task in |sequence| and try to run it. | |
|
robliao
2016/03/30 19:38:27
Nit: This comment can be removed here as it just r
fdoray
2016/03/30 19:56:37
Done.
| |
| 79 task_tracker_->RunTask(sequence->PeekTask()); | |
| 80 | |
| 81 // Notify |ran_task_from_sequence_callback_| that this WorkerThread tried to | |
| 82 // run a Task from |sequence|. | |
|
robliao
2016/03/30 19:38:26
Same here
fdoray
2016/03/30 19:56:37
Done.
| |
| 83 ran_task_from_sequence_callback_.Run(this, std::move(sequence)); | |
| 84 | |
| 85 // Calling WakeUp() guarantees that this WorkerThread will run Tasks from | |
|
robliao
2016/03/30 19:38:27
This comment is worth keeping as it explains the r
fdoray
2016/03/30 19:56:37
Acknowledged.
| |
| 86 // Sequences returned by |get_work_callback_| until the callback returns | |
| 87 // nullptr. Resetting |wake_up_event_| here doesn't break this invariant and | |
| 88 // avoids a useless loop iteration before going to sleep if WakeUp() is | |
| 89 // called while this WorkerThread is awake. | |
| 90 wake_up_event_.Reset(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 } // namespace internal | |
| 95 } // namespace base | |
| OLD | NEW |