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_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 std::unique_ptr<SchedulerWorkerThread> SchedulerWorkerThread::Create( | |
18 ThreadPriority thread_priority, | |
19 std::unique_ptr<Delegate> delegate, | |
20 TaskTracker* task_tracker) { | |
21 std::unique_ptr<SchedulerWorkerThread> worker_thread( | |
22 new SchedulerWorkerThread(thread_priority, std::move(delegate), | |
23 task_tracker)); | |
24 | |
25 if (worker_thread->thread_handle_.is_null()) | |
26 return nullptr; | |
27 return worker_thread; | |
28 } | |
29 | |
30 SchedulerWorkerThread::~SchedulerWorkerThread() { | |
31 DCHECK(ShouldExitForTesting()); | |
32 } | |
33 | |
34 void SchedulerWorkerThread::WakeUp() { | |
35 wake_up_event_.Signal(); | |
36 } | |
37 | |
38 void SchedulerWorkerThread::JoinForTesting() { | |
39 { | |
40 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | |
41 should_exit_for_testing_ = true; | |
42 } | |
43 WakeUp(); | |
44 PlatformThread::Join(thread_handle_); | |
45 } | |
46 | |
47 SchedulerWorkerThread::SchedulerWorkerThread(ThreadPriority thread_priority, | |
48 std::unique_ptr<Delegate> delegate, | |
49 TaskTracker* task_tracker) | |
50 : wake_up_event_(WaitableEvent::ResetPolicy::AUTOMATIC, | |
51 WaitableEvent::InitialState::NOT_SIGNALED), | |
52 delegate_(std::move(delegate)), | |
53 task_tracker_(task_tracker) { | |
54 DCHECK(delegate_); | |
55 DCHECK(task_tracker_); | |
56 | |
57 const size_t kDefaultStackSize = 0; | |
58 PlatformThread::CreateWithPriority(kDefaultStackSize, this, &thread_handle_, | |
59 thread_priority); | |
60 } | |
61 | |
62 void SchedulerWorkerThread::ThreadMain() { | |
63 delegate_->OnMainEntry(this); | |
64 | |
65 // A SchedulerWorkerThread starts out sleeping. | |
66 wake_up_event_.Wait(); | |
67 | |
68 while (!task_tracker_->shutdown_completed() && !ShouldExitForTesting()) { | |
69 // Get the sequence containing the next task to execute. | |
70 scoped_refptr<Sequence> sequence = delegate_->GetWork(this); | |
71 | |
72 if (!sequence) { | |
73 TimeDelta sleep_time = delegate_->GetSleepTimeout(); | |
74 if (sleep_time.is_max()) { | |
75 // Calling TimedWait with TimeDelta::Max is not recommended per | |
76 // http://crbug.com/465948. | |
77 wake_up_event_.Wait(); | |
78 } else { | |
79 wake_up_event_.TimedWait(sleep_time); | |
80 } | |
81 continue; | |
82 } | |
83 | |
84 task_tracker_->RunTask(sequence->PeekTask()); | |
85 | |
86 const bool sequence_became_empty = sequence->PopTask(); | |
87 | |
88 // If |sequence| isn't empty immediately after the pop, re-enqueue it to | |
89 // maintain the invariant that a non-empty Sequence is always referenced by | |
90 // either a PriorityQueue or a SchedulerWorkerThread. If it is empty and | |
91 // there are live references to it, it will be enqueued when a Task is added | |
92 // to it. Otherwise, it will be destroyed at the end of this scope. | |
93 if (!sequence_became_empty) | |
94 delegate_->ReEnqueueSequence(std::move(sequence)); | |
95 | |
96 // Calling WakeUp() guarantees that this SchedulerWorkerThread will run | |
97 // Tasks from Sequences returned by the GetWork() method of |delegate_| | |
98 // until it returns nullptr. Resetting |wake_up_event_| here doesn't break | |
99 // this invariant and avoids a useless loop iteration before going to sleep | |
100 // if WakeUp() is called while this SchedulerWorkerThread is awake. | |
101 wake_up_event_.Reset(); | |
102 } | |
103 } | |
104 | |
105 bool SchedulerWorkerThread::ShouldExitForTesting() const { | |
106 AutoSchedulerLock auto_lock(should_exit_for_testing_lock_); | |
107 return should_exit_for_testing_; | |
108 } | |
109 | |
110 } // namespace internal | |
111 } // namespace base | |
OLD | NEW |