| 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 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 namespace edk { | 12 namespace edk { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky | 16 base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky |
| 17 g_current_context = LAZY_INSTANCE_INITIALIZER; | 17 g_current_context = LAZY_INSTANCE_INITIALIZER; |
| 18 | 18 |
| 19 } // namespace | 19 } // namespace |
| 20 | 20 |
| 21 RequestContext::RequestContext() { | 21 RequestContext::RequestContext() : tls_context_(g_current_context.Pointer()){ |
| 22 // We allow nested RequestContexts to exist as long as they aren't actually | 22 // We allow nested RequestContexts to exist as long as they aren't actually |
| 23 // used for anything. | 23 // used for anything. |
| 24 if (!g_current_context.Pointer()->Get()) | 24 if (!tls_context_->Get()) |
| 25 g_current_context.Pointer()->Set(this); | 25 tls_context_->Set(this); |
| 26 } | 26 } |
| 27 | 27 |
| 28 RequestContext::~RequestContext() { | 28 RequestContext::~RequestContext() { |
| 29 // NOTE: Callbacks invoked by this destructor are allowed to initiate new | 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 | 30 // EDK requests on this thread, so we need to reset the thread-local context |
| 31 // pointer before calling them. | 31 // pointer before calling them. |
| 32 if (IsCurrent()) | 32 if (IsCurrent()) |
| 33 g_current_context.Pointer()->Set(nullptr); | 33 tls_context_->Set(nullptr); |
| 34 | 34 |
| 35 for (const WatchNotifyFinalizer& watch : | 35 for (const WatchNotifyFinalizer& watch : |
| 36 watch_notify_finalizers_.container()) { | 36 watch_notify_finalizers_.container()) { |
| 37 // Establish a new request context for the extent of each callback to ensure | 37 // Establish a new request context for the extent of each callback to ensure |
| 38 // that they don't themselves invoke callbacks while holding a watcher lock. | 38 // that they don't themselves invoke callbacks while holding a watcher lock. |
| 39 RequestContext request_context; | 39 RequestContext request_context; |
| 40 watch.watcher->MaybeInvokeCallback(watch.result, watch.state); | 40 watch.watcher->MaybeInvokeCallback(watch.result, watch.state); |
| 41 } | 41 } |
| 42 | 42 |
| 43 for (const scoped_refptr<Watcher>& watcher : | 43 for (const scoped_refptr<Watcher>& watcher : |
| (...skipping 15 matching lines...) Expand all Loading... |
| 59 watch_notify_finalizers_->push_back( | 59 watch_notify_finalizers_->push_back( |
| 60 WatchNotifyFinalizer(watcher, result, state)); | 60 WatchNotifyFinalizer(watcher, result, state)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { | 63 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { |
| 64 DCHECK(IsCurrent()); | 64 DCHECK(IsCurrent()); |
| 65 watch_cancel_finalizers_->push_back(watcher); | 65 watch_cancel_finalizers_->push_back(watcher); |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool RequestContext::IsCurrent() const { | 68 bool RequestContext::IsCurrent() const { |
| 69 return g_current_context.Pointer()->Get() == this; | 69 return tls_context_->Get() == this; |
| 70 } | 70 } |
| 71 | 71 |
| 72 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( | 72 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( |
| 73 scoped_refptr<Watcher> watcher, | 73 scoped_refptr<Watcher> watcher, |
| 74 MojoResult result, | 74 MojoResult result, |
| 75 const HandleSignalsState& state) | 75 const HandleSignalsState& state) |
| 76 : watcher(watcher), result(result), state(state) { | 76 : watcher(watcher), result(result), state(state) { |
| 77 } | 77 } |
| 78 | 78 |
| 79 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} | 79 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} |
| 80 | 80 |
| 81 } // namespace edk | 81 } // namespace edk |
| 82 } // namespace mojo | 82 } // namespace mojo |
| OLD | NEW |