Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
TimerThread Class Reference

#include <TimerThread.h>

Inheritance diagram for TimerThread:
Inheritance graph
[legend]
Collaboration diagram for TimerThread:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 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)
void UpdateFilter (PRUint32 aDelay, PRIntervalTime aTimeout, PRIntervalTime aNow)
void DoBeforeSleep ()
void DoAfterSleep ()
NS_IMETHOD Run ()=0
 Defines an entry point for a newly created thread.
void run ()
void observe (in nsISupports aSubject, in string aTopic, in wstring aData)
 Observe will be called when there is a notification for the topic |aTopic|.

Public Attributes

nsCOMPtr< nsIEventQueueServicemEventQueueService

Private Member Functions

 ~TimerThread ()
PRInt32 AddTimerInternal (nsTimerImpl *aTimer)
PRBool RemoveTimerInternal (nsTimerImpl *aTimer)

Private Attributes

PRInt32 mInitInProgress
PRBool mInitialized
nsCOMPtr< nsIThreadmThread
PRLockmLock
PRCondVarmCondVar
PRPackedBool mShutdown
PRPackedBool mWaiting
PRPackedBool mSleeping
nsVoidArray mTimers
PRInt32 mDelayLine [DELAY_LINE_LENGTH]
PRUint32 mDelayLineCounter
PRUint32 mMinTimerPeriod
PRInt32 mTimeoutAdjustment

Detailed Description

Definition at line 57 of file TimerThread.h.


Constructor & Destructor Documentation

Definition at line 66 of file TimerThread.cpp.

{
  if (mCondVar)
    PR_DestroyCondVar(mCondVar);
  if (mLock)
    PR_DestroyLock(mLock);

  mThread = nsnull;

  PRInt32 n = mTimers.Count();
  while (--n >= 0) {
    nsTimerImpl *timer = NS_STATIC_CAST(nsTimerImpl *, mTimers[n]);
    NS_RELEASE(timer);
  }
}

Here is the call graph for this function:


Member Function Documentation

Definition at line 325 of file TimerThread.cpp.

