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

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

Issue 2725133002: 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/request_context.h ('k') | mojo/edk/system/watch.h » ('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/request_context.h" 5 #include "mojo/edk/system/request_context.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/threading/thread_local.h" 9 #include "base/threading/thread_local.h"
10 10
(...skipping 18 matching lines...) Expand all
29 } 29 }
30 30
31 RequestContext::~RequestContext() { 31 RequestContext::~RequestContext() {
32 if (IsCurrent()) { 32 if (IsCurrent()) {
33 // NOTE: Callbacks invoked by this destructor are allowed to initiate new 33 // NOTE: Callbacks invoked by this destructor are allowed to initiate new
34 // EDK requests on this thread, so we need to reset the thread-local context 34 // EDK requests on this thread, so we need to reset the thread-local context
35 // pointer before calling them. We persist the original notification source 35 // pointer before calling them. We persist the original notification source
36 // since we're starting over at the bottom of the stack. 36 // since we're starting over at the bottom of the stack.
37 tls_context_->Set(nullptr); 37 tls_context_->Set(nullptr);
38 38
39 MojoWatchNotificationFlags flags = MOJO_WATCH_NOTIFICATION_FLAG_NONE; 39 MojoWatcherNotificationFlags flags = MOJO_WATCHER_NOTIFICATION_FLAG_NONE;
40 if (source_ == Source::SYSTEM) 40 if (source_ == Source::SYSTEM)
41 flags |= MOJO_WATCH_NOTIFICATION_FLAG_FROM_SYSTEM; 41 flags |= MOJO_WATCHER_NOTIFICATION_FLAG_FROM_SYSTEM;
42 42
43 // We run all cancellation finalizers first. This is necessary because it's 43 // We send all cancellation notifications first. This is necessary because
44 // possible that one of the cancelled watchers has other pending finalizers 44 // it's possible that cancelled watches have other pending notifications
45 // attached to this RequestContext. 45 // attached to this RequestContext.
46 // 46 //
47 // From the application's perspective the watch has already been cancelled, 47 // From the application's perspective the watch is cancelled as soon as this
48 // so we have to honor our contract which guarantees no more notifications. 48 // notification is received, and dispatching the cancellation notification
49 for (const scoped_refptr<Watcher>& watcher : 49 // updates some internal Watch state to ensure no further notifications
50 watch_cancel_finalizers_.container()) 50 // fire. Because notifications on a single Watch are mutually exclusive,
51 watcher->Cancel(); 51 // this is sufficient to guarantee that MOJO_RESULT_CANCELLED is the last
52 // notification received; which is the guarantee the API makes.
53 for (const scoped_refptr<Watch>& watch :
54 watch_cancel_finalizers_.container()) {
55 static const HandleSignalsState closed_state = {0, 0};
56
57 // Establish a new RequestContext to capture and run any new notifications
58 // triggered by the callback invocation.
59 RequestContext inner_context(source_);
60 watch->InvokeCallback(MOJO_RESULT_CANCELLED, closed_state, flags);
61 }
52 62
53 for (const WatchNotifyFinalizer& watch : 63 for (const WatchNotifyFinalizer& watch :
54 watch_notify_finalizers_.container()) { 64 watch_notify_finalizers_.container()) {
55 // Establish a new request context for the extent of each callback to 65 RequestContext inner_context(source_);
56 // ensure that they don't themselves invoke callbacks while holding a 66 watch.watch->InvokeCallback(watch.result, watch.state, flags);
57 // watcher lock.
58 RequestContext request_context(source_);
59 watch.watcher->MaybeInvokeCallback(watch.result, watch.state, flags);
60 } 67 }
61 } else { 68 } else {
62 // It should be impossible for nested contexts to have finalizers. 69 // It should be impossible for nested contexts to have finalizers.
63 DCHECK(watch_notify_finalizers_.container().empty()); 70 DCHECK(watch_notify_finalizers_.container().empty());
64 DCHECK(watch_cancel_finalizers_.container().empty()); 71 DCHECK(watch_cancel_finalizers_.container().empty());
65 } 72 }
66 } 73 }
67 74
68 // static 75 // static
69 RequestContext* RequestContext::current() { 76 RequestContext* RequestContext::current() {
70 DCHECK(g_current_context.Pointer()->Get()); 77 DCHECK(g_current_context.Pointer()->Get());
71 return g_current_context.Pointer()->Get(); 78 return g_current_context.Pointer()->Get();
72 } 79 }
73 80
74 void RequestContext::AddWatchNotifyFinalizer( 81 void RequestContext::AddWatchNotifyFinalizer(scoped_refptr<Watch> watch,
75 scoped_refptr<Watcher> watcher, 82 MojoResult result,
76 MojoResult result, 83 const HandleSignalsState& state) {
77 const HandleSignalsState& state) {
78 DCHECK(IsCurrent()); 84 DCHECK(IsCurrent());
79 watch_notify_finalizers_->push_back( 85 watch_notify_finalizers_->push_back(
80 WatchNotifyFinalizer(std::move(watcher), result, state)); 86 WatchNotifyFinalizer(std::move(watch), result, state));
81 } 87 }
82 88
83 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { 89 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watch> watch) {
84 DCHECK(IsCurrent()); 90 DCHECK(IsCurrent());
85 watch_cancel_finalizers_->push_back(std::move(watcher)); 91 watch_cancel_finalizers_->push_back(std::move(watch));
86 } 92 }
87 93
88 bool RequestContext::IsCurrent() const { 94 bool RequestContext::IsCurrent() const {
89 return tls_context_->Get() == this; 95 return tls_context_->Get() == this;
90 } 96 }
91 97
92 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( 98 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer(
93 scoped_refptr<Watcher> watcher, 99 scoped_refptr<Watch> watch,
94 MojoResult result, 100 MojoResult result,
95 const HandleSignalsState& state) 101 const HandleSignalsState& state)
96 : watcher(std::move(watcher)), result(result), state(state) {} 102 : watch(std::move(watch)), result(result), state(state) {}
97 103
98 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( 104 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer(
99 const WatchNotifyFinalizer& other) = default; 105 const WatchNotifyFinalizer& other) = default;
100 106
101 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} 107 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {}
102 108
103 } // namespace edk 109 } // namespace edk
104 } // namespace mojo 110 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/request_context.h ('k') | mojo/edk/system/watch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698