nsTimerImpl.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Stuart Parmenter <pavlov@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsTimerImpl.h"
#include "TimerThread.h"
#include "nsAutoLock.h"
#include "nsVoidArray.h"
#include "nsIEventQueue.h"
#include "prmem.h"
static PRInt32 gGenerator = 0;
#ifdef DEBUG_TIMERS
#include <math.h>
double nsTimerImpl::sDeltaSumSquared = 0;
double nsTimerImpl::sDeltaSum = 0;
double nsTimerImpl::sDeltaNum = 0;
static void
double *meanResult, double *stdDevResult)
{
if (n > 0.0 && sumOfValues >= 0) {
mean = sumOfValues / n;
var = 0.0;
else
// for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
}
*meanResult = mean;
*stdDevResult = stdDev;
}
#endif
{
if (count == 0) {
/* enable this to find non-threadsafe destructors: */
/* NS_ASSERT_OWNINGTHREAD(nsTimerImpl); */
NS_DELETEXPCOM(this);
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.
return 0;
}
return count;
}
nsTimerImpl::nsTimerImpl() :
mGeneration(0),
mDelay(0),
mTimeout(0)
{
// XXXbsmedberg: shouldn't this be in Init()?
#ifdef DEBUG_TIMERS
mStart = 0;
mStart2 = 0;
#endif
}
{
}
//static
{
gThread = new TimerThread();
if (!gThread) return NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
void nsTimerImpl::Shutdown()
{
#ifdef DEBUG_TIMERS
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("sDeltaNum = %f, sDeltaSum = %f, sDeltaSumSquared = %f\n", sDeltaNum, sDeltaSum, sDeltaSumSquared));
}
#endif
if (!gThread)
return;
}
{
/**
* In case of re-Init, both with and without a preceding Cancel, clear the
* mCanceled flag and assign a new mGeneration. But first, remove any armed
* timer from the timer thread's list.
*
* If we are racing with the timer thread to remove this timer and we lose,
* the RemoveTimer call made here will fail to find this timer in the timer
* thread's list, and will return false harmlessly. We test mArmed here to
* avoid the small overhead in RemoveTimer of locking the timer thread and
* checking its list for this timer. It's safe to test mArmed even though
* it might be cleared on another thread in the next cycle (or even already
* be cleared by another CPU whose store hasn't reached our CPU's cache),
* because RemoveTimer is idempotent.
*/
if (mArmed)
gThread->RemoveTimer(this);
}
void *aClosure,
{
}
{
}
{
}
{
if (gThread)
gThread->RemoveTimer(this);
return NS_OK;
}
{
// If we're already repeating precisely, update mTimeout now so that the
// new delay takes effect in the future.
mTimeout = PR_IntervalNow();
gThread->TimerDelayChanged(this);
return NS_OK;
}
{
return NS_OK;
}
{
// XXX if this is called, we should change the actual type.. this could effect
// repeating timers. we need to ensure in Fire() that if mType has changed
// during the callback that we don't end up with the timer in the queue twice.
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
{
return NS_OK;
}
void nsTimerImpl::Fire()
{
if (mCanceled)
return;
#ifdef DEBUG_TIMERS
sDeltaSum += d;
sDeltaSumSquared += double(d) * double(d);
sDeltaNum++;
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("[this=%p] actual delay time %4dms\n", this, PR_IntervalToMilliseconds(a)));
PR_LOG(gTimerLog, PR_LOG_DEBUG, ("[this=%p] delta %4dms\n", this, (a > b) ? (PRInt32)d : -(PRInt32)d));
mStart2 = 0;
}
#endif
if (mType == TYPE_REPEATING_PRECISE) {
// Precise repeating timers advance mTimeout by mDelay without fail before
// calling Fire().
}
switch (mCallbackType) {
case CALLBACK_TYPE_FUNC:
break;
case CALLBACK_TYPE_INTERFACE:
break;
case CALLBACK_TYPE_OBSERVER:
nsnull);
break;
default:;
}
#ifdef DEBUG_TIMERS
("[this=%p] Took %dms to fire timer callback\n",
}
#endif
if (mType == TYPE_REPEATING_SLACK) {
if (gThread)
}
}
struct TimerEventType : public PLEvent {
#ifdef DEBUG_TIMERS
#endif
};
{
return nsnull;
#ifdef DEBUG_TIMERS
("[this=%p] time between PostTimerEvent() and Fire(): %dms\n",
}
#endif
if (gFireOnIdle) {
if (idle) {
if (gManager)
return nsnull;
}
}
return nsnull;
}
{
}
void nsTimerImpl::PostTimerEvent()
{
// XXX we may want to reuse the PLEvent in the case of repeating timers.
// construct
if (!event)
return;
// initialize
// Since TimerThread addref'd 'this' for us, we don't need to addref here.
// We will release in destroyMyEvent. We do need to copy the generation
// number from this timer into the event, so we can avoid firing a timer
// that was re-initialized after being canceled.
#ifdef DEBUG_TIMERS
}
#endif
// If this is a repeating precise timer, we need to calculate the time for
// the next timer to fire before we make the callback.
if (mType == TYPE_REPEATING_PRECISE) {
if (gThread)
}
NS_WARNING("Dropping timer event because thread is dead");
return;
}
if (gThread)
if (queue)
}
{
if (delayInterval > DELAY_INTERVAL_MAX) {
}
#ifdef DEBUG_TIMERS
if (mStart == 0)
else
}
#endif
}
/**
* Timer Manager code
*/
{
mLock = PR_NewLock();
gManager = this;
}
{
}
}
{
return NS_ERROR_FAILURE;
return NS_OK;
}
{
return NS_OK;
}
{
*aHasTimers = (count != 0);
return NS_OK;
}
{
if (!timer)
return NS_ERROR_FAILURE;
return NS_OK;
}
{
return NS_OK;
}
{
if (count == 0)
return NS_OK;
}
return NS_OK;
}
// NOT FOR PUBLIC CONSUMPTION!
{
return NS_ERROR_OUT_OF_MEMORY;
return rv;
}
return NS_OK;
}