Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
nsTimerImpl.cpp File Reference
#include "nsTimerImpl.h"
#include "TimerThread.h"
#include "nsAutoLock.h"
#include "nsVoidArray.h"
#include "nsIEventQueue.h"
#include "prmem.h"

Go to the source code of this file.


struct  TimerEventType


 NS_IMETHODIMP_ (nsrefcnt) nsTimerImpl
voidhandleTimerEvent (TimerEventType *event)
void destroyTimerEvent (TimerEventType *event)
nsresult NS_NewTimer (nsITimer **aResult, nsTimerCallbackFunc aCallback, void *aClosure, PRUint32 aDelay, PRUint32 aType)


static PRInt32 gGenerator = 0
static TimerThreadgThread = nsnull
static PRBool gFireOnIdle = PR_FALSE
static nsTimerManagergManager = nsnull

Function Documentation

Definition at line 464 of file nsTimerImpl.cpp.

  nsTimerImpl *timer = NS_STATIC_CAST(nsTimerImpl*, event->owner);

Here is the caller graph for this function:

Definition at line 433 of file nsTimerImpl.cpp.

  nsTimerImpl* timer = NS_STATIC_CAST(nsTimerImpl*, event->owner);
  if (event->mGeneration != timer->GetGeneration())
    return nsnull;

  if (PR_LOG_TEST(gTimerLog, PR_LOG_DEBUG)) {
    PRIntervalTime now = PR_IntervalNow();
    PR_LOG(gTimerLog, PR_LOG_DEBUG,
           ("[this=%p] time between PostTimerEvent() and Fire(): %dms\n",
            event->owner, PR_IntervalToMilliseconds(now - event->mInitTime)));

  if (gFireOnIdle) {
    PRBool idle = PR_FALSE;
    if (idle) {
      NS_ASSERTION(gManager, "Global Thread Manager is null!");
      if (gManager)
      return nsnull;


  return nsnull;

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 86 of file nsTimerImpl.cpp.

  nsrefcnt count;

  NS_PRECONDITION(0 != mRefCnt, "dup release");
  count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
  NS_LOG_RELEASE(this, count, "nsTimerImpl");
  if (count == 0) {
    mRefCnt = 1; /* stabilize */

    /* enable this to find non-threadsafe destructors: */
    /* NS_ASSERT_OWNINGTHREAD(nsTimerImpl); */
    return 0;

  // If only one reference remains, and mArmed is set, then the ref must be
  // from the TimerThread::mTimers array, so we Cancel this timer to remove
  // the mTimers element, and return 0 if Cancel in fact disarmed the timer.
  // We use an inlined version of nsTimerImpl::Cancel here to check for the
  // NS_ERROR_NOT_AVAILABLE code returned by gThread->RemoveTimer when this
  // timer is not found in the mTimers array -- i.e., when the timer was not
  // in fact armed once we acquired TimerThread::mLock, in spite of mArmed
  // being true here.  That can happen if the armed timer is being fired by
  // TimerThread::Run as we race and test mArmed just before it is cleared by
  // the timer thread.  If the RemoveTimer call below doesn't find this timer
  // in the mTimers array, then the last ref to this timer is held manually
  // and temporarily by the TimerThread, so we should fall through to the
  // final return and return 1, not 0.
  // The original version of this thread-based timer code kept weak refs from
  // TimerThread::mTimers, removing this timer's weak ref in the destructor,
  // but that leads to double-destructions in the race described above, and
  // adding mArmed doesn't help, because destructors can't be deferred, once
  // begun.  But by combining reference-counting and a specialized Release
  // method with "is this timer still in the mTimers array once we acquire
  // the TimerThread's lock" testing, we defer destruction until we're sure
  // that only one thread has its hot little hands on this timer.
  // Note that both approaches preclude a timer creator, and everyone else
  // except the TimerThread who might have a strong ref, from dropping all
  // their strong refs without implicitly canceling the timer.  Timers need
  // non-mTimers-element strong refs to stay alive.

  if (count == 1 && mArmed) {
    mCanceled = PR_TRUE;

    if (NS_SUCCEEDED(gThread->RemoveTimer(this)))
      return 0;

  return count;

Here is the call graph for this function:

nsresult NS_NewTimer ( nsITimer **  aResult,
nsTimerCallbackFunc  aCallback,
void aClosure,
PRUint32  aDelay,
PRUint32  aType 

Definition at line 636 of file nsTimerImpl.cpp.

    nsTimerImpl* timer = new nsTimerImpl();
    if (timer == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;

    nsresult rv = timer->InitWithFuncCallback(aCallback, aClosure, 
                                              aDelay, aType);
    if (NS_FAILED(rv)) {
        return rv;

    *aResult = timer;
    return NS_OK;

Here is the call graph for this function:

Variable Documentation

Definition at line 53 of file nsTimerImpl.cpp.

PRInt32 gGenerator = 0 [static]

Definition at line 51 of file nsTimerImpl.cpp.

Definition at line 54 of file nsTimerImpl.cpp.

TimerThread* gThread = nsnull [static]

Definition at line 52 of file nsTimerImpl.cpp.