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

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

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: . Created 3 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 2017 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/watch.h"
6
7 #include "mojo/edk/system/request_context.h"
8 #include "mojo/edk/system/watcher_dispatcher.h"
9
10 namespace mojo {
11 namespace edk {
12
13 Watch::Watch(const scoped_refptr<WatcherDispatcher>& watcher,
14 const scoped_refptr<Dispatcher>& dispatcher,
15 uintptr_t context,
16 MojoHandleSignals signals)
17 : watcher_(watcher),
18 dispatcher_(dispatcher),
19 context_(context),
20 signals_(signals) {}
21
22 MojoResult Watch::NotifyState(const HandleSignalsState& state,
23 bool allowed_to_call_callback) {
24 // NOTE: This method must NEVER call into |dispatcher_| directly, because it
25 // may be called while |dispatcher_| holds a lock.
26
27 RequestContext* const request_context = RequestContext::current();
28 if (state.satisfies(signals_)) {
29 if (allowed_to_call_callback) {
30 request_context->AddWatchNotifyFinalizer(this, MOJO_RESULT_OK, state);
31 }
32 return MOJO_RESULT_OK;
33 } else if (!state.can_satisfy(signals_)) {
34 if (allowed_to_call_callback) {
35 request_context->AddWatchNotifyFinalizer(
36 this, MOJO_RESULT_FAILED_PRECONDITION, state);
37 }
38 return MOJO_RESULT_FAILED_PRECONDITION;
39 }
40
41 return MOJO_RESULT_SHOULD_WAIT;
42 }
43
44 void Watch::Cancel() {
45 RequestContext::current()->AddWatchCancelFinalizer(this);
46 }
47
48 void Watch::InvokeCallback(MojoResult result,
49 const HandleSignalsState& state,
50 MojoWatcherNotificationFlags flags) {
51 // We hold the lock through invocation to ensure that only one notification
52 // callback runs for this context at any given time.
53 base::AutoLock lock(notification_lock_);
54 if (result == MOJO_RESULT_CANCELLED) {
55 // Make sure cancellation is the last notification we dispatch.
56 DCHECK(!is_cancelled_);
57 is_cancelled_ = true;
58 } else if (is_cancelled_) {
59 return;
60 }
61
62 // NOTE: This will acquire |watcher_|'s internal lock. It's safe because a
63 // thread can only enter InvokeCallback() from within a RequestContext
64 // destructor where no dispatcher locks are held.
65 watcher_->InvokeWatchCallback(context_, result, state, flags);
66 }
67
68 Watch::~Watch() {}
69
70 } // namespace edk
71 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698