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

Side by Side Diff: mojo/public/cpp/bindings/lib/connector.h

Issue 1832193002: Mojo C++ bindings: refactor SyncHandleWatcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/connector.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
7 7
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/threading/thread_checker.h" 11 #include "base/threading/thread_checker.h"
12 #include "mojo/public/cpp/bindings/callback.h" 12 #include "mojo/public/cpp/bindings/callback.h"
13 #include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h"
13 #include "mojo/public/cpp/bindings/message.h" 14 #include "mojo/public/cpp/bindings/message.h"
14 #include "mojo/public/cpp/system/core.h" 15 #include "mojo/public/cpp/system/core.h"
15 #include "mojo/public/cpp/system/watcher.h" 16 #include "mojo/public/cpp/system/watcher.h"
16 17
17 namespace base { 18 namespace base {
18 class Lock; 19 class Lock;
19 } 20 }
20 21
21 namespace mojo { 22 namespace mojo {
22 namespace internal { 23 namespace internal {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 void ResumeIncomingMethodCallProcessing(); 113 void ResumeIncomingMethodCallProcessing();
113 114
114 // MessageReceiver implementation: 115 // MessageReceiver implementation:
115 bool Accept(Message* message) override; 116 bool Accept(Message* message) override;
116 117
117 MessagePipeHandle handle() const { 118 MessagePipeHandle handle() const {
118 DCHECK(thread_checker_.CalledOnValidThread()); 119 DCHECK(thread_checker_.CalledOnValidThread());
119 return message_pipe_.get(); 120 return message_pipe_.get();
120 } 121 }
121 122
122 // Requests to register |message_pipe_| with SyncHandleWatcher whenever this 123 // Allows |message_pipe_| be watched while others perform sync handle watching
123 // instance is expecting incoming messages. 124 // on the same thread. Please see comments of
124 // 125 // SyncHandleWatcher::AllowWokenUpBySyncWatchOnSameThread().
125 // Please note that UnregisterSyncHandleWatch() needs to be called as many 126 void AllowWokenUpBySyncWatchOnSameThread();
126 // times as successful RegisterSyncHandleWatch() calls in order to cancel the
127 // effect.
128 bool RegisterSyncHandleWatch();
129 void UnregisterSyncHandleWatch();
130 127
131 // Watches all handles registered with SyncHandleWatcher on the same thread. 128 // Watches |message_pipe_| (as well as other handles registered to be watched
132 // The method returns true when |*should_stop| is set to true; returns false 129 // together) synchronously.
133 // when any failure occurs during the watch, including |message_pipe_| is 130 // This method:
134 // closed. 131 // - returns true when |should_stop| is set to true;
135 bool RunSyncHandleWatch(const bool* should_stop); 132 // - return false when any error occurs, including |message_pipe_| being
133 // closed.
134 bool SyncWatch(const bool* should_stop);
136 135
137 // Whether currently the control flow is inside the sync handle watcher 136 // Whether currently the control flow is inside the sync handle watcher
138 // callback. 137 // callback.
139 bool during_sync_handle_watcher_callback() const { 138 bool during_sync_handle_watcher_callback() const {
140 return sync_handle_watcher_callback_count_ > 0; 139 return sync_handle_watcher_callback_count_ > 0;
141 } 140 }
142 141
143 private: 142 private:
144 // Callback of mojo::Watcher. 143 // Callback of mojo::Watcher.
145 void OnWatcherHandleReady(MojoResult result); 144 void OnWatcherHandleReady(MojoResult result);
(...skipping 11 matching lines...) Expand all
157 156
158 // If |force_pipe_reset| is true, this method replaces the existing 157 // If |force_pipe_reset| is true, this method replaces the existing
159 // |message_pipe_| with a dummy message pipe handle (whose peer is closed). 158 // |message_pipe_| with a dummy message pipe handle (whose peer is closed).
160 // If |force_async_handler| is true, |connection_error_handler_| is called 159 // If |force_async_handler| is true, |connection_error_handler_| is called
161 // asynchronously. 160 // asynchronously.
162 void HandleError(bool force_pipe_reset, bool force_async_handler); 161 void HandleError(bool force_pipe_reset, bool force_async_handler);
163 162
164 // Cancels any calls made to |waiter_|. 163 // Cancels any calls made to |waiter_|.
165 void CancelWait(); 164 void CancelWait();
166 165
166 void EnsureSyncWatcherExists();
167
167 Closure connection_error_handler_; 168 Closure connection_error_handler_;
168 169
169 ScopedMessagePipeHandle message_pipe_; 170 ScopedMessagePipeHandle message_pipe_;
170 MessageReceiver* incoming_receiver_; 171 MessageReceiver* incoming_receiver_;
171 172
172 Watcher handle_watcher_; 173 Watcher handle_watcher_;
173 174
174 bool error_; 175 bool error_;
175 bool drop_writes_; 176 bool drop_writes_;
176 bool enforce_errors_from_incoming_receiver_; 177 bool enforce_errors_from_incoming_receiver_;
177 178
178 bool paused_; 179 bool paused_;
179 180
180 // If sending messages is allowed from multiple threads, |lock_| is used to 181 // If sending messages is allowed from multiple threads, |lock_| is used to
181 // protect modifications to |message_pipe_| and |drop_writes_|. 182 // protect modifications to |message_pipe_| and |drop_writes_|.
182 scoped_ptr<base::Lock> lock_; 183 scoped_ptr<base::Lock> lock_;
183 184
184 // If non-zero, |message_pipe_| should be registered with SyncHandleWatcher. 185 scoped_ptr<SyncHandleWatcher> sync_watcher_;
185 size_t register_sync_handle_watch_count_; 186 bool allow_woken_up_by_others_;
186 // Whether |message_pipe_| has been registered with SyncHandleWatcher.
187 bool registered_with_sync_handle_watcher_;
188 // If non-zero, currently the control flow is inside the sync handle watcher 187 // If non-zero, currently the control flow is inside the sync handle watcher
189 // callback. 188 // callback.
190 size_t sync_handle_watcher_callback_count_; 189 size_t sync_handle_watcher_callback_count_;
191 scoped_refptr<base::RefCountedData<bool>> should_stop_sync_handle_watch_;
192 190
193 base::ThreadChecker thread_checker_; 191 base::ThreadChecker thread_checker_;
194 192
195 // Create a single weak ptr and use it everywhere, to avoid the malloc/free 193 // Create a single weak ptr and use it everywhere, to avoid the malloc/free
196 // cost of creating a new weak ptr whenever it is needed. 194 // cost of creating a new weak ptr whenever it is needed.
197 base::WeakPtr<Connector> weak_self_; 195 base::WeakPtr<Connector> weak_self_;
198 base::WeakPtrFactory<Connector> weak_factory_; 196 base::WeakPtrFactory<Connector> weak_factory_;
199 197
200 DISALLOW_COPY_AND_ASSIGN(Connector); 198 DISALLOW_COPY_AND_ASSIGN(Connector);
201 }; 199 };
202 200
203 } // namespace internal 201 } // namespace internal
204 } // namespace mojo 202 } // namespace mojo
205 203
206 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 204 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/connector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698