xref: /aosp_15_r20/external/cronet/base/task/thread_pool/pooled_task_runner_delegate.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
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_THREAD_POOL_POOLED_TASK_RUNNER_DELEGATE_H_
6 #define BASE_TASK_THREAD_POOL_POOLED_TASK_RUNNER_DELEGATE_H_
7 
8 #include "base/base_export.h"
9 #include "base/task/task_traits.h"
10 #include "base/task/thread_pool/job_task_source.h"
11 #include "base/task/thread_pool/sequence.h"
12 #include "base/task/thread_pool/task.h"
13 #include "base/task/thread_pool/task_source.h"
14 
15 namespace base {
16 namespace internal {
17 
18 // Delegate interface for PooledParallelTaskRunner and
19 // PooledSequencedTaskRunner.
20 class BASE_EXPORT PooledTaskRunnerDelegate {
21  public:
22   PooledTaskRunnerDelegate();
23   virtual ~PooledTaskRunnerDelegate();
24 
25   // Returns true if the current PooledTaskRunnerDelegate instance in the
26   // process matches `delegate`. This is needed in case of unit tests wherein
27   // a TaskRunner outlives the ThreadPoolInstance that created it, in which case
28   // the current delegate would be null (and not match) or even have been
29   // re-initialized to a new delegate in a following test.
30   static bool MatchesCurrentDelegate(PooledTaskRunnerDelegate* delegate);
31 
32   // Returns true if |task_source| currently running *must* return ASAP.
33   // Thread-safe but may return an outdated result (if a task unnecessarily
34   // yields due to this, it will simply be re-scheduled).
35   virtual bool ShouldYield(const TaskSource* task_source) = 0;
36 
37   // Invoked when a |task| is posted to the PooledParallelTaskRunner or
38   // PooledSequencedTaskRunner. The implementation must post |task| to
39   // |sequence| within the appropriate priority queue, depending on |sequence|
40   // traits. Returns true if task was successfully posted.
41   virtual bool PostTaskWithSequence(Task task,
42                                     scoped_refptr<Sequence> sequence) = 0;
43 
44   // Invoked when a task is posted as a Job. The implementation must add
45   // |task_source| to the appropriate priority queue, depending on |task_source|
46   // traits, if it's not there already. Returns true if task source was
47   // successfully enqueued or was already enqueued.
48   virtual bool EnqueueJobTaskSource(
49       scoped_refptr<JobTaskSource> task_source) = 0;
50 
51   // Removes |task_source| from the priority queue.
52   virtual void RemoveJobTaskSource(
53       scoped_refptr<JobTaskSource> task_source) = 0;
54 
55   // Invoked when the priority of |sequence|'s TaskRunner is updated. The
56   // implementation must update |sequence|'s priority to |priority|, then place
57   // |sequence| in the correct priority-queue position within the appropriate
58   // thread group.
59   virtual void UpdatePriority(scoped_refptr<TaskSource> task_source,
60                               TaskPriority priority) = 0;
61   virtual void UpdateJobPriority(scoped_refptr<TaskSource> task_source,
62                                  TaskPriority priority) = 0;
63 };
64 
65 }  // namespace internal
66 }  // namespace base
67 
68 #endif  // BASE_TASK_THREAD_POOL_POOLED_TASK_RUNNER_DELEGATE_H_
69