2014-06-30 08:39:45 -07: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 12:12:37 +01: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 06:13:17 +00:00
|
|
|
|
|
|
|
#ifndef nsTimerImpl_h___
|
|
|
|
#define nsTimerImpl_h___
|
|
|
|
|
|
|
|
#include "nsITimer.h"
|
2008-07-24 10:20:33 -07:00
|
|
|
#include "nsIEventTarget.h"
|
2003-08-06 00:37:43 +00:00
|
|
|
#include "nsIObserver.h"
|
2001-12-16 06:13:17 +00:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
2015-09-10 00:50:51 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-05-19 11:15:34 -07:00
|
|
|
#include "mozilla/Logging.h"
|
2010-07-15 09:59:24 -04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-09-10 00:50:51 -07:00
|
|
|
#include "mozilla/Variant.h"
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2014-03-27 16:49:06 +08:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
#include "TracedTaskCommon.h"
|
|
|
|
#endif
|
|
|
|
|
2015-10-19 12:50:14 -07:00
|
|
|
extern mozilla::LogModule* GetTimerLog();
|
2001-12-16 06:13:17 +00:00
|
|
|
|
|
|
|
#define NS_TIMER_CID \
|
|
|
|
{ /* 5ff24248-1dd2-11b2-8427-fbab44f29bc8 */ \
|
|
|
|
0x5ff24248, \
|
|
|
|
0x1dd2, \
|
|
|
|
0x11b2, \
|
|
|
|
{0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8} \
|
|
|
|
}
|
|
|
|
|
2015-03-21 12:28:04 -04:00
|
|
|
class nsTimerImpl final : public nsITimer
|
2001-12-16 06:13:17 +00:00
|
|
|
{
|
|
|
|
public:
|
2010-07-15 09:59:24 -04:00
|
|
|
typedef mozilla::TimeStamp TimeStamp;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
|
|
|
nsTimerImpl();
|
|
|
|
|
2014-06-03 00:08:21 +12:00
|
|
|
static nsresult Startup();
|
|
|
|
static void Shutdown();
|
2001-12-16 06:13:17 +00:00
|
|
|
|
|
|
|
friend class TimerThread;
|
2015-06-18 12:00:56 -04:00
|
|
|
friend class nsTimerEvent;
|
2015-07-22 12:39:34 -05:00
|
|
|
friend struct TimerAdditionComparator;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2013-07-18 21:31:26 -05:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2002-09-07 05:38:16 +00:00
|
|
|
NS_DECL_NSITIMER
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2015-03-21 12:28:04 -04:00
|
|
|
virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const override;
|
2014-06-22 23:02:59 +01:00
|
|
|
|
2001-12-16 06:13:17 +00:00
|
|
|
private:
|
2015-06-17 21:40:18 -04:00
|
|
|
void SetDelayInternal(uint32_t aDelay);
|
|
|
|
|
2015-06-18 12:00:56 -04:00
|
|
|
void Fire();
|
|
|
|
|
2015-06-18 12:02:23 -04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
void GetTLSTraceInfo();
|
|
|
|
mozilla::tasktracer::TracedTaskCommon GetTracedTask();
|
|
|
|
#endif
|
|
|
|
|
2015-06-29 10:33:30 -04:00
|
|
|
// If a failure is encountered, the reference is returned to the caller
|
|
|
|
static already_AddRefed<nsTimerImpl> PostTimerEvent(
|
|
|
|
already_AddRefed<nsTimerImpl> aTimerRef);
|
|
|
|
|
2015-06-29 13:09:11 -04:00
|
|
|
int32_t GetGeneration()
|
|
|
|
{
|
|
|
|
return mGeneration;
|
|
|
|
}
|
|
|
|
|
2015-04-20 14:08:57 -04:00
|
|
|
enum class CallbackType : uint8_t {
|
|
|
|
Unknown = 0,
|
|
|
|
Interface = 1,
|
|
|
|
Function = 2,
|
|
|
|
Observer = 3,
|
|
|
|
};
|
|
|
|
|
2004-01-15 06:14:18 +00:00
|
|
|
~nsTimerImpl();
|
2015-09-14 15:57:17 -07:00
|
|
|
nsresult InitCommon(uint32_t aDelay, uint32_t aType);
|
2002-12-04 00:57:20 +00:00
|
|
|
|
2003-08-06 00:37:43 +00:00
|
|
|
void ReleaseCallback()
|
|
|
|
{
|
2008-02-13 22:51:06 -08:00
|
|
|
// if we're the last owner of the callback object, make
|
|
|
|
// sure that we don't recurse into ReleaseCallback in case
|
|
|
|
// the callback's destructor calls Cancel() or similar.
|
2015-04-20 14:08:57 -04:00
|
|
|
CallbackType cbType = mCallbackType;
|
|
|
|
mCallbackType = CallbackType::Unknown;
|
2008-02-13 22:51:06 -08:00
|
|
|
|
2015-04-20 14:08:57 -04:00
|
|
|
if (cbType == CallbackType::Interface) {
|
2003-08-06 00:37:43 +00:00
|
|
|
NS_RELEASE(mCallback.i);
|
2015-04-20 14:08:57 -04:00
|
|
|
} else if (cbType == CallbackType::Observer) {
|
2003-08-06 00:37:43 +00:00
|
|
|
NS_RELEASE(mCallback.o);
|
2014-05-27 10:15:35 +03:00
|
|
|
}
|
2003-08-06 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:15:35 +03:00
|
|
|
bool IsRepeating() const
|
|
|
|
{
|
2015-04-20 13:56:46 -04:00
|
|
|
static_assert(TYPE_ONE_SHOT < TYPE_REPEATING_SLACK,
|
|
|
|
"invalid ordering of timer types!");
|
|
|
|
static_assert(TYPE_REPEATING_SLACK < TYPE_REPEATING_PRECISE,
|
|
|
|
"invalid ordering of timer types!");
|
|
|
|
static_assert(TYPE_REPEATING_PRECISE < TYPE_REPEATING_PRECISE_CAN_SKIP,
|
|
|
|
"invalid ordering of timer types!");
|
2011-04-28 19:33:52 -04:00
|
|
|
return mType >= TYPE_REPEATING_SLACK;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:15:35 +03:00
|
|
|
bool IsRepeatingPrecisely() const
|
|
|
|
{
|
2011-04-28 19:33:52 -04:00
|
|
|
return mType >= TYPE_REPEATING_PRECISE;
|
|
|
|
}
|
|
|
|
|
2008-07-24 10:20:33 -07:00
|
|
|
nsCOMPtr<nsIEventTarget> mEventTarget;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2014-05-27 10:15:35 +03:00
|
|
|
void* mClosure;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2014-05-27 10:15:35 +03:00
|
|
|
union CallbackUnion
|
|
|
|
{
|
2001-12-16 06:13:17 +00:00
|
|
|
nsTimerCallbackFunc c;
|
2015-05-28 10:14:00 -04:00
|
|
|
// These refcounted references are managed manually, as they are in a union
|
|
|
|
nsITimerCallback* MOZ_OWNING_REF i;
|
|
|
|
nsIObserver* MOZ_OWNING_REF o;
|
2001-12-16 06:13:17 +00:00
|
|
|
} mCallback;
|
|
|
|
|
2015-09-10 00:50:51 -07:00
|
|
|
void LogFiring(CallbackType aCallbackType, CallbackUnion aCallbackUnion);
|
|
|
|
|
|
|
|
// |Name| is a tagged union type representing one of (a) nothing, (b) a
|
|
|
|
// string, or (c) a function. mozilla::Variant doesn't naturally handle the
|
|
|
|
// "nothing" case, so we define a dummy type and value (which is unused and
|
|
|
|
// so the exact value doesn't matter) for it.
|
|
|
|
typedef const int NameNothing;
|
|
|
|
typedef const char* NameString;
|
|
|
|
typedef nsTimerNameCallbackFunc NameFunc;
|
|
|
|
typedef mozilla::Variant<NameNothing, NameString, NameFunc> Name;
|
|
|
|
static const NameNothing Nothing;
|
|
|
|
|
|
|
|
nsresult InitWithFuncCallbackCommon(nsTimerCallbackFunc aFunc,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aDelay,
|
|
|
|
uint32_t aType,
|
|
|
|
Name aName);
|
|
|
|
|
|
|
|
// This is set by Init. It records the name (if there is one) for the timer,
|
|
|
|
// for use when logging timer firings.
|
|
|
|
Name mName;
|
|
|
|
|
2007-10-30 08:56:59 -07:00
|
|
|
// Some callers expect to be able to access the callback while the
|
|
|
|
// timer is firing.
|
|
|
|
nsCOMPtr<nsITimerCallback> mTimerCallbackWhileFiring;
|
|
|
|
|
2015-04-20 14:21:01 -04:00
|
|
|
// These members are set by Init and never reset.
|
2015-04-20 14:08:57 -04:00
|
|
|
CallbackType mCallbackType;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2002-03-08 20:11:49 +00:00
|
|
|
// These members are set by the initiating thread, when the timer's type is
|
|
|
|
// changed and during the period where it fires on that thread.
|
2012-08-22 11:56:38 -04:00
|
|
|
uint8_t mType;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mFiring;
|
2001-12-17 06:59:55 +00:00
|
|
|
|
2002-03-08 20:11:49 +00:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
// Use a bool (int) here to isolate loads and stores of these two members
|
2002-03-08 20:11:49 +00:00
|
|
|
// done on various threads under the protection of TimerThread::mLock, from
|
|
|
|
// loads and stores done on the initiating/type-changing/timer-firing thread
|
2012-08-22 11:56:38 -04:00
|
|
|
// to the above uint8_t/bool members.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mArmed;
|
|
|
|
bool mCanceled;
|
2002-03-08 20:11:49 +00:00
|
|
|
|
2002-12-04 00:57:20 +00:00
|
|
|
// The generation number of this timer, re-generated each time the timer is
|
|
|
|
// initialized so one-shot timers can be canceled and re-initialized by the
|
|
|
|
// arming thread without any bad race conditions.
|
2012-08-22 11:56:38 -04:00
|
|
|
int32_t mGeneration;
|
2002-12-04 00:57:20 +00:00
|
|
|
|
2012-08-22 11:56:38 -04:00
|
|
|
uint32_t mDelay;
|
2010-07-15 09:59:24 -04:00
|
|
|
TimeStamp mTimeout;
|
2001-12-16 06:13:17 +00:00
|
|
|
|
2014-03-27 16:49:06 +08:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
2015-06-16 10:57:19 +08:00
|
|
|
mozilla::tasktracer::TracedTaskCommon mTracedTask;
|
2014-03-27 16:49:06 +08:00
|
|
|
#endif
|
|
|
|
|
2010-07-15 09:59:24 -04:00
|
|
|
TimeStamp mStart, mStart2;
|
2002-03-08 20:11:49 +00:00
|
|
|
static double sDeltaSum;
|
|
|
|
static double sDeltaSumSquared;
|
|
|
|
static double sDeltaNum;
|
2001-12-16 06:13:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsTimerImpl_h___ */
|