| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_TASK_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TASK_H_ |
| 6 #define BASE_TASK_SCHEDULER_TASK_H_ | 6 #define BASE_TASK_SCHEDULER_TASK_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/pending_task.h" | 13 #include "base/pending_task.h" |
| 14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/task_scheduler/task_traits.h" | 16 #include "base/task_scheduler/task_traits.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 namespace internal { | 20 namespace internal { |
| 21 | 21 |
| 22 // A task is a unit of work inside the task scheduler. Support for tracing and | 22 // A task is a unit of work inside the task scheduler. Support for tracing and |
| 23 // profiling inherited from PendingTask. | 23 // profiling inherited from PendingTask. |
| 24 struct BASE_EXPORT Task : public PendingTask { | 24 struct BASE_EXPORT Task : public PendingTask { |
| 25 enum class TaskType { |
| 26 // A task provided by an external caller of the task scheduler. |
| 27 NORMAL, |
| 28 // A task created by the task scheduler. |
| 29 INTERNAL, |
| 30 }; |
| 31 |
| 25 // |posted_from| is the site the task was posted from. |task| is the closure | 32 // |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 | 33 // 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 | 34 // 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 | 35 // behavior in |traits| is BLOCK_SHUTDOWN, the shutdown behavior is |
| 29 // automatically adjusted to SKIP_ON_SHUTDOWN. | 36 // automatically adjusted to SKIP_ON_SHUTDOWN. |
| 30 Task(const tracked_objects::Location& posted_from, | 37 Task(const tracked_objects::Location& posted_from, |
| 31 const Closure& task, | 38 const Closure& task, |
| 32 const TaskTraits& traits, | 39 const TaskTraits& traits, |
| 33 TimeDelta delay); | 40 TimeDelta delay, |
| 41 TaskType task_type = TaskType::NORMAL); |
| 34 ~Task(); | 42 ~Task(); |
| 35 | 43 |
| 36 // The TaskTraits of this task. | 44 // The TaskTraits of this task. |
| 37 const TaskTraits traits; | 45 const TaskTraits traits; |
| 38 | 46 |
| 39 // The delay that must expire before the task runs. | 47 // The delay that must expire before the task runs. |
| 40 const TimeDelta delay; | 48 const TimeDelta delay; |
| 41 | 49 |
| 42 // The time at which the task was inserted in its sequence. For an undelayed | 50 // The time at which the task was inserted in its sequence. For an undelayed |
| 43 // task, this happens at post time. For a delayed task, this happens some | 51 // task, this happens at post time. For a delayed task, this happens some |
| 44 // time after the task's delay has expired. If the task hasn't been inserted | 52 // time after the task's delay has expired. If the task hasn't been inserted |
| 45 // in a sequence yet, this defaults to a null TimeTicks. | 53 // in a sequence yet, this defaults to a null TimeTicks. |
| 46 TimeTicks sequenced_time; | 54 TimeTicks sequenced_time; |
| 47 | 55 |
| 56 // The TaskType for this task. |
| 57 const TaskType task_type; |
| 58 |
| 48 // A reference to the SequencedTaskRunner or SingleThreadTaskRunner that | 59 // A reference to the SequencedTaskRunner or SingleThreadTaskRunner that |
| 49 // posted this task, if any. Used to set ThreadTaskRunnerHandle and/or | 60 // posted this task, if any. Used to set ThreadTaskRunnerHandle and/or |
| 50 // SequencedTaskRunnerHandle while the task is running. | 61 // SequencedTaskRunnerHandle while the task is running. |
| 51 // Note: this creates an ownership cycle | 62 // Note: this creates an ownership cycle |
| 52 // Sequence -> Task -> TaskRunner -> Sequence -> ... | 63 // Sequence -> Task -> TaskRunner -> Sequence -> ... |
| 53 // but that's okay as it's broken when the Task is popped from its Sequence | 64 // but that's okay as it's broken when the Task is popped from its Sequence |
| 54 // after being executed which means this cycle forces the TaskRunner to stick | 65 // after being executed which means this cycle forces the TaskRunner to stick |
| 55 // around until all its tasks have been executed which is a requirement to | 66 // around until all its tasks have been executed which is a requirement to |
| 56 // support TaskRunnerHandles. | 67 // support TaskRunnerHandles. |
| 57 scoped_refptr<SequencedTaskRunner> sequenced_task_runner_ref; | 68 scoped_refptr<SequencedTaskRunner> sequenced_task_runner_ref; |
| 58 scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner_ref; | 69 scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner_ref; |
| 59 | 70 |
| 60 private: | 71 private: |
| 61 // Disallow copies to make sure no unnecessary ref-bumps are incurred. Making | 72 // Disallow copies to make sure no unnecessary ref-bumps are incurred. Making |
| 62 // it move-only would be an option, but isn't necessary for now. | 73 // it move-only would be an option, but isn't necessary for now. |
| 63 DISALLOW_COPY_AND_ASSIGN(Task); | 74 DISALLOW_COPY_AND_ASSIGN(Task); |
| 64 }; | 75 }; |
| 65 | 76 |
| 66 } // namespace internal | 77 } // namespace internal |
| 67 } // namespace base | 78 } // namespace base |
| 68 | 79 |
| 69 #endif // BASE_TASK_SCHEDULER_TASK_H_ | 80 #endif // BASE_TASK_SCHEDULER_TASK_H_ |
| OLD | NEW |