blob: fc5f350db2ed00df09f8cd31257b833f1ff3d877 [file] [log] [blame]
Evan Shrubsole58264632022-01-27 10:35:21 +01001/*
2 * Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef API_METRONOME_METRONOME_H_
12#define API_METRONOME_METRONOME_H_
13
14#include "api/task_queue/task_queue_base.h"
15#include "api/units/time_delta.h"
16#include "rtc_base/system/rtc_export.h"
17
18namespace webrtc {
19
20// The Metronome posts OnTick() on task queues provided by its listeners' task
21// queue periodically. The metronome can be used as an alternative to using
22// PostDelayedTask on a thread or task queue for coalescing work and reducing
23// the number of idle-wakeups.
24//
25// Listeners can be added and removed from any sequence, but it is illegal to
26// remove a listener from an OnTick invocation.
27//
28// The metronome concept is still under experimentation, and may not be availble
29// in all platforms or applications. See https://crbug.com/1253787 for more
30// details.
31//
32// Metronome implementations must be thread-safe.
33class RTC_EXPORT Metronome {
34 public:
35 class RTC_EXPORT TickListener {
36 public:
37 virtual ~TickListener() = default;
38
39 // OnTick is run on the task queue provided by OnTickTaskQueue each time the
40 // metronome ticks.
41 virtual void OnTick() = 0;
42
43 // The task queue that OnTick will run on. Must not be null.
44 virtual TaskQueueBase* OnTickTaskQueue() = 0;
45 };
46
47 virtual ~Metronome() = default;
48
49 // Adds a tick listener to the metronome. Once this method has returned
50 // OnTick will be invoked on each metronome tick. A listener may
51 // only be added to the metronome once.
52 virtual void AddListener(TickListener* listener) = 0;
53
54 // Removes the tick listener from the metronome. Once this method has returned
55 // OnTick will never be called again. This method must not be called from
56 // within OnTick.
57 virtual void RemoveListener(TickListener* listener) = 0;
58
59 // Returns the current tick period of the metronome.
60 virtual TimeDelta TickPeriod() const = 0;
61};
62
63} // namespace webrtc
64
65#endif // API_METRONOME_METRONOME_H_