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

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

Issue 2345013002: Mojo C++ bindings: remove the lock in MultiplexRouter if it only serves a single interface. (Closed)
Patch Set: . Created 4 years, 3 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
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 #include "mojo/public/cpp/bindings/connector.h" 5 #include "mojo/public/cpp/bindings/connector.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
15 #include "mojo/public/cpp/bindings/lib/may_auto_lock.h"
15 #include "mojo/public/cpp/bindings/sync_handle_watcher.h" 16 #include "mojo/public/cpp/bindings/sync_handle_watcher.h"
16 17
17 namespace mojo { 18 namespace mojo {
18 19
19 namespace {
20
21 // Similar to base::AutoLock, except that it does nothing if |lock| passed into
22 // the constructor is null.
23 class MayAutoLock {
24 public:
25 explicit MayAutoLock(base::Lock* lock) : lock_(lock) {
26 if (lock_)
27 lock_->Acquire();
28 }
29
30 ~MayAutoLock() {
31 if (lock_) {
32 lock_->AssertAcquired();
33 lock_->Release();
34 }
35 }
36
37 private:
38 base::Lock* lock_;
39 DISALLOW_COPY_AND_ASSIGN(MayAutoLock);
40 };
41
42 } // namespace
43
44 // ----------------------------------------------------------------------------
45
46 Connector::Connector(ScopedMessagePipeHandle message_pipe, 20 Connector::Connector(ScopedMessagePipeHandle message_pipe,
47 ConnectorConfig config, 21 ConnectorConfig config,
48 scoped_refptr<base::SingleThreadTaskRunner> runner) 22 scoped_refptr<base::SingleThreadTaskRunner> runner)
49 : message_pipe_(std::move(message_pipe)), 23 : message_pipe_(std::move(message_pipe)),
50 task_runner_(std::move(runner)), 24 task_runner_(std::move(runner)),
51 handle_watcher_(task_runner_), 25 handle_watcher_(task_runner_),
52 lock_(config == MULTI_THREADED_SEND ? new base::Lock : nullptr), 26 lock_(config == MULTI_THREADED_SEND ? new base::Lock : nullptr),
53 weak_factory_(this) { 27 weak_factory_(this) {
54 weak_self_ = weak_factory_.GetWeakPtr(); 28 weak_self_ = weak_factory_.GetWeakPtr();
55 // Even though we don't have an incoming receiver, we still want to monitor 29 // Even though we don't have an incoming receiver, we still want to monitor
(...skipping 10 matching lines...) Expand all
66 } 40 }
67 41
68 DCHECK(thread_checker_.CalledOnValidThread()); 42 DCHECK(thread_checker_.CalledOnValidThread());
69 CancelWait(); 43 CancelWait();
70 } 44 }
71 45
72 void Connector::CloseMessagePipe() { 46 void Connector::CloseMessagePipe() {
73 DCHECK(thread_checker_.CalledOnValidThread()); 47 DCHECK(thread_checker_.CalledOnValidThread());
74 48
75 CancelWait(); 49 CancelWait();
76 MayAutoLock locker(lock_.get()); 50 internal::MayAutoLock locker(lock_.get());
77 message_pipe_.reset(); 51 message_pipe_.reset();
78 52
79 base::AutoLock lock(connected_lock_); 53 base::AutoLock lock(connected_lock_);
80 connected_ = false; 54 connected_ = false;
81 } 55 }
82 56
83 ScopedMessagePipeHandle Connector::PassMessagePipe() { 57 ScopedMessagePipeHandle Connector::PassMessagePipe() {
84 DCHECK(thread_checker_.CalledOnValidThread()); 58 DCHECK(thread_checker_.CalledOnValidThread());
85 59
86 CancelWait(); 60 CancelWait();
87 MayAutoLock locker(lock_.get()); 61 internal::MayAutoLock locker(lock_.get());
88 ScopedMessagePipeHandle message_pipe = std::move(message_pipe_); 62 ScopedMessagePipeHandle message_pipe = std::move(message_pipe_);
89 63
90 base::AutoLock lock(connected_lock_); 64 base::AutoLock lock(connected_lock_);
91 connected_ = false; 65 connected_ = false;
92 return message_pipe; 66 return message_pipe;
93 } 67 }
94 68
95 void Connector::RaiseError() { 69 void Connector::RaiseError() {
96 DCHECK(thread_checker_.CalledOnValidThread()); 70 DCHECK(thread_checker_.CalledOnValidThread());
97 71
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 116
143 bool Connector::Accept(Message* message) { 117 bool Connector::Accept(Message* message) {
144 DCHECK(lock_ || thread_checker_.CalledOnValidThread()); 118 DCHECK(lock_ || thread_checker_.CalledOnValidThread());
145 119
146 // It shouldn't hurt even if |error_| may be changed by a different thread at 120 // It shouldn't hurt even if |error_| may be changed by a different thread at
147 // the same time. The outcome is that we may write into |message_pipe_| after 121 // the same time. The outcome is that we may write into |message_pipe_| after
148 // encountering an error, which should be fine. 122 // encountering an error, which should be fine.
149 if (error_) 123 if (error_)
150 return false; 124 return false;
151 125
152 MayAutoLock locker(lock_.get()); 126 internal::MayAutoLock locker(lock_.get());
153 127
154 if (!message_pipe_.is_valid() || drop_writes_) 128 if (!message_pipe_.is_valid() || drop_writes_)
155 return true; 129 return true;
156 130
157 MojoResult rv = 131 MojoResult rv =
158 WriteMessageNew(message_pipe_.get(), message->TakeMojoMessage(), 132 WriteMessageNew(message_pipe_.get(), message->TakeMojoMessage(),
159 MOJO_WRITE_MESSAGE_FLAG_NONE); 133 MOJO_WRITE_MESSAGE_FLAG_NONE);
160 134
161 switch (rv) { 135 switch (rv) {
162 case MOJO_RESULT_OK: 136 case MOJO_RESULT_OK:
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 // receiving messages. We need to wait until the user starts receiving 298 // receiving messages. We need to wait until the user starts receiving
325 // messages again. 299 // messages again.
326 force_async_handler = true; 300 force_async_handler = true;
327 } 301 }
328 302
329 if (!force_pipe_reset && force_async_handler) 303 if (!force_pipe_reset && force_async_handler)
330 force_pipe_reset = true; 304 force_pipe_reset = true;
331 305
332 if (force_pipe_reset) { 306 if (force_pipe_reset) {
333 CancelWait(); 307 CancelWait();
334 MayAutoLock locker(lock_.get()); 308 internal::MayAutoLock locker(lock_.get());
335 message_pipe_.reset(); 309 message_pipe_.reset();
336 MessagePipe dummy_pipe; 310 MessagePipe dummy_pipe;
337 message_pipe_ = std::move(dummy_pipe.handle0); 311 message_pipe_ = std::move(dummy_pipe.handle0);
338 } else { 312 } else {
339 CancelWait(); 313 CancelWait();
340 } 314 }
341 315
342 if (force_async_handler) { 316 if (force_async_handler) {
343 if (!paused_) 317 if (!paused_)
344 WaitToReadMore(); 318 WaitToReadMore();
345 } else { 319 } else {
346 error_ = true; 320 error_ = true;
347 if (!connection_error_handler_.is_null()) 321 if (!connection_error_handler_.is_null())
348 connection_error_handler_.Run(); 322 connection_error_handler_.Run();
349 } 323 }
350 } 324 }
351 325
352 void Connector::EnsureSyncWatcherExists() { 326 void Connector::EnsureSyncWatcherExists() {
353 if (sync_watcher_) 327 if (sync_watcher_)
354 return; 328 return;
355 sync_watcher_.reset(new SyncHandleWatcher( 329 sync_watcher_.reset(new SyncHandleWatcher(
356 message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE, 330 message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE,
357 base::Bind(&Connector::OnSyncHandleWatcherHandleReady, 331 base::Bind(&Connector::OnSyncHandleWatcherHandleReady,
358 base::Unretained(this)))); 332 base::Unretained(this))));
359 } 333 }
360 334
361 } // namespace mojo 335 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/binding_state.cc ('k') | mojo/public/cpp/bindings/lib/interface_ptr_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698