{
  nsAutoLock lock(mLock);

  // Add the timer to our list.
  PRInt32 i = AddTimerInternal(aTimer);
  if (i < 0)
    return NS_ERROR_OUT_OF_MEMORY;

  // Awaken the timer thread.
  if (mCondVar && mWaiting && i == 0)
    PR_NotifyCondVar(mCondVar);

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 382 of file TimerThread.cpp.

{
  PRIntervalTime now = PR_IntervalNow();
  PRInt32 count = mTimers.Count();
  PRInt32 i = 0;
  for (; i < count; i++) {
    nsTimerImpl *timer = NS_STATIC_CAST(nsTimerImpl *, mTimers[i]);

    // Don't break till we have skipped any overdue timers.  Do not include
    // mTimeoutAdjustment here, because we are really trying to avoid calling
    // TIMER_LESS_THAN(t, u), where the t is now + DELAY_INTERVAL_MAX, u is
    // now - overdue, and DELAY_INTERVAL_MAX + overdue > DELAY_INTERVAL_LIMIT.
    // In other words, we want to use now-based time, now adjusted time, even
    // though "overdue" ultimately depends on adjusted time.

    // XXX does this hold for TYPE_REPEATING_PRECISE?  /be

    if (TIMER_LESS_THAN(now, timer->mTimeout) &&
        TIMER_LESS_THAN(aTimer->mTimeout, timer->mTimeout)) {
      break;
    }
  }

  if (!mTimers.InsertElementAt(aTimer, i))
    return -1;

  aTimer->mArmed = PR_TRUE;
  NS_ADDREF(aTimer);
  return i;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 429 of file TimerThread.cpp.

{
  mSleeping = PR_TRUE; // wake may be notified without preceding sleep notification
  for (PRInt32 i = 0; i < mTimers.Count(); i ++) {
    nsTimerImpl *timer = NS_STATIC_CAST(nsTimerImpl*, mTimers[i]);
    // get and set the delay to cause its timeout to be recomputed
    PRUint32 delay;
    timer->GetDelay(&delay);
    timer->SetDelay(delay);
  }

  // nuke the stored adjustments, so they get recalibrated
  mTimeoutAdjustment = 0;
  mDelayLineCounter = 0;
  mSleeping = PR_FALSE;
}

Definition at line 424 of file TimerThread.cpp.

NS_DECL_ISUPPORTS NS_DECL_NSIRUNNABLE NS_DECL_NSIOBSERVER TimerThread::NS_HIDDEN_ ( nsresult  )
void nsIObserver::observe ( in nsISupports  aSubject,
in string  aTopic,
in wstring  aData 
) [inherited]

Observe will be called when there is a notification for the topic |aTopic|.

This assumes that the object implementing this interface has been registered with an observer service such as the nsIObserverService.

If you expect multiple topics/subjects, the impl is responsible for filtering.

You should not modify, add, remove, or enumerate notifications in the implemention of observe.

Parameters:
aSubject: Notification specific interface pointer.
aTopic: The notification topic or subject.
aData: Notification specific wide string. subject event.

Definition at line 360 of file TimerThread.cpp.

{
  nsAutoLock lock(mLock);

  // Remove the timer from our array.  Tell callers that aTimer was not found
  // by returning NS_ERROR_NOT_AVAILABLE.  Unlike the TimerDelayChanged case
  // immediately above, our caller may be passing a (now-)weak ref in via the
  // aTimer param, specifically when nsTimerImpl::Release loses a race with
  // TimerThread::Run, must wait for the mLock auto-lock here, and during the
  // wait Run drops the only remaining ref to aTimer via RemoveTimerInternal.

  if (!RemoveTimerInternal(aTimer))
    return NS_ERROR_NOT_AVAILABLE;

  // Awaken the timer thread.
  if (mCondVar && mWaiting)
    PR_NotifyCondVar(mCondVar);

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 413 of file TimerThread.cpp.

{
  if (!mTimers.RemoveElement(aTimer))
    return PR_FALSE;

  // Order is crucial here -- see nsTimerImpl::Release.
  aTimer->mArmed = PR_FALSE;
  NS_RELEASE(aTimer);
  return PR_TRUE;
}

Here is the caller graph for this function:

void nsIRunnable::run ( ) [inherited]
NS_IMETHOD nsIRunnable::Run ( ) [pure virtual, inherited]

Definition at line 341 of file TimerThread.cpp.

{
  nsAutoLock lock(mLock);

  // Our caller has a strong ref to aTimer, so it can't go away here under
  // ReleaseTimerInternal.
  RemoveTimerInternal(aTimer);

  PRInt32 i = AddTimerInternal(aTimer);
  if (i < 0)
    return NS_ERROR_OUT_OF_MEMORY;

  // Awaken the timer thread.
  if (mCondVar && mWaiting && i == 0)
    PR_NotifyCondVar(mCondVar);

  return NS_OK;
}

Here is the call graph for this function:

Definition at line 181 of file TimerThread.cpp.

{
  PRInt32 slack = (PRInt32) (aTimeout - aNow);
  double smoothSlack = 0;
  PRUint32 i, filterLength;
  static PRIntervalTime kFilterFeedbackMaxTicks =
    PR_MillisecondsToInterval(FILTER_FEEDBACK_MAX);

  if (slack > 0) {
    if (slack > (PRInt32)kFilterFeedbackMaxTicks)
      slack = kFilterFeedbackMaxTicks;
  } else {
    if (slack < -(PRInt32)kFilterFeedbackMaxTicks)
      slack = -(PRInt32)kFilterFeedbackMaxTicks;
  }
  mDelayLine[mDelayLineCounter & DELAY_LINE_LENGTH_MASK] = slack;
  if (++mDelayLineCounter < DELAY_LINE_LENGTH) {
    // Startup mode: accumulate a full delay line before filtering.
    PR_ASSERT(mTimeoutAdjustment == 0);
    filterLength = 0;
  } else {
    // Past startup: compute number of filter taps based on mMinTimerPeriod.
    if (mMinTimerPeriod == 0) {
      mMinTimerPeriod = (aDelay != 0) ? aDelay : 1;
    } else if (aDelay != 0 && aDelay < mMinTimerPeriod) {
      mMinTimerPeriod = aDelay;
    }

    filterLength = (PRUint32) (FILTER_DURATION / mMinTimerPeriod);
    if (filterLength > DELAY_LINE_LENGTH)
      filterLength = DELAY_LINE_LENGTH;
    else if (filterLength < 4)
      filterLength = 4;

    for (i = 1; i <= filterLength; i++)
      smoothSlack += mDelayLine[(mDelayLineCounter-i) & DELAY_LINE_LENGTH_MASK];
    smoothSlack /= filterLength;

    // XXXbe do we need amplification?  hacking a fudge factor, need testing...
    mTimeoutAdjustment = (PRInt32) (smoothSlack * 1.5);
  }

#ifdef DEBUG_TIMERS
  if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
    PR_LOG(gTimerLog, PR_LOG_DEBUG,
           ("UpdateFilter: smoothSlack = %g, filterLength = %u\n",
            smoothSlack, filterLength));
  }
#endif
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 101 of file TimerThread.h.

Definition at line 113 of file TimerThread.h.

Definition at line 114 of file TimerThread.h.

Definition at line 82 of file TimerThread.h.

Definition at line 91 of file TimerThread.h.

Definition at line 90 of file TimerThread.h.

Definition at line 100 of file TimerThread.h.

Definition at line 115 of file TimerThread.h.

Definition at line 103 of file TimerThread.h.

Definition at line 105 of file TimerThread.h.

Definition at line 99 of file TimerThread.h.

Definition at line 116 of file TimerThread.h.

Definition at line 107 of file TimerThread.h.

Definition at line 104 of file TimerThread.h.


The documentation for this class was generated from the following files: