OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // This file contains types/constants and functions specific to data pipes. | |
6 // | |
7 // Note: This header should be compilable as C. | |
8 | |
9 #ifndef MOJO_PUBLIC_C_INCLUDE_MOJO_SYSTEM_DATA_PIPE_H_ | |
10 #define MOJO_PUBLIC_C_INCLUDE_MOJO_SYSTEM_DATA_PIPE_H_ | |
11 | |
12 #include <mojo/macros.h> | |
13 #include <mojo/result.h> | |
14 #include <mojo/system/handle.h> | |
15 | |
16 // |MojoCreateDataPipeOptions|: Used to specify creation parameters for a data | |
17 // pipe to |MojoCreateDataPipe()|. | |
18 // |uint32_t struct_size|: Set to the size of the |MojoCreateDataPipeOptions| | |
19 // struct. (Used to allow for future extensions.) | |
20 // |MojoCreateDataPipeOptionsFlags flags|: Used to specify different modes of | |
21 // operation. | |
22 // |MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE|: No flags; default mode. | |
23 // |uint32_t element_num_bytes|: The size of an element, in bytes. All | |
24 // transactions and buffers will consist of an integral number of | |
25 // elements. Must be nonzero. | |
26 // |uint32_t capacity_num_bytes|: The capacity of the data pipe, in number of | |
27 // bytes; must be a multiple of |element_num_bytes|. The data pipe will | |
28 // always be able to queue AT LEAST this much data. Set to zero to opt for | |
29 // a system-dependent automatically-calculated capacity (which will always | |
30 // be at least one element). | |
31 | |
32 typedef uint32_t MojoCreateDataPipeOptionsFlags; | |
33 | |
34 #define MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE \ | |
35 ((MojoCreateDataPipeOptionsFlags)0) | |
36 | |
37 MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) == 8, "int64_t has weird alignment"); | |
38 struct MOJO_ALIGNAS(8) MojoCreateDataPipeOptions { | |
39 uint32_t struct_size; | |
40 MojoCreateDataPipeOptionsFlags flags; | |
41 uint32_t element_num_bytes; | |
42 uint32_t capacity_num_bytes; | |
43 }; | |
44 MOJO_STATIC_ASSERT(sizeof(struct MojoCreateDataPipeOptions) == 16, | |
45 "MojoCreateDataPipeOptions has wrong size"); | |
46 | |
47 // |MojoDataPipeProducerOptions|: Used to specify data pipe producer options (to | |
48 // |MojoSetDataPipeProducerOptions()| and from | |
49 // |MojoGetDataPipeProducerOptions()|). | |
50 // |uint32_t struct_size|: Set to the size of the | |
51 // |MojoDataPipeProducerOptions| struct. (Used to allow for future | |
52 // extensions.) | |
53 // |uint32_t write_threshold_num_bytes|: Set to the minimum amount of space | |
54 // required to be available, in number of bytes, for the handle to be | |
55 // signaled with |MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD|; must be a multiple | |
56 // of the data pipe's element size. Set to zero to opt for the default, | |
57 // which is the size of a single element. | |
58 | |
59 struct MOJO_ALIGNAS(8) MojoDataPipeProducerOptions { | |
60 uint32_t struct_size; | |
61 uint32_t write_threshold_num_bytes; | |
62 }; | |
63 MOJO_STATIC_ASSERT(sizeof(struct MojoDataPipeProducerOptions) == 8, | |
64 "MojoDataPipeProducerOptions has wrong size"); | |
65 | |
66 // |MojoWriteDataFlags|: Used to specify different modes to |MojoWriteData()| | |
67 // and |MojoBeginWriteData()|. | |
68 // |MOJO_WRITE_DATA_FLAG_NONE| - No flags; default mode. | |
69 // |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| - Write either all the elements | |
70 // requested or none of them. | |
71 | |
72 typedef uint32_t MojoWriteDataFlags; | |
73 | |
74 #define MOJO_WRITE_DATA_FLAG_NONE ((MojoWriteDataFlags)0) | |
75 #define MOJO_WRITE_DATA_FLAG_ALL_OR_NONE ((MojoWriteDataFlags)1 << 0) | |
76 | |
77 // |MojoDataPipeConsumerOptions|: Used to specify data pipe consumer options (to | |
78 // |MojoSetDataPipeConsumerOptions()| and from | |
79 // |MojoGetDataPipeConsumerOptions()|). | |
80 // |uint32_t struct_size|: Set to the size of the | |
81 // |MojoDataPipeConsumerOptions| struct. (Used to allow for future | |
82 // extensions.) | |
83 // |uint32_t read_threshold_num_bytes|: Set to the minimum number of bytes | |
84 // required to be available for the handle to be signaled with | |
85 // |MOJO_HANDLE_SIGNAL_READ_THRESHOLD|; must be a multiple | |
86 // of the data pipe's element size. Set to zero to opt for the default, | |
87 // which is the size of a single element. Note: If the producer handle is | |
88 // closed with less than this amount of data in the data pipe, the | |
89 // consumer's |MOJO_HANDLE_SIGNAL_READ_THRESHOLD| will be considered | |
90 // unsatisfiable; if there is actually some data remaining in the data | |
91 // pipe, the status of this signal may be changed if this value is | |
92 // modified. | |
93 | |
94 struct MOJO_ALIGNAS(8) MojoDataPipeConsumerOptions { | |
95 uint32_t struct_size; | |
96 uint32_t read_threshold_num_bytes; | |
97 }; | |
98 MOJO_STATIC_ASSERT(sizeof(struct MojoDataPipeConsumerOptions) == 8, | |
99 "MojoDataPipeConsumerOptions has wrong size"); | |
100 | |
101 // |MojoReadDataFlags|: Used to specify different modes to |MojoReadData()| and | |
102 // |MojoBeginReadData()|. | |
103 // |MOJO_READ_DATA_FLAG_NONE| - No flags; default mode. | |
104 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| - Read (or discard) either the requested | |
105 // number of elements or none. | |
106 // |MOJO_READ_DATA_FLAG_DISCARD| - Discard (up to) the requested number of | |
107 // elements. | |
108 // |MOJO_READ_DATA_FLAG_QUERY| - Query the number of elements available to | |
109 // read. For use with |MojoReadData()| only. Mutually exclusive with | |
110 // |MOJO_READ_DATA_FLAG_DISCARD|, and |MOJO_READ_DATA_FLAG_ALL_OR_NONE| | |
111 // is ignored if this flag is set. | |
112 // |MOJO_READ_DATA_FLAG_PEEK| - Read elements without removing them. For use | |
113 // with |MojoReadData()| only. Mutually exclusive with | |
114 // |MOJO_READ_DATA_FLAG_DISCARD| and |MOJO_READ_DATA_FLAG_QUERY|. | |
115 | |
116 typedef uint32_t MojoReadDataFlags; | |
117 | |
118 #define MOJO_READ_DATA_FLAG_NONE ((MojoReadDataFlags)0) | |
119 #define MOJO_READ_DATA_FLAG_ALL_OR_NONE ((MojoReadDataFlags)1 << 0) | |
120 #define MOJO_READ_DATA_FLAG_DISCARD ((MojoReadDataFlags)1 << 1) | |
121 #define MOJO_READ_DATA_FLAG_QUERY ((MojoReadDataFlags)1 << 2) | |
122 #define MOJO_READ_DATA_FLAG_PEEK ((MojoReadDataFlags)1 << 3) | |
123 | |
124 MOJO_BEGIN_EXTERN_C | |
125 | |
126 // |MojoCreateDataPipe()|: Creates a data pipe, which is a unidirectional | |
127 // communication channel for unframed data, with the given options. Data is | |
128 // unframed, but must come as (multiples of) discrete elements, of the size | |
129 // given in |options|. See |MojoCreateDataPipeOptions| for a description of the | |
130 // different options available for data pipes. | |
131 // | |
132 // |options| may be set to null for a data pipe with the default options (which | |
133 // will have an element size of one byte and have some system-dependent | |
134 // capacity). | |
135 // | |
136 // On success, |*data_pipe_producer_handle| will be set to the handle for the | |
137 // producer and |*data_pipe_consumer_handle| will be set to the handle for the | |
138 // consumer. (On failure, they are not modified.) The producer handle will have | |
139 // (at least) the following rights: |MOJO_HANDLE_RIGHT_TRANSFER|, | |
140 // |MOJO_HANDLE_RIGHT_WRITE|, |MOJO_HANDLE_RIGHT_GET_OPTIONS|, and | |
141 // |MOJO_HANDLE_RIGHT_SET_OPTIONS|. The consumer handle will have (at least) the | |
142 // following rights: |MOJO_HANDLE_RIGHT_TRANSFER|, |MOJO_HANDLE_RIGHT_READ|, | |
143 // |MOJO_HANDLE_RIGHT_GET_OPTIONS|, and |MOJO_HANDLE_RIGHT_SET_OPTIONS| | |
144 // | |
145 // Returns: | |
146 // |MOJO_RESULT_OK| on success. | |
147 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
148 // |*options| is invalid). | |
149 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has | |
150 // been reached (e.g., if the requested capacity was too large, or if the | |
151 // maximum number of handles was exceeded). | |
152 // |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|. | |
153 MojoResult MojoCreateDataPipe( | |
154 const struct MojoCreateDataPipeOptions* MOJO_RESTRICT | |
155 options, // Optional in. | |
156 MojoHandle* MOJO_RESTRICT data_pipe_producer_handle, // Out. | |
157 MojoHandle* MOJO_RESTRICT data_pipe_consumer_handle); // Out. | |
158 | |
159 // TODO(vtl): Probably should have a way of getting the | |
160 // |MojoCreateDataPipeOptions| (maybe just rename it to |MojoDataPipeOptions|?) | |
161 // from either handle as well. | |
162 | |
163 // |MojoSetDataPipeProducerOptions()|: Sets options for the data pipe producer | |
164 // handle |data_pipe_producer_handle| (which must have the | |
165 // |MOJO_HANDLE_RIGHT_SET_OPTIONS| right). | |
166 // | |
167 // |options| may be set to null to reset back to the default options. | |
168 // | |
169 // Note that changing the write threshold may also result in the state (both | |
170 // satisfied or satisfiable) of the |MOJO_HANDLE_SIGNAL_WRITE_THRESHOLD| handle | |
171 // signal being changed. | |
172 // | |
173 // Returns: | |
174 // |MOJO_RESULT_OK| on success. | |
175 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
176 // |data_pipe_producer_handle| is not a valid data pipe producer handle or | |
177 // |*options| is invalid). | |
178 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_producer_handle| does not | |
179 // have the |MOJO_HANDLE_RIGHT_SET_OPTIONS| right. | |
180 // |MOJO_RESULT_BUSY| if |data_pipe_producer_handle| is currently in use in | |
181 // some transaction (that, e.g., may result in it being invalidated, such | |
182 // as being sent in a message). | |
183 MojoResult MojoSetDataPipeProducerOptions( | |
184 MojoHandle data_pipe_producer_handle, // In. | |
185 const struct MojoDataPipeProducerOptions* options); // Optional in. | |
186 | |
187 // |MojoGetDataPipeProducerOptions()|: Gets options for the data pipe producer | |
188 // handle |data_pipe_producer_handle| (which must have the | |
189 // |MOJO_HANDLE_RIGHT_GET_OPTIONS| right). |options| should be non-null and | |
190 // point to a buffer of size |options_num_bytes|; |options_num_bytes| should be | |
191 // at least 8 (the size of the first, and currently only, version of | |
192 // |MojoDataPipeProducerOptions|). | |
193 // | |
194 // On success, |*options| will be filled with information about the given | |
195 // buffer. Note that if additional (larger) versions of | |
196 // |MojoDataPipeProducerOptions| are defined, the largest version permitted by | |
197 // |options_num_bytes| that is supported by the implementation will be filled. | |
198 // Callers expecting more than the first 16-byte version must check the | |
199 // resulting |options->struct_size|. | |
200 // | |
201 // Returns: | |
202 // |MOJO_RESULT_OK| on success. | |
203 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
204 // |data_pipe_producer_handle| is not a valid data pipe producer handle, | |
205 // |*options| is null, or |options_num_bytes| is too small). | |
206 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_producer_handle| does not | |
207 // have the |MOJO_HANDLE_RIGHT_GET_OPTIONS| right. | |
208 // |MOJO_RESULT_BUSY| if |data_pipe_producer_handle| is currently in use in | |
209 // some transaction (that, e.g., may result in it being invalidated, such | |
210 // as being sent in a message). | |
211 MojoResult MojoGetDataPipeProducerOptions( | |
212 MojoHandle data_pipe_producer_handle, // In. | |
213 struct MojoDataPipeProducerOptions* options, // Out. | |
214 uint32_t options_num_bytes); // In. | |
215 | |
216 // |MojoWriteData()|: Writes the given data to the data pipe producer given by | |
217 // |data_pipe_producer_handle| (which must have the |MOJO_HANDLE_RIGHT_WRITE| | |
218 // right). |elements| points to data of size |*num_bytes|; |*num_bytes| should | |
219 // be a multiple of the data pipe's element size. If | |
220 // |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| is set in |flags|, either all the data | |
221 // will be written or none is. | |
222 // | |
223 // On success, |*num_bytes| is set to the amount of data that was actually | |
224 // written. | |
225 // | |
226 // Returns: | |
227 // |MOJO_RESULT_OK| on success. | |
228 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
229 // |data_pipe_producer_handle| is not a handle to a data pipe producer or | |
230 // |*num_bytes| is not a multiple of the data pipe's element size). | |
231 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_producer_handle| does not | |
232 // have the |MOJO_HANDLE_RIGHT_WRITE| right. | |
233 // |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer handle has been | |
234 // closed. | |
235 // |MOJO_RESULT_OUT_OF_RANGE| if |flags| has | |
236 // |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set and the required amount of data | |
237 // (specified by |*num_bytes|) could not be written. | |
238 // |MOJO_RESULT_BUSY| if |data_pipe_producer_handle| is currently in use in | |
239 // some transaction (that, e.g., may result in it being invalidated, such | |
240 // as being sent in a message), or if there is a two-phase write ongoing | |
241 // with |data_pipe_producer_handle| (i.e., |MojoBeginWriteData()| has been | |
242 // called, but not yet the matching |MojoEndWriteData()|). | |
243 // |MOJO_RESULT_SHOULD_WAIT| if no data can currently be written (and the | |
244 // consumer is still open) and |flags| does *not* have | |
245 // |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set. | |
246 // | |
247 // TODO(vtl): Should there be a way of querying how much data can be written? | |
248 MojoResult MojoWriteData(MojoHandle data_pipe_producer_handle, // In. | |
249 const void* MOJO_RESTRICT elements, // In. | |
250 uint32_t* MOJO_RESTRICT num_bytes, // In/out. | |
251 MojoWriteDataFlags flags); // In. | |
252 | |
253 // |MojoBeginWriteData()|: Begins a two-phase write to the data pipe producer | |
254 // given by |data_pipe_producer_handle| (which must have the | |
255 // |MOJO_HANDLE_RIGHT_WRITE| right). On success, |*buffer| will be a pointer to | |
256 // which the caller can write |*buffer_num_bytes| bytes of data. There are | |
257 // currently no flags allowed, so |flags| should be |MOJO_WRITE_DATA_FLAG_NONE|. | |
258 // | |
259 // During a two-phase write, |data_pipe_producer_handle| is *not* writable. | |
260 // E.g., if another thread tries to write to it, it will get |MOJO_RESULT_BUSY|; | |
261 // that thread can then wait for |data_pipe_producer_handle| to become writable | |
262 // again. | |
263 // | |
264 // When |MojoBeginWriteData()| returns |MOJO_RESULT_OK|, and the caller has | |
265 // finished writing data to |*buffer|, it should call |MojoEndWriteData()| to | |
266 // specify the amount written and to complete the two-phase write. | |
267 // |MojoEndWriteData()| need not be called for other return values. | |
268 // | |
269 // Note: After a successful |MojoBeginWriteData()| on a given handle and before | |
270 // a corresponding |MojoEndWriteData()|, any operation that invalidates the | |
271 // handle (such as closing the handle, replacing the handle with one with | |
272 // reduced rights, or transferring the handle over a message pipe) will abort | |
273 // the two-phase write. That is, the behavior is equivalent to ending the | |
274 // two-phase write with no data written. That operation will also invalidate the | |
275 // buffer pointer: the behavior if data continues to be written to the buffer is | |
276 // undefined. | |
277 // | |
278 // Returns: | |
279 // |MOJO_RESULT_OK| on success. | |
280 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
281 // |data_pipe_producer_handle| is not a handle to a data pipe producer or | |
282 // flags has |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set). | |
283 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_producer_handle| does not | |
284 // have the |MOJO_HANDLE_RIGHT_WRITE| right. | |
285 // |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer handle has been | |
286 // closed. | |
287 // |MOJO_RESULT_BUSY| if |data_pipe_producer_handle| is currently in use in | |
288 // some transaction (that, e.g., may result in it being invalidated, such | |
289 // as being sent in a message), or if there is already a two-phase write | |
290 // ongoing with |data_pipe_producer_handle| (i.e., |MojoBeginWriteData()| | |
291 // has been called, but not yet the matching |MojoEndWriteData()|). | |
292 // |MOJO_RESULT_SHOULD_WAIT| if no data can currently be written (and the | |
293 // consumer is still open). | |
294 MojoResult MojoBeginWriteData(MojoHandle data_pipe_producer_handle, // In. | |
295 void** MOJO_RESTRICT buffer, // Out. | |
296 uint32_t* MOJO_RESTRICT buffer_num_bytes, // Out. | |
297 MojoWriteDataFlags flags); // In. | |
298 | |
299 // |MojoEndWriteData()|: Ends a two-phase write to the data pipe producer given | |
300 // by |data_pipe_producer_handle| (which must have the |MOJO_HANDLE_RIGHT_WRITE| | |
301 // right) that was begun by a call to |MojoBeginWriteData()| on the same handle. | |
302 // |num_bytes_written| should indicate the amount of data actually written; it | |
303 // must be less than or equal to the value of |*buffer_num_bytes| output by | |
304 // |MojoBeginWriteData()| and must be a multiple of the element size. The buffer | |
305 // given by |*buffer| from |MojoBeginWriteData()| must have been filled with | |
306 // exactly |num_bytes_written| bytes of data. | |
307 // | |
308 // On failure, the two-phase write (if any) is ended (so the handle may become | |
309 // writable again, if there's space available) but no data written to |*buffer| | |
310 // is "put into" the data pipe. | |
311 // | |
312 // Returns: | |
313 // |MOJO_RESULT_OK| on success. | |
314 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
315 // |data_pipe_producer_handle| is not a handle to a data pipe producer or | |
316 // |num_bytes_written| is invalid (greater than the maximum value provided | |
317 // by |MojoBeginWriteData()| or not a multiple of the element size). | |
318 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_producer_handle| does not | |
319 // have the |MOJO_HANDLE_RIGHT_WRITE| right. | |
320 // |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer is not in a | |
321 // two-phase write (e.g., |MojoBeginWriteData()| was not called or | |
322 // |MojoEndWriteData()| has already been called). | |
323 // |MOJO_RESULT_BUSY| if |data_pipe_producer_handle| is currently in use in | |
324 // some transaction (that, e.g., may result in it being invalidated, such | |
325 // as being sent in a message). | |
326 MojoResult MojoEndWriteData(MojoHandle data_pipe_producer_handle, // In. | |
327 uint32_t num_bytes_written); // In. | |
328 | |
329 // |MojoSetDataPipeConsumerOptions()|: Sets options for the data pipe consumer | |
330 // handle |data_pipe_consumer_handle| (which must have the | |
331 // |MOJO_HANDLE_RIGHT_SET_OPTIONS| right). | |
332 // | |
333 // |options| may be set to null to reset back to the default options. | |
334 // | |
335 // Note that changing the read threshold may also result in the state (both | |
336 // satisfied or satisfiable) of the |MOJO_HANDLE_SIGNAL_READ_THRESHOLD| handle | |
337 // signal being changed. | |
338 // | |
339 // Returns: | |
340 // |MOJO_RESULT_OK| on success. | |
341 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
342 // |data_pipe_consumer_handle| is not a valid data pipe consumer handle or | |
343 // |*options| is invalid). | |
344 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_consumer_handle| does not | |
345 // have the |MOJO_HANDLE_RIGHT_SET_OPTIONS| right. | |
346 // |MOJO_RESULT_BUSY| if |data_pipe_consumer_handle| is currently in use in | |
347 // some transaction (that, e.g., may result in it being invalidated, such | |
348 // as being sent in a message). | |
349 MojoResult MojoSetDataPipeConsumerOptions( | |
350 MojoHandle data_pipe_consumer_handle, // In. | |
351 const struct MojoDataPipeConsumerOptions* options); // Optional in. | |
352 | |
353 // |MojoGetDataPipeConsumerOptions()|: Gets options for the data pipe consumer | |
354 // handle |data_pipe_consumer_handle| (which must have the | |
355 // |MOJO_HANDLE_RIGHT_GET_OPTIONS| right). |options| should be non-null and | |
356 // point to a buffer of size |options_num_bytes|; |options_num_bytes| should be | |
357 // at least 8 (the size of the first, and currently only, version of | |
358 // |MojoDataPipeConsumerOptions|). | |
359 // | |
360 // On success, |*options| will be filled with information about the given | |
361 // buffer. Note that if additional (larger) versions of | |
362 // |MojoDataPipeConsumerOptions| are defined, the largest version permitted by | |
363 // |options_num_bytes| that is supported by the implementation will be filled. | |
364 // Callers expecting more than the first 16-byte version must check the | |
365 // resulting |options->struct_size|. | |
366 // | |
367 // Returns: | |
368 // |MOJO_RESULT_OK| on success. | |
369 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
370 // |data_pipe_consumer_handle| is not a valid data pipe consumer handle, | |
371 // |*options| is null, or |options_num_bytes| is too small). | |
372 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_consumer_handle| does not | |
373 // have the |MOJO_HANDLE_RIGHT_GET_OPTIONS| right. | |
374 // |MOJO_RESULT_BUSY| if |data_pipe_consumer_handle| is currently in use in | |
375 // some transaction (that, e.g., may result in it being invalidated, such | |
376 // as being sent in a message). | |
377 MojoResult MojoGetDataPipeConsumerOptions( | |
378 MojoHandle data_pipe_consumer_handle, // In. | |
379 struct MojoDataPipeConsumerOptions* options, // Out. | |
380 uint32_t options_num_bytes); // In. | |
381 | |
382 // |MojoReadData()|: Reads data from the data pipe consumer given by | |
383 // |data_pipe_consumer_handle| (which must have the |MOJO_HANDLE_RIGHT_READ| | |
384 // right). May also be used to discard data or query the amount of data | |
385 // available. | |
386 // | |
387 // If |flags| has neither |MOJO_READ_DATA_FLAG_DISCARD| nor | |
388 // |MOJO_READ_DATA_FLAG_QUERY| set, this tries to read up to |*num_bytes| (which | |
389 // must be a multiple of the data pipe's element size) bytes of data to | |
390 // |elements| and set |*num_bytes| to the amount actually read. If flags has | |
391 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set, it will either read exactly | |
392 // |*num_bytes| bytes of data or none. Additionally, if flags has | |
393 // |MOJO_READ_DATA_FLAG_PEEK| set, the data read will remain in the pipe and be | |
394 // available to future reads. | |
395 // | |
396 // If flags has |MOJO_READ_DATA_FLAG_DISCARD| set, it discards up to | |
397 // |*num_bytes| (which again must be a multiple of the element size) bytes of | |
398 // data, setting |*num_bytes| to the amount actually discarded. If flags has | |
399 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE|, it will either discard exactly | |
400 // |*num_bytes| bytes of data or none. In this case, |MOJO_READ_DATA_FLAG_QUERY| | |
401 // must not be set, and |elements| is ignored (and should typically be set to | |
402 // null). | |
403 // | |
404 // If flags has |MOJO_READ_DATA_FLAG_QUERY| set, it queries the amount of data | |
405 // available, setting |*num_bytes| to the number of bytes available. In this | |
406 // case, |MOJO_READ_DATA_FLAG_DISCARD| must not be set, and | |
407 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| is ignored, as are |elements| and the input | |
408 // value of |*num_bytes|. | |
409 // | |
410 // Returns: | |
411 // |MOJO_RESULT_OK| on success (see above for a description of the different | |
412 // operations). | |
413 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
414 // |data_pipe_consumer_handle| is invalid, the combination of flags in | |
415 // |flags| is invalid, etc.). | |
416 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_consumer_handle| does not | |
417 // have the |MOJO_HANDLE_RIGHT_READ| right. | |
418 // |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer handle has been | |
419 // closed and data (or the required amount of data) was not available to | |
420 // be read or discarded. | |
421 // |MOJO_RESULT_OUT_OF_RANGE| if |flags| has |MOJO_READ_DATA_FLAG_ALL_OR_NONE| | |
422 // set and the required amount of data is not available to be read or | |
423 // discarded (and the producer is still open). | |
424 // |MOJO_RESULT_BUSY| if |data_pipe_consumer_handle| is currently in use in | |
425 // some transaction (that, e.g., may result in it being invalidated, such | |
426 // as being sent in a message), or if there is a two-phase read ongoing | |
427 // with |data_pipe_consumer_handle| (i.e., |MojoBeginReadData()| has been | |
428 // called, but not yet the matching |MojoEndReadData()|). | |
429 // |MOJO_RESULT_SHOULD_WAIT| if there is no data to be read or discarded (and | |
430 // the producer is still open) and |flags| does *not* have | |
431 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set. | |
432 MojoResult MojoReadData(MojoHandle data_pipe_consumer_handle, // In. | |
433 void* MOJO_RESTRICT elements, // Out. | |
434 uint32_t* MOJO_RESTRICT num_bytes, // In/out. | |
435 MojoReadDataFlags flags); // In. | |
436 | |
437 // |MojoBeginReadData()|: Begins a two-phase read from the data pipe consumer | |
438 // given by |data_pipe_consumer_handle| (which must have the | |
439 // |MOJO_HANDLE_RIGHT_READ| right). On success, |*buffer| will be a pointer from | |
440 // which the caller can read |*buffer_num_bytes| bytes of data. There are | |
441 // currently no valid flags, so |flags| must be |MOJO_READ_DATA_FLAG_NONE|. | |
442 // | |
443 // During a two-phase read, |data_pipe_consumer_handle| is *not* readable. | |
444 // E.g., if another thread tries to read from it, it will get | |
445 // |MOJO_RESULT_BUSY|; that thread can then wait for |data_pipe_consumer_handle| | |
446 // to become readable again. | |
447 // | |
448 // Once the caller has finished reading data from |*buffer|, it should call | |
449 // |MojoEndReadData()| to specify the amount read and to complete the two-phase | |
450 // read. | |
451 // | |
452 // Note: After a successful |MojoBeginReadData()| on a given handle and before a | |
453 // corresponding |MojoEndReadData()|, any operation that invalidates the handle | |
454 // (such as closing the handle, replacing the handle with one with reduced | |
455 // rights, or transferring the handle over a message pipe) will abort the | |
456 // two-phase read. That is, the behavior is equivalent to ending the two-phase | |
457 // read with no data consumed. That operation will also invalidate the buffer | |
458 // pointer: the behavior if data continues to be read from the buffer is | |
459 // undefined. | |
460 // | |
461 // Returns: | |
462 // |MOJO_RESULT_OK| on success. | |
463 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
464 // |data_pipe_consumer_handle| is not a handle to a data pipe consumer, | |
465 // or |flags| has invalid flags set). | |
466 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_consumer_handle| does not | |
467 // have the |MOJO_HANDLE_RIGHT_READ| right. | |
468 // |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer handle has been | |
469 // closed. | |
470 // |MOJO_RESULT_BUSY| if |data_pipe_consumer_handle| is currently in use in | |
471 // some transaction (that, e.g., may result in it being invalidated, such | |
472 // as being sent in a message), or if there is already a two-phase read | |
473 // ongoing with |data_pipe_consumer_handle| (i.e., |MojoBeginReadData()| | |
474 // has been called, but not yet the matching |MojoEndReadData()|). | |
475 // |MOJO_RESULT_SHOULD_WAIT| if no data can currently be read (and the | |
476 // producer is still open). | |
477 MojoResult MojoBeginReadData(MojoHandle data_pipe_consumer_handle, // In. | |
478 const void** MOJO_RESTRICT buffer, // Out. | |
479 uint32_t* MOJO_RESTRICT buffer_num_bytes, // Out. | |
480 MojoReadDataFlags flags); // In. | |
481 | |
482 // |MojoEndReadData()|: Ends a two-phase read from the data pipe consumer given | |
483 // by |data_pipe_consumer_handle| (which must have the |MOJO_HANDLE_RIGHT_READ| | |
484 // right) that was begun by a call to |MojoBeginReadData()| on the same handle. | |
485 // |num_bytes_read| should indicate the amount of data actually read; it must be | |
486 // less than or equal to the value of |*buffer_num_bytes| output by | |
487 // |MojoBeginReadData()| and must be a multiple of the element size. | |
488 // | |
489 // On failure, the two-phase read (if any) is ended (so the handle may become | |
490 // readable again) but no data is "removed" from the data pipe. | |
491 // | |
492 // Returns: | |
493 // |MOJO_RESULT_OK| on success. | |
494 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., | |
495 // |data_pipe_consumer_handle| is not a handle to a data pipe consumer or | |
496 // |num_bytes_written| is greater than the maximum value provided by | |
497 // |MojoBeginReadData()| or not a multiple of the element size). | |
498 // |MOJO_RESULT_PERMISSION_DENIED| if |data_pipe_consumer_handle| does not | |
499 // have the |MOJO_HANDLE_RIGHT_READ| right. | |
500 // |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer is not in a | |
501 // two-phase read (e.g., |MojoBeginReadData()| was not called or | |
502 // |MojoEndReadData()| has already been called). | |
503 // |MOJO_RESULT_BUSY| if |data_pipe_consumer_handle| is currently in use in | |
504 // some transaction (that, e.g., may result in it being invalidated, such | |
505 // as being sent in a message). | |
506 MojoResult MojoEndReadData(MojoHandle data_pipe_consumer_handle, // In. | |
507 uint32_t num_bytes_read); // In. | |
508 | |
509 MOJO_END_EXTERN_C | |
510 | |
511 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_SYSTEM_DATA_PIPE_H_ | |
OLD | NEW |