Chromium Code Reviews| 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" | |
|
Anand Mistry (off Chromium)
2016/03/02 01:50:36
#include "base/logging.h"
Ken Rockot(use gerrit already)
2016/03/02 02:09:39
done
| |
| 8 #include "base/threading/thread_local.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 namespace edk { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 base::LazyInstance<base::ThreadLocalPointer<RequestContext>>::Leaky | |
| 16 g_current_context; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 RequestContext::RequestContext() { | |
| 21 DCHECK(!g_current_context.Pointer()->Get()); | |
| 22 g_current_context.Pointer()->Set(this); | |
| 23 } | |
| 24 | |
| 25 RequestContext::~RequestContext() { | |
| 26 // NOTE: Callbacks invoked by this destructor are allowed to initiate new | |
| 27 // EDK requests on this thread, so we need to reset the thread-local context | |
| 28 // pointer before calling them. | |
| 29 DCHECK(this == g_current_context.Pointer()->Get()); | |
| 30 g_current_context.Pointer()->Set(nullptr); | |
| 31 | |
| 32 for (const WatchNotifyFinalizer& watch : watch_notify_finalizers_.container()) | |
| 33 watch.watcher->MaybeInvokeCallback(watch.result, watch.state); | |
| 34 | |
| 35 for (const scoped_refptr<Watcher>& watcher : | |
| 36 watch_cancel_finalizers_.container()) | |
| 37 watcher->Cancel(); | |
| 38 } | |
| 39 | |
| 40 // static | |
| 41 RequestContext* RequestContext::current() { | |
| 42 DCHECK(g_current_context.Pointer()->Get()); | |
| 43 return g_current_context.Pointer()->Get(); | |
| 44 } | |
| 45 | |
| 46 void RequestContext::AddWatchNotifyFinalizer( | |
| 47 scoped_refptr<Watcher> watcher, | |
| 48 MojoResult result, | |
| 49 const HandleSignalsState& state) { | |
| 50 watch_notify_finalizers_->push_back( | |
| 51 WatchNotifyFinalizer(watcher, result, state)); | |
| 52 } | |
| 53 | |
| 54 void RequestContext::AddWatchCancelFinalizer(scoped_refptr<Watcher> watcher) { | |
| 55 watch_cancel_finalizers_->push_back(watcher); | |
| 56 } | |
| 57 | |
| 58 RequestContext::WatchNotifyFinalizer::WatchNotifyFinalizer( | |
| 59 scoped_refptr<Watcher> watcher, | |
| 60 MojoResult result, | |
| 61 const HandleSignalsState& state) | |
| 62 : watcher(watcher), result(result), state(state) { | |
| 63 } | |
| 64 | |
| 65 RequestContext::WatchNotifyFinalizer::~WatchNotifyFinalizer() {} | |
| 66 | |
| 67 } // namespace edk | |
| 68 } // namespace mojo | |
| OLD | NEW |