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