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() : tls_context_(g_current_context.Pointer()){ | 21 RequestContext::RequestContext() : RequestContext(Source::LOCAL_API_CALL) {} |
22 | |
23 RequestContext::RequestContext(Source source) | |
24 : source_(source), tls_context_(g_current_context.Pointer()) { | |
22 // We allow nested RequestContexts to exist as long as they aren't actually | 25 // We allow nested RequestContexts to exist as long as they aren't actually |
23 // used for anything. | 26 // used for anything. |
24 if (!tls_context_->Get()) | 27 if (!tls_context_->Get()) |
25 tls_context_->Set(this); | 28 tls_context_->Set(this); |
26 } | 29 } |
27 | 30 |
28 RequestContext::~RequestContext() { | 31 RequestContext::~RequestContext() { |
29 // NOTE: Callbacks invoked by this destructor are allowed to initiate new | 32 if (IsCurrent()) { |
30 // EDK requests on this thread, so we need to reset the thread-local context | 33 // NOTE: Callbacks invoked by this destructor are allowed to initiate new |
31 // pointer before calling them. | 34 // EDK requests on this thread, so we need to reset the thread-local context |
32 if (IsCurrent()) | 35 // pointer before calling them. We persist the original notification source |
36 // since we're starting over at the bottom of the stack. | |
33 tls_context_->Set(nullptr); | 37 tls_context_->Set(nullptr); |
34 | 38 |
35 for (const WatchNotifyFinalizer& watch : | 39 MojoWatchNotificationFlags flags = MOJO_WATCH_NOTIFICATION_FLAG_NONE; |
36 watch_notify_finalizers_.container()) { | 40 if (source_ == Source::SYSTEM) |
37 // Establish a new request context for the extent of each callback to ensure | 41 flags |= MOJO_WATCH_NOTIFICATION_FLAG_FROM_SYSTEM; |
38 // that they don't themselves invoke callbacks while holding a watcher lock. | 42 |
39 RequestContext request_context; | 43 for (const WatchNotifyFinalizer& watch : |
40 watch.watcher->MaybeInvokeCallback(watch.result, watch.state); | 44 watch_notify_finalizers_.container()) { |
45 // Establish a new request context for the extent of each callback to | |
46 // ensure that they don't themselves invoke callbacks while holding a | |
47 // watcher lock. | |
48 RequestContext request_context(source_); | |
49 watch.watcher->MaybeInvokeCallback(watch.result, watch.state, flags); | |
50 } | |
51 | |
52 for (const scoped_refptr<Watcher>& watcher : | |
53 watch_cancel_finalizers_.container()) | |
54 watcher->Cancel(); | |
55 } else { | |
56 // It should be impossible for nested contexts to have finalizers. | |
57 CHECK(watch_notify_finalizers_.container().empty()); | |
Anand Mistry (off Chromium)
2016/03/17 17:26:49
optional nit: I'd argue these should be DCHECK ins
Ken Rockot(use gerrit already)
2016/03/17 17:29:56
Done
| |
58 CHECK(watch_cancel_finalizers_.container().empty()); | |
41 } | 59 } |
42 | |
43 for (const scoped_refptr<Watcher>& watcher : | |
44 watch_cancel_finalizers_.container()) | |
45 watcher->Cancel(); | |
46 } | 60 } |
47 | 61 |
48 // static | 62 // static |
49 RequestContext* RequestContext::current() { | 63 RequestContext* RequestContext::current() { |
50 DCHECK(g_current_context.Pointer()->Get()); | 64 DCHECK(g_current_context.Pointer()->Get()); |
51 return g_current_context.Pointer()->Get(); | 65 return g_current_context.Pointer()->Get(); |
52 } | 66 } |
53 | 67 |
54 void RequestContext::AddWatchNotifyFinalizer( | 68 void RequestContext::AddWatchNotifyFinalizer( |
55 scoped_refptr<Watcher> watcher, | 69 scoped_refptr<Watcher> watcher, |
(...skipping 17 matching lines...) Expand all Loading... | |
73 scoped_refptr<Watcher> watcher, | 87 scoped_refptr<Watcher> watcher, |
74 MojoResult result, | 88 MojoResult result, |
75 const HandleSignalsState& state) | 89 const HandleSignalsState& state) |
76 : watcher(watcher), result(result), state(state) { | 90 : watcher(watcher), result(result), state(state) { |
77 } | 91 } |
78 | 92 |
79 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} | 93 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} |
80 | 94 |
81 } // namespace edk | 95 } // namespace edk |
82 } // namespace mojo | 96 } // namespace mojo |
OLD | NEW |