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/task.h"
6
7 #include <utility>
8
9 #include "base/atomic_sequence_num.h"
10 #include "base/critical_closure.h"
11
12 namespace base {
13 namespace internal {
14
15 namespace {
16
17 AtomicSequenceNumber g_sequence_nums_for_tracing;
18
19 } // namespace
20
Task(const Location & posted_from,OnceClosure task,const TaskTraits & traits,TimeDelta delay)21 Task::Task(const Location& posted_from,
22 OnceClosure task,
23 const TaskTraits& traits,
24 TimeDelta delay)
25 : PendingTask(
26 posted_from,
27 traits.shutdown_behavior() == TaskShutdownBehavior::BLOCK_SHUTDOWN
28 ? MakeCriticalClosure(std::move(task))
29 : std::move(task),
30 delay.is_zero() ? TimeTicks() : TimeTicks::Now() + delay,
31 Nestable::kNonNestable),
32 // Prevent a delayed BLOCK_SHUTDOWN task from blocking shutdown before it
33 // starts running by changing its shutdown behavior to SKIP_ON_SHUTDOWN.
34 traits(
35 (!delay.is_zero() &&
36 traits.shutdown_behavior() == TaskShutdownBehavior::BLOCK_SHUTDOWN)
37 ? TaskTraits::Override(traits,
38 {TaskShutdownBehavior::SKIP_ON_SHUTDOWN})
39 : traits),
40 delay(delay) {
41 // TaskScheduler doesn't use |sequence_num| but tracing (toplevel.flow) relies
42 // on it being unique. While this subtle dependency is a bit overreaching,
43 // TaskScheduler is the only task system that doesn't use |sequence_num| and
44 // the dependent code rarely changes so this isn't worth a big change and
45 // faking it here isn't too bad for now (posting tasks is full of atomic ops
46 // already).
47 this->sequence_num = g_sequence_nums_for_tracing.GetNext();
48 }
49
50 // This should be "= default but MSVC has trouble with "noexcept = default" in
51 // this case.
Task(Task && other)52 Task::Task(Task&& other) noexcept
53 : PendingTask(std::move(other)),
54 traits(other.traits),
55 delay(other.delay),
56 sequenced_time(other.sequenced_time),
57 sequenced_task_runner_ref(std::move(other.sequenced_task_runner_ref)),
58 single_thread_task_runner_ref(
59 std::move(other.single_thread_task_runner_ref)) {}
60
61 Task::~Task() = default;
62
63 Task& Task::operator=(Task&& other) = default;
64
65 } // namespace internal
66 } // namespace base
67