OLD | NEW |
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/system/data_pipe.h" | 5 #include "mojo/system/data_pipe.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 producer_waiter_list_.reset(); | 87 producer_waiter_list_.reset(); |
88 // Not a bug, except possibly in "user" code. | 88 // Not a bug, except possibly in "user" code. |
89 DVLOG_IF(2, producer_in_two_phase_write_no_lock()) | 89 DVLOG_IF(2, producer_in_two_phase_write_no_lock()) |
90 << "Producer closed with active two-phase write"; | 90 << "Producer closed with active two-phase write"; |
91 producer_two_phase_max_num_bytes_written_ = 0; | 91 producer_two_phase_max_num_bytes_written_ = 0; |
92 ProducerCloseImplNoLock(); | 92 ProducerCloseImplNoLock(); |
93 AwakeConsumerWaitersForStateChangeNoLock( | 93 AwakeConsumerWaitersForStateChangeNoLock( |
94 ConsumerGetHandleSignalsStateNoLock()); | 94 ConsumerGetHandleSignalsStateNoLock()); |
95 } | 95 } |
96 | 96 |
97 MojoResult DataPipe::ProducerWriteData(const void* elements, | 97 MojoResult DataPipe::ProducerWriteData(UserPointer<const void> elements, |
98 uint32_t* num_bytes, | 98 UserPointer<uint32_t> num_bytes, |
99 bool all_or_none) { | 99 bool all_or_none) { |
100 base::AutoLock locker(lock_); | 100 base::AutoLock locker(lock_); |
101 DCHECK(has_local_producer_no_lock()); | 101 DCHECK(has_local_producer_no_lock()); |
102 | 102 |
103 if (producer_in_two_phase_write_no_lock()) | 103 if (producer_in_two_phase_write_no_lock()) |
104 return MOJO_RESULT_BUSY; | 104 return MOJO_RESULT_BUSY; |
105 if (!consumer_open_no_lock()) | 105 if (!consumer_open_no_lock()) |
106 return MOJO_RESULT_FAILED_PRECONDITION; | 106 return MOJO_RESULT_FAILED_PRECONDITION; |
107 | 107 |
108 // Returning "busy" takes priority over "invalid argument". | 108 // Returning "busy" takes priority over "invalid argument". |
109 if (*num_bytes % element_num_bytes_ != 0) | 109 uint32_t max_num_bytes_to_write = num_bytes.Get(); |
| 110 if (max_num_bytes_to_write % element_num_bytes_ != 0) |
110 return MOJO_RESULT_INVALID_ARGUMENT; | 111 return MOJO_RESULT_INVALID_ARGUMENT; |
111 | 112 |
112 if (*num_bytes == 0) | 113 if (max_num_bytes_to_write == 0) |
113 return MOJO_RESULT_OK; // Nothing to do. | 114 return MOJO_RESULT_OK; // Nothing to do. |
114 | 115 |
| 116 uint32_t min_num_bytes_to_write = all_or_none ? max_num_bytes_to_write : 0; |
| 117 |
115 HandleSignalsState old_consumer_state = ConsumerGetHandleSignalsStateNoLock(); | 118 HandleSignalsState old_consumer_state = ConsumerGetHandleSignalsStateNoLock(); |
116 MojoResult rv = ProducerWriteDataImplNoLock(elements, num_bytes, all_or_none); | 119 MojoResult rv = ProducerWriteDataImplNoLock(elements, num_bytes, |
| 120 max_num_bytes_to_write, |
| 121 min_num_bytes_to_write); |
117 HandleSignalsState new_consumer_state = ConsumerGetHandleSignalsStateNoLock(); | 122 HandleSignalsState new_consumer_state = ConsumerGetHandleSignalsStateNoLock(); |
118 if (!new_consumer_state.equals(old_consumer_state)) | 123 if (!new_consumer_state.equals(old_consumer_state)) |
119 AwakeConsumerWaitersForStateChangeNoLock(new_consumer_state); | 124 AwakeConsumerWaitersForStateChangeNoLock(new_consumer_state); |
120 return rv; | 125 return rv; |
121 } | 126 } |
122 | 127 |
123 MojoResult DataPipe::ProducerBeginWriteData( | 128 MojoResult DataPipe::ProducerBeginWriteData( |
124 UserPointer<void*> buffer, | 129 UserPointer<void*> buffer, |
125 UserPointer<uint32_t> buffer_num_bytes, | 130 UserPointer<uint32_t> buffer_num_bytes, |
126 bool all_or_none) { | 131 bool all_or_none) { |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 void DataPipe::AwakeConsumerWaitersForStateChangeNoLock( | 420 void DataPipe::AwakeConsumerWaitersForStateChangeNoLock( |
416 const HandleSignalsState& new_consumer_state) { | 421 const HandleSignalsState& new_consumer_state) { |
417 lock_.AssertAcquired(); | 422 lock_.AssertAcquired(); |
418 if (!has_local_consumer_no_lock()) | 423 if (!has_local_consumer_no_lock()) |
419 return; | 424 return; |
420 consumer_waiter_list_->AwakeWaitersForStateChange(new_consumer_state); | 425 consumer_waiter_list_->AwakeWaitersForStateChange(new_consumer_state); |
421 } | 426 } |
422 | 427 |
423 } // namespace system | 428 } // namespace system |
424 } // namespace mojo | 429 } // namespace mojo |
OLD | NEW |