/* -*- 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 * 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/. */
// 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 {
~nsTimerImpl() {
MOZ_ASSERT(!mIsInTimerThread);
// The nsITimer interface requires that its users keep a reference to the // timers they use while those timers are initialized but have not yet // fired. If this assert ever fails, it is a bug in the code that created // and used the timer. // // Further, note that this should never fail even with a misbehaving user, // because nsTimer::Release checks for a refcount of 1 with an armed timer // (a timer whose only reference is from the timer thread) and when it hits // this will remove the timer from the timer thread and thus destroy the // last reference, preventing this situation from occurring.
MOZ_ASSERT(
mCallback.is<UnknownCallback>() || mEventTarget->IsOnCurrentThread(), "Must not release mCallback off-target without canceling");
}
using InterfaceCallback = nsCOMPtr<nsITimerCallback>;
using ObserverCallback = nsCOMPtr<nsIObserver>;
/// A raw function pointer and its closed-over state, along with its name for /// logging purposes. struct FuncCallback {
nsTimerCallbackFunc mFunc; void* mClosure; constchar* mName;
};
/// A callback defined by an owned closure and its name for logging purposes. struct ClosureCallback {
std::function<void(nsITimer*)> mFunc; constchar* mName;
};
using Callback =
mozilla::Variant<UnknownCallback, InterfaceCallback, ObserverCallback,
FuncCallback, ClosureCallback>;
// Is this timer currently referenced from a TimerThread::Entry? // Note: It is cleared before the Entry is destroyed. Take() also sets it to // false, to indicate it's no longer in the TimerThread's list. This Take() // call is NOT made under the nsTimerImpl's mutex (all other // SetIsInTimerThread calls are under the mutex). However, ALL accesses to // mIsInTimerThread are under the TimerThread's Monitor lock, so consistency // is guaranteed by that. bool mIsInTimerThread;
// 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.
uint8_t mType;
// 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. // Updated only after this timer has been removed from the timer thread.
int32_t mGeneration;
mozilla::TimeDuration mDelay MOZ_GUARDED_BY(mMutex); // Never updated while in the TimerThread's timer list. Only updated // before adding to that list or during nsTimerImpl::Fire(), when it has // been removed from the TimerThread's list. TimerThread can access // mTimeout of any timer in the list safely
mozilla::TimeStamp mTimeout;
RefPtr<nsITimer> mITimer MOZ_GUARDED_BY(mMutex);
mozilla::Mutex mMutex;
Callback mCallback MOZ_GUARDED_BY(mMutex); // Counter because in rare cases we can Fire reentrantly unsignedint mFiring MOZ_GUARDED_BY(mMutex);
// NOTE: This constructor is not exposed on `nsITimer` as NS_FORWARD_SAFE_ // does not support forwarding rvalue references.
nsresult InitWithClosureCallback(std::function<void(nsITimer*)>&& aCallback, const mozilla::TimeDuration& aDelay,
uint32_t aType, constchar* aNameString) { return mImpl ? mImpl->InitWithClosureCallback(std::move(aCallback), aDelay,
aType, aNameString)
: NS_ERROR_NULL_POINTER;
}
// 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);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.