blob: e3088291d1490f85086b8e758efecdd44f4da9c1 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base_export.h"
#include "base/memory/ref_counted.h"
#include "base/task/task_scheduler/can_schedule_sequence_observer.h"
#include "base/task/task_scheduler/sequence.h"
#include "base/task/task_scheduler/task.h"
#include "base/task/task_scheduler/tracked_ref.h"
namespace base {
namespace internal {
class TaskTracker;
// Interface for a worker pool.
class BASE_EXPORT SchedulerWorkerPool : public CanScheduleSequenceObserver {
// Delegate interface for SchedulerWorkerPool.
class BASE_EXPORT Delegate {
virtual ~Delegate() = default;
// Invoked when the Sequence in |sequence_and_transaction| is non-empty
// after the SchedulerWorkerPool has run a task from it. The implementation
// must return the pool in which the Sequence should be reenqueued.
virtual SchedulerWorkerPool* GetWorkerPoolForTraits(
const TaskTraits& traits) = 0;
~SchedulerWorkerPool() override;
// CanScheduleSequenceObserver:
void OnCanScheduleSequence(scoped_refptr<Sequence> sequence) override = 0;
// Posts |task| to be executed by this SchedulerWorkerPool as part of
// the Sequence in |sequence_and_transaction|. This must only be called after
// |task| has gone through TaskTracker::WillPostTask() and after |task|'s
// delayed run time.
void PostTaskWithSequenceNow(Task task,
SequenceAndTransaction sequence_and_transaction);
// Registers the worker pool in TLS.
void BindToCurrentThread();
// Resets the worker pool in TLS.
void UnbindFromCurrentThread();
// Returns true if the worker pool is registered in TLS.
bool IsBoundToCurrentThread() const;
// Prevents new tasks from starting to run and waits for currently running
// tasks to complete their execution. It is guaranteed that no thread will do
// work on behalf of this SchedulerWorkerPool after this returns. It is
// invalid to post a task once this is called. TaskTracker::Flush() can be
// called before this to complete existing tasks, which might otherwise post a
// task during JoinForTesting(). This can only be called once.
virtual void JoinForTesting() = 0;
// Enqueues the Sequence in |sequence_and_transaction| which was previously in
// a different worker pool into this worker pool's priority queue.
virtual void ReEnqueueSequenceChangingPool(
SequenceAndTransaction sequence_and_transaction) = 0;
// Called when the Sequence in |sequence_and_transaction| can be scheduled.
// It is expected that TaskTracker::RunNextTask() will be called with
// the Sequence as argument after this is called.
virtual void OnCanScheduleSequence(
SequenceAndTransaction sequence_and_transaction) = 0;
SchedulerWorkerPool(TrackedRef<TaskTracker> task_tracker,
TrackedRef<Delegate> delegate);
const TrackedRef<TaskTracker> task_tracker_;
const TrackedRef<Delegate> delegate_;
} // namespace internal
} // namespace base