OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/edk/system/request_context.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/threading/thread_local.h" |
| 10 |
| 11 namespace mojo { |
| 12 namespace edk { |
| 13 |
| 14 namespace { |
| 15 |
| 16 base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky |
| 17 g_current_context; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 RequestContext::RequestContext() { |
| 22 // We allow nested RequestContexts to exist as long as they aren't actually |
| 23 // used for anything. |
| 24 if (!g_current_context.Pointer()->Get()) |
| 25 g_current_context.Pointer()->Set(this); |
| 26 } |
| 27 |
| 28 RequestContext::~RequestContext() { |
| 29 // NOTE: Callbacks invoked by this destructor are allowed to initiate new |
| 30 // EDK requests on this thread, so we need to reset the thread-local context |
| 31 // pointer before calling them. |
| 32 if (IsCurrent()) |
| 33 g_current_context.Pointer()->Set(nullptr); |
| 34 |
| 35 for (const WatchNotifyFinalizer& watch : watch_notify_finalizers_.container()) |
| 36 watch.watcher->MaybeInvokeCallback(watch.result, watch.state); |
| 37 |
| 38 for (const scoped_refptr<Watcher>& watcher : |
| 39 watch_cancel_finalizers_.container()) |
| 40 watcher->Cancel(); |
| 41 } |
| 42 |
| 43 // static |
| 44 RequestContext* RequestContext::current() { |
| 45 DCHECK(g_current_context.Pointer()->Get()); |
| 46 return g_current_context.Pointer()->Get(); |
| 47 } |
| 48 |
| 49 void RequestContext::AddWatchNotifyFinalizer( |
| 50 scoped_refptr<Watcher> watcher, |
| 51 MojoResult result, |
| 52 const HandleSignalsState& state) { |
| 53 DCHECK(IsCurrent()); |
| 54 watch_notify_finalizers_->push_back( |
| 55 WatchNotifyFinalizer(watcher, result, state)); |
| 56 } |
| 57 |
| 58 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { |
| 59 DCHECK(IsCurrent()); |
| 60 watch_cancel_finalizers_->push_back(watcher); |
| 61 } |
| 62 |
| 63 bool RequestContext::IsCurrent() const { |
| 64 return g_current_context.Pointer()->Get() == this; |
| 65 } |
| 66 |
| 67 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( |
| 68 scoped_refptr<Watcher> watcher, |
| 69 MojoResult result, |
| 70 const HandleSignalsState& state) |
| 71 : watcher(watcher), result(result), state(state) { |
| 72 } |
| 73 |
| 74 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} |
| 75 |
| 76 } // namespace edk |
| 77 } // namespace mojo |
OLD | NEW |