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

Side by Side Diff: mojo/common/data_pipe_utils.cc

Issue 2744943002: Mojo: Move waiting APIs to public library (Closed)
Patch Set: . Created 3 years, 9 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
« no previous file with comments | « ipc/ipc_channel_mojo_unittest.cc ('k') | mojo/edk/embedder/configuration.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/common/data_pipe_utils.h" 5 #include "mojo/common/data_pipe_utils.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "mojo/public/cpp/system/wait.h"
10 11
11 namespace mojo { 12 namespace mojo {
12 namespace common { 13 namespace common {
13 namespace { 14 namespace {
14 15
15 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source, 16 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source,
16 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { 17 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
17 for (;;) { 18 for (;;) {
18 const void* buffer; 19 const void* buffer;
19 uint32_t num_bytes; 20 uint32_t num_bytes;
20 MojoResult result = BeginReadDataRaw( 21 MojoResult result = BeginReadDataRaw(
21 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); 22 source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
22 if (result == MOJO_RESULT_OK) { 23 if (result == MOJO_RESULT_OK) {
23 size_t bytes_written = write_bytes.Run(buffer, num_bytes); 24 size_t bytes_written = write_bytes.Run(buffer, num_bytes);
24 result = EndReadDataRaw(source.get(), num_bytes); 25 result = EndReadDataRaw(source.get(), num_bytes);
25 if (bytes_written < num_bytes || result != MOJO_RESULT_OK) 26 if (bytes_written < num_bytes || result != MOJO_RESULT_OK)
26 return false; 27 return false;
27 } else if (result == MOJO_RESULT_SHOULD_WAIT) { 28 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
28 result = Wait(source.get(), 29 result = Wait(source.get(), MOJO_HANDLE_SIGNAL_READABLE);
29 MOJO_HANDLE_SIGNAL_READABLE,
30 MOJO_DEADLINE_INDEFINITE,
31 nullptr);
32 if (result != MOJO_RESULT_OK) { 30 if (result != MOJO_RESULT_OK) {
33 // If the producer handle was closed, then treat as EOF. 31 // If the producer handle was closed, then treat as EOF.
34 return result == MOJO_RESULT_FAILED_PRECONDITION; 32 return result == MOJO_RESULT_FAILED_PRECONDITION;
35 } 33 }
36 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) { 34 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) {
37 // If the producer handle was closed, then treat as EOF. 35 // If the producer handle was closed, then treat as EOF.
38 return true; 36 return true;
39 } else { 37 } else {
40 // Some other error occurred. 38 // Some other error occurred.
41 break; 39 break;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 if (result == MOJO_RESULT_OK) { 73 if (result == MOJO_RESULT_OK) {
76 char* char_buffer = static_cast<char*>(buffer); 74 char* char_buffer = static_cast<char*>(buffer);
77 uint32_t byte_index = 0; 75 uint32_t byte_index = 0;
78 while (it != source.end() && byte_index < buffer_num_bytes) { 76 while (it != source.end() && byte_index < buffer_num_bytes) {
79 char_buffer[byte_index++] = *it++; 77 char_buffer[byte_index++] = *it++;
80 } 78 }
81 EndWriteDataRaw(destination.get(), byte_index); 79 EndWriteDataRaw(destination.get(), byte_index);
82 if (it == source.end()) 80 if (it == source.end())
83 return true; 81 return true;
84 } else if (result == MOJO_RESULT_SHOULD_WAIT) { 82 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
85 result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE, 83 result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE);
86 MOJO_DEADLINE_INDEFINITE, nullptr);
87 if (result != MOJO_RESULT_OK) { 84 if (result != MOJO_RESULT_OK) {
88 // If the consumer handle was closed, then treat as EOF. 85 // If the consumer handle was closed, then treat as EOF.
89 return result == MOJO_RESULT_FAILED_PRECONDITION; 86 return result == MOJO_RESULT_FAILED_PRECONDITION;
90 } 87 }
91 } else { 88 } else {
92 // If the consumer handle was closed, then treat as EOF. 89 // If the consumer handle was closed, then treat as EOF.
93 return result == MOJO_RESULT_FAILED_PRECONDITION; 90 return result == MOJO_RESULT_FAILED_PRECONDITION;
94 } 91 }
95 } 92 }
96 } 93 }
97 94
98 } // namespace common 95 } // namespace common
99 } // namespace mojo 96 } // namespace mojo
OLDNEW
« no previous file with comments | « ipc/ipc_channel_mojo_unittest.cc ('k') | mojo/edk/embedder/configuration.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698