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

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

Issue 1748503002: [mojo-edk] Add MojoWatch and MojoCancelWatch APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: revert RequestContext usage, nits Created 4 years, 10 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
« no previous file with comments | « mojo/edk/system/request_context.h ('k') | mojo/edk/system/watch_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/edk/system/request_context.cc
diff --git a/mojo/edk/system/request_context.cc b/mojo/edk/system/request_context.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4b7e011ffd9e9f44684a73d6fe8b9b56b0cbe32e
--- /dev/null
+++ b/mojo/edk/system/request_context.cc
@@ -0,0 +1,77 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/edk/system/request_context.h"
+
+#include "base/lazy_instance.h"
+#include "base/logging.h"
+#include "base/threading/thread_local.h"
+
+namespace mojo {
+namespace edk {
+
+namespace {
+
+base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky
+ g_current_context;
+
+} // namespace
+
+RequestContext::RequestContext() {
+ // We allow nested RequestContexts to exist as long as they aren't actually
+ // used for anything.
+ if (!g_current_context.Pointer()->Get())
+ g_current_context.Pointer()->Set(this);
+}
+
+RequestContext::~RequestContext() {
+ // NOTE: Callbacks invoked by this destructor are allowed to initiate new
+ // EDK requests on this thread, so we need to reset the thread-local context
+ // pointer before calling them.
+ if (IsCurrent())
+ g_current_context.Pointer()->Set(nullptr);
+
+ for (const WatchNotifyFinalizer& watch : watch_notify_finalizers_.container())
+ watch.watcher->MaybeInvokeCallback(watch.result, watch.state);
+
+ for (const scoped_refptr<Watcher>& watcher :
+ watch_cancel_finalizers_.container())
+ watcher->Cancel();
+}
+
+// static
+RequestContext* RequestContext::current() {
+ DCHECK(g_current_context.Pointer()->Get());
+ return g_current_context.Pointer()->Get();
+}
+
+void RequestContext::AddWatchNotifyFinalizer(
+ scoped_refptr<Watcher> watcher,
+ MojoResult result,
+ const HandleSignalsState& state) {
+ DCHECK(IsCurrent());
+ watch_notify_finalizers_->push_back(
+ WatchNotifyFinalizer(watcher, result, state));
+}
+
+void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) {
+ DCHECK(IsCurrent());
+ watch_cancel_finalizers_->push_back(watcher);
+}
+
+bool RequestContext::IsCurrent() const {
+ return g_current_context.Pointer()->Get() == this;
+}
+
+RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer(
+ scoped_refptr<Watcher> watcher,
+ MojoResult result,
+ const HandleSignalsState& state)
+ : watcher(watcher), result(result), state(state) {
+}
+
+RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {}
+
+} // namespace edk
+} // namespace mojo
« no previous file with comments | « mojo/edk/system/request_context.h ('k') | mojo/edk/system/watch_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698