mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-23 04:41:11 +00:00
Bug 1921288 - Generalize MediaTimer over the timestamp kind it uses internally. r=pehrsons
This inlines into the header, and moves the TimeStamp to a Maybe since AwakeTimeStamp doesn't support being null. Differential Revision: https://phabricator.services.mozilla.com/D223794
This commit is contained in:
parent
da6e72dc6c
commit
de4f6616f3
@ -6,6 +6,7 @@
|
||||
|
||||
#include "MediaTimer.h"
|
||||
|
||||
#include "mozilla/AwakeTimeStamp.h"
|
||||
#include "mozilla/DebugOnly.h"
|
||||
#include "mozilla/RefPtr.h"
|
||||
#include "mozilla/SharedThreadPool.h"
|
||||
@ -16,9 +17,10 @@
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
MediaTimer::MediaTimer(bool aFuzzy)
|
||||
template <typename T>
|
||||
MediaTimer<T>::MediaTimer(bool aFuzzy)
|
||||
: mMonitor("MediaTimer Monitor"),
|
||||
mCreationTimeStamp(TimeStamp::Now()),
|
||||
mCreationTimeStamp(T::Now()),
|
||||
mUpdateScheduled(false),
|
||||
mFuzzy(aFuzzy) {
|
||||
TIMER_LOG("MediaTimer::MediaTimer");
|
||||
@ -31,7 +33,8 @@ MediaTimer::MediaTimer(bool aFuzzy)
|
||||
mTimer = NS_NewTimer(mThread);
|
||||
}
|
||||
|
||||
void MediaTimer::DispatchDestroy() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::DispatchDestroy() {
|
||||
// Hold a strong reference to the thread so that it doesn't get deleted in
|
||||
// Destroy(), which may run completely before the stack if Dispatch() begins
|
||||
// to unwind.
|
||||
@ -45,7 +48,9 @@ void MediaTimer::DispatchDestroy() {
|
||||
(void)rv;
|
||||
}
|
||||
|
||||
void MediaTimer::Destroy() {
|
||||
// Runs on the timer thread.
|
||||
template <typename T>
|
||||
void MediaTimer<T>::Destroy() {
|
||||
MOZ_ASSERT(OnMediaTimerThread());
|
||||
TIMER_LOG("MediaTimer::Destroy");
|
||||
|
||||
@ -61,19 +66,22 @@ void MediaTimer::Destroy() {
|
||||
delete this;
|
||||
}
|
||||
|
||||
bool MediaTimer::OnMediaTimerThread() {
|
||||
template <typename T>
|
||||
bool MediaTimer<T>::OnMediaTimerThread() {
|
||||
bool rv = false;
|
||||
mThread->IsOnCurrentThread(&rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
RefPtr<MediaTimerPromise> MediaTimer::WaitFor(const TimeDuration& aDuration,
|
||||
StaticString aCallSite) {
|
||||
return WaitUntil(TimeStamp::Now() + aDuration, aCallSite);
|
||||
template <typename T>
|
||||
RefPtr<MediaTimerPromise> MediaTimer<T>::WaitFor(
|
||||
const typename T::DurationType& aDuration, StaticString aCallSite) {
|
||||
return WaitUntil(T::Now() + aDuration, aCallSite);
|
||||
}
|
||||
|
||||
RefPtr<MediaTimerPromise> MediaTimer::WaitUntil(const TimeStamp& aTimeStamp,
|
||||
StaticString aCallSite) {
|
||||
template <typename T>
|
||||
RefPtr<MediaTimerPromise> MediaTimer<T>::WaitUntil(const T& aTimeStamp,
|
||||
StaticString aCallSite) {
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
TIMER_LOG("MediaTimer::WaitUntil %" PRId64, RelativeMicroseconds(aTimeStamp));
|
||||
Entry e(aTimeStamp, aCallSite);
|
||||
@ -83,13 +91,15 @@ RefPtr<MediaTimerPromise> MediaTimer::WaitUntil(const TimeStamp& aTimeStamp,
|
||||
return p;
|
||||
}
|
||||
|
||||
void MediaTimer::Cancel() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::Cancel() {
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
TIMER_LOG("MediaTimer::Cancel");
|
||||
Reject();
|
||||
}
|
||||
|
||||
void MediaTimer::ScheduleUpdate() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::ScheduleUpdate() {
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
if (mUpdateScheduled) {
|
||||
return;
|
||||
@ -104,23 +114,26 @@ void MediaTimer::ScheduleUpdate() {
|
||||
(void)rv;
|
||||
}
|
||||
|
||||
void MediaTimer::Update() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::Update() {
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
UpdateLocked();
|
||||
}
|
||||
|
||||
bool MediaTimer::IsExpired(const TimeStamp& aTarget, const TimeStamp& aNow) {
|
||||
template <typename T>
|
||||
bool MediaTimer<T>::IsExpired(const T& aTarget, const T& aNow) {
|
||||
MOZ_ASSERT(OnMediaTimerThread());
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
// Treat this timer as expired in fuzzy mode even if it is fired
|
||||
// slightly (< 1ms) before the schedule target. So we don't need to schedule a
|
||||
// timer with very small timeout again when the client doesn't need a high-res
|
||||
// timer.
|
||||
TimeStamp t = mFuzzy ? aTarget - TimeDuration::FromMilliseconds(1) : aTarget;
|
||||
// slightly (< 1ms) before the schedule target. So we don't need to schedule
|
||||
// a timer with very small timeout again when the client doesn't need a
|
||||
// high-res timer.
|
||||
T t = mFuzzy ? aTarget - T::DurationType::FromMilliseconds(1) : aTarget;
|
||||
return t <= aNow;
|
||||
}
|
||||
|
||||
void MediaTimer::UpdateLocked() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::UpdateLocked() {
|
||||
MOZ_ASSERT(OnMediaTimerThread());
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
mUpdateScheduled = false;
|
||||
@ -128,10 +141,10 @@ void MediaTimer::UpdateLocked() {
|
||||
TIMER_LOG("MediaTimer::UpdateLocked");
|
||||
|
||||
// Resolve all the promises whose time is up.
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
T now = T::Now();
|
||||
while (!mEntries.empty() && IsExpired(mEntries.top().mTimeStamp, now)) {
|
||||
mEntries.top().mPromise->Resolve(true, __func__);
|
||||
DebugOnly<TimeStamp> poppedTimeStamp = mEntries.top().mTimeStamp;
|
||||
DebugOnly<T> poppedTimeStamp = mEntries.top().mTimeStamp;
|
||||
mEntries.pop();
|
||||
MOZ_ASSERT_IF(!mEntries.empty(),
|
||||
*&poppedTimeStamp <= mEntries.top().mTimeStamp);
|
||||
@ -144,13 +157,15 @@ void MediaTimer::UpdateLocked() {
|
||||
}
|
||||
|
||||
// We've got more entries - (re)arm the timer for the soonest one.
|
||||
if (!TimerIsArmed() || mEntries.top().mTimeStamp < mCurrentTimerTarget) {
|
||||
if (!TimerIsArmed() ||
|
||||
mEntries.top().mTimeStamp < mCurrentTimerTarget.value()) {
|
||||
CancelTimerIfArmed();
|
||||
ArmTimer(mEntries.top().mTimeStamp, now);
|
||||
}
|
||||
}
|
||||
|
||||
void MediaTimer::Reject() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::Reject() {
|
||||
mMonitor.AssertCurrentThreadOwns();
|
||||
while (!mEntries.empty()) {
|
||||
mEntries.top().mPromise->Reject(false, __func__);
|
||||
@ -158,35 +173,51 @@ void MediaTimer::Reject() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* We use a callback function, rather than a callback method, to ensure that
|
||||
* the nsITimer does not artifically keep the refcount of the MediaTimer above
|
||||
* zero. When the MediaTimer is destroyed, it safely cancels the nsITimer so
|
||||
* that we never fire against a dangling closure.
|
||||
*/
|
||||
|
||||
/* static */
|
||||
void MediaTimer::TimerCallback(nsITimer* aTimer, void* aClosure) {
|
||||
static_cast<MediaTimer*>(aClosure)->TimerFired();
|
||||
template <typename T>
|
||||
/* static */ void MediaTimer<T>::TimerCallback(nsITimer* aTimer,
|
||||
void* aClosure) {
|
||||
static_cast<MediaTimer<T>*>(aClosure)->TimerFired();
|
||||
}
|
||||
|
||||
void MediaTimer::TimerFired() {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::TimerFired() {
|
||||
MonitorAutoLock mon(mMonitor);
|
||||
MOZ_ASSERT(OnMediaTimerThread());
|
||||
mCurrentTimerTarget = TimeStamp();
|
||||
mCurrentTimerTarget = Nothing();
|
||||
UpdateLocked();
|
||||
}
|
||||
|
||||
void MediaTimer::ArmTimer(const TimeStamp& aTarget, const TimeStamp& aNow) {
|
||||
template <typename T>
|
||||
void MediaTimer<T>::ArmTimer(const T& aTarget, const T& aNow) {
|
||||
MOZ_DIAGNOSTIC_ASSERT(!TimerIsArmed());
|
||||
MOZ_DIAGNOSTIC_ASSERT(aTarget > aNow);
|
||||
|
||||
const TimeDuration delay = aTarget - aNow;
|
||||
const typename T::DurationType delay = aTarget - aNow;
|
||||
TIMER_LOG("MediaTimer::ArmTimer delay=%.3fms", delay.ToMilliseconds());
|
||||
mCurrentTimerTarget = aTarget;
|
||||
mCurrentTimerTarget.emplace(aTarget);
|
||||
TimeDuration duration =
|
||||
TimeDuration::FromMicroseconds(delay.ToMicroseconds());
|
||||
MOZ_ALWAYS_SUCCEEDS(mTimer->InitHighResolutionWithNamedFuncCallback(
|
||||
&TimerCallback, this, delay, nsITimer::TYPE_ONE_SHOT,
|
||||
&TimerCallback, this, duration, nsITimer::TYPE_ONE_SHOT,
|
||||
"MediaTimer::TimerCallback"));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool MediaTimer<T>::TimerIsArmed() {
|
||||
return mCurrentTimerTarget.isSome();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void MediaTimer<T>::CancelTimerIfArmed() {
|
||||
MOZ_ASSERT(OnMediaTimerThread());
|
||||
if (TimerIsArmed()) {
|
||||
TIMER_LOG("MediaTimer::CancelTimerIfArmed canceling timer");
|
||||
mTimer->Cancel();
|
||||
mCurrentTimerTarget = Nothing();
|
||||
}
|
||||
}
|
||||
|
||||
template class MediaTimer<AwakeTimeStamp>;
|
||||
template class MediaTimer<TimeStamp>;
|
||||
|
||||
} // namespace mozilla
|
||||
|
@ -10,10 +10,11 @@
|
||||
# include <queue>
|
||||
|
||||
# include "mozilla/AbstractThread.h"
|
||||
# include "mozilla/IntegerPrintfMacros.h"
|
||||
# include "mozilla/AwakeTimeStamp.h"
|
||||
# include "mozilla/Monitor.h"
|
||||
# include "mozilla/MozPromise.h"
|
||||
# include "mozilla/RefPtr.h"
|
||||
# include "mozilla/SharedThreadPool.h"
|
||||
# include "mozilla/TimeStamp.h"
|
||||
# include "mozilla/Unused.h"
|
||||
# include "nsITimer.h"
|
||||
@ -26,16 +27,17 @@ extern LazyLogModule gMediaTimerLog;
|
||||
MOZ_ASSERT(gMediaTimerLog); \
|
||||
MOZ_LOG(gMediaTimerLog, LogLevel::Debug, \
|
||||
("[MediaTimer=%p relative_t=%" PRId64 "]" x, this, \
|
||||
RelativeMicroseconds(TimeStamp::Now()), ##__VA_ARGS__))
|
||||
RelativeMicroseconds(T::Now()), ##__VA_ARGS__))
|
||||
|
||||
// This promise type is only exclusive because so far there isn't a reason for
|
||||
// it not to be. Feel free to change that.
|
||||
typedef MozPromise<bool, bool, /* IsExclusive = */ true> MediaTimerPromise;
|
||||
using MediaTimerPromise = MozPromise<bool, bool, true>;
|
||||
|
||||
// Timers only know how to fire at a given thread, which creates an impedence
|
||||
// mismatch with code that operates with TaskQueues. This class solves
|
||||
// that mismatch with a dedicated (but shared) thread and a nice MozPromise-y
|
||||
// interface.
|
||||
template <typename T>
|
||||
class MediaTimer {
|
||||
public:
|
||||
explicit MediaTimer(bool aFuzzy = false);
|
||||
@ -43,45 +45,45 @@ class MediaTimer {
|
||||
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(MediaTimer,
|
||||
DispatchDestroy());
|
||||
|
||||
RefPtr<MediaTimerPromise> WaitFor(const TimeDuration& aDuration,
|
||||
RefPtr<MediaTimerPromise> WaitFor(const typename T::DurationType& aDuration,
|
||||
StaticString aCallSite);
|
||||
RefPtr<MediaTimerPromise> WaitUntil(const TimeStamp& aTimeStamp,
|
||||
|
||||
RefPtr<MediaTimerPromise> WaitUntil(const T& aTimeStamp,
|
||||
StaticString aCallSite);
|
||||
void Cancel(); // Cancel and reject any unresolved promises with false.
|
||||
|
||||
// Cancel and reject any unresolved promises with false.
|
||||
void Cancel();
|
||||
|
||||
private:
|
||||
virtual ~MediaTimer() { MOZ_ASSERT(OnMediaTimerThread()); }
|
||||
|
||||
void DispatchDestroy(); // Invoked by Release on an arbitrary thread.
|
||||
void Destroy(); // Runs on the timer thread.
|
||||
|
||||
void DispatchDestroy();
|
||||
// Runs on the timer thread.
|
||||
void Destroy();
|
||||
bool OnMediaTimerThread();
|
||||
void ScheduleUpdate();
|
||||
void Update();
|
||||
void UpdateLocked();
|
||||
bool IsExpired(const TimeStamp& aTarget, const TimeStamp& aNow);
|
||||
bool IsExpired(const T& aTarget, const T& aNow);
|
||||
void Reject();
|
||||
|
||||
/*
|
||||
* We use a callback function, rather than a callback method, to ensure that
|
||||
* the nsITimer does not artifically keep the refcount of the MediaTimer above
|
||||
* zero. When the MediaTimer is destroyed, it safely cancels the nsITimer so
|
||||
* that we never fire against a dangling closure.
|
||||
*/
|
||||
static void TimerCallback(nsITimer* aTimer, void* aClosure);
|
||||
void TimerFired();
|
||||
void ArmTimer(const TimeStamp& aTarget, const TimeStamp& aNow);
|
||||
void ArmTimer(const T& aTarget, const T& aNow);
|
||||
|
||||
bool TimerIsArmed() { return !mCurrentTimerTarget.IsNull(); }
|
||||
|
||||
void CancelTimerIfArmed() {
|
||||
MOZ_ASSERT(OnMediaTimerThread());
|
||||
if (TimerIsArmed()) {
|
||||
TIMER_LOG("MediaTimer::CancelTimerIfArmed canceling timer");
|
||||
mTimer->Cancel();
|
||||
mCurrentTimerTarget = TimeStamp();
|
||||
}
|
||||
}
|
||||
bool TimerIsArmed();
|
||||
void CancelTimerIfArmed();
|
||||
|
||||
struct Entry {
|
||||
TimeStamp mTimeStamp;
|
||||
T mTimeStamp;
|
||||
RefPtr<MediaTimerPromise::Private> mPromise;
|
||||
|
||||
explicit Entry(const TimeStamp& aTimeStamp, StaticString aCallSite)
|
||||
explicit Entry(const T& aTimeStamp, StaticString aCallSite)
|
||||
: mTimeStamp(aTimeStamp),
|
||||
mPromise(new MediaTimerPromise::Private(aCallSite)) {}
|
||||
|
||||
@ -97,12 +99,12 @@ class MediaTimer {
|
||||
std::priority_queue<Entry> mEntries;
|
||||
Monitor mMonitor MOZ_UNANNOTATED;
|
||||
nsCOMPtr<nsITimer> mTimer;
|
||||
TimeStamp mCurrentTimerTarget;
|
||||
Maybe<T> mCurrentTimerTarget;
|
||||
|
||||
// Timestamps only have relative meaning, so we need a base timestamp for
|
||||
// logging purposes.
|
||||
TimeStamp mCreationTimeStamp;
|
||||
int64_t RelativeMicroseconds(const TimeStamp& aTimeStamp) {
|
||||
T mCreationTimeStamp;
|
||||
int64_t RelativeMicroseconds(const T& aTimeStamp) {
|
||||
return (int64_t)(aTimeStamp - mCreationTimeStamp).ToMicroseconds();
|
||||
}
|
||||
|
||||
@ -111,33 +113,33 @@ class MediaTimer {
|
||||
};
|
||||
|
||||
// Class for managing delayed dispatches on target thread.
|
||||
template <typename T>
|
||||
class DelayedScheduler {
|
||||
public:
|
||||
explicit DelayedScheduler(nsISerialEventTarget* aTargetThread,
|
||||
bool aFuzzy = false)
|
||||
: mTargetThread(aTargetThread), mMediaTimer(new MediaTimer(aFuzzy)) {
|
||||
: mTargetThread(aTargetThread), mMediaTimer(new MediaTimer<T>(aFuzzy)) {
|
||||
MOZ_ASSERT(mTargetThread);
|
||||
}
|
||||
|
||||
bool IsScheduled() const { return !mTarget.IsNull(); }
|
||||
bool IsScheduled() const { return mTarget.isSome(); }
|
||||
|
||||
void Reset() {
|
||||
MOZ_ASSERT(mTargetThread->IsOnCurrentThread(),
|
||||
"Must be on target thread to disconnect");
|
||||
mRequest.DisconnectIfExists();
|
||||
mTarget = TimeStamp();
|
||||
mTarget = Nothing();
|
||||
}
|
||||
|
||||
template <typename ResolveFunc, typename RejectFunc>
|
||||
void Ensure(mozilla::TimeStamp& aTarget, ResolveFunc&& aResolver,
|
||||
RejectFunc&& aRejector) {
|
||||
void Ensure(T& aTarget, ResolveFunc&& aResolver, RejectFunc&& aRejector) {
|
||||
MOZ_ASSERT(mTargetThread->IsOnCurrentThread());
|
||||
if (IsScheduled() && mTarget <= aTarget) {
|
||||
if (IsScheduled() && mTarget.value() <= aTarget) {
|
||||
return;
|
||||
}
|
||||
Reset();
|
||||
mTarget = aTarget;
|
||||
mMediaTimer->WaitUntil(mTarget, __func__)
|
||||
mTarget.emplace(aTarget);
|
||||
mMediaTimer->WaitUntil(mTarget.value(), __func__)
|
||||
->Then(mTargetThread, __func__, std::forward<ResolveFunc>(aResolver),
|
||||
std::forward<RejectFunc>(aRejector))
|
||||
->Track(mRequest);
|
||||
@ -146,16 +148,19 @@ class DelayedScheduler {
|
||||
void CompleteRequest() {
|
||||
MOZ_ASSERT(mTargetThread->IsOnCurrentThread());
|
||||
mRequest.Complete();
|
||||
mTarget = TimeStamp();
|
||||
mTarget = Nothing();
|
||||
}
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsISerialEventTarget> mTargetThread;
|
||||
RefPtr<MediaTimer> mMediaTimer;
|
||||
RefPtr<MediaTimer<T>> mMediaTimer;
|
||||
Maybe<T> mTarget;
|
||||
MozPromiseRequestHolder<mozilla::MediaTimerPromise> mRequest;
|
||||
TimeStamp mTarget;
|
||||
};
|
||||
|
||||
using MediaTimerTimeStamp = MediaTimer<TimeStamp>;
|
||||
using MediaTimerAwakeTimeStamp = MediaTimer<AwakeTimeStamp>;
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user