2014-06-30 19:39:45 +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
|
|
|
|
|
|
|
#ifndef nsTimerImpl_h___
|
|
|
|
#define nsTimerImpl_h___
|
|
|
|
|
|
|
|
#include "nsITimer.h"
|
2008-07-24 21:20:33 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2003-08-06 04:37:43 +04:00
|
|
|
#include "nsIObserver.h"
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
2015-09-10 10:50:51 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2017-01-11 22:59:19 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2010-07-15 17:59:24 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-09-10 10:50:51 +03:00
|
|
|
#include "mozilla/Variant.h"
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
# include "TracedTaskCommon.h"
|
|
|
|
#endif
|
|
|
|
|
2015-10-19 22:50:14 +03:00
|
|
|
extern mozilla::LogModule* GetTimerLog();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
|
|
|
#define NS_TIMER_CID \
|
|
|
|
{ /* 5ff24248-1dd2-11b2-8427-fbab44f29bc8 */ \
|
|
|
|
0x5ff24248, 0x1dd2, 0x11b2, { \
|
|
|
|
0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8 \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
class nsTimerImplHolder;
|
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
// TimerThread, nsTimerEvent, and nsTimer have references to these. nsTimer has
|
|
|
|
// a separate lifecycle so we can Cancel() the underlying timer when the user of
|
|
|
|
// the nsTimer has let go of its last reference.
|
|
|
|
class nsTimerImpl {
|
2017-04-21 00:56:07 +03:00
|
|
|
~nsTimerImpl() { MOZ_ASSERT(!mHolder); }
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
public:
|
2010-07-15 17:59:24 +04:00
|
|
|
typedef mozilla::TimeStamp TimeStamp;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2018-10-18 02:57:36 +03:00
|
|
|
nsTimerImpl(nsITimer* aTimer, nsIEventTarget* aTarget);
|
2016-07-20 23:16:40 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsTimerImpl)
|
|
|
|
NS_DECL_NON_VIRTUAL_NSITIMER
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-06-02 16:08:21 +04:00
|
|
|
static nsresult Startup();
|
|
|
|
static void Shutdown();
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2016-08-05 20:50:00 +03:00
|
|
|
void SetDelayInternal(uint32_t aDelay, TimeStamp aBase = TimeStamp::Now());
|
2017-09-14 20:19:24 +03:00
|
|
|
void CancelImpl(bool aClearITimer);
|
2015-06-18 04:40:18 +03:00
|
|
|
|
2017-01-11 22:59:19 +03:00
|
|
|
void Fire(int32_t aGeneration);
|
2015-06-18 19:00:56 +03:00
|
|
|
|
2015-06-18 19:02:23 +03:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
|
|
|
void GetTLSTraceInfo();
|
|
|
|
mozilla::tasktracer::TracedTaskCommon GetTracedTask();
|
|
|
|
#endif
|
|
|
|
|
2015-06-29 20:09:11 +03:00
|
|
|
int32_t GetGeneration() { return mGeneration; }
|
|
|
|
|
2017-01-11 22:59:19 +03:00
|
|
|
struct Callback {
|
|
|
|
Callback() : mType(Type::Unknown), mName(Nothing), mClosure(nullptr) {
|
|
|
|
mCallback.c = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:53:18 +03:00
|
|
|
Callback(const Callback& other) : Callback() { *this = other; }
|
2017-01-11 22:59:19 +03:00
|
|
|
|
2019-04-18 17:53:18 +03:00
|
|
|
enum class Type : uint8_t {
|
|
|
|
Unknown = 0,
|
|
|
|
Interface = 1,
|
|
|
|
Function = 2,
|
|
|
|
Observer = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
Callback& operator=(const Callback& other) {
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
mType = other.mType;
|
|
|
|
switch (mType) {
|
|
|
|
case Type::Unknown:
|
|
|
|
break;
|
|
|
|
case Type::Interface:
|
|
|
|
mCallback.i = other.mCallback.i;
|
|
|
|
NS_ADDREF(mCallback.i);
|
|
|
|
break;
|
|
|
|
case Type::Function:
|
|
|
|
mCallback.c = other.mCallback.c;
|
|
|
|
break;
|
|
|
|
case Type::Observer:
|
|
|
|
mCallback.o = other.mCallback.o;
|
|
|
|
NS_ADDREF(mCallback.o);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mName = other.mName;
|
|
|
|
mClosure = other.mClosure;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Callback() { clear(); }
|
|
|
|
|
|
|
|
void clear() {
|
2017-01-11 22:59:19 +03:00
|
|
|
if (mType == Type::Interface) {
|
|
|
|
NS_RELEASE(mCallback.i);
|
|
|
|
} else if (mType == Type::Observer) {
|
|
|
|
NS_RELEASE(mCallback.o);
|
|
|
|
}
|
2019-04-18 17:53:18 +03:00
|
|
|
mType = Type::Unknown;
|
2017-01-11 22:59:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void swap(Callback& other) {
|
|
|
|
std::swap(mType, other.mType);
|
|
|
|
std::swap(mCallback, other.mCallback);
|
|
|
|
std::swap(mName, other.mName);
|
|
|
|
std::swap(mClosure, other.mClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
Type mType;
|
|
|
|
|
|
|
|
union CallbackUnion {
|
|
|
|
nsTimerCallbackFunc c;
|
|
|
|
// These refcounted references are managed manually, as they are in a
|
|
|
|
// union
|
|
|
|
nsITimerCallback* MOZ_OWNING_REF i;
|
|
|
|
nsIObserver* MOZ_OWNING_REF o;
|
|
|
|
} mCallback;
|
|
|
|
|
|
|
|
// |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;
|
|
|
|
Name mName;
|
|
|
|
|
|
|
|
void* mClosure;
|
|
|
|
};
|
|
|
|
|
2017-06-01 03:13:19 +03:00
|
|
|
nsresult InitCommon(uint32_t aDelayMS, uint32_t aType,
|
|
|
|
Callback&& newCallback);
|
|
|
|
|
2017-05-29 12:19:17 +03:00
|
|
|
nsresult InitCommon(const mozilla::TimeDuration& aDelay, uint32_t aType,
|
2017-06-01 03:13:19 +03:00
|
|
|
Callback&& newCallback);
|
2017-05-01 22:50:37 +03:00
|
|
|
|
2017-01-11 22:59:19 +03:00
|
|
|
Callback& GetCallback() {
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
return mCallback;
|
|
|
|
}
|
2016-07-20 23:16:40 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
bool IsRepeating() const {
|
2016-07-20 23:16:40 +03:00
|
|
|
static_assert(nsITimer::TYPE_ONE_SHOT < nsITimer::TYPE_REPEATING_SLACK,
|
2015-04-20 20:56:46 +03:00
|
|
|
"invalid ordering of timer types!");
|
2016-07-20 23:16:40 +03:00
|
|
|
static_assert(
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK < nsITimer::TYPE_REPEATING_PRECISE,
|
|
|
|
"invalid ordering of timer types!");
|
|
|
|
static_assert(nsITimer::TYPE_REPEATING_PRECISE <
|
|
|
|
nsITimer::TYPE_REPEATING_PRECISE_CAN_SKIP,
|
|
|
|
"invalid ordering of timer types!");
|
2017-05-25 04:12:55 +03:00
|
|
|
return mType >= nsITimer::TYPE_REPEATING_SLACK &&
|
|
|
|
mType < nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLowPriority() const {
|
|
|
|
return mType == nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY ||
|
|
|
|
mType == nsITimer::TYPE_REPEATING_SLACK_LOW_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsSlack() const {
|
|
|
|
return mType == nsITimer::TYPE_REPEATING_SLACK ||
|
|
|
|
mType == nsITimer::TYPE_REPEATING_SLACK_LOW_PRIORITY;
|
2011-04-29 03:33:52 +04:00
|
|
|
}
|
|
|
|
|
2016-11-23 07:40:13 +03:00
|
|
|
void GetName(nsACString& aName);
|
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
void SetHolder(nsTimerImplHolder* aHolder);
|
|
|
|
|
2008-07-24 21:20:33 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mEventTarget;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2017-01-11 22:59:19 +03:00
|
|
|
void LogFiring(const Callback& aCallback, uint8_t aType, uint32_t aDelay);
|
2015-09-10 10:50:51 +03:00
|
|
|
|
|
|
|
nsresult InitWithFuncCallbackCommon(nsTimerCallbackFunc aFunc, void* aClosure,
|
|
|
|
uint32_t aDelay, uint32_t aType,
|
2017-01-31 02:56:05 +03:00
|
|
|
const Callback::Name& aName);
|
2015-09-10 10:50:51 +03:00
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
// This weak reference must be cleared by the nsTimerImplHolder by calling
|
|
|
|
// SetHolder(nullptr) before the holder is destroyed.
|
|
|
|
nsTimerImplHolder* mHolder;
|
|
|
|
|
2002-03-08 23:11:49 +03: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 19:56:38 +04:00
|
|
|
uint8_t mType;
|
2002-03-08 23:11:49 +03:00
|
|
|
|
2002-12-04 03:57:20 +03: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.
|
2017-01-11 22:59:19 +03:00
|
|
|
// Updated only after this timer has been removed from the timer thread.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mGeneration;
|
2002-12-04 03:57:20 +03:00
|
|
|
|
2017-05-29 12:19:17 +03:00
|
|
|
mozilla::TimeDuration mDelay;
|
2017-01-11 22:59:19 +03:00
|
|
|
// Updated only after this timer has been removed from the timer thread.
|
2017-05-29 12:19:17 +03:00
|
|
|
mozilla::TimeStamp mTimeout;
|
2001-12-16 09:13:17 +03:00
|
|
|
|
2014-03-27 12:49:06 +04:00
|
|
|
#ifdef MOZ_TASK_TRACER
|
2015-06-16 05:57:19 +03:00
|
|
|
mozilla::tasktracer::TracedTaskCommon mTracedTask;
|
2014-03-27 12:49:06 +04:00
|
|
|
#endif
|
|
|
|
|
2002-03-08 23:11:49 +03:00
|
|
|
static double sDeltaSum;
|
|
|
|
static double sDeltaSumSquared;
|
|
|
|
static double sDeltaNum;
|
2017-09-14 20:19:24 +03:00
|
|
|
RefPtr<nsITimer> mITimer;
|
2017-01-11 22:59:19 +03:00
|
|
|
mozilla::Mutex mMutex;
|
|
|
|
Callback mCallback;
|
2019-04-18 17:53:18 +03:00
|
|
|
// Counter because in rare cases we can Fire reentrantly
|
|
|
|
unsigned int mFiring;
|
2016-07-20 23:16:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsTimer final : public nsITimer {
|
2018-10-18 02:57:36 +03:00
|
|
|
explicit nsTimer(nsIEventTarget* aTarget)
|
|
|
|
: mImpl(new nsTimerImpl(this, aTarget)) {}
|
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
virtual ~nsTimer();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
public:
|
|
|
|
friend class TimerThread;
|
|
|
|
friend class nsTimerEvent;
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_FORWARD_SAFE_NSITIMER(mImpl);
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const override;
|
|
|
|
|
2018-10-18 02:57:36 +03:00
|
|
|
// Create a timer targeting the given target. nullptr indicates that the
|
|
|
|
// current thread should be used as the timer's target.
|
|
|
|
static RefPtr<nsTimer> WithEventTarget(nsIEventTarget* aTarget);
|
|
|
|
|
|
|
|
static nsresult XPCOMConstructor(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult);
|
|
|
|
|
2016-07-20 23:16:40 +03:00
|
|
|
private:
|
|
|
|
// nsTimerImpl holds a strong ref to us. When our refcount goes to 1, we will
|
|
|
|
// null this to break the cycle.
|
|
|
|
RefPtr<nsTimerImpl> mImpl;
|
2001-12-16 09:13:17 +03:00
|
|
|
};
|
|
|
|
|
2017-04-21 00:56:07 +03:00
|
|
|
// A class that holds on to an nsTimerImpl. This lets the nsTimerImpl object
|
|
|
|
// directly instruct its holder to forget the timer, avoiding list lookups.
|
|
|
|
class nsTimerImplHolder {
|
|
|
|
public:
|
|
|
|
explicit nsTimerImplHolder(nsTimerImpl* aTimerImpl) : mTimerImpl(aTimerImpl) {
|
|
|
|
if (mTimerImpl) {
|
|
|
|
mTimerImpl->SetHolder(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsTimerImplHolder() {
|
|
|
|
if (mTimerImpl) {
|
|
|
|
mTimerImpl->SetHolder(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Forget(nsTimerImpl* aTimerImpl) {
|
|
|
|
if (MOZ_UNLIKELY(!mTimerImpl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(aTimerImpl == mTimerImpl);
|
|
|
|
mTimerImpl->SetHolder(nullptr);
|
|
|
|
mTimerImpl = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RefPtr<nsTimerImpl> mTimerImpl;
|
|
|
|
};
|
|
|
|
|
2001-12-16 09:13:17 +03:00
|
|
|
#endif /* nsTimerImpl_h___ */
|