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

Unified Diff: mojo/system/local_data_pipe.cc

Issue 98013005: Mojo: DataPipe: Implement "may discard" mode for simple writes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: mojo/system/local_data_pipe.cc
diff --git a/mojo/system/local_data_pipe.cc b/mojo/system/local_data_pipe.cc
index 936c68c4b80a0995074ff82c7f50c5351cbc84e8..81bc7b557aab31c970bf14149f7b16e9ce1b8866 100644
--- a/mojo/system/local_data_pipe.cc
+++ b/mojo/system/local_data_pipe.cc
@@ -51,13 +51,32 @@ MojoResult LocalDataPipe::ProducerWriteDataImplNoLock(const void* elements,
DCHECK_EQ(*num_bytes % element_num_bytes(), 0u);
DCHECK_GT(*num_bytes, 0u);
- // TODO(vtl): Consider this return value.
- if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_)
- return MOJO_RESULT_OUT_OF_RANGE;
-
- size_t num_bytes_to_write =
- std::min(static_cast<size_t>(*num_bytes),
- capacity_num_bytes() - current_num_bytes_);
+ size_t num_bytes_to_write = 0;
+ if (may_discard()) {
+ if (all_or_none && *num_bytes > capacity_num_bytes())
+ return MOJO_RESULT_OUT_OF_RANGE;
+
+ num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes),
+ capacity_num_bytes());
+ if (num_bytes_to_write > capacity_num_bytes() - current_num_bytes_) {
+ // Discard as much as needed (discard oldest first).
+ size_t num_bytes_to_discard =
+ num_bytes_to_write - (capacity_num_bytes() - current_num_bytes_);
+ start_index_ += num_bytes_to_discard;
+ start_index_ %= capacity_num_bytes();
+ current_num_bytes_ -= num_bytes_to_discard;
+ }
+ } else {
+ // TODO(vtl): Consider this return value. Maybe "out of range" when greater
darin (slow to review) 2014/01/06 21:29:28 hmm, your approach makes sense to me.
+ // than capacity, and "should wait" for the other case?
+ if (all_or_none && *num_bytes > capacity_num_bytes() - current_num_bytes_) {
+ return (*num_bytes > capacity_num_bytes()) ? MOJO_RESULT_OUT_OF_RANGE :
+ MOJO_RESULT_SHOULD_WAIT;
+ }
+
+ num_bytes_to_write = std::min(static_cast<size_t>(*num_bytes),
+ capacity_num_bytes() - current_num_bytes_);
+ }
if (num_bytes_to_write == 0)
return MOJO_RESULT_SHOULD_WAIT;

Powered by Google App Engine
This is Rietveld 408576698