| OLD | NEW |
| 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 Loading... |
| 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 MojoWatcherNotificationFlags flags = MOJO_WATCHER_NOTIFICATION_FLAG_NONE; | 39 MojoWatchNotificationFlags flags = MOJO_WATCH_NOTIFICATION_FLAG_NONE; |
| 40 if (source_ == Source::SYSTEM) | 40 if (source_ == Source::SYSTEM) |
| 41 flags |= MOJO_WATCHER_NOTIFICATION_FLAG_FROM_SYSTEM; | 41 flags |= MOJO_WATCH_NOTIFICATION_FLAG_FROM_SYSTEM; |
| 42 | 42 |
| 43 // We send all cancellation notifications first. This is necessary because | 43 // We run all cancellation finalizers first. This is necessary because it's |
| 44 // it's possible that cancelled watches have other pending notifications | 44 // possible that one of the cancelled watchers has other pending finalizers |
| 45 // attached to this RequestContext. | 45 // attached to this RequestContext. |
| 46 // | 46 // |
| 47 // From the application's perspective the watch is cancelled as soon as this | 47 // From the application's perspective the watch has already been cancelled, |
| 48 // notification is received, and dispatching the cancellation notification | 48 // so we have to honor our contract which guarantees no more notifications. |
| 49 // updates some internal Watch state to ensure no further notifications | 49 for (const scoped_refptr<Watcher>& watcher : |
| 50 // fire. Because notifications on a single Watch are mutually exclusive, | 50 watch_cancel_finalizers_.container()) |
| 51 // this is sufficient to guarantee that MOJO_RESULT_CANCELLED is the last | 51 watcher->Cancel(); |
| 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 } | |
| 62 | 52 |
| 63 for (const WatchNotifyFinalizer& watch : | 53 for (const WatchNotifyFinalizer& watch : |
| 64 watch_notify_finalizers_.container()) { | 54 watch_notify_finalizers_.container()) { |
| 65 RequestContext inner_context(source_); | 55 // Establish a new request context for the extent of each callback to |
| 66 watch.watch->InvokeCallback(watch.result, watch.state, flags); | 56 // ensure that they don't themselves invoke callbacks while holding a |
| 57 // watcher lock. |
| 58 RequestContext request_context(source_); |
| 59 watch.watcher->MaybeInvokeCallback(watch.result, watch.state, flags); |
| 67 } | 60 } |
| 68 } else { | 61 } else { |
| 69 // It should be impossible for nested contexts to have finalizers. | 62 // It should be impossible for nested contexts to have finalizers. |
| 70 DCHECK(watch_notify_finalizers_.container().empty()); | 63 DCHECK(watch_notify_finalizers_.container().empty()); |
| 71 DCHECK(watch_cancel_finalizers_.container().empty()); | 64 DCHECK(watch_cancel_finalizers_.container().empty()); |
| 72 } | 65 } |
| 73 } | 66 } |
| 74 | 67 |
| 75 // static | 68 // static |
| 76 RequestContext* RequestContext::current() { | 69 RequestContext* RequestContext::current() { |
| 77 DCHECK(g_current_context.Pointer()->Get()); | 70 DCHECK(g_current_context.Pointer()->Get()); |
| 78 return g_current_context.Pointer()->Get(); | 71 return g_current_context.Pointer()->Get(); |
| 79 } | 72 } |
| 80 | 73 |
| 81 void RequestContext::AddWatchNotifyFinalizer(scoped_refptr<Watch> watch, | 74 void RequestContext::AddWatchNotifyFinalizer( |
| 82 MojoResult result, | 75 scoped_refptr<Watcher> watcher, |
| 83 const HandleSignalsState& state) { | 76 MojoResult result, |
| 77 const HandleSignalsState& state) { |
| 84 DCHECK(IsCurrent()); | 78 DCHECK(IsCurrent()); |
| 85 watch_notify_finalizers_->push_back( | 79 watch_notify_finalizers_->push_back( |
| 86 WatchNotifyFinalizer(std::move(watch), result, state)); | 80 WatchNotifyFinalizer(std::move(watcher), result, state)); |
| 87 } | 81 } |
| 88 | 82 |
| 89 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watch> watch) { | 83 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { |
| 90 DCHECK(IsCurrent()); | 84 DCHECK(IsCurrent()); |
| 91 watch_cancel_finalizers_->push_back(std::move(watch)); | 85 watch_cancel_finalizers_->push_back(std::move(watcher)); |
| 92 } | 86 } |
| 93 | 87 |
| 94 bool RequestContext::IsCurrent() const { | 88 bool RequestContext::IsCurrent() const { |
| 95 return tls_context_->Get() == this; | 89 return tls_context_->Get() == this; |
| 96 } | 90 } |
| 97 | 91 |
| 98 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( | 92 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( |
| 99 scoped_refptr<Watch> watch, | 93 scoped_refptr<Watcher> watcher, |
| 100 MojoResult result, | 94 MojoResult result, |
| 101 const HandleSignalsState& state) | 95 const HandleSignalsState& state) |
| 102 : watch(std::move(watch)), result(result), state(state) {} | 96 : watcher(std::move(watcher)), result(result), state(state) {} |
| 103 | 97 |
| 104 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( | 98 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( |
| 105 const WatchNotifyFinalizer& other) = default; | 99 const WatchNotifyFinalizer& other) = default; |
| 106 | 100 |
| 107 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} | 101 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} |
| 108 | 102 |
| 109 } // namespace edk | 103 } // namespace edk |
| 110 } // namespace mojo | 104 } // namespace mojo |
| OLD | NEW |