• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SEQUENCE_MANAGER_TIME_DOMAIN_H_
6 #define BASE_TASK_SEQUENCE_MANAGER_TIME_DOMAIN_H_
7 
8 #include "base/base_export.h"
9 #include "base/check.h"
10 #include "base/memory/raw_ptr.h"
11 #include "base/task/common/lazy_now.h"
12 #include "base/task/sequence_manager/tasks.h"
13 #include "base/time/tick_clock.h"
14 #include "base/values.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16 
17 namespace base {
18 namespace sequence_manager {
19 
20 class SequenceManager;
21 
22 namespace internal {
23 class SequenceManagerImpl;
24 }  // namespace internal
25 
26 // TimeDomain allows subclasses to enable clock overriding
27 // (e.g. auto-advancing virtual time, throttled clock, etc).
28 class BASE_EXPORT TimeDomain : public TickClock {
29  public:
30   TimeDomain(const TimeDomain&) = delete;
31   TimeDomain& operator=(const TimeDomain&) = delete;
32   ~TimeDomain() override = default;
33 
34   // Invoked when the thread reaches idle. Gives an opportunity to a virtual
35   // time domain impl to fast-forward time and return true to indicate that
36   // there's more work to run. If RunLoop::QuitWhenIdle has been called then
37   // `quit_when_idle_requested` will be true.
38   virtual bool MaybeFastForwardToWakeUp(absl::optional<WakeUp> next_wake_up,
39                                         bool quit_when_idle_requested) = 0;
40 
41   // Debug info.
42   Value::Dict AsValue() const;
43 
44  protected:
45   TimeDomain() = default;
46 
47   virtual const char* GetName() const = 0;
48 
49   // Tells SequenceManager that internal policy might have changed to
50   // re-evaluate MaybeFastForwardToWakeUp().
51   void NotifyPolicyChanged();
52 
53   // Called when the TimeDomain is assigned to a SequenceManagerImpl.
54   // `sequence_manager` is expected to be valid for the duration of TimeDomain's
55   // existence. TODO(scheduler-dev): Pass SequenceManager in the constructor.
56   void OnAssignedToSequenceManager(
57       internal::SequenceManagerImpl* sequence_manager);
58 
59  private:
60   friend class internal::SequenceManagerImpl;
61 
62   raw_ptr<internal::SequenceManagerImpl, DanglingUntriaged> sequence_manager_ =
63       nullptr;
64 };
65 
66 }  // namespace sequence_manager
67 }  // namespace base
68 
69 #endif  // BASE_TASK_SEQUENCE_MANAGER_TIME_DOMAIN_H_
70