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/debug/task_annotator.h" |
| 10 #include "base/task_scheduler/delayed_task_manager.h" |
| 11 #include "base/task_scheduler/priority_queue.h" |
| 12 #include "base/task_scheduler/shutdown_manager.h" |
| 13 |
| 14 namespace base { |
| 15 namespace task_scheduler { |
| 16 |
| 17 const char kQueueFunctionName[] = "task_scheduler::PostTask"; |
| 18 |
| 19 void PostTaskHelper(const Task& task, |
| 20 scoped_refptr<Sequence> sequence, |
| 21 PriorityQueue* priority_queue, |
| 22 ShutdownManager* shutdown_manager, |
| 23 DelayedTaskManager* delayed_task_manager) { |
| 24 DCHECK(!task.task.is_null()); |
| 25 DCHECK(sequence.get()); |
| 26 DCHECK(priority_queue); |
| 27 DCHECK(shutdown_manager); |
| 28 DCHECK(delayed_task_manager); |
| 29 |
| 30 debug::TaskAnnotator task_annotator; |
| 31 task_annotator.DidQueueTask(kQueueFunctionName, task); |
| 32 |
| 33 if (task.delayed_run_time.is_null()) |
| 34 PostTaskNowHelper(task, sequence, priority_queue, shutdown_manager); |
| 35 else |
| 36 delayed_task_manager->AddDelayedTask(task, sequence, priority_queue); |
| 37 } |
| 38 |
| 39 void PostTaskNowHelper(const Task& task, |
| 40 scoped_refptr<Sequence> sequence, |
| 41 PriorityQueue* priority_queue, |
| 42 ShutdownManager* shutdown_manager) { |
| 43 DCHECK(!task.task.is_null()); |
| 44 DCHECK(sequence.get()); |
| 45 DCHECK(priority_queue); |
| 46 DCHECK(shutdown_manager); |
| 47 |
| 48 if (!shutdown_manager->ShouldPostTask(task.traits.shutdown_behavior())) |
| 49 return; |
| 50 |
| 51 size_t prev_num_tasks; |
| 52 sequence->PushTask(make_scoped_ptr(new Task(task)), &prev_num_tasks); |
| 53 |
| 54 if (prev_num_tasks == 0) { |
| 55 // Get the sequence sort key before creating the transaction. This ensures |
| 56 // that the sequence's lock is not held at the same time as the priority |
| 57 // queue's lock. |
| 58 SequenceSortKey sequence_sort_key = sequence->GetSortKey(); |
| 59 priority_queue->BeginTransaction()->PushSequence(sequence, |
| 60 sequence_sort_key); |
| 61 } |
| 62 } |
| 63 |
| 64 } // namespace task_scheduler |
| 65 } // namespace base |
OLD | NEW |