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

Side by Side Diff: mojo/public/cpp/bindings/lib/sync_handle_watcher.cc

Issue 1723673002: Reland "Mojo C++ bindings: support sync methods - part 2" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/sync_handle_watcher.h ('k') | mojo/public/cpp/bindings/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/public/cpp/bindings/lib/sync_handle_watcher.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "base/threading/thread_local.h"
11 #include "mojo/public/c/system/core.h"
12
13 namespace mojo {
14 namespace internal {
15 namespace {
16
17 base::LazyInstance<base::ThreadLocalPointer<SyncHandleWatcher>>
18 g_current_sync_handle_watcher = LAZY_INSTANCE_INITIALIZER;
19
20 } // namespace
21
22 // static
23 SyncHandleWatcher* SyncHandleWatcher::current() {
24 SyncHandleWatcher* result = g_current_sync_handle_watcher.Pointer()->Get();
25 if (!result) {
26 // This object will be destroyed when the current message loop goes away.
27 result = new SyncHandleWatcher();
28 DCHECK_EQ(result, g_current_sync_handle_watcher.Pointer()->Get());
29 }
30 return result;
31 }
32
33 bool SyncHandleWatcher::RegisterHandle(const Handle& handle,
34 MojoHandleSignals handle_signals,
35 const HandleCallback& callback) {
36 DCHECK(thread_checker_.CalledOnValidThread());
37
38 if (ContainsKey(handles_, handle))
39 return false;
40
41 MojoResult result = MojoAddHandle(wait_set_handle_.get().value(),
42 handle.value(), handle_signals);
43 if (result != MOJO_RESULT_OK)
44 return false;
45
46 handles_[handle] = callback;
47 return true;
48 }
49
50 void SyncHandleWatcher::UnregisterHandle(const Handle& handle) {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 DCHECK(ContainsKey(handles_, handle));
53
54 MojoResult result =
55 MojoRemoveHandle(wait_set_handle_.get().value(), handle.value());
56 DCHECK_EQ(MOJO_RESULT_OK, result);
57
58 handles_.erase(handle);
59 }
60
61 bool SyncHandleWatcher::WatchAllHandles(const bool* should_stop[],
62 size_t count) {
63 DCHECK(thread_checker_.CalledOnValidThread());
64
65 MojoResult result;
66 uint32_t num_ready_handles;
67 MojoHandle ready_handle;
68 MojoResult ready_handle_result;
69
70 while (true) {
71 for (size_t i = 0; i < count; ++i)
72 if (*should_stop[i])
73 return true;
74 do {
75 result = Wait(wait_set_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE,
76 MOJO_DEADLINE_INDEFINITE, nullptr);
77 if (result != MOJO_RESULT_OK)
78 return false;
79
80 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we
81 // give priority to the handle that is waiting for sync response.
82 num_ready_handles = 1;
83 result = MojoGetReadyHandles(wait_set_handle_.get().value(),
84 &num_ready_handles, &ready_handle,
85 &ready_handle_result, nullptr);
86 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT)
87 return false;
88 } while (result == MOJO_RESULT_SHOULD_WAIT);
89
90 const auto iter = handles_.find(Handle(ready_handle));
91 iter->second.Run(ready_handle_result);
92 };
93
94 return true;
95 }
96
97 SyncHandleWatcher::SyncHandleWatcher() {
98 MojoHandle handle;
99 MojoResult result = MojoCreateWaitSet(&handle);
100 CHECK_EQ(MOJO_RESULT_OK, result);
101 wait_set_handle_.reset(Handle(handle));
102 CHECK(wait_set_handle_.is_valid());
103
104 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get());
105 g_current_sync_handle_watcher.Pointer()->Set(this);
106
107 base::MessageLoop::current()->AddDestructionObserver(this);
108 }
109
110 SyncHandleWatcher::~SyncHandleWatcher() {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 DCHECK(handles_.empty());
113 g_current_sync_handle_watcher.Pointer()->Set(nullptr);
114 }
115
116 void SyncHandleWatcher::WillDestroyCurrentMessageLoop() {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get());
119
120 base::MessageLoop::current()->RemoveDestructionObserver(this);
121 delete this;
122 }
123
124 } // namespace internal
125 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/sync_handle_watcher.h ('k') | mojo/public/cpp/bindings/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698