Chromium Code Reviews| 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..43d54cf8f1b2ce9c652106d7012a64a081e9fd64 |
| --- /dev/null |
| +++ b/mojo/edk/system/request_context.cc |
| @@ -0,0 +1,64 @@ |
| +// 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/threading/thread_local.h" |
| + |
| +namespace mojo { |
| +namespace edk { |
| + |
| +namespace { |
| + |
| +base::ThreadLocalPointer<RequestContext> g_current_context; |
|
Anand Mistry (off Chromium)
2016/03/01 07:30:40
Needs to be a LazyInstance, otherwise you're intro
Ken Rockot(use gerrit already)
2016/03/01 08:17:44
done
|
| + |
| +} // namespace |
| + |
| +RequestContext::RequestContext() { |
| + // We allow nested RequestContexts to exist as long as they aren't actually |
| + // used for anything. |
| + if (!g_current_context.Get()) |
| + g_current_context.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.Set(nullptr); |
| + |
| + for (const WatchFinalizer& watch : watch_finalizers_.container()) |
| + watch.watcher->MaybeInvokeCallback(watch.result, watch.state); |
| +} |
| + |
| +// static |
| +RequestContext* RequestContext::current() { |
| + DCHECK(g_current_context.Get()); |
| + return g_current_context.Get(); |
| +} |
| + |
| +void RequestContext::AddWatchFinalizer( |
| + scoped_refptr<Watcher> watcher, |
| + MojoResult result, |
| + MojoHandleSignalsState state) { |
| + DCHECK(IsCurrent()); |
| + watch_finalizers_->push_back(WatchFinalizer(watcher, result, state)); |
| +} |
| + |
| +bool RequestContext::IsCurrent() const { |
| + return g_current_context.Get() == this; |
| +} |
| + |
| +RequestContext::WatchFinalizer::WatchFinalizer( |
| + scoped_refptr<Watcher> watcher, |
| + MojoResult result, |
| + MojoHandleSignalsState state) |
| + : watcher(watcher), result(result), state(state) { |
| +} |
| + |
| +RequestContext::WatchFinalizer::~WatchFinalizer() {} |
| + |
| +} // namespace edk |
| +} // namespace mojo |