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_service_thread.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/synchronization/waitable_event.h" | |
12 #include "base/task_scheduler/delayed_task_manager.h" | |
13 #include "base/task_scheduler/scheduler_worker.h" | |
14 #include "base/task_scheduler/sequence.h" | |
15 #include "base/threading/thread_checker.h" | |
16 #include "base/time/time.h" | |
17 | |
18 namespace base { | |
19 namespace internal { | |
20 namespace { | |
21 | |
22 class ServiceThreadDelegate : public SchedulerWorker::Delegate { | |
23 public: | |
24 ServiceThreadDelegate(DelayedTaskManager* delayed_task_manager) | |
25 : delayed_task_manager_(delayed_task_manager) {} | |
26 | |
27 // SchedulerWorker::Delegate: | |
28 void OnMainEntry(SchedulerWorker* worker, | |
29 const TimeDelta& detach_duration) override { | |
30 DCHECK(detach_duration.is_max()); | |
31 } | |
32 | |
33 scoped_refptr<Sequence> GetWork(SchedulerWorker* worker) override { | |
34 delayed_task_manager_->PostReadyTasks(); | |
35 return nullptr; | |
36 } | |
37 | |
38 void DidRunTaskWithPriority(TaskPriority task_priority, | |
39 const TimeDelta& task_latency) override { | |
40 NOTREACHED() | |
41 << "GetWork() never returns a sequence so no task should ever run."; | |
42 } | |
43 | |
44 void ReEnqueueSequence(scoped_refptr<Sequence> sequence) override { | |
45 NOTREACHED() << | |
46 "GetWork() never returns a sequence so there's nothing to reenqueue."; | |
47 } | |
48 | |
49 TimeDelta GetSleepTimeout() override { | |
50 const TimeTicks next_time = delayed_task_manager_->GetDelayedRunTime(); | |
51 if (next_time.is_null()) | |
52 return TimeDelta::Max(); | |
53 | |
54 // For delayed tasks with delays that are really close to each other, it is | |
55 // possible for the current time to advance beyond the required | |
56 // GetDelayedWaitTime. Return a minimum of TimeDelta() in the event that | |
57 // happens. | |
58 TimeDelta sleep_time = next_time - delayed_task_manager_->Now(); | |
59 const TimeDelta zero_delta; | |
60 return sleep_time < zero_delta ? zero_delta : sleep_time; | |
61 } | |
62 | |
63 bool CanDetach(SchedulerWorker* worker) override { | |
64 return false; | |
65 } | |
66 | |
67 private: | |
68 DelayedTaskManager* const delayed_task_manager_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(ServiceThreadDelegate); | |
71 }; | |
72 | |
73 } // namespace | |
74 | |
75 SchedulerServiceThread::~SchedulerServiceThread() = default; | |
76 | |
77 // static | |
78 std::unique_ptr<SchedulerServiceThread> SchedulerServiceThread::Create( | |
79 TaskTracker* task_tracker, DelayedTaskManager* delayed_task_manager) { | |
80 std::unique_ptr<SchedulerWorker> worker = SchedulerWorker::Create( | |
81 ThreadPriority::NORMAL, | |
82 MakeUnique<ServiceThreadDelegate>(delayed_task_manager), task_tracker, | |
83 SchedulerWorker::InitialState::ALIVE); | |
84 if (!worker) | |
85 return nullptr; | |
86 | |
87 return WrapUnique(new SchedulerServiceThread(std::move(worker))); | |
88 } | |
89 | |
90 void SchedulerServiceThread::WakeUp() { | |
91 worker_->WakeUp(); | |
92 } | |
93 | |
94 void SchedulerServiceThread::JoinForTesting() { | |
95 worker_->JoinForTesting(); | |
96 } | |
97 | |
98 SchedulerServiceThread::SchedulerServiceThread( | |
99 std::unique_ptr<SchedulerWorker> worker) : worker_(std::move(worker)) {} | |
100 | |
101 } // namespace internal | |
102 } // namespace base | |
OLD | NEW |