timer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_TIMER_H_
  26. #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_TIMER_H_
  27. #include "base/location.h"
  28. #include "base/macros.h"
  29. #include "base/memory/weak_ptr.h"
  30. #include "base/single_thread_task_runner.h"
  31. #include "base/time/time.h"
  32. #include "third_party/blink/renderer/platform/heap/handle.h"
  33. #include "third_party/blink/renderer/platform/heap/persistent.h"
  34. #include "third_party/blink/renderer/platform/platform_export.h"
  35. #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
  36. #include "third_party/blink/renderer/platform/wtf/sanitizers.h"
  37. #include "third_party/blink/renderer/platform/wtf/threading.h"
  38. namespace blink {
  39. // Time intervals are all in seconds.
  40. class PLATFORM_EXPORT TimerBase {
  41. public:
  42. explicit TimerBase(scoped_refptr<base::SingleThreadTaskRunner>);
  43. virtual ~TimerBase();
  44. void Start(base::TimeDelta next_fire_interval,
  45. base::TimeDelta repeat_interval,
  46. const base::Location&);
  47. void StartRepeating(base::TimeDelta repeat_interval,
  48. const base::Location& caller) {
  49. Start(repeat_interval, repeat_interval, caller);
  50. }
  51. void StartOneShot(base::TimeDelta interval, const base::Location& caller) {
  52. Start(interval, base::TimeDelta(), caller);
  53. }
  54. // Timer cancellation is fast enough that you shouldn't have to worry
  55. // about it unless you're canceling tens of thousands of tasks.
  56. virtual void Stop();
  57. bool IsActive() const;
  58. const base::Location& GetLocation() const { return location_; }
  59. base::TimeDelta NextFireInterval() const;
  60. base::TimeDelta RepeatInterval() const { return repeat_interval_; }
  61. void AugmentRepeatInterval(base::TimeDelta delta) {
  62. base::TimeTicks now = TimerCurrentTimeTicks();
  63. SetNextFireTime(now,
  64. std::max(next_fire_time_ - now + delta, base::TimeDelta()));
  65. repeat_interval_ += delta;
  66. }
  67. void MoveToNewTaskRunner(scoped_refptr<base::SingleThreadTaskRunner>);
  68. struct PLATFORM_EXPORT Comparator {
  69. bool operator()(const TimerBase* a, const TimerBase* b) const;
  70. };
  71. private:
  72. virtual void Fired() = 0;
  73. NO_SANITIZE_ADDRESS
  74. virtual bool CanFire() const { return true; }
  75. base::TimeTicks TimerCurrentTimeTicks() const;
  76. void SetNextFireTime(base::TimeTicks now, base::TimeDelta delay);
  77. void RunInternal();
  78. base::TimeTicks next_fire_time_; // 0 if inactive
  79. base::TimeDelta repeat_interval_; // 0 if not repeating
  80. base::Location location_;
  81. scoped_refptr<base::SingleThreadTaskRunner> web_task_runner_;
  82. #if DCHECK_IS_ON()
  83. base::PlatformThreadId thread_;
  84. #endif
  85. base::WeakPtrFactory<TimerBase> weak_ptr_factory_{this};
  86. friend class ThreadTimers;
  87. friend class TimerHeapLessThanFunction;
  88. friend class TimerHeapReference;
  89. DISALLOW_COPY_AND_ASSIGN(TimerBase);
  90. };
  91. template <typename TimerFiredClass,
  92. bool = WTF::IsGarbageCollectedTypeInternal<TimerFiredClass>::value>
  93. class TaskRunnerTimer;
  94. template <typename TimerFiredClass>
  95. class TaskRunnerTimer<TimerFiredClass, false> : public TimerBase {
  96. public:
  97. using TimerFiredFunction = void (TimerFiredClass::*)(TimerBase*);
  98. TaskRunnerTimer(scoped_refptr<base::SingleThreadTaskRunner> web_task_runner,
  99. TimerFiredClass* o,
  100. TimerFiredFunction f)
  101. : TimerBase(std::move(web_task_runner)), object_(o), function_(f) {}
  102. ~TaskRunnerTimer() override = default;
  103. protected:
  104. void Fired() override { (object_->*function_)(this); }
  105. NO_SANITIZE_ADDRESS
  106. bool CanFire() const override { return true; }
  107. private:
  108. TimerFiredClass* object_;
  109. TimerFiredFunction function_;
  110. };
  111. template <typename TimerFiredClass>
  112. class TaskRunnerTimer<TimerFiredClass, true> : public TimerBase {
  113. public:
  114. using TimerFiredFunction = void (TimerFiredClass::*)(TimerBase*);
  115. TaskRunnerTimer(scoped_refptr<base::SingleThreadTaskRunner> web_task_runner,
  116. TimerFiredClass* o,
  117. TimerFiredFunction f)
  118. : TimerBase(std::move(web_task_runner)), object_(o), function_(f) {}
  119. ~TaskRunnerTimer() override = default;
  120. protected:
  121. void Fired() override { (object_->*function_)(this); }
  122. NO_SANITIZE_ADDRESS
  123. bool CanFire() const override { return object_.IsClearedUnsafe(); }
  124. private:
  125. GC_PLUGIN_IGNORE("363031")
  126. WeakPersistent<TimerFiredClass> object_;
  127. TimerFiredFunction function_;
  128. };
  129. NO_SANITIZE_ADDRESS
  130. inline bool TimerBase::IsActive() const {
  131. #if DCHECK_IS_ON()
  132. DCHECK_EQ(thread_, CurrentThread());
  133. #endif
  134. return weak_ptr_factory_.HasWeakPtrs();
  135. }
  136. } // namespace blink
  137. #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_TIMER_H_