2016-05-25 00:45:44 +03: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: */
|
2015-03-19 00:27:49 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#if !defined(StateWatching_h_)
|
|
|
|
# define StateWatching_h_
|
|
|
|
|
2015-07-16 21:52:43 +03:00
|
|
|
# include "mozilla/AbstractThread.h"
|
2016-11-05 02:21:59 +03:00
|
|
|
# include "mozilla/Logging.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
# include "mozilla/TaskDispatcher.h"
|
2015-03-19 00:27:49 +03:00
|
|
|
# include "mozilla/UniquePtr.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
# include "mozilla/Unused.h"
|
2015-03-19 00:27:49 +03:00
|
|
|
|
|
|
|
# include "nsISupportsImpl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The state-watching machinery automates the process of responding to changes
|
|
|
|
* in various pieces of state.
|
|
|
|
*
|
|
|
|
* A standard programming pattern is as follows:
|
|
|
|
*
|
|
|
|
* mFoo = ...;
|
|
|
|
* NotifyStuffChanged();
|
|
|
|
* ...
|
|
|
|
* mBar = ...;
|
|
|
|
* NotifyStuffChanged();
|
|
|
|
*
|
|
|
|
* This pattern is error-prone and difficult to audit because it requires the
|
|
|
|
* programmer to manually trigger the update routine. This can be especially
|
|
|
|
* problematic when the update routine depends on numerous pieces of state, and
|
|
|
|
* when that state is modified across a variety of helper methods. In these
|
|
|
|
* cases the responsibility for invoking the routine is often unclear, causing
|
|
|
|
* developers to scatter calls to it like pixie dust. This can result in
|
|
|
|
* duplicate invocations (which is wasteful) and missing invocations in corner-
|
|
|
|
* cases (which is a source of bugs).
|
|
|
|
*
|
|
|
|
* This file provides a set of primitives that automatically handle updates and
|
|
|
|
* allow the programmers to explicitly construct a graph of state dependencies.
|
|
|
|
* When used correctly, it eliminates the guess-work and wasted cycles described
|
|
|
|
* above.
|
|
|
|
*
|
|
|
|
* There are two basic pieces:
|
|
|
|
* (1) Objects that can be watched for updates. These inherit WatchTarget.
|
|
|
|
* (2) Objects that receive objects and trigger processing. These inherit
|
2015-04-29 05:02:31 +03:00
|
|
|
* AbstractWatcher. In the current machinery, these exist only internally
|
|
|
|
* within the WatchManager, though that could change.
|
2015-03-19 00:27:49 +03:00
|
|
|
*
|
|
|
|
* Note that none of this machinery is thread-safe - it must all happen on the
|
|
|
|
* same owning thread. To solve multi-threaded use-cases, use state mirroring
|
|
|
|
* and watch the mirrored value.
|
|
|
|
*
|
|
|
|
* Given that semantics may change and comments tend to go out of date, we
|
|
|
|
* deliberately don't provide usage examples here. Grep around to find them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-10-19 22:50:14 +03:00
|
|
|
extern LazyLogModule gStateWatchingLog;
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2018-11-15 15:47:59 +03:00
|
|
|
# define WATCH_LOG(x, ...) \
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStateWatchingLog, LogLevel::Debug, (x, ##__VA_ARGS__))
|
2015-03-19 00:27:49 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* AbstractWatcher is a superclass from which all watchers must inherit.
|
|
|
|
*/
|
|
|
|
class AbstractWatcher {
|
|
|
|
public:
|
2015-04-27 21:51:46 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AbstractWatcher)
|
2018-11-15 15:47:59 +03:00
|
|
|
AbstractWatcher() : mDestroyed(false) {}
|
2015-03-19 00:27:49 +03:00
|
|
|
bool IsDestroyed() { return mDestroyed; }
|
|
|
|
virtual void Notify() = 0;
|
|
|
|
|
|
|
|
protected:
|
2015-04-29 05:02:31 +03:00
|
|
|
virtual ~AbstractWatcher() { MOZ_ASSERT(mDestroyed); }
|
2015-03-19 00:27:49 +03:00
|
|
|
bool mDestroyed;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WatchTarget is a superclass from which all watchable things must inherit.
|
|
|
|
* Unlike AbstractWatcher, it is a fully-implemented Mix-in, and the subclass
|
|
|
|
* needs only to invoke NotifyWatchers when something changes.
|
|
|
|
*
|
|
|
|
* The functionality that this class provides is not threadsafe, and should only
|
|
|
|
* be used on the thread that owns that WatchTarget.
|
|
|
|
*/
|
|
|
|
class WatchTarget {
|
|
|
|
public:
|
2018-11-15 15:47:59 +03:00
|
|
|
explicit WatchTarget(const char* aName) : mName(aName) {}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:00:14 +03:00
|
|
|
void AddWatcher(AbstractWatcher* aWatcher) {
|
2015-03-19 00:27:49 +03:00
|
|
|
MOZ_ASSERT(!mWatchers.Contains(aWatcher));
|
|
|
|
mWatchers.AppendElement(aWatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveWatcher(AbstractWatcher* aWatcher) {
|
|
|
|
MOZ_ASSERT(mWatchers.Contains(aWatcher));
|
|
|
|
mWatchers.RemoveElement(aWatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void NotifyWatchers() {
|
|
|
|
WATCH_LOG("%s[%p] notifying watchers\n", mName, this);
|
|
|
|
PruneWatchers();
|
|
|
|
for (size_t i = 0; i < mWatchers.Length(); ++i) {
|
|
|
|
mWatchers[i]->Notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// We don't have Watchers explicitly unregister themselves when they die,
|
|
|
|
// because then they'd need back-references to all the WatchTargets they're
|
|
|
|
// subscribed to, and WatchTargets aren't reference-counted. So instead we
|
|
|
|
// just prune dead ones at appropriate times, which works just fine.
|
|
|
|
void PruneWatchers() {
|
|
|
|
for (int i = mWatchers.Length() - 1; i >= 0; --i) {
|
|
|
|
if (mWatchers[i]->IsDestroyed()) {
|
|
|
|
mWatchers.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<AbstractWatcher>> mWatchers;
|
2015-04-01 04:44:13 +03:00
|
|
|
|
|
|
|
protected:
|
2015-03-19 00:27:49 +03:00
|
|
|
const char* mName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Watchable is a wrapper class that turns any primitive into a WatchTarget.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class Watchable : public WatchTarget {
|
|
|
|
public:
|
|
|
|
Watchable(const T& aInitialValue, const char* aName)
|
2018-11-15 15:47:59 +03:00
|
|
|
: WatchTarget(aName), mValue(aInitialValue) {}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-06-03 05:01:55 +03:00
|
|
|
const T& Ref() const { return mValue; }
|
|
|
|
operator const T&() const { return Ref(); }
|
2015-03-19 00:27:49 +03:00
|
|
|
Watchable& operator=(const T& aNewValue) {
|
|
|
|
if (aNewValue != mValue) {
|
|
|
|
mValue = aNewValue;
|
|
|
|
NotifyWatchers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-11-15 15:47:59 +03:00
|
|
|
Watchable(const Watchable& aOther) = delete;
|
|
|
|
Watchable& operator=(const Watchable& aOther) = delete;
|
2015-03-19 00:27:49 +03:00
|
|
|
|
|
|
|
T mValue;
|
|
|
|
};
|
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
// Manager class for state-watching. Declare one of these in any class for which
|
|
|
|
// you want to invoke method callbacks.
|
|
|
|
//
|
|
|
|
// Internally, WatchManager maintains one AbstractWatcher per callback method.
|
|
|
|
// Consumers invoke Watch/Unwatch on a particular (WatchTarget, Callback) tuple.
|
2018-11-15 15:47:59 +03:00
|
|
|
// This causes an AbstractWatcher for |Callback| to be instantiated if it
|
|
|
|
// doesn't already exist, and registers it with |WatchTarget|.
|
2015-04-29 05:02:31 +03:00
|
|
|
//
|
|
|
|
// Using Direct Tasks on the TailDispatcher, WatchManager ensures that we fire
|
2018-11-15 15:47:59 +03:00
|
|
|
// watch callbacks no more than once per task, once all other operations for
|
|
|
|
// that task have been completed.
|
2015-04-29 05:02:31 +03:00
|
|
|
//
|
|
|
|
// WatchManager<OwnerType> is intended to be declared as a member of |OwnerType|
|
2018-11-15 15:47:59 +03:00
|
|
|
// objects. Given that, it and its owned objects can't hold permanent strong
|
|
|
|
// refs to the owner, since that would keep the owner alive indefinitely.
|
|
|
|
// Instead, it _only_ holds strong refs while waiting for Direct Tasks to fire.
|
|
|
|
// This ensures that everything is kept alive just long enough.
|
|
|
|
template <typename OwnerType>
|
2015-04-29 05:02:31 +03:00
|
|
|
class WatchManager {
|
2015-03-19 00:27:49 +03:00
|
|
|
public:
|
2018-11-15 15:47:59 +03:00
|
|
|
typedef void (OwnerType::*CallbackMethod)();
|
2015-05-01 02:21:14 +03:00
|
|
|
explicit WatchManager(OwnerType* aOwner, AbstractThread* aOwnerThread)
|
2018-11-15 15:47:59 +03:00
|
|
|
: mOwner(aOwner), mOwnerThread(aOwnerThread) {}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
~WatchManager() {
|
2015-05-01 02:21:14 +03:00
|
|
|
if (!IsShutdown()) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShutdown() const { return !mOwner; }
|
|
|
|
|
|
|
|
// Shutdown needs to happen on mOwnerThread. If the WatchManager will be
|
|
|
|
// destroyed on a different thread, Shutdown() must be called manually.
|
|
|
|
void Shutdown() {
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2018-11-15 15:47:59 +03:00
|
|
|
for (auto& watcher : mWatchers) {
|
|
|
|
watcher->Destroy();
|
2015-03-19 00:27:49 +03:00
|
|
|
}
|
2015-05-01 02:21:14 +03:00
|
|
|
mWatchers.Clear();
|
|
|
|
mOwner = nullptr;
|
2015-03-19 00:27:49 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
void Watch(WatchTarget& aTarget, CallbackMethod aMethod) {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2015-04-29 05:02:31 +03:00
|
|
|
aTarget.AddWatcher(&EnsureWatcher(aMethod));
|
|
|
|
}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
void Unwatch(WatchTarget& aTarget, CallbackMethod aMethod) {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2015-04-29 05:02:31 +03:00
|
|
|
PerCallbackWatcher* watcher = GetWatcher(aMethod);
|
|
|
|
MOZ_ASSERT(watcher);
|
|
|
|
aTarget.RemoveWatcher(watcher);
|
2015-03-19 00:27:49 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
void ManualNotify(CallbackMethod aMethod) {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2015-04-29 05:02:31 +03:00
|
|
|
PerCallbackWatcher* watcher = GetWatcher(aMethod);
|
|
|
|
MOZ_ASSERT(watcher);
|
|
|
|
watcher->Notify();
|
|
|
|
}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
private:
|
|
|
|
class PerCallbackWatcher : public AbstractWatcher {
|
|
|
|
public:
|
2018-11-15 15:47:59 +03:00
|
|
|
PerCallbackWatcher(OwnerType* aOwner, AbstractThread* aOwnerThread,
|
|
|
|
CallbackMethod aMethod)
|
|
|
|
: mOwner(aOwner),
|
|
|
|
mOwnerThread(aOwnerThread),
|
|
|
|
mCallbackMethod(aMethod) {}
|
2015-04-29 05:02:31 +03:00
|
|
|
|
|
|
|
void Destroy() {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2015-04-29 05:02:31 +03:00
|
|
|
mDestroyed = true;
|
|
|
|
mOwner = nullptr;
|
|
|
|
}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
void Notify() override {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2018-11-15 15:47:59 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mOwner,
|
|
|
|
"mOwner is only null after destruction, "
|
|
|
|
"at which point we shouldn't be notified");
|
|
|
|
if (mNotificationPending) {
|
2015-04-29 05:02:31 +03:00
|
|
|
// We've already got a notification job in the pipe.
|
|
|
|
return;
|
|
|
|
}
|
2018-11-15 15:47:59 +03:00
|
|
|
mNotificationPending = true;
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
// Queue up our notification jobs to run in a stable state.
|
2017-06-12 22:34:10 +03:00
|
|
|
mOwnerThread->TailDispatcher().AddDirectTask(
|
2018-11-15 15:47:59 +03:00
|
|
|
NS_NewRunnableFunction("WatchManager::PerCallbackWatcher::Notify",
|
|
|
|
[self = RefPtr<PerCallbackWatcher>(this),
|
|
|
|
owner = RefPtr<OwnerType>(mOwner)]() {
|
|
|
|
if (!self->mDestroyed) {
|
|
|
|
((*owner).*(self->mCallbackMethod))();
|
|
|
|
}
|
|
|
|
self->mNotificationPending = false;
|
|
|
|
}));
|
2015-03-19 00:27:49 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
bool CallbackMethodIs(CallbackMethod aMethod) const {
|
|
|
|
return mCallbackMethod == aMethod;
|
|
|
|
}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
private:
|
2018-11-15 15:47:59 +03:00
|
|
|
~PerCallbackWatcher() = default;
|
2015-04-29 05:02:31 +03:00
|
|
|
|
|
|
|
OwnerType* mOwner; // Never null.
|
2018-11-15 15:47:59 +03:00
|
|
|
bool mNotificationPending = false;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AbstractThread> mOwnerThread;
|
2015-04-29 05:02:31 +03:00
|
|
|
CallbackMethod mCallbackMethod;
|
|
|
|
};
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
PerCallbackWatcher* GetWatcher(CallbackMethod aMethod) {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2018-11-15 15:47:59 +03:00
|
|
|
for (auto& watcher : mWatchers) {
|
|
|
|
if (watcher->CallbackMethodIs(aMethod)) {
|
|
|
|
return watcher;
|
2015-04-29 05:02:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-03-19 00:27:49 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 05:02:31 +03:00
|
|
|
PerCallbackWatcher& EnsureWatcher(CallbackMethod aMethod) {
|
2015-05-01 02:21:14 +03:00
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2015-04-29 05:02:31 +03:00
|
|
|
PerCallbackWatcher* watcher = GetWatcher(aMethod);
|
|
|
|
if (watcher) {
|
|
|
|
return *watcher;
|
|
|
|
}
|
2018-11-15 15:47:59 +03:00
|
|
|
watcher = mWatchers
|
|
|
|
.AppendElement(MakeAndAddRef<PerCallbackWatcher>(
|
|
|
|
mOwner, mOwnerThread, aMethod))
|
|
|
|
->get();
|
2015-04-29 05:02:31 +03:00
|
|
|
return *watcher;
|
|
|
|
}
|
2015-03-19 00:27:49 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<PerCallbackWatcher>> mWatchers;
|
2015-04-29 05:02:31 +03:00
|
|
|
OwnerType* mOwner;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AbstractThread> mOwnerThread;
|
2015-04-29 05:02:31 +03:00
|
|
|
};
|
2015-03-19 00:27:49 +03:00
|
|
|
|
|
|
|
# undef WATCH_LOG
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|