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/utils.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/callback.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/task_scheduler/priority_queue.h" |
| 13 #include "base/task_scheduler/scheduler_task_executor.h" |
| 14 #include "base/task_scheduler/sequence_sort_key.h" |
| 15 #include "base/task_scheduler/task_tracker.h" |
| 16 #include "base/time/time.h" |
| 17 |
| 18 namespace base { |
| 19 namespace internal { |
| 20 |
| 21 bool PostTaskToExecutor(const tracked_objects::Location& posted_from, |
| 22 const Closure& closure, |
| 23 const TaskTraits& traits, |
| 24 const TimeDelta& delay, |
| 25 scoped_refptr<Sequence> sequence, |
| 26 SchedulerTaskExecutor* executor, |
| 27 TaskTracker* task_tracker) { |
| 28 DCHECK(!closure.is_null()); |
| 29 DCHECK(sequence); |
| 30 DCHECK(executor); |
| 31 DCHECK(task_tracker); |
| 32 |
| 33 // TODO(fdoray): Support delayed tasks. |
| 34 DCHECK(delay.is_zero()); |
| 35 |
| 36 std::unique_ptr<Task> task(new Task(posted_from, closure, traits)); |
| 37 |
| 38 if (!task_tracker->WillPostTask(task.get())) |
| 39 return false; |
| 40 |
| 41 executor->PostTaskWithSequence(std::move(task), std::move(sequence)); |
| 42 return true; |
| 43 } |
| 44 |
| 45 bool AddTaskToSequenceAndPriorityQueue(std::unique_ptr<Task> task, |
| 46 scoped_refptr<Sequence> sequence, |
| 47 PriorityQueue* priority_queue) { |
| 48 DCHECK(task); |
| 49 DCHECK(sequence); |
| 50 DCHECK(priority_queue); |
| 51 |
| 52 // Confirm that |task| is ready to run (its delayed run time is either null or |
| 53 // in the past). |
| 54 DCHECK_LE(task->delayed_run_time, TimeTicks::Now()); |
| 55 |
| 56 const bool sequence_was_empty = sequence->PushTask(std::move(task)); |
| 57 if (sequence_was_empty) { |
| 58 // Insert |sequence| in |priority_queue| if it was empty before |task| was |
| 59 // inserted into it. Otherwise, one of these must be true: |
| 60 // - |sequence| is already in a PriorityQueue (not necessarily |
| 61 // |priority_queue|), or, |
| 62 // - A worker thread is running a Task from |sequence|. It will insert |
| 63 // |sequence| in a PriorityQueue once it's done running the Task. |
| 64 const auto sequence_sort_key = sequence->GetSortKey(); |
| 65 priority_queue->BeginTransaction()->Push( |
| 66 WrapUnique(new PriorityQueue::SequenceAndSortKey(std::move(sequence), |
| 67 sequence_sort_key))); |
| 68 } |
| 69 |
| 70 return sequence_was_empty; |
| 71 } |
| 72 |
| 73 } // namespace internal |
| 74 } // namespace base |
OLD | NEW |