repeating_timer.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <base/bind.h>
  18. #include <base/cancelable_callback.h>
  19. #include <base/location.h>
  20. #include <future>
  21. namespace bluetooth {
  22. namespace common {
  23. class MessageLoopThread;
  24. /**
  25. * An alarm clock that posts a delayed task to a specified MessageLoopThread
  26. * periodically.
  27. *
  28. * Warning: MessageLoopThread must be running when any task is scheduled or
  29. * being executed
  30. */
  31. class RepeatingTimer final {
  32. public:
  33. RepeatingTimer() : expected_time_next_task_us_(0) {}
  34. ~RepeatingTimer();
  35. /**
  36. * Schedule a delayed periodic task to the MessageLoopThread. Only one task
  37. * can be scheduled at a time. If another task is scheduled, it will cancel
  38. * the previous task synchronously and schedule the new periodic task; this
  39. * blocks until the previous task is cancelled.
  40. *
  41. * @param thread thread to run the task
  42. * @param from_here location where this task is originated
  43. * @param task task created through base::Bind()
  44. * @param period period for the task to be executed
  45. * @return true iff task is scheduled successfully
  46. */
  47. bool SchedulePeriodic(const base::WeakPtr<MessageLoopThread>& thread,
  48. const base::Location& from_here,
  49. base::RepeatingClosure task, base::TimeDelta period);
  50. /**
  51. * Post an event which cancels the current task asynchronously
  52. */
  53. void Cancel();
  54. /**
  55. * Post an event which cancels the current task and wait for the cancellation
  56. * to be completed
  57. */
  58. void CancelAndWait();
  59. /**
  60. * Returns true when there is a pending task scheduled on a running thread,
  61. * otherwise false.
  62. */
  63. bool IsScheduled() const;
  64. private:
  65. base::WeakPtr<MessageLoopThread> message_loop_thread_;
  66. base::CancelableClosure task_wrapper_;
  67. base::RepeatingClosure task_;
  68. base::TimeDelta period_;
  69. uint64_t expected_time_next_task_us_; // Using clock boot time in time_util.h
  70. mutable std::recursive_mutex api_mutex_;
  71. void CancelHelper(std::promise<void> promise);
  72. void CancelClosure(std::promise<void> promise);
  73. void RunTask();
  74. DISALLOW_COPY_AND_ASSIGN(RepeatingTimer);
  75. };
  76. } // namespace common
  77. } // namespace bluetooth