coalesced_tasks.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2022 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. #ifndef THIRD_PARTY_WEBRTC_OVERRIDES_COALESCED_TASKS_H_
  5. #define THIRD_PARTY_WEBRTC_OVERRIDES_COALESCED_TASKS_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include "base/callback.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/time/time.h"
  12. #include "third_party/abseil-cpp/absl/functional/any_invocable.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "third_party/webrtc/rtc_base/system/rtc_export.h"
  15. namespace blink {
  16. // A thread-safe class for storing queued tasks until they are scheduled to run.
  17. // This is useful for implementing metronome-like task queues where tasks are
  18. // coalesced onto metronome ticks but the tasks still need to execute in order.
  19. class RTC_EXPORT CoalescedTasks {
  20. public:
  21. typedef base::RepeatingCallback<absl::optional<base::TimeTicks>()>
  22. PrepareRunTaskCallback;
  23. typedef base::RepeatingCallback<void(absl::optional<base::TimeTicks>)>
  24. FinalizeRunTaskCallback;
  25. ~CoalescedTasks();
  26. // Queue a delayed tasks that can later be run with RunScheduledTasks().
  27. // `task_time` is the original run time of `task` and `scheduled_time` is the
  28. // `task_time` but snapped to the next time that scheduled tasks may run.
  29. // If true is returned, this is the first task that has been queued to run on
  30. // `scheduled_time`. In this case, the caller is responsible for scheduling a
  31. // call to RunScheduledTasks() at `scheduled_time`.
  32. bool QueueDelayedTask(base::TimeTicks task_time,
  33. absl::AnyInvocable<void() &&> task,
  34. base::TimeTicks scheduled_time);
  35. // Run all queued tasks up to and including `scheduled_time`. If multiple
  36. // tasks were queued onto the same `scheduled_time` they will execute in order
  37. // of their `task_time`. Optionally, the prepare/finalize callbacks can be
  38. // used to sample task run times by being called before/after each task.
  39. void RunScheduledTasks(base::TimeTicks scheduled_time,
  40. PrepareRunTaskCallback prepare_run_task_callback =
  41. PrepareRunTaskCallback(),
  42. FinalizeRunTaskCallback finalize_run_task_callback =
  43. FinalizeRunTaskCallback());
  44. // Clear the queue, deleting all tasks on the calling sequence without running
  45. // them.
  46. void Clear();
  47. private:
  48. // The (time_ticks, unique_id) pair allows multiple tasks to be scheduled on
  49. // the same `time_ticks`.
  50. struct RTC_EXPORT UniqueTimeTicks {
  51. UniqueTimeTicks(base::TimeTicks time_ticks, uint64_t unique_id);
  52. // Used for std::map<> ordering.
  53. bool operator<(const UniqueTimeTicks& other) const;
  54. base::TimeTicks time_ticks;
  55. uint64_t unique_id;
  56. };
  57. base::Lock lock_;
  58. std::set<base::TimeTicks> scheduled_ticks_ GUARDED_BY(lock_);
  59. uint64_t next_unique_id_ GUARDED_BY(lock_) = 0;
  60. std::map<UniqueTimeTicks, absl::AnyInvocable<void() &&>> delayed_tasks_
  61. GUARDED_BY(lock_);
  62. };
  63. } // namespace blink
  64. #endif // THIRD_PARTY_WEBRTC_OVERRIDES_COALESCED_TASKS_H_