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/edk/system/data_pipe.h" | 5 #include "mojo/edk/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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 return MOJO_RESULT_INVALID_ARGUMENT; | 45 return MOJO_RESULT_INVALID_ARGUMENT; |
46 | 46 |
47 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, flags, reader)) | 47 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, flags, reader)) |
48 return MOJO_RESULT_OK; | 48 return MOJO_RESULT_OK; |
49 if ((reader.options().flags & ~kKnownFlags)) | 49 if ((reader.options().flags & ~kKnownFlags)) |
50 return MOJO_RESULT_UNIMPLEMENTED; | 50 return MOJO_RESULT_UNIMPLEMENTED; |
51 out_options->flags = reader.options().flags; | 51 out_options->flags = reader.options().flags; |
52 | 52 |
53 // Checks for fields beyond |flags|: | 53 // Checks for fields beyond |flags|: |
54 | 54 |
55 if (!OPTIONS_STRUCT_HAS_MEMBER( | 55 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, element_num_bytes, |
56 MojoCreateDataPipeOptions, element_num_bytes, reader)) | 56 reader)) |
57 return MOJO_RESULT_OK; | 57 return MOJO_RESULT_OK; |
58 if (reader.options().element_num_bytes == 0) | 58 if (reader.options().element_num_bytes == 0) |
59 return MOJO_RESULT_INVALID_ARGUMENT; | 59 return MOJO_RESULT_INVALID_ARGUMENT; |
60 out_options->element_num_bytes = reader.options().element_num_bytes; | 60 out_options->element_num_bytes = reader.options().element_num_bytes; |
61 | 61 |
62 if (!OPTIONS_STRUCT_HAS_MEMBER( | 62 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, capacity_num_bytes, |
63 MojoCreateDataPipeOptions, capacity_num_bytes, reader) || | 63 reader) || |
64 reader.options().capacity_num_bytes == 0) { | 64 reader.options().capacity_num_bytes == 0) { |
65 // Round the default capacity down to a multiple of the element size (but at | 65 // Round the default capacity down to a multiple of the element size (but at |
66 // least one element). | 66 // least one element). |
67 size_t default_data_pipe_capacity_bytes = | 67 size_t default_data_pipe_capacity_bytes = |
68 GetConfiguration().default_data_pipe_capacity_bytes; | 68 GetConfiguration().default_data_pipe_capacity_bytes; |
69 out_options->capacity_num_bytes = | 69 out_options->capacity_num_bytes = |
70 std::max(static_cast<uint32_t>(default_data_pipe_capacity_bytes - | 70 std::max(static_cast<uint32_t>(default_data_pipe_capacity_bytes - |
71 (default_data_pipe_capacity_bytes % | 71 (default_data_pipe_capacity_bytes % |
72 out_options->element_num_bytes)), | 72 out_options->element_num_bytes)), |
73 out_options->element_num_bytes); | 73 out_options->element_num_bytes); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 if (!consumer_open_no_lock()) | 148 if (!consumer_open_no_lock()) |
149 return MOJO_RESULT_FAILED_PRECONDITION; | 149 return MOJO_RESULT_FAILED_PRECONDITION; |
150 | 150 |
151 uint32_t min_num_bytes_to_write = 0; | 151 uint32_t min_num_bytes_to_write = 0; |
152 if (all_or_none) { | 152 if (all_or_none) { |
153 min_num_bytes_to_write = buffer_num_bytes.Get(); | 153 min_num_bytes_to_write = buffer_num_bytes.Get(); |
154 if (min_num_bytes_to_write % element_num_bytes_ != 0) | 154 if (min_num_bytes_to_write % element_num_bytes_ != 0) |
155 return MOJO_RESULT_INVALID_ARGUMENT; | 155 return MOJO_RESULT_INVALID_ARGUMENT; |
156 } | 156 } |
157 | 157 |
158 MojoResult rv = ProducerBeginWriteDataImplNoLock( | 158 MojoResult rv = ProducerBeginWriteDataImplNoLock(buffer, buffer_num_bytes, |
159 buffer, buffer_num_bytes, min_num_bytes_to_write); | 159 min_num_bytes_to_write); |
160 if (rv != MOJO_RESULT_OK) | 160 if (rv != MOJO_RESULT_OK) |
161 return rv; | 161 return rv; |
162 // Note: No need to awake producer waiters, even though we're going from | 162 // Note: No need to awake producer waiters, even though we're going from |
163 // writable to non-writable (since you can't wait on non-writability). | 163 // writable to non-writable (since you can't wait on non-writability). |
164 // Similarly, though this may have discarded data (in "may discard" mode), | 164 // Similarly, though this may have discarded data (in "may discard" mode), |
165 // making it non-readable, there's still no need to awake consumer waiters. | 165 // making it non-readable, there's still no need to awake consumer waiters. |
166 DCHECK(producer_in_two_phase_write_no_lock()); | 166 DCHECK(producer_in_two_phase_write_no_lock()); |
167 return MOJO_RESULT_OK; | 167 return MOJO_RESULT_OK; |
168 } | 168 } |
169 | 169 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 if (consumer_in_two_phase_read_no_lock()) | 345 if (consumer_in_two_phase_read_no_lock()) |
346 return MOJO_RESULT_BUSY; | 346 return MOJO_RESULT_BUSY; |
347 | 347 |
348 uint32_t min_num_bytes_to_read = 0; | 348 uint32_t min_num_bytes_to_read = 0; |
349 if (all_or_none) { | 349 if (all_or_none) { |
350 min_num_bytes_to_read = buffer_num_bytes.Get(); | 350 min_num_bytes_to_read = buffer_num_bytes.Get(); |
351 if (min_num_bytes_to_read % element_num_bytes_ != 0) | 351 if (min_num_bytes_to_read % element_num_bytes_ != 0) |
352 return MOJO_RESULT_INVALID_ARGUMENT; | 352 return MOJO_RESULT_INVALID_ARGUMENT; |
353 } | 353 } |
354 | 354 |
355 MojoResult rv = ConsumerBeginReadDataImplNoLock( | 355 MojoResult rv = ConsumerBeginReadDataImplNoLock(buffer, buffer_num_bytes, |
356 buffer, buffer_num_bytes, min_num_bytes_to_read); | 356 min_num_bytes_to_read); |
357 if (rv != MOJO_RESULT_OK) | 357 if (rv != MOJO_RESULT_OK) |
358 return rv; | 358 return rv; |
359 DCHECK(consumer_in_two_phase_read_no_lock()); | 359 DCHECK(consumer_in_two_phase_read_no_lock()); |
360 return MOJO_RESULT_OK; | 360 return MOJO_RESULT_OK; |
361 } | 361 } |
362 | 362 |
363 MojoResult DataPipe::ConsumerEndReadData(uint32_t num_bytes_read) { | 363 MojoResult DataPipe::ConsumerEndReadData(uint32_t num_bytes_read) { |
364 base::AutoLock locker(lock_); | 364 base::AutoLock locker(lock_); |
365 DCHECK(has_local_consumer_no_lock()); | 365 DCHECK(has_local_consumer_no_lock()); |
366 | 366 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 void DataPipe::AwakeConsumerWaitersForStateChangeNoLock( | 472 void DataPipe::AwakeConsumerWaitersForStateChangeNoLock( |
473 const HandleSignalsState& new_consumer_state) { | 473 const HandleSignalsState& new_consumer_state) { |
474 lock_.AssertAcquired(); | 474 lock_.AssertAcquired(); |
475 if (!has_local_consumer_no_lock()) | 475 if (!has_local_consumer_no_lock()) |
476 return; | 476 return; |
477 consumer_waiter_list_->AwakeWaitersForStateChange(new_consumer_state); | 477 consumer_waiter_list_->AwakeWaitersForStateChange(new_consumer_state); |
478 } | 478 } |
479 | 479 |
480 } // namespace system | 480 } // namespace system |
481 } // namespace mojo | 481 } // namespace mojo |
OLD | NEW |