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

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

Issue 1704113002: TaskScheduler [6] SchedulerWorkerThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_4_shutdown
Patch Set: CR robliao #31 Created 4 years, 8 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
(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>();
danakj 2016/03/31 00:25:13 return nullptr
fdoray 2016/03/31 03:26:31 Done.
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 // A WorkerThread starts out sleeping.
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 wake_up_event_.Wait();
74 continue;
75 }
76
77 task_tracker_->RunTask(sequence->PeekTask());
78 ran_task_from_sequence_callback_.Run(this, std::move(sequence));
79
80 // Calling WakeUp() guarantees that this WorkerThread will run Tasks from
81 // Sequences returned by |get_work_callback_| until the callback returns
82 // nullptr. Resetting |wake_up_event_| here doesn't break this invariant and
83 // avoids a useless loop iteration before going to sleep if WakeUp() is
84 // called while this WorkerThread is awake.
85 wake_up_event_.Reset();
86 }
87 }
88
89 } // namespace internal
90 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698