2013-03-07 02:29:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
#include "nsTimerImpl.h"
|
|
|
|
#include "TimerThread.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-07-12 00:21:45 +04:00
|
|
|
#include "pratom.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2013-08-22 19:14:42 +04:00
|
|
|
#include "mozilla/Atomics.h"
|
2015-06-04 01:25:57 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2014-11-24 11:36:35 +03:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
2015-06-16 05:57:19 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "GeckoTaskTracerImpl.h"
|
|
|
|
using namespace mozilla::tasktracer;
|
|
|
|
#endif
|
2004-05-11 13:38:50 +04:00
|
|
|
|
2013-08-22 19:14:42 +04:00
|
|
|
using mozilla::Atomic;
|
2015-06-04 01:25:57 +03:00
|
|
|
using mozilla::LogLevel;
|
2010-07-15 17:59:24 +04:00
|
|
|
using mozilla::TimeDuration;
|
|
|
|
using mozilla::TimeStamp;
|
|
|
|
|
2013-08-22 19:14:42 +04:00
|
|
|
static Atomic<int32_t> gGenerator;
|
2012-07-30 18:20:58 +04:00
|
|
|
static TimerThread* gThread = nullptr;
|
2002-04-14 04:27:16 +04:00
|
|
|
|
2012-10-30 03:32:10 +04:00
|
|
|
PRLogModuleInfo*
|
|
|
|
GetTimerLog()
|
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog) {
|
2012-10-30 03:32:10 +04:00
|
|
|
sLog = PR_NewLogModule("nsTimerImpl");
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2012-10-30 03:32:10 +04:00
|
|
|
return sLog;
|
|
|
|
}
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
double nsTimerImpl::sDeltaSumSquared = 0;
|
|
|
|
double nsTimerImpl::sDeltaSum = 0;
|
2002-03-08 23:11:49 +03:00
|
|
|
double nsTimerImpl::sDeltaNum = 0;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
myNS_MeanAndStdDev(double n, double sumOfValues, double sumOfSquaredValues,
|
2014-05-27 11:15:35 +04:00
|
|
|
double* meanResult, double* stdDevResult)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
|
|
|
double mean = 0.0, var = 0.0, stdDev = 0.0;
|
|
|
|
if (n > 0.0 && sumOfValues >= 0) {
|
|
|
|
mean = sumOfValues / n;
|
|
|
|
double temp = (n * sumOfSquaredValues) - (sumOfValues * sumOfValues);
|
2014-05-27 11:15:35 +04:00
|
|
|
if (temp < 0.0 || n <= 1) {
|
2001-12-16 09:13:17 +03:00
|
|
|
var = 0.0;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2001-12-16 09:13:17 +03:00
|
|
|
var = temp / (n * (n - 1));
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
// for some reason, Windows says sqrt(0.0) is "-1.#J" (?!) so do this:
|
|
|
|
stdDev = var != 0.0 ? sqrt(var) : 0.0;
|
|
|
|
}
|
|
|
|
*meanResult = mean;
|
|
|
|
*stdDevResult = stdDev;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsTimerImpl, nsITimer)
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_IMPL_ADDREF(nsTimerImpl)
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsTimerImpl::Release(void)
|
2002-03-08 23:11:49 +03:00
|
|
|
{
|
|
|
|
nsrefcnt count;
|
|
|
|
|
2013-02-08 08:50:05 +04:00
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
|
2013-07-19 06:31:26 +04:00
|
|
|
count = --mRefCnt;
|
2002-03-08 23:11:49 +03:00
|
|
|
NS_LOG_RELEASE(this, count, "nsTimerImpl");
|
|
|
|
if (count == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
|
|
|
|
/* enable this to find non-threadsafe destructors: */
|
|
|
|
/* NS_ASSERT_OWNINGTHREAD(nsTimerImpl); */
|
2010-07-05 13:42:18 +04:00
|
|
|
delete this;
|
2002-03-08 23:11:49 +03:00
|
|
|
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) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanceled = true;
|
2002-03-08 23:11:49 +03:00
|
|
|
|
2013-02-08 09:54:20 +04:00
|
|
|
MOZ_ASSERT(gThread, "Armed timer exists after the thread timer stopped.");
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_SUCCEEDED(gThread->RemoveTimer(this))) {
|
2002-03-08 23:11:49 +03:00
|
|
|
return 0;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-03-08 23:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
nsTimerImpl::nsTimerImpl() :
|
2012-07-30 18:20:58 +04:00
|
|
|
mClosure(nullptr),
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType(CallbackType::Unknown),
|
2011-10-17 18:59:28 +04:00
|
|
|
mFiring(false),
|
|
|
|
mArmed(false),
|
|
|
|
mCanceled(false),
|
2002-12-04 03:57:20 +03:00
|
|
|
mGeneration(0),
|
2010-07-15 17:59:24 +04:00
|
|
|
mDelay(0)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2004-05-11 13:38:50 +04:00
|
|
|
// XXXbsmedberg: shouldn't this be in Init()?
|
2008-07-24 21:20:33 +04:00
|
|
|
mEventTarget = static_cast<nsIEventTarget*>(NS_GetCurrentThread());
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mCallback.c = nullptr;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTimerImpl::~nsTimerImpl()
|
|
|
|
{
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2004-05-11 13:38:50 +04:00
|
|
|
//static
|
|
|
|
nsresult
|
|
|
|
nsTimerImpl::Startup()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
gThread = new TimerThread();
|
|
|
|
|
|
|
|
NS_ADDREF(gThread);
|
|
|
|
rv = gThread->InitLocks();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(gThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
|
|
|
nsTimerImpl::Shutdown()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerLog(), LogLevel::Debug)) {
|
2002-01-15 04:20:47 +03:00
|
|
|
double mean = 0, stddev = 0;
|
2002-03-08 23:11:49 +03:00
|
|
|
myNS_MeanAndStdDev(sDeltaNum, sDeltaSum, sDeltaSumSquared, &mean, &stddev);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("sDeltaNum = %f, sDeltaSum = %f, sDeltaSumSquared = %f\n",
|
|
|
|
sDeltaNum, sDeltaSum, sDeltaSumSquared));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("mean: %fms, stddev: %fms\n", mean, stddev));
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!gThread) {
|
2001-12-16 09:13:17 +03:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
gThread->Shutdown();
|
2002-02-18 03:10:55 +03:00
|
|
|
NS_RELEASE(gThread);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsresult
|
|
|
|
nsTimerImpl::InitCommon(uint32_t aType, uint32_t aDelay)
|
2002-12-04 03:57:20 +03:00
|
|
|
{
|
2004-05-11 13:38:50 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!gThread)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2013-04-23 21:24:20 +04:00
|
|
|
if (!mEventTarget) {
|
|
|
|
NS_ERROR("mEventTarget is NULL");
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2004-05-11 13:38:50 +04:00
|
|
|
|
|
|
|
rv = gThread->Init();
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2004-05-11 13:38:50 +04:00
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mArmed) {
|
2002-12-04 03:57:20 +03:00
|
|
|
gThread->RemoveTimer(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanceled = false;
|
2011-11-03 07:46:08 +04:00
|
|
|
mTimeout = TimeStamp();
|
2013-08-22 19:14:42 +04:00
|
|
|
mGeneration = gGenerator++;
|
2002-12-04 03:57:20 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
mType = (uint8_t)aType;
|
2002-12-04 03:57:20 +03:00
|
|
|
SetDelayInternal(aDelay);
|
|
|
|
|
|
|
|
return gThread->AddTimer(this);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::InitWithFuncCallback(nsTimerCallbackFunc aFunc,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aFunc)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType = CallbackType::Function;
|
2002-12-04 03:57:20 +03:00
|
|
|
mCallback.c = aFunc;
|
2001-12-16 09:13:17 +03:00
|
|
|
mClosure = aClosure;
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
return InitCommon(aType, aDelay);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::InitWithCallback(nsITimerCallback* aCallback,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aCallback)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2005-11-02 03:46:37 +03:00
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType = CallbackType::Interface;
|
2001-12-16 09:13:17 +03:00
|
|
|
mCallback.i = aCallback;
|
2001-12-17 09:59:55 +03:00
|
|
|
NS_ADDREF(mCallback.i);
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
return InitCommon(aType, aDelay);
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::Init(nsIObserver* aObserver, uint32_t aDelay, uint32_t aType)
|
2002-02-20 07:01:34 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2005-11-02 03:46:37 +03:00
|
|
|
|
2003-08-06 04:37:43 +04:00
|
|
|
ReleaseCallback();
|
2015-04-20 21:08:57 +03:00
|
|
|
mCallbackType = CallbackType::Observer;
|
2002-02-20 07:01:34 +03:00
|
|
|
mCallback.o = aObserver;
|
|
|
|
NS_ADDREF(mCallback.o);
|
|
|
|
|
2002-12-04 03:57:20 +03:00
|
|
|
return InitCommon(aType, aDelay);
|
2002-02-20 07:01:34 +03:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::Cancel()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanceled = true;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (gThread) {
|
2001-12-31 07:04:22 +03:00
|
|
|
gThread->RemoveTimer(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-02-20 07:01:34 +03:00
|
|
|
|
2007-10-24 23:11:41 +04:00
|
|
|
ReleaseCallback();
|
|
|
|
|
2002-02-20 07:01:34 +03:00
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::SetDelay(uint32_t aDelay)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2015-04-20 21:08:57 +03:00
|
|
|
if (mCallbackType == CallbackType::Unknown && mType == TYPE_ONE_SHOT) {
|
2009-02-03 17:42:21 +03:00
|
|
|
// This may happen if someone tries to re-use a one-shot timer
|
|
|
|
// by re-setting delay instead of reinitializing the timer.
|
|
|
|
NS_ERROR("nsITimer->SetDelay() called when the "
|
|
|
|
"one-shot timer is not set up.");
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2002-04-26 01:07:54 +04:00
|
|
|
// If we're already repeating precisely, update mTimeout now so that the
|
|
|
|
// new delay takes effect in the future.
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mTimeout.IsNull() && mType == TYPE_REPEATING_PRECISE) {
|
2010-07-15 17:59:24 +04:00
|
|
|
mTimeout = TimeStamp::Now();
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-04-26 01:07:54 +04:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
SetDelayInternal(aDelay);
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!mFiring && gThread) {
|
2001-12-16 09:13:17 +03:00
|
|
|
gThread->TimerDelayChanged(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-09-07 09:38:16 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::GetDelay(uint32_t* aDelay)
|
2002-09-07 09:38:16 +04:00
|
|
|
{
|
|
|
|
*aDelay = mDelay;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::SetType(uint32_t aType)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
mType = (uint8_t)aType;
|
2001-12-16 09:13:17 +03:00
|
|
|
// XXX if this is called, we should change the actual type.. this could effect
|
2002-04-14 04:27:16 +04:00
|
|
|
// repeating timers. we need to ensure in Fire() that if mType has changed
|
2001-12-16 09:13:17 +03:00
|
|
|
// during the callback that we don't end up with the timer in the queue twice.
|
2002-09-07 09:38:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::GetType(uint32_t* aType)
|
2002-09-07 09:38:16 +04:00
|
|
|
{
|
|
|
|
*aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::GetClosure(void** aClosure)
|
2002-09-07 09:38:16 +04:00
|
|
|
{
|
|
|
|
*aClosure = mClosure;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::GetCallback(nsITimerCallback** aCallback)
|
2005-05-05 00:22:32 +04:00
|
|
|
{
|
2015-04-20 21:08:57 +03:00
|
|
|
if (mCallbackType == CallbackType::Interface) {
|
2005-05-05 00:22:32 +04:00
|
|
|
NS_IF_ADDREF(*aCallback = mCallback.i);
|
2014-05-27 11:15:35 +04:00
|
|
|
} else if (mTimerCallbackWhileFiring) {
|
2007-10-30 18:56:59 +03:00
|
|
|
NS_ADDREF(*aCallback = mTimerCallbackWhileFiring);
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aCallback = nullptr;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2005-05-05 00:22:32 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::GetTarget(nsIEventTarget** aTarget)
|
2008-07-24 21:20:33 +04:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aTarget = mEventTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTimerImpl::SetTarget(nsIEventTarget* aTarget)
|
2008-07-24 21:20:33 +04:00
|
|
|
{
|
2015-04-20 21:08:57 +03:00
|
|
|
if (NS_WARN_IF(mCallbackType != CallbackType::Unknown)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2008-07-24 21:20:33 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
if (aTarget) {
|
2008-07-24 21:20:33 +04:00
|
|
|
mEventTarget = aTarget;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2008-07-24 21:20:33 +04:00
|
|
|
mEventTarget = static_cast<nsIEventTarget*>(NS_GetCurrentThread());
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2008-07-24 21:20:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
|
|
|
nsTimerImpl::Fire()
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mCanceled) {
|
2001-12-17 09:59:55 +03:00
|
|
|
return;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-17 09:59:55 +03:00
|
|
|
|
2015-04-03 16:52:00 +03:00
|
|
|
#if !defined(MOZILLA_XPCOMRT_API)
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("Timer", "Fire",
|
2014-08-25 23:17:15 +04:00
|
|
|
js::ProfileEntry::Category::OTHER);
|
2015-04-03 16:52:00 +03:00
|
|
|
#endif
|
2011-12-03 02:05:33 +04:00
|
|
|
|
2013-08-20 23:22:27 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerLog(), LogLevel::Debug)) {
|
2010-07-15 17:59:24 +04:00
|
|
|
TimeDuration a = now - mStart; // actual delay in intervals
|
|
|
|
TimeDuration b = TimeDuration::FromMilliseconds(mDelay); // expected delay in intervals
|
|
|
|
TimeDuration delta = (a > b) ? a - b : b - a;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t d = delta.ToMilliseconds(); // delta in ms
|
2002-01-15 04:20:47 +03:00
|
|
|
sDeltaSum += d;
|
|
|
|
sDeltaSumSquared += double(d) * double(d);
|
2002-03-08 23:11:49 +03:00
|
|
|
sDeltaNum++;
|
2002-01-15 04:20:47 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] expected delay time %4ums\n", this, mDelay));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] actual delay time %fms\n", this,
|
|
|
|
a.ToMilliseconds()));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] (mType is %d) -------\n", this, mType));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2014-05-27 11:15:35 +04:00
|
|
|
("[this=%p] delta %4dms\n",
|
|
|
|
this, (a > b) ? (int32_t)d : -(int32_t)d));
|
2002-01-15 04:20:47 +03:00
|
|
|
|
|
|
|
mStart = mStart2;
|
2010-07-22 20:00:26 +04:00
|
|
|
mStart2 = TimeStamp();
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2010-07-15 17:59:24 +04:00
|
|
|
TimeStamp timeout = mTimeout;
|
2011-04-29 03:33:52 +04:00
|
|
|
if (IsRepeatingPrecisely()) {
|
2002-02-18 03:10:55 +03:00
|
|
|
// Precise repeating timers advance mTimeout by mDelay without fail before
|
2002-04-14 04:27:16 +04:00
|
|
|
// calling Fire().
|
2010-07-15 17:59:24 +04:00
|
|
|
timeout -= TimeDuration::FromMilliseconds(mDelay);
|
2002-02-18 03:10:55 +03:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:08:57 +03:00
|
|
|
if (mCallbackType == CallbackType::Interface) {
|
2007-10-30 18:56:59 +03:00
|
|
|
mTimerCallbackWhileFiring = mCallback.i;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mFiring = true;
|
2014-03-27 12:49:06 +04:00
|
|
|
|
2007-10-24 23:11:41 +04:00
|
|
|
// Handle callbacks that re-init the timer, but avoid leaking.
|
|
|
|
// See bug 330128.
|
|
|
|
CallbackUnion callback = mCallback;
|
2015-04-20 21:08:57 +03:00
|
|
|
CallbackType callbackType = mCallbackType;
|
|
|
|
if (callbackType == CallbackType::Interface) {
|
2007-10-24 23:11:41 +04:00
|
|
|
NS_ADDREF(callback.i);
|
2015-04-20 21:08:57 +03:00
|
|
|
} else if (callbackType == CallbackType::Observer) {
|
2007-10-24 23:11:41 +04:00
|
|
|
NS_ADDREF(callback.o);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2007-10-24 23:11:41 +04:00
|
|
|
ReleaseCallback();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2007-10-24 23:11:41 +04:00
|
|
|
switch (callbackType) {
|
2015-04-20 21:08:57 +03:00
|
|
|
case CallbackType::Function:
|
2007-10-24 23:11:41 +04:00
|
|
|
callback.c(this, mClosure);
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
2015-04-20 21:08:57 +03:00
|
|
|
case CallbackType::Interface:
|
2007-10-24 23:11:41 +04:00
|
|
|
callback.i->Notify(this);
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
2015-04-20 21:08:57 +03:00
|
|
|
case CallbackType::Observer:
|
2007-10-24 23:11:41 +04:00
|
|
|
callback.o->Observe(static_cast<nsITimer*>(this),
|
|
|
|
NS_TIMER_CALLBACK_TOPIC,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr);
|
2002-03-08 23:11:49 +03:00
|
|
|
break;
|
2014-05-27 11:15:35 +04:00
|
|
|
default:
|
|
|
|
;
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
|
2007-10-24 23:11:41 +04:00
|
|
|
// If the callback didn't re-init the timer, and it's not a one-shot timer,
|
|
|
|
// restore the callback state.
|
2015-04-20 21:08:57 +03:00
|
|
|
if (mCallbackType == CallbackType::Unknown &&
|
2008-01-23 08:20:14 +03:00
|
|
|
mType != TYPE_ONE_SHOT && !mCanceled) {
|
2007-10-24 23:11:41 +04:00
|
|
|
mCallback = callback;
|
|
|
|
mCallbackType = callbackType;
|
|
|
|
} else {
|
|
|
|
// The timer was a one-shot, or the callback was reinitialized.
|
2015-04-20 21:08:57 +03:00
|
|
|
if (callbackType == CallbackType::Interface) {
|
2007-10-24 23:11:41 +04:00
|
|
|
NS_RELEASE(callback.i);
|
2015-04-20 21:08:57 +03:00
|
|
|
} else if (callbackType == CallbackType::Observer) {
|
2007-10-24 23:11:41 +04:00
|
|
|
NS_RELEASE(callback.o);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2007-10-24 23:11:41 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mFiring = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimerCallbackWhileFiring = nullptr;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetTimerLog(), LogLevel::Debug,
|
2015-05-07 19:43:37 +03:00
|
|
|
("[this=%p] Took %fms to fire timer callback\n",
|
|
|
|
this, (TimeStamp::Now() - now).ToMilliseconds()));
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2011-04-29 03:33:52 +04:00
|
|
|
// Reschedule repeating timers, except REPEATING_PRECISE which already did
|
|
|
|
// that in PostTimerEvent, but make sure that we aren't armed already (which
|
|
|
|
// can happen if the callback reinitialized the timer).
|
|
|
|
if (IsRepeating() && mType != TYPE_REPEATING_PRECISE && !mArmed) {
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mType == TYPE_REPEATING_SLACK) {
|
|
|
|
SetDelayInternal(mDelay); // force mTimeout to be recomputed. For
|
|
|
|
}
|
|
|
|
// REPEATING_PRECISE_CAN_SKIP timers this has
|
|
|
|
// already happened.
|
|
|
|
if (gThread) {
|
2001-12-31 07:04:22 +03:00
|
|
|
gThread->AddTimer(this);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
void
|
|
|
|
nsTimerImpl::SetDelayInternal(uint32_t aDelay)
|
2001-12-16 09:13:17 +03:00
|
|
|
{
|
2010-07-15 17:59:24 +04:00
|
|
|
TimeDuration delayInterval = TimeDuration::FromMilliseconds(aDelay);
|
2002-04-26 01:07:54 +04:00
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
mDelay = aDelay;
|
|
|
|
|
2010-07-15 17:59:24 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mTimeout.IsNull() || mType != TYPE_REPEATING_PRECISE) {
|
2002-02-18 03:10:55 +03:00
|
|
|
mTimeout = now;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2002-04-26 01:07:54 +04:00
|
|
|
mTimeout += delayInterval;
|
2002-03-08 23:11:49 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(GetTimerLog(), LogLevel::Debug)) {
|
2014-05-27 11:15:35 +04:00
|
|
|
if (mStart.IsNull()) {
|
2002-01-15 04:20:47 +03:00
|
|
|
mStart = now;
|
2014-05-27 11:15:35 +04:00
|
|
|
} else {
|
2002-01-15 04:20:47 +03:00
|
|
|
mStart2 = now;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2002-01-15 04:20:47 +03:00
|
|
|
}
|
2001-12-16 09:13:17 +03:00
|
|
|
}
|
2002-01-24 09:37:38 +03:00
|
|
|
|
2014-06-23 02:02:59 +04:00
|
|
|
size_t
|
|
|
|
nsTimerImpl::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this);
|
|
|
|
}
|
2015-06-16 05:57:19 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
void
|
|
|
|
nsTimerImpl::GetTLSTraceInfo()
|
|
|
|
{
|
|
|
|
mTracedTask.GetTLSTraceInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
TracedTaskCommon
|
|
|
|
nsTimerImpl::GetTracedTask()
|
|
|
|
{
|
|
|
|
return mTracedTask;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|