Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / mojo / public / c / system / data_pipe.h
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_SYSTEM_DATA_PIPE_H_
10 #define MOJO_PUBLIC_C_SYSTEM_DATA_PIPE_H_
11
12 #include "mojo/public/c/system/macros.h"
13 #include "mojo/public/c/system/system_export.h"
14 #include "mojo/public/c/system/types.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 //     |MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD|: May discard data for
24 //         whatever reason; best-effort delivery. In particular, if the capacity
25 //         is reached, old data may be discard to make room for new data.
26 //   |uint32_t element_num_bytes|: The size of an element, in bytes. All
27 //       transactions and buffers will consist of an integral number of
28 //       elements. Must be nonzero.
29 //   |uint32_t capacity_num_bytes|: The capacity of the data pipe, in number of
30 //       bytes; must be a multiple of |element_num_bytes|. The data pipe will
31 //       always be able to queue AT LEAST this much data. Set to zero to opt for
32 //       a system-dependent automatically-calculated capacity (which will always
33 //       be at least one element).
34
35 typedef uint32_t MojoCreateDataPipeOptionsFlags;
36
37 #ifdef __cplusplus
38 const MojoCreateDataPipeOptionsFlags MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE =
39     0;
40 const MojoCreateDataPipeOptionsFlags
41     MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD = 1 << 0;
42 #else
43 #define MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE \
44   ((MojoCreateDataPipeOptionsFlags)0)
45 #define MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD \
46   ((MojoCreateDataPipeOptionsFlags)1 << 0)
47 #endif
48
49 MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) == 8, "int64_t has weird alignment");
50 struct MOJO_ALIGNAS(8) MojoCreateDataPipeOptions {
51   uint32_t struct_size;
52   MojoCreateDataPipeOptionsFlags flags;
53   uint32_t element_num_bytes;
54   uint32_t capacity_num_bytes;
55 };
56 MOJO_STATIC_ASSERT(sizeof(MojoCreateDataPipeOptions) == 16,
57                    "MojoCreateDataPipeOptions has wrong size");
58
59 // |MojoWriteDataFlags|: Used to specify different modes to |MojoWriteData()|
60 // and |MojoBeginWriteData()|.
61 //   |MOJO_WRITE_DATA_FLAG_NONE| - No flags; default mode.
62 //   |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| - Write either all the elements
63 //       requested or none of them.
64
65 typedef uint32_t MojoWriteDataFlags;
66
67 #ifdef __cplusplus
68 const MojoWriteDataFlags MOJO_WRITE_DATA_FLAG_NONE = 0;
69 const MojoWriteDataFlags MOJO_WRITE_DATA_FLAG_ALL_OR_NONE = 1 << 0;
70 #else
71 #define MOJO_WRITE_DATA_FLAG_NONE ((MojoWriteDataFlags)0)
72 #define MOJO_WRITE_DATA_FLAG_ALL_OR_NONE ((MojoWriteDataFlags)1 << 0)
73 #endif
74
75 // |MojoReadDataFlags|: Used to specify different modes to |MojoReadData()| and
76 // |MojoBeginReadData()|.
77 //   |MOJO_READ_DATA_FLAG_NONE| - No flags; default mode.
78 //   |MOJO_READ_DATA_FLAG_ALL_OR_NONE| - Read (or discard) either the requested
79 //        number of elements or none.
80 //   |MOJO_READ_DATA_FLAG_DISCARD| - Discard (up to) the requested number of
81 //        elements.
82 //   |MOJO_READ_DATA_FLAG_QUERY| - Query the number of elements available to
83 //       read. For use with |MojoReadData()| only. Mutually exclusive with
84 //       |MOJO_READ_DATA_FLAG_DISCARD| and |MOJO_READ_DATA_FLAG_ALL_OR_NONE| is
85 //       ignored if this flag is set.
86 //   |MOJO_READ_DATA_FLAG_PEEK| - Read elements without removing them. For use
87 //       with |MojoReadData()| only. Mutually exclusive with
88 //       |MOJO_READ_DATA_FLAG_DISCARD| and |MOJO_READ_DATA_FLAG_QUERY|.
89
90 typedef uint32_t MojoReadDataFlags;
91
92 #ifdef __cplusplus
93 const MojoReadDataFlags MOJO_READ_DATA_FLAG_NONE = 0;
94 const MojoReadDataFlags MOJO_READ_DATA_FLAG_ALL_OR_NONE = 1 << 0;
95 const MojoReadDataFlags MOJO_READ_DATA_FLAG_DISCARD = 1 << 1;
96 const MojoReadDataFlags MOJO_READ_DATA_FLAG_QUERY = 1 << 2;
97 const MojoReadDataFlags MOJO_READ_DATA_FLAG_PEEK = 1 << 3;
98 #else
99 #define MOJO_READ_DATA_FLAG_NONE ((MojoReadDataFlags)0)
100 #define MOJO_READ_DATA_FLAG_ALL_OR_NONE ((MojoReadDataFlags)1 << 0)
101 #define MOJO_READ_DATA_FLAG_DISCARD ((MojoReadDataFlags)1 << 1)
102 #define MOJO_READ_DATA_FLAG_QUERY ((MojoReadDataFlags)1 << 2)
103 #define MOJO_READ_DATA_FLAG_PEEK ((MojoReadDataFlags)1 << 3)
104 #endif
105
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109
110 // Note: See the comment in functions.h about the meaning of the "optional"
111 // label for pointer parameters.
112
113 // Creates a data pipe, which is a unidirectional communication channel for
114 // unframed data, with the given options. Data is unframed, but must come as
115 // (multiples of) discrete elements, of the size given in |options|. See
116 // |MojoCreateDataPipeOptions| for a description of the different options
117 // available for data pipes.
118 //
119 // |options| may be set to null for a data pipe with the default options (which
120 // will have an element size of one byte and have some system-dependent
121 // capacity).
122 //
123 // On success, |*data_pipe_producer_handle| will be set to the handle for the
124 // producer and |*data_pipe_consumer_handle| will be set to the handle for the
125 // consumer. (On failure, they are not modified.)
126 //
127 // Returns:
128 //   |MOJO_RESULT_OK| on success.
129 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
130 //       |*options| is invalid).
131 //   |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has
132 //       been reached (e.g., if the requested capacity was too large, or if the
133 //       maximum number of handles was exceeded).
134 //   |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|.
135 MOJO_SYSTEM_EXPORT MojoResult MojoCreateDataPipe(
136     const struct MojoCreateDataPipeOptions* options,  // Optional.
137     MojoHandle* data_pipe_producer_handle,            // Out.
138     MojoHandle* data_pipe_consumer_handle);           // Out.
139
140 // Writes the given data to the data pipe producer given by
141 // |data_pipe_producer_handle|. |elements| points to data of size |*num_bytes|;
142 // |*num_bytes| should be a multiple of the data pipe's element size. If
143 // |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| is set in |flags|, either all the data
144 // will be written or none is.
145 //
146 // On success, |*num_bytes| is set to the amount of data that was actually
147 // written.
148 //
149 // Note: If the data pipe has the "may discard" option flag (specified on
150 // creation), this will discard as much data as required to write the given
151 // data, starting with the earliest written data that has not been consumed.
152 // However, even with "may discard", if |*num_bytes| is greater than the data
153 // pipe's capacity (and |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| is not set), this
154 // will write the maximum amount possible (namely, the data pipe's capacity) and
155 // set |*num_bytes| to that amount. It will *not* discard data from |elements|.
156 //
157 // Returns:
158 //   |MOJO_RESULT_OK| on success.
159 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
160 //       |data_pipe_producer_dispatcher| is not a handle to a data pipe
161 //       producer or |*num_bytes| is not a multiple of the data pipe's element
162 //       size).
163 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer handle has been
164 //       closed.
165 //   |MOJO_RESULT_OUT_OF_RANGE| if |flags| has
166 //       |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set and the required amount of data
167 //       (specified by |*num_bytes|) could not be written.
168 //   |MOJO_RESULT_BUSY| if there is a two-phase write ongoing with
169 //       |data_pipe_producer_handle| (i.e., |MojoBeginWriteData()| has been
170 //       called, but not yet the matching |MojoEndWriteData()|).
171 //   |MOJO_RESULT_SHOULD_WAIT| if no data can currently be written (and the
172 //       consumer is still open) and |flags| does *not* have
173 //       |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set.
174 //
175 // TODO(vtl): Should there be a way of querying how much data can be written?
176 MOJO_SYSTEM_EXPORT MojoResult
177     MojoWriteData(MojoHandle data_pipe_producer_handle,
178                   const void* elements,
179                   uint32_t* num_bytes,  // In/out.
180                   MojoWriteDataFlags flags);
181
182 // Begins a two-phase write to the data pipe producer given by
183 // |data_pipe_producer_handle|. On success, |*buffer| will be a pointer to which
184 // the caller can write |*buffer_num_bytes| bytes of data. If flags has
185 // |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set, then the output value
186 // |*buffer_num_bytes| will be at least as large as its input value, which must
187 // also be a multiple of the element size (if |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE|
188 // is not set, the input value of |*buffer_num_bytes| is ignored).
189 //
190 // During a two-phase write, |data_pipe_producer_handle| is *not* writable.
191 // E.g., if another thread tries to write to it, it will get |MOJO_RESULT_BUSY|;
192 // that thread can then wait for |data_pipe_producer_handle| to become writable
193 // again.
194 //
195 // When |MojoBeginWriteData()| returns MOJO_RESULT_OK, and the caller has
196 // finished writing data to |*buffer|, it should call |MojoEndWriteData()| to
197 // specify the amount written and to complete the two-phase write.
198 // |MojoEndWriteData()| need not be called for other return values.
199 //
200 // Note: If the data pipe has the "may discard" option flag (specified on
201 // creation) and |flags| has |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set, this may
202 // discard some data.
203 //
204 // Returns:
205 //   |MOJO_RESULT_OK| on success.
206 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
207 //       |data_pipe_producer_handle| is not a handle to a data pipe producer or
208 //       flags has |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set and
209 //       |*buffer_num_bytes| is not a multiple of the element size).
210 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer handle has been
211 //       closed.
212 //   |MOJO_RESULT_OUT_OF_RANGE| if |flags| has
213 //       |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set and the required amount of data
214 //       (specified by |*buffer_num_bytes|) cannot be written contiguously at
215 //       this time. (Note that there may be space available for the required
216 //       amount of data, but the "next" write position may not be large enough.)
217 //   |MOJO_RESULT_BUSY| if there is already a two-phase write ongoing with
218 //       |data_pipe_producer_handle| (i.e., |MojoBeginWriteData()| has been
219 //       called, but not yet the matching |MojoEndWriteData()|).
220 //   |MOJO_RESULT_SHOULD_WAIT| if no data can currently be written (and the
221 //       consumer is still open).
222 MOJO_SYSTEM_EXPORT MojoResult
223     MojoBeginWriteData(MojoHandle data_pipe_producer_handle,
224                        void** buffer,               // Out.
225                        uint32_t* buffer_num_bytes,  // In/out.
226                        MojoWriteDataFlags flags);
227
228 // Ends a two-phase write to the data pipe producer given by
229 // |data_pipe_producer_handle| that was begun by a call to
230 // |MojoBeginWriteData()| on the same handle. |num_bytes_written| should
231 // indicate the amount of data actually written; it must be less than or equal
232 // to the value of |*buffer_num_bytes| output by |MojoBeginWriteData()| and must
233 // be a multiple of the element size. The buffer given by |*buffer| from
234 // |MojoBeginWriteData()| must have been filled with exactly |num_bytes_written|
235 // bytes of data.
236 //
237 // On failure, the two-phase write (if any) is ended (so the handle may become
238 // writable again, if there's space available) but no data written to |*buffer|
239 // is "put into" the data pipe.
240 //
241 // Returns:
242 //   |MOJO_RESULT_OK| on success.
243 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
244 //       |data_pipe_producer_handle| is not a handle to a data pipe producer or
245 //       |num_bytes_written| is invalid (greater than the maximum value provided
246 //       by |MojoBeginWriteData()| or not a multiple of the element size).
247 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer is not in a
248 //       two-phase write (e.g., |MojoBeginWriteData()| was not called or
249 //       |MojoEndWriteData()| has already been called).
250 MOJO_SYSTEM_EXPORT MojoResult
251     MojoEndWriteData(MojoHandle data_pipe_producer_handle,
252                      uint32_t num_bytes_written);
253
254 // Reads data from the data pipe consumer given by |data_pipe_consumer_handle|.
255 // May also be used to discard data or query the amount of data available.
256 //
257 // If |flags| has neither |MOJO_READ_DATA_FLAG_DISCARD| nor
258 // |MOJO_READ_DATA_FLAG_QUERY| set, this tries to read up to |*num_bytes| (which
259 // must be a multiple of the data pipe's element size) bytes of data to
260 // |elements| and set |*num_bytes| to the amount actually read. If flags has
261 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set, it will either read exactly
262 // |*num_bytes| bytes of data or none. Additionally, if flags has
263 // |MOJO_READ_DATA_FLAG_PEEK| set, the data read will remain in the pipe and be
264 // available to future reads.
265 //
266 // If flags has |MOJO_READ_DATA_FLAG_DISCARD| set, it discards up to
267 // |*num_bytes| (which again be a multiple of the element size) bytes of data,
268 // setting |*num_bytes| to the amount actually discarded. If flags has
269 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE|, it will either discard exactly
270 // |*num_bytes| bytes of data or none. In this case, |MOJO_READ_DATA_FLAG_QUERY|
271 // must not be set, and |elements| is ignored (and should typically be set to
272 // null).
273 //
274 // If flags has |MOJO_READ_DATA_FLAG_QUERY| set, it queries the amount of data
275 // available, setting |*num_bytes| to the number of bytes available. In this
276 // case, |MOJO_READ_DATA_FLAG_DISCARD| must not be set, and
277 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| is ignored, as are |elements| and the input
278 // value of |*num_bytes|.
279 //
280 // Returns:
281 //   |MOJO_RESULT_OK| on success (see above for a description of the different
282 //       operations).
283 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
284 //       |data_pipe_consumer_handle| is invalid, the combination of flags in
285 //       |flags| is invalid, etc.).
286 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer handle has been
287 //       closed and data (or the required amount of data) was not available to
288 //       be read or discarded.
289 //   |MOJO_RESULT_OUT_OF_RANGE| if |flags| has |MOJO_READ_DATA_FLAG_ALL_OR_NONE|
290 //       set and the required amount of data is not available to be read or
291 //       discarded (and the producer is still open).
292 //   |MOJO_RESULT_BUSY| if there is a two-phase read ongoing with
293 //       |data_pipe_consumer_handle| (i.e., |MojoBeginReadData()| has been
294 //       called, but not yet the matching |MojoEndReadData()|).
295 //   |MOJO_RESULT_SHOULD_WAIT| if there is no data to be read or discarded (and
296 //       the producer is still open) and |flags| does *not* have
297 //       |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set.
298 MOJO_SYSTEM_EXPORT MojoResult MojoReadData(MojoHandle data_pipe_consumer_handle,
299                                            void* elements,       // Out.
300                                            uint32_t* num_bytes,  // In/out.
301                                            MojoReadDataFlags flags);
302
303 // Begins a two-phase read from the data pipe consumer given by
304 // |data_pipe_consumer_handle|. On success, |*buffer| will be a pointer from
305 // which the caller can read |*buffer_num_bytes| bytes of data. If flags has
306 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set, then the output value
307 // |*buffer_num_bytes| will be at least as large as its input value, which must
308 // also be a multiple of the element size (if |MOJO_READ_DATA_FLAG_ALL_OR_NONE|
309 // is not set, the input value of |*buffer_num_bytes| is ignored). |flags| must
310 // not have |MOJO_READ_DATA_FLAG_DISCARD|, |MOJO_READ_DATA_FLAG_QUERY|, or
311 // |MOJO_READ_DATA_FLAG_PEEK| set.
312 //
313 // During a two-phase read, |data_pipe_consumer_handle| is *not* readable.
314 // E.g., if another thread tries to read from it, it will get
315 // |MOJO_RESULT_BUSY|; that thread can then wait for |data_pipe_consumer_handle|
316 // to become readable again.
317 //
318 // Once the caller has finished reading data from |*buffer|, it should call
319 // |MojoEndReadData()| to specify the amount read and to complete the two-phase
320 // read.
321 //
322 // Returns:
323 //   |MOJO_RESULT_OK| on success.
324 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
325 //       |data_pipe_consumer_handle| is not a handle to a data pipe consumer,
326 //       |flags| has |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set and
327 //       |*buffer_num_bytes| is not a multiple of the element size, or |flags|
328 //       has invalid flags set).
329 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer handle has been
330 //       closed.
331 //   |MOJO_RESULT_OUT_OF_RANGE| if |flags| has |MOJO_READ_DATA_FLAG_ALL_OR_NONE|
332 //       set and the required amount of data (specified by |*buffer_num_bytes|)
333 //       cannot be read from a contiguous buffer at this time. (Note that there
334 //       may be the required amount of data, but it may not be contiguous.)
335 //   |MOJO_RESULT_BUSY| if there is already a two-phase read ongoing with
336 //       |data_pipe_consumer_handle| (i.e., |MojoBeginReadData()| has been
337 //       called, but not yet the matching |MojoEndReadData()|).
338 //   |MOJO_RESULT_SHOULD_WAIT| if no data can currently be read (and the
339 //       producer is still open).
340 MOJO_SYSTEM_EXPORT MojoResult
341     MojoBeginReadData(MojoHandle data_pipe_consumer_handle,
342                       const void** buffer,         // Out.
343                       uint32_t* buffer_num_bytes,  // In/out.
344                       MojoReadDataFlags flags);
345
346 // Ends a two-phase read from the data pipe consumer given by
347 // |data_pipe_consumer_handle| that was begun by a call to |MojoBeginReadData()|
348 // on the same handle. |num_bytes_read| should indicate the amount of data
349 // actually read; it must be less than or equal to the value of
350 // |*buffer_num_bytes| output by |MojoBeginReadData()| and must be a multiple of
351 // the element size.
352 //
353 // On failure, the two-phase read (if any) is ended (so the handle may become
354 // readable again) but no data is "removed" from the data pipe.
355 //
356 // Returns:
357 //   |MOJO_RESULT_OK| on success.
358 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
359 //       |data_pipe_consumer_handle| is not a handle to a data pipe consumer or
360 //       |num_bytes_written| is greater than the maximum value provided by
361 //       |MojoBeginReadData()| or not a multiple of the element size).
362 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer is not in a
363 //       two-phase read (e.g., |MojoBeginReadData()| was not called or
364 //       |MojoEndReadData()| has already been called).
365 MOJO_SYSTEM_EXPORT MojoResult
366     MojoEndReadData(MojoHandle data_pipe_consumer_handle,
367                     uint32_t num_bytes_read);
368
369 #ifdef __cplusplus
370 }  // extern "C"
371 #endif
372
373 #endif  // MOJO_PUBLIC_C_SYSTEM_DATA_PIPE_H_