blob: b31ecabf60dd2f29504531fb43155e29060ce091 [file] [log] [blame]
Etienne Pierre-dorayb38e0fd2019-03-18 19:35:381// Copyright 2019 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_TASK_SCHEDULER_TASK_SOURCE_H_
6#define BASE_TASK_TASK_SCHEDULER_TASK_SOURCE_H_
7
8#include <stddef.h>
9
10#include "base/base_export.h"
11#include "base/macros.h"
12#include "base/memory/ref_counted.h"
13#include "base/optional.h"
14#include "base/task/common/intrusive_heap.h"
15#include "base/task/task_scheduler/scheduler_lock.h"
16#include "base/task/task_scheduler/sequence_sort_key.h"
17#include "base/task/task_scheduler/task.h"
18#include "base/task/task_traits.h"
19
20namespace base {
21namespace internal {
22
23// A TaskSource is a virtual class that provides a series of Tasks that must be
24// executed.
25//
26// In order to execute a task from this TaskSource, a worker should first make
27// sure that it can take up an additional worker with NeedsWorker(). TakeTask()
28// can then be called to access the next Task, and Pop() must be called after
29// the task executed and before accessing any subsequent Tasks. This ensure that
30// the number of workers concurrently running tasks never go over the intended
31// concurrency.
32//
33// In comments below, an "empty TaskSource" is a TaskSource with no Task.
34//
35// Note: there is a known refcounted-ownership cycle in the Scheduler
36// architecture: TaskSource -> Task -> TaskRunner -> TaskSource -> ...
37// This is okay so long as the other owners of TaskSource (PriorityQueue and
38// SchedulerWorker in alternation and
39// SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork()
40// temporarily) keep running it (and taking Tasks from it as a result). A
41// dangling reference cycle would only occur should they release their reference
42// to it while it's not empty. In other words, it is only correct for them to
43// release it after IsEmpty() returns true.
44// TODO(etiennep): Break ownership cycle by moving TaskRunner reference from
45// Task to TaskSource.
46//