xref: /aosp_15_r20/external/libchrome/base/task_scheduler/scheduler_worker_pool.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
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_SCHEDULER_WORKER_POOL_H_
6 #define BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_H_
7 
8 #include "base/base_export.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/task_runner.h"
12 #include "base/task_scheduler/can_schedule_sequence_observer.h"
13 #include "base/task_scheduler/sequence.h"
14 #include "base/task_scheduler/task.h"
15 #include "base/task_scheduler/task_traits.h"
16 #include "base/task_scheduler/tracked_ref.h"
17 
18 namespace base {
19 namespace internal {
20 
21 class DelayedTaskManager;
22 class TaskTracker;
23 
24 // Interface for a worker pool.
25 class BASE_EXPORT SchedulerWorkerPool : public CanScheduleSequenceObserver {
26  public:
27   ~SchedulerWorkerPool() override;
28 
29   // Returns a TaskRunner whose PostTask invocations result in scheduling tasks
30   // in this SchedulerWorkerPool using |traits|. Tasks may run in any order and
31   // in parallel.
32   scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits(
33       const TaskTraits& traits);
34 
35   // Returns a SequencedTaskRunner whose PostTask invocations result in
36   // scheduling tasks in this SchedulerWorkerPool using |traits|. Tasks run one
37   // at a time in posting order.
38   scoped_refptr<SequencedTaskRunner> CreateSequencedTaskRunnerWithTraits(
39       const TaskTraits& traits);
40 
41   // Posts |task| to be executed by this SchedulerWorkerPool as part of
42   // |sequence|. |task| won't be executed before its delayed run time, if any.
43   // Returns true if |task| is posted.
44   bool PostTaskWithSequence(Task task, scoped_refptr<Sequence> sequence);
45 
46   // Registers the worker pool in TLS.
47   void BindToCurrentThread();
48 
49   // Resets the worker pool in TLS.
50   void UnbindFromCurrentThread();
51 
52   // Prevents new tasks from starting to run and waits for currently running
53   // tasks to complete their execution. It is guaranteed that no thread will do
54   // work on behalf of this SchedulerWorkerPool after this returns. It is
55   // invalid to post a task once this is called. TaskTracker::Flush() can be
56   // called before this to complete existing tasks, which might otherwise post a
57   // task during JoinForTesting(). This can only be called once.
58   virtual void JoinForTesting() = 0;
59 
60  protected:
61   SchedulerWorkerPool(TrackedRef<TaskTracker> task_tracker,
62                       DelayedTaskManager* delayed_task_manager);
63 
64   // Posts |task| to be executed by this SchedulerWorkerPool as part of
65   // |sequence|. This must only be called after |task| has gone through
66   // PostTaskWithSequence() and after |task|'s delayed run time.
67   void PostTaskWithSequenceNow(Task task, scoped_refptr<Sequence> sequence);
68 
69   const TrackedRef<TaskTracker> task_tracker_;
70   DelayedTaskManager* const delayed_task_manager_;
71 
72  private:
73   DISALLOW_COPY_AND_ASSIGN(SchedulerWorkerPool);
74 };
75 
76 }  // namespace internal
77 }  // namespace base
78 
79 #endif  // BASE_TASK_SCHEDULER_SCHEDULER_WORKER_POOL_H_
80