Home | History | Annotate | Download | only in task_scheduler
      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 #ifndef BASE_TASK_SCHEDULER_TASK_H_
      6 #define BASE_TASK_SCHEDULER_TASK_H_
      7 
      8 #include "base/base_export.h"
      9 #include "base/callback.h"
     10 #include "base/location.h"
     11 #include "base/macros.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/pending_task.h"
     14 #include "base/sequenced_task_runner.h"
     15 #include "base/single_thread_task_runner.h"
     16 #include "base/task_scheduler/task_traits.h"
     17 #include "base/time/time.h"
     18 
     19 namespace base {
     20 namespace internal {
     21 
     22 // A task is a unit of work inside the task scheduler. Support for tracing and
     23 // profiling inherited from PendingTask.
     24 struct BASE_EXPORT Task : public PendingTask {
     25   // |posted_from| is the site the task was posted from. |task| is the closure
     26   // to run. |traits_in| is metadata about the task. |delay| is a delay that
     27   // must expire before the Task runs. If |delay| is non-zero and the shutdown
     28   // behavior in |traits| is BLOCK_SHUTDOWN, the shutdown behavior is
     29   // automatically adjusted to SKIP_ON_SHUTDOWN.
     30   Task(const Location& posted_from,
     31        OnceClosure task,
     32        const TaskTraits& traits,
     33        TimeDelta delay);
     34 
     35   // Task is move-only to avoid mistakes that cause reference counts to be
     36   // accidentally bumped.
     37   Task(Task&& other) noexcept;
     38 
     39   ~Task();
     40 
     41   Task& operator=(Task&& other);
     42 
     43   // The TaskTraits of this task.
     44   TaskTraits traits;
     45 
     46   // The delay that must expire before the task runs.
     47   TimeDelta delay;
     48 
     49   // The time at which the task was inserted in its sequence. For an undelayed
     50   // task, this happens at post time. For a delayed task, this happens some
     51   // time after the task's delay has expired. If the task hasn't been inserted
     52   // in a sequence yet, this defaults to a null TimeTicks.
     53   TimeTicks sequenced_time;
     54 
     55   // A reference to the SequencedTaskRunner or SingleThreadTaskRunner that
     56   // posted this task, if any. Used to set ThreadTaskRunnerHandle and/or
     57   // SequencedTaskRunnerHandle while the task is running.
     58   // Note: this creates an ownership cycle
     59   //   Sequence -> Task -> TaskRunner -> Sequence -> ...
     60   // but that's okay as it's broken when the Task is popped from its Sequence
     61   // after being executed which means this cycle forces the TaskRunner to stick
     62   // around until all its tasks have been executed which is a requirement to
     63   // support TaskRunnerHandles.
     64   scoped_refptr<SequencedTaskRunner> sequenced_task_runner_ref;
     65   scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner_ref;
     66 
     67  private:
     68   DISALLOW_COPY_AND_ASSIGN(Task);
     69 };
     70 
     71 }  // namespace internal
     72 }  // namespace base
     73 
     74 #endif  // BASE_TASK_SCHEDULER_TASK_H_
     75