Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: mojo/edk/system/request_context.cc

Issue 1748503002: [mojo-edk] Add MojoWatch and MojoCancelWatch APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/threading/thread_local.h"
8
9 namespace mojo {
10 namespace edk {
11
12 namespace {
13
14 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
15
16 } // namespace
17
18 RequestContext::RequestContext() {
19 // We allow nested RequestContexts to exist as long as they aren't actually
20 // used for anything.
21 if (!g_current_context.Get())
22 g_current_context.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 if (IsCurrent())
30 g_current_context.Set(nullptr);
31
32 for (const WatchFinalizer& watch : watch_finalizers_.container())
33 watch.watcher->MaybeInvokeCallback(watch.result, watch.state);
34 }
35
36 // static
37 RequestContext* RequestContext::current() {
38 DCHECK(g_current_context.Get());
39 return g_current_context.Get();
40 }
41
42 void RequestContext::AddWatchFinalizer(
43 scoped_refptr<Watcher> watcher,
44 MojoResult result,
45 MojoHandleSignalsState state) {
46 DCHECK(IsCurrent());
47 watch_finalizers_->push_back(WatchFinalizer(watcher, result, state));
48 }
49
50 bool RequestContext::IsCurrent() const {
51 return g_current_context.Get() == this;
52 }
53
54 RequestContext::WatchFinalizer::WatchFinalizer(
55 scoped_refptr<Watcher> watcher,
56 MojoResult result,
57 MojoHandleSignalsState state)
58 : watcher(watcher), result(result), state(state) {
59 }
60
61 RequestContext::WatchFinalizer::~WatchFinalizer() {}
62
63 } // namespace edk
64 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698