Bug 1430948. P1 - add fuzzy mode to MediaTimer. r=jya

We will resolve the timer promise even if it is fired slightly before
the scheduled target. This is used by MDSM which doesn't require high-res
timers.

MozReview-Commit-ID: 1IAsG5gG9D7

--HG--
extra : rebase_source : 71c9bec9d196cd736effabb5b9da54c58b44044c
extra : source : 7b96fc15d13d54b78b0aebbf21bb16b08e4eca50
This commit is contained in:
JW Wang 2018-01-16 16:07:21 +08:00
Родитель 214ca05b67
Коммит 5648dab805
2 изменённых файлов: 21 добавлений и 5 удалений

Просмотреть файл

@ -18,11 +18,12 @@ namespace mozilla {
NS_IMPL_ADDREF(MediaTimer)
NS_IMPL_RELEASE_WITH_DESTROY(MediaTimer, DispatchDestroy())
MediaTimer::MediaTimer()
MediaTimer::MediaTimer(bool aFuzzy)
: mMonitor("MediaTimer Monitor")
, mTimer(NS_NewTimer())
, mCreationTimeStamp(TimeStamp::Now())
, mUpdateScheduled(false)
, mFuzzy(aFuzzy)
{
TIMER_LOG("MediaTimer::MediaTimer");
@ -114,6 +115,19 @@ MediaTimer::Update()
UpdateLocked();
}
bool
MediaTimer::IsExpired(const TimeStamp& aTarget, const TimeStamp& 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;
return t <= aNow;
}
void
MediaTimer::UpdateLocked()
{
@ -125,7 +139,7 @@ MediaTimer::UpdateLocked()
// Resolve all the promises whose time is up.
TimeStamp now = TimeStamp::Now();
while (!mEntries.empty() && mEntries.top().mTimeStamp <= now) {
while (!mEntries.empty() && IsExpired(mEntries.top().mTimeStamp, now)) {
mEntries.top().mPromise->Resolve(true, __func__);
DebugOnly<TimeStamp> poppedTimeStamp = mEntries.top().mTimeStamp;
mEntries.pop();

Просмотреть файл

@ -36,7 +36,7 @@ typedef MozPromise<bool, bool, /* IsExclusive = */ true> MediaTimerPromise;
class MediaTimer
{
public:
MediaTimer();
explicit MediaTimer(bool aFuzzy = false);
// We use a release with a custom Destroy().
NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
@ -54,6 +54,7 @@ private:
void ScheduleUpdate();
void Update();
void UpdateLocked();
bool IsExpired(const TimeStamp& aTarget, const TimeStamp& aNow);
static void TimerCallback(nsITimer* aTimer, void* aClosure);
void TimerFired();
@ -111,13 +112,14 @@ private:
}
bool mUpdateScheduled;
const bool mFuzzy;
};
// Class for managing delayed dispatches on target thread.
class DelayedScheduler {
public:
explicit DelayedScheduler(AbstractThread* aTargetThread)
: mTargetThread(aTargetThread), mMediaTimer(new MediaTimer())
explicit DelayedScheduler(AbstractThread* aTargetThread, bool aFuzzy = false)
: mTargetThread(aTargetThread), mMediaTimer(new MediaTimer(aFuzzy))
{
MOZ_ASSERT(mTargetThread);
}