OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/edk/system/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 DCHECK(!delegate_); | 195 DCHECK(!delegate_); |
196 delegate_ = delegate; | 196 delegate_ = delegate; |
197 | 197 |
198 if (read_buffer_->num_valid_bytes_ || | 198 if (read_buffer_->num_valid_bytes_ || |
199 !write_buffer_->message_queue_.IsEmpty()) { | 199 !write_buffer_->message_queue_.IsEmpty()) { |
200 LazyInitialize(); | 200 LazyInitialize(); |
201 } | 201 } |
202 } | 202 } |
203 | 203 |
204 void RawChannel::EnsureLazyInitialized() { | 204 void RawChannel::EnsureLazyInitialized() { |
205 if (!initialized_) { | 205 { |
206 internal::g_io_thread_task_runner->PostTask( | 206 base::AutoLock locker(write_lock_); |
207 FROM_HERE, | 207 if (initialized_) |
208 base::Bind(&RawChannel::LockAndCallLazyInitialize, | 208 return; |
209 weak_ptr_factory_.GetWeakPtr())); | |
210 } | 209 } |
| 210 |
| 211 internal::g_io_thread_task_runner->PostTask( |
| 212 FROM_HERE, |
| 213 base::Bind(&RawChannel::LockAndCallLazyInitialize, |
| 214 weak_ptr_factory_.GetWeakPtr())); |
211 } | 215 } |
212 | 216 |
213 void RawChannel::LockAndCallLazyInitialize() { | 217 void RawChannel::LockAndCallLazyInitialize() { |
214 base::AutoLock read_locker(read_lock_); | 218 base::AutoLock read_locker(read_lock_); |
215 base::AutoLock locker(write_lock_); | 219 base::AutoLock locker(write_lock_); |
216 LazyInitialize(); | 220 LazyInitialize(); |
217 } | 221 } |
218 | 222 |
219 void RawChannel::LazyInitialize() { | 223 void RawChannel::LazyInitialize() { |
220 read_lock_.AssertAcquired(); | 224 read_lock_.AssertAcquired(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 | 283 |
280 // Normally, we want to flush any pending writes before shutting down. This | 284 // Normally, we want to flush any pending writes before shutting down. This |
281 // doesn't apply when 1) we don't have a handle (for obvious reasons), | 285 // doesn't apply when 1) we don't have a handle (for obvious reasons), |
282 // 2) we have a read or write error before (doesn't matter which), or 3) when | 286 // 2) we have a read or write error before (doesn't matter which), or 3) when |
283 // there are no pending messages to be written. | 287 // there are no pending messages to be written. |
284 if (!IsHandleValid() || error_occurred_ || empty) { | 288 if (!IsHandleValid() || error_occurred_ || empty) { |
285 { | 289 { |
286 base::AutoLock read_locker(read_lock_); | 290 base::AutoLock read_locker(read_lock_); |
287 base::AutoLock locker(write_lock_); | 291 base::AutoLock locker(write_lock_); |
288 OnShutdownNoLock(std::move(read_buffer_), std::move(write_buffer_)); | 292 OnShutdownNoLock(std::move(read_buffer_), std::move(write_buffer_)); |
| 293 if (initialized_) |
| 294 base::MessageLoop::current()->RemoveDestructionObserver(this); |
289 } | 295 } |
290 | 296 |
291 if (initialized_) { | |
292 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
293 } | |
294 delete this; | 297 delete this; |
295 return; | 298 return; |
296 } | 299 } |
297 | 300 |
298 base::AutoLock read_locker(read_lock_); | 301 base::AutoLock read_locker(read_lock_); |
299 base::AutoLock locker(write_lock_); | 302 base::AutoLock locker(write_lock_); |
300 DCHECK(read_buffer_->IsEmpty()) << | 303 DCHECK(read_buffer_->IsEmpty()) << |
301 "RawChannel::Shutdown called but there is pending data to be read"; | 304 "RawChannel::Shutdown called but there is pending data to be read"; |
302 | 305 |
303 write_stopped_ = true; | 306 write_stopped_ = true; |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 OnReadCompletedNoLock(io_result, bytes_read); | 724 OnReadCompletedNoLock(io_result, bytes_read); |
722 } | 725 } |
723 | 726 |
724 void RawChannel::WillDestroyCurrentMessageLoop() { | 727 void RawChannel::WillDestroyCurrentMessageLoop() { |
725 base::AutoLock locker(read_lock_); | 728 base::AutoLock locker(read_lock_); |
726 OnReadCompletedNoLock(IO_FAILED_SHUTDOWN, 0); | 729 OnReadCompletedNoLock(IO_FAILED_SHUTDOWN, 0); |
727 } | 730 } |
728 | 731 |
729 } // namespace edk | 732 } // namespace edk |
730 } // namespace mojo | 733 } // namespace mojo |
OLD | NEW |