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

Side by Side Diff: mojo/edk/system/watcher_set.cc

Issue 2750373002: Revert of Mojo: Armed Watchers (Closed)
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « mojo/edk/system/watcher_set.h ('k') | mojo/edk/system/watcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/system/watcher_set.h" 5 #include "mojo/edk/system/watcher_set.h"
6 6
7 #include <utility> 7 #include "mojo/edk/system/request_context.h"
8 #include "mojo/public/c/system/types.h"
8 9
9 namespace mojo { 10 namespace mojo {
10 namespace edk { 11 namespace edk {
11 12
12 WatcherSet::WatcherSet(Dispatcher* owner) : owner_(owner) {} 13 WatcherSet::WatcherSet() {}
13 14
14 WatcherSet::~WatcherSet() = default; 15 WatcherSet::~WatcherSet() {}
15 16
16 void WatcherSet::NotifyState(const HandleSignalsState& state) { 17 void WatcherSet::NotifyForStateChange(const HandleSignalsState& state) {
17 // Avoid notifying watchers if they have already seen this state.
18 if (last_known_state_.has_value() && state.equals(last_known_state_.value()))
19 return;
20 last_known_state_ = state;
21 for (const auto& entry : watchers_) 18 for (const auto& entry : watchers_)
22 entry.first->NotifyHandleState(owner_, state); 19 entry.second->NotifyForStateChange(state);
23 } 20 }
24 21
25 void WatcherSet::NotifyClosed() { 22 void WatcherSet::NotifyClosed() {
26 for (const auto& entry : watchers_) 23 for (const auto& entry : watchers_)
27 entry.first->NotifyHandleClosed(owner_); 24 entry.second->NotifyClosed();
28 } 25 }
29 26
30 MojoResult WatcherSet::Add(const scoped_refptr<WatcherDispatcher>& watcher, 27 MojoResult WatcherSet::Add(MojoHandleSignals signals,
28 const Watcher::WatchCallback& callback,
31 uintptr_t context, 29 uintptr_t context,
32 const HandleSignalsState& current_state) { 30 const HandleSignalsState& current_state) {
33 auto it = watchers_.find(watcher.get()); 31 auto it = watchers_.find(context);
34 if (it == watchers_.end()) { 32 if (it != watchers_.end())
35 auto result =
36 watchers_.insert(std::make_pair(watcher.get(), Entry{watcher}));
37 it = result.first;
38 }
39
40 if (!it->second.contexts.insert(context).second)
41 return MOJO_RESULT_ALREADY_EXISTS; 33 return MOJO_RESULT_ALREADY_EXISTS;
42 34
43 if (last_known_state_.has_value() && 35 if (!current_state.can_satisfy(signals))
44 !current_state.equals(last_known_state_.value())) { 36 return MOJO_RESULT_FAILED_PRECONDITION;
45 // This new state may be relevant to everyone, in which case we just
46 // notify everyone.
47 NotifyState(current_state);
48 } else {
49 // Otherwise only notify the newly added Watcher.
50 watcher->NotifyHandleState(owner_, current_state);
51 }
52 return MOJO_RESULT_OK;
53 }
54 37
55 MojoResult WatcherSet::Remove(WatcherDispatcher* watcher, uintptr_t context) { 38 scoped_refptr<Watcher> watcher(new Watcher(signals, callback));
56 auto it = watchers_.find(watcher); 39 watchers_.insert(std::make_pair(context, watcher));
57 if (it == watchers_.end())
58 return MOJO_RESULT_NOT_FOUND;
59 40
60 ContextSet& contexts = it->second.contexts; 41 watcher->NotifyForStateChange(current_state);
61 auto context_it = contexts.find(context);
62 if (context_it == contexts.end())
63 return MOJO_RESULT_NOT_FOUND;
64
65 contexts.erase(context_it);
66 if (contexts.empty())
67 watchers_.erase(it);
68 42
69 return MOJO_RESULT_OK; 43 return MOJO_RESULT_OK;
70 } 44 }
71 45
72 WatcherSet::Entry::Entry(const scoped_refptr<WatcherDispatcher>& dispatcher) 46 MojoResult WatcherSet::Remove(uintptr_t context) {
73 : dispatcher(dispatcher) {} 47 auto it = watchers_.find(context);
48 if (it == watchers_.end())
49 return MOJO_RESULT_INVALID_ARGUMENT;
74 50
75 WatcherSet::Entry::Entry(Entry&& other) = default; 51 RequestContext::current()->AddWatchCancelFinalizer(it->second);
76 52 watchers_.erase(it);
77 WatcherSet::Entry::~Entry() = default; 53 return MOJO_RESULT_OK;
78 54 }
79 WatcherSet::Entry& WatcherSet::Entry::operator=(Entry&& other) = default;
80 55
81 } // namespace edk 56 } // namespace edk
82 } // namespace mojo 57 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/watcher_set.h ('k') | mojo/edk/system/watcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698