2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef TimerThread_h___
|
|
|
|
#define TimerThread_h___
|
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
|
|
|
|
#include "nsTimerImpl.h"
|
2013-03-04 16:04:59 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-03 09:43:08 -07:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-29 12:21:57 -07:00
|
|
|
#include "mozilla/Monitor.h"
|
2010-07-15 06:59:24 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class TimerThread MOZ_FINAL : public nsIRunnable,
|
|
|
|
public nsIObserver
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2011-04-29 12:21:57 -07:00
|
|
|
typedef mozilla::Monitor Monitor;
|
2010-07-15 06:59:24 -07:00
|
|
|
typedef mozilla::TimeStamp TimeStamp;
|
|
|
|
typedef mozilla::TimeDuration TimeDuration;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
TimerThread();
|
|
|
|
NS_HIDDEN_(nsresult) InitLocks();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
NS_HIDDEN_(nsresult) Init();
|
|
|
|
NS_HIDDEN_(nsresult) Shutdown();
|
|
|
|
|
|
|
|
nsresult AddTimer(nsTimerImpl *aTimer);
|
|
|
|
nsresult TimerDelayChanged(nsTimerImpl *aTimer);
|
|
|
|
nsresult RemoveTimer(nsTimerImpl *aTimer);
|
|
|
|
|
|
|
|
#define FILTER_DURATION 1e3 /* one second */
|
|
|
|
#define FILTER_FEEDBACK_MAX 100 /* 1/10th of a second */
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void UpdateFilter(uint32_t aDelay, TimeStamp aTimeout,
|
2010-07-15 06:59:24 -07:00
|
|
|
TimeStamp aNow);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void DoBeforeSleep();
|
|
|
|
void DoAfterSleep();
|
|
|
|
|
2013-03-04 16:04:59 -08:00
|
|
|
bool IsOnTimerThread() const
|
|
|
|
{
|
|
|
|
return mThread == NS_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
~TimerThread();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mInitInProgress;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// These two internal helper methods must be called while mLock is held.
|
|
|
|
// AddTimerInternal returns the position where the timer was added in the
|
|
|
|
// list, or -1 if it failed.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t AddTimerInternal(nsTimerImpl *aTimer);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool RemoveTimerInternal(nsTimerImpl *aTimer);
|
2008-03-14 09:25:14 -07:00
|
|
|
void ReleaseTimerInternal(nsTimerImpl *aTimer);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2011-04-29 12:21:57 -07:00
|
|
|
Monitor mMonitor;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mShutdown;
|
|
|
|
bool mWaiting;
|
|
|
|
bool mSleeping;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-03 09:43:08 -07:00
|
|
|
nsTArray<nsTimerImpl*> mTimers;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define DELAY_LINE_LENGTH_LOG2 5
|
2012-10-19 19:01:43 -07:00
|
|
|
#define DELAY_LINE_LENGTH_MASK ((1u << DELAY_LINE_LENGTH_LOG2) - 1)
|
|
|
|
#define DELAY_LINE_LENGTH (1u << DELAY_LINE_LENGTH_LOG2)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mDelayLine[DELAY_LINE_LENGTH]; // milliseconds
|
|
|
|
uint32_t mDelayLineCounter;
|
|
|
|
uint32_t mMinTimerPeriod; // milliseconds
|
2010-07-15 06:59:24 -07:00
|
|
|
TimeDuration mTimeoutAdjustment;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-02-13 07:11:53 -08:00
|
|
|
struct TimerAdditionComparator {
|
|
|
|
TimerAdditionComparator(const mozilla::TimeStamp &aNow,
|
|
|
|
const mozilla::TimeDuration &aTimeoutAdjustment,
|
|
|
|
nsTimerImpl *aTimerToInsert) :
|
|
|
|
now(aNow),
|
|
|
|
timeoutAdjustment(aTimeoutAdjustment)
|
|
|
|
#ifdef DEBUG
|
|
|
|
, timerToInsert(aTimerToInsert)
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
PRBool LessThan(nsTimerImpl *fromArray, nsTimerImpl *newTimer) const {
|
|
|
|
NS_ABORT_IF_FALSE(newTimer == timerToInsert, "Unexpected timer ordering");
|
|
|
|
|
|
|
|
// Skip any overdue timers.
|
|
|
|
|
|
|
|
// XXXbz why? Given our definition of overdue in terms of
|
|
|
|
// mTimeoutAdjustment, aTimer might be overdue already! Why not
|
|
|
|
// just fire timers in order?
|
|
|
|
return now >= fromArray->mTimeout + timeoutAdjustment ||
|
|
|
|
fromArray->mTimeout <= newTimer->mTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Equals(nsTimerImpl* fromArray, nsTimerImpl* newTimer) const {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const mozilla::TimeStamp &now;
|
|
|
|
const mozilla::TimeDuration &timeoutAdjustment;
|
|
|
|
#ifdef DEBUG
|
|
|
|
const nsTimerImpl * const timerToInsert;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif /* TimerThread_h___ */
|