• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_
6 #define BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_
7 
8 #include "base/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/task/sequence_manager/lazy_now.h"
11 #include "base/time/time.h"
12 
13 namespace base {
14 
15 class TickClock;
16 struct PendingTask;
17 
18 namespace sequence_manager {
19 namespace internal {
20 
21 class SequencedTaskSource;
22 
23 // Implementation of this interface is used by SequenceManager to schedule
24 // actual work to be run. Hopefully we can stop using MessageLoop and this
25 // interface will become more concise.
26 class ThreadController {
27  public:
28   virtual ~ThreadController() = default;
29 
30   // Sets the number of tasks executed in a single invocation of DoWork.
31   // Increasing the batch size can reduce the overhead of yielding back to the
32   // main message loop.
33   virtual void SetWorkBatchSize(int work_batch_size = 1) = 0;
34 
35   // Notifies that |pending_task| is about to be enqueued. Needed for tracing
36   // purposes. The impl may use this opportunity add metadata to |pending_task|
37   // before it is moved into the queue.
38   virtual void WillQueueTask(PendingTask* pending_task) = 0;
39 
40   // Notify the controller that its associated sequence has immediate work
41   // to run. Shortly after this is called, the thread associated with this
42   // controller will run a task returned by sequence->TakeTask(). Can be called
43   // from any sequence.
44   //
45   // TODO(altimin): Change this to "the thread associated with this
46   // controller will run tasks returned by sequence->TakeTask() until it
47   // returns null or sequence->DidRunTask() returns false" once the
48   // code is changed to work that way.
49   virtual void ScheduleWork() = 0;
50 
51   // Notify the controller that SequencedTaskSource will have a delayed work
52   // ready to be run at |run_time|. This call cancels any previously
53   // scheduled delayed work. Can only be called from the main sequence.
54   // NOTE: DelayTillNextTask might return a different value as it also takes
55   // immediate work into account.
56   // TODO(kraynov): Remove |lazy_now| parameter.
57   virtual void SetNextDelayedDoWork(LazyNow* lazy_now, TimeTicks run_time) = 0;
58 
59   // Sets the sequenced task source from which to take tasks after
60   // a Schedule*Work() call is made.
61   // Must be called before the first call to Schedule*Work().
62   virtual void SetSequencedTaskSource(SequencedTaskSource*) = 0;
63 
64   // TODO(altimin): Get rid of the methods below.
65   // These methods exist due to current integration of SequenceManager
66   // with MessageLoop.
67 
68   virtual bool RunsTasksInCurrentSequence() = 0;
69 
70   virtual const TickClock* GetClock() = 0;
71 
72   virtual void SetDefaultTaskRunner(scoped_refptr<SingleThreadTaskRunner>) = 0;
73 
74   virtual void RestoreDefaultTaskRunner() = 0;
75 
76   virtual void AddNestingObserver(RunLoop::NestingObserver* observer) = 0;
77 
78   virtual void RemoveNestingObserver(RunLoop::NestingObserver* observer) = 0;
79 };
80 
81 }  // namespace internal
82 }  // namespace sequence_manager
83 }  // namespace base
84 
85 #endif  // BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_
86