Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(661)

Unified Diff: mojo/edk/system/watcher_set.cc

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/edk/system/watcher_set.cc
diff --git a/mojo/edk/system/watcher_set.cc b/mojo/edk/system/watcher_set.cc
index 878f29a54b2987b6bc956820aa7619777d0f33a7..0355b58795f2287b1c845b06bf17ed530eaebaf0 100644
--- a/mojo/edk/system/watcher_set.cc
+++ b/mojo/edk/system/watcher_set.cc
@@ -4,54 +4,79 @@
#include "mojo/edk/system/watcher_set.h"
-#include "mojo/edk/system/request_context.h"
-#include "mojo/public/c/system/types.h"
+#include <utility>
namespace mojo {
namespace edk {
-WatcherSet::WatcherSet() {}
+WatcherSet::WatcherSet(Dispatcher* owner) : owner_(owner) {}
-WatcherSet::~WatcherSet() {}
+WatcherSet::~WatcherSet() = default;
-void WatcherSet::NotifyForStateChange(const HandleSignalsState& state) {
+void WatcherSet::NotifyState(const HandleSignalsState& state) {
+ // Avoid notifying watchers if they have already seen this state.
+ if (last_known_state_.has_value() && state.equals(last_known_state_.value()))
+ return;
+ last_known_state_ = state;
for (const auto& entry : watchers_)
- entry.second->NotifyForStateChange(state);
+ entry.first->NotifyHandleState(owner_, state);
}
void WatcherSet::NotifyClosed() {
for (const auto& entry : watchers_)
- entry.second->NotifyClosed();
+ entry.first->NotifyHandleClosed(owner_);
}
-MojoResult WatcherSet::Add(MojoHandleSignals signals,
- const Watcher::WatchCallback& callback,
+MojoResult WatcherSet::Add(const scoped_refptr<WatcherDispatcher>& watcher,
uintptr_t context,
const HandleSignalsState& current_state) {
- auto it = watchers_.find(context);
- if (it != watchers_.end())
- return MOJO_RESULT_ALREADY_EXISTS;
-
- if (!current_state.can_satisfy(signals))
- return MOJO_RESULT_FAILED_PRECONDITION;
-
- scoped_refptr<Watcher> watcher(new Watcher(signals, callback));
- watchers_.insert(std::make_pair(context, watcher));
+ auto it = watchers_.find(watcher.get());
+ if (it == watchers_.end()) {
+ auto result =
+ watchers_.insert(std::make_pair(watcher.get(), Entry{watcher}));
+ it = result.first;
+ }
- watcher->NotifyForStateChange(current_state);
+ if (!it->second.contexts.insert(context).second)
+ return MOJO_RESULT_ALREADY_EXISTS;
+ if (last_known_state_.has_value() &&
+ !current_state.equals(last_known_state_.value())) {
+ // This new state may be relevant to everyone, in which case we just
+ // notify everyone.
+ NotifyState(current_state);
+ } else {
+ // Otherwise only notify the newly added Watcher.
+ watcher->NotifyHandleState(owner_, current_state);
+ }
return MOJO_RESULT_OK;
}
-MojoResult WatcherSet::Remove(uintptr_t context) {
- auto it = watchers_.find(context);
+MojoResult WatcherSet::Remove(WatcherDispatcher* watcher, uintptr_t context) {
+ auto it = watchers_.find(watcher);
if (it == watchers_.end())
- return MOJO_RESULT_INVALID_ARGUMENT;
+ return MOJO_RESULT_NOT_FOUND;
+
+ ContextSet& contexts = it->second.contexts;
+ auto context_it = contexts.find(context);
+ if (context_it == contexts.end())
+ return MOJO_RESULT_NOT_FOUND;
+
+ contexts.erase(context_it);
+ if (contexts.empty())
+ watchers_.erase(it);
- RequestContext::current()->AddWatchCancelFinalizer(it->second);
- watchers_.erase(it);
return MOJO_RESULT_OK;
}
+WatcherSet::Entry::Entry(const scoped_refptr<WatcherDispatcher>& dispatcher)
+ : dispatcher(dispatcher) {}
+
+WatcherSet::Entry::Entry(Entry&& other) = default;
+
+WatcherSet::Entry::~Entry() = default;
+
+WatcherSet::Entry& WatcherSet::Entry::operator=(Entry&& other) = default;
+
} // namespace edk
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698