Home | History | Annotate | Download | only in system
      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 <stdint.h>
     13 
     14 #include "mojo/public/c/system/macros.h"
     15 #include "mojo/public/c/system/system_export.h"
     16 #include "mojo/public/c/system/types.h"
     17 
     18 // Flags passed to |MojoCreateDataPipe()| via |MojoCreateDataPipeOptions|. See
     19 // values defined below.
     20 typedef uint32_t MojoCreateDataPipeFlags;
     21 
     22 // No flags. Default behavior.
     23 #define MOJO_CREATE_DATA_PIPE_FLAG_NONE ((uint32_t)0)
     24 
     25 // Options passed to |MojoCreateDataPipe()|.
     26 struct MOJO_ALIGNAS(8) MojoCreateDataPipeOptions {
     27   // The size of this structure, used for versioning.
     28   uint32_t struct_size;
     29 
     30   // See |MojoCreateDataPipeFlags|.
     31   MojoCreateDataPipeFlags flags;
     32 
     33   // The size of an element in bytes. All transactions and buffer sizes must
     34   // consist of an integral number of elements. Must be non-zero.
     35   uint32_t element_num_bytes;
     36 
     37   // The capacity of the data pipe in bytes. Must be a multiple of
     38   // |element_num_bytes|. If successfully created, the pipe will always be able
     39   // to queue at least this much data. If zero, the pipe buffer will be of a
     40   // system-dependent capacity of at least one element in size.
     41   uint32_t capacity_num_bytes;
     42 };
     43 MOJO_STATIC_ASSERT(MOJO_ALIGNOF(int64_t) <= 8, "int64_t has weird alignment");
     44 MOJO_STATIC_ASSERT(sizeof(MojoCreateDataPipeOptions) == 16,
     45                    "MojoCreateDataPipeOptions has wrong size");
     46 
     47 // Flags passed to |MojoWriteData()| via |MojoWriteDataOptions|. See values
     48 // defined below.
     49 typedef uint32_t MojoWriteDataFlags;
     50 
     51 // No flags. Default behavior.
     52 #define MOJO_WRITE_DATA_FLAG_NONE ((uint32_t)0)
     53 
     54 // Requires that all provided data must fit into the pipe's available capacity
     55 // in order for the write to succeed. Otherwise the write fails and no data is
     56 // written into the pipe.
     57 #define MOJO_WRITE_DATA_FLAG_ALL_OR_NONE ((uint32_t)1 << 0)
     58 
     59 // Options passed to |MojoWriteData()|.
     60 struct MOJO_ALIGNAS(8) MojoWriteDataOptions {
     61   // The size of this structure, used for versioning.
     62   uint32_t struct_size;
     63 
     64   // See |MojoWriteDataFlags|.
     65   MojoWriteDataFlags flags;
     66 };
     67 MOJO_STATIC_ASSERT(sizeof(MojoWriteDataOptions) == 8,
     68                    "MojoWriteDataOptions has wrong size");
     69 
     70 // Flags passed to |MojoBeginWriteData()| via |MojoBeginWriteDataOptions|. See
     71 // values defined below.
     72 typedef uint32_t MojoBeginWriteDataFlags;
     73 
     74 // No flags. Default behavior.
     75 #define MOJO_BEGIN_WRITE_DATA_FLAG_NONE ((uint32_t)0)
     76 
     77 // Options passed to |MojoBeginWriteData()|.
     78 struct MOJO_ALIGNAS(8) MojoBeginWriteDataOptions {
     79   // The size of this structure, used for versioning.
     80   uint32_t struct_size;
     81 
     82   // See |MojoBeginWriteDataFlags|.
     83   MojoBeginWriteDataFlags flags;
     84 };
     85 MOJO_STATIC_ASSERT(sizeof(MojoBeginWriteDataOptions) == 8,
     86                    "MojoBeginWriteDataOptions has wrong size");
     87 
     88 // Flags passed to |MojoEndWriteData()| via |MojoEndWriteDataOptions|. See
     89 // values defined below.
     90 typedef uint32_t MojoEndWriteDataFlags;
     91 
     92 // No flags. Default behavior.
     93 #define MOJO_END_WRITE_DATA_FLAG_NONE ((uint32_t)0)
     94 
     95 // Options passed to |MojoEndWriteData()|.
     96 struct MOJO_ALIGNAS(8) MojoEndWriteDataOptions {
     97   // The size of this structure, used for versioning.
     98   uint32_t struct_size;
     99 
    100   // See |MojoEndWriteDataFlags|.
    101   MojoEndWriteDataFlags flags;
    102 };
    103 MOJO_STATIC_ASSERT(sizeof(MojoEndWriteDataOptions) == 8,
    104                    "MojoEndWriteDataOptions has wrong size");
    105 
    106 // Flags passed to |MojoReadData()| via |MojoReadDataOptions|.
    107 typedef uint32_t MojoReadDataFlags;
    108 
    109 // No flags. Default behavior.
    110 #define MOJO_READ_DATA_FLAG_NONE ((uint32_t)0)
    111 
    112 // Requires that all request bytes can be read from the data pipe in order for
    113 // the read to succeed. If that many bytes are not available for reading, the
    114 // read will fail and no bytes will be read. Ignored of
    115 // |MOJO_READ_DATA_FLAG_QUERY| is also set.
    116 #define MOJO_READ_DATA_FLAG_ALL_OR_NONE ((uint32_t)1 << 0)
    117 
    118 // Discards the data read rather than copying it into the caller's provided
    119 // buffer. May not be combined with |MOJO_READ_DATA_FLAG_PEEK| or
    120 // |MOJO_READ_DATA_FLAG_QUERY|.
    121 #define MOJO_READ_DATA_FLAG_DISCARD ((uint32_t)1 << 1)
    122 
    123 // Queries the number of bytes available for reading without actually reading
    124 // the data. May not be combined with |MOJO_READ_DATA_FLAG_DISCARD| or
    125 // |MOJO_READ_DATA_FLAG_PEEK|. |MOJO_READ_DATA_FLAG_ALL_OR_NONE| is ignored if
    126 // this is set.
    127 #define MOJO_READ_DATA_FLAG_QUERY ((uint32_t)1 << 2)
    128 
    129 // Reads data from the pipe and copies it to the caller's provided buffer
    130 // without actually removing the data from the pipe. May not be combined with
    131 // |MOJO_READ_DATA_FLAG_DISCARD| or |MOJO_READ_DATA_FLAG_QUERY|.
    132 #define MOJO_READ_DATA_FLAG_PEEK ((uint32_t)1 << 3)
    133 
    134 // Options passed to |MojoReadData()|.
    135 struct MOJO_ALIGNAS(8) MojoReadDataOptions {
    136   // The size of this structure, used for versioning.
    137   uint32_t struct_size;
    138 
    139   // See |MojoReadDataFlags|.
    140   MojoReadDataFlags flags;
    141 };
    142 MOJO_STATIC_ASSERT(sizeof(MojoReadDataOptions) == 8,
    143                    "MojoReadDataOptions has wrong size");
    144 
    145 // Flags passed to |MojoBeginReadData()| via |MojoBeginReadDataOptions|. See
    146 // values defined below.
    147 typedef uint32_t MojoBeginReadDataFlags;
    148 
    149 // No flags. Default behavior.
    150 #define MOJO_BEGIN_READ_DATA_FLAG_NONE ((uint32_t)0)
    151 
    152 // Options passed to |MojoBeginReadData()|.
    153 struct MOJO_ALIGNAS(8) MojoBeginReadDataOptions {
    154   // The size of this structure, used for versioning.
    155   uint32_t struct_size;
    156 
    157   // See |MojoBeginReadDataFlags|.
    158   MojoBeginReadDataFlags flags;
    159 };
    160 MOJO_STATIC_ASSERT(sizeof(MojoBeginReadDataOptions) == 8,
    161                    "MojoBeginReadDataOptions has wrong size");
    162 
    163 // Flags passed to |MojoEndReadData()| via |MojoEndReadDataOptions|. See
    164 // values defined below.
    165 typedef uint32_t MojoEndReadDataFlags;
    166 
    167 // No flags. Default behavior.
    168 #define MOJO_END_READ_DATA_FLAG_NONE ((uint32_t)0)
    169 
    170 // Options passed to |MojoEndReadData()|.
    171 struct MOJO_ALIGNAS(8) MojoEndReadDataOptions {
    172   // The size of this structure, used for versioning.
    173   uint32_t struct_size;
    174 
    175   // See |MojoEndReadDataFlags|.
    176   MojoEndReadDataFlags flags;
    177 };
    178 MOJO_STATIC_ASSERT(sizeof(MojoEndReadDataOptions) == 8,
    179                    "MojoEndReadDataOptions has wrong size");
    180 
    181 #ifdef __cplusplus
    182 extern "C" {
    183 #endif
    184 
    185 // Creates a data pipe, which is a unidirectional communication channel for
    186 // unframed data. Data must be read and written in multiples of discrete
    187 // discrete elements of size given in |options|.
    188 //
    189 // See |MojoCreateDataPipeOptions| for a description of the different options
    190 // available for data pipes.
    191 //
    192 // |options| may be set to null for a data pipe with the default options (which
    193 // will have an element size of one byte and have some system-dependent
    194 // capacity).
    195 //
    196 // On success, |*data_pipe_producer_handle| will be set to the handle for the
    197 // producer and |*data_pipe_consumer_handle| will be set to the handle for the
    198 // consumer. On failure they are not modified.
    199 //
    200 // Returns:
    201 //   |MOJO_RESULT_OK| on success.
    202 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid, e.g.,
    203 //       |*options| is invalid, specified capacity or element size is zero, or
    204 //       the specified element size exceeds the specified capacity.
    205 //   |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has
    206 //       been reached (e.g., if the requested capacity was too large, or if the
    207 //       maximum number of handles was exceeded).
    208 //   |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|.
    209 MOJO_SYSTEM_EXPORT MojoResult
    210 MojoCreateDataPipe(const struct MojoCreateDataPipeOptions* options,
    211                    MojoHandle* data_pipe_producer_handle,
    212                    MojoHandle* data_pipe_consumer_handle);
    213 
    214 // Writes the data pipe producer given by |data_pipe_producer_handle|.
    215 //
    216 // |elements| points to data of size |*num_bytes|; |*num_bytes| must be a
    217 // multiple of the data pipe's element size.
    218 //
    219 // On success |*num_bytes| is set to the amount of data that was actually
    220 // written. On failure it is unmodified.
    221 //
    222 // |options| may be null for default options. See |MojoWriteDataOptions| for
    223 // the effect of various options on the behavior of this function.
    224 //
    225 // Returns:
    226 //   |MOJO_RESULT_OK| on success.
    227 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
    228 //       |data_pipe_producer_dispatcher| is not a handle to a data pipe
    229 //       producer or |*num_bytes| is not a multiple of the data pipe's element
    230 //       size.)
    231 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer handle has been
    232 //       closed.
    233 //   |MOJO_RESULT_OUT_OF_RANGE| if |options->flags| has
    234 //       |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set and the required amount of data
    235 //       (specified by |*num_bytes|) could not be written.
    236 //   |MOJO_RESULT_BUSY| if there is a two-phase write ongoing with
    237 //       |data_pipe_producer_handle| (i.e., |MojoBeginWriteData()| has been
    238 //       called, but not yet the matching |MojoEndWriteData()|).
    239 //   |MOJO_RESULT_SHOULD_WAIT| if no data can currently be written (and the
    240 //       consumer is still open) and |options->flags| does *not* have
    241 //       |MOJO_WRITE_DATA_FLAG_ALL_OR_NONE| set.
    242 MOJO_SYSTEM_EXPORT MojoResult
    243 MojoWriteData(MojoHandle data_pipe_producer_handle,
    244               const void* elements,
    245               uint32_t* num_bytes,
    246               const struct MojoWriteDataOptions* options);
    247 
    248 // Begins a two-phase write to the data pipe producer given by
    249 // |data_pipe_producer_handle|. On success |*buffer| will be a pointer to which
    250 // the caller can write up to |*buffer_num_bytes| bytes of data.
    251 //
    252 // During a two-phase write, |data_pipe_producer_handle| is *not* writable.
    253 // If another caller tries to write to it by calling |MojoWriteData()| or
    254 // |MojoBeginWriteData()|, their request will fail with |MOJO_RESULT_BUSY|.
    255 //
    256 // If |MojoBeginWriteData()| returns MOJO_RESULT_OK and once the caller has
    257 // finished writing data to |*buffer|, |MojoEndWriteData()| must be called to
    258 // indicate the amount of data actually written and to complete the two-phase
    259 // write operation. |MojoEndWriteData()| need not be called when
    260 // |MojoBeginWriteData()| fails.
    261 //
    262 // |options| may be null for default options.
    263 //
    264 // Returns:
    265 //   |MOJO_RESULT_OK| on success.
    266 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
    267 //       |data_pipe_producer_handle| is not a handle to a data pipe producer or
    268 //       |*options| is invalid.
    269 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer handle has been
    270 //       closed.
    271 //   |MOJO_RESULT_BUSY| if there is already a two-phase write ongoing with
    272 //       |data_pipe_producer_handle| (i.e., |MojoBeginWriteData()| has been
    273 //       called, but not yet the matching |MojoEndWriteData()|).
    274 //   |MOJO_RESULT_SHOULD_WAIT| if no data can currently be written (and the
    275 //       consumer is still open).
    276 MOJO_SYSTEM_EXPORT MojoResult
    277 MojoBeginWriteData(MojoHandle data_pipe_producer_handle,
    278                    const struct MojoBeginWriteDataOptions* options,
    279                    void** buffer,
    280                    uint32_t* buffer_num_bytes);
    281 
    282 // Ends a two-phase write that was previously initiated by
    283 // |MojoBeginWriteData()| for the same |data_pipe_producer_handle|.
    284 //
    285 // |num_bytes_written| must indicate the number of bytes actually written into
    286 // the two-phase write buffer. It must be less than or equal to the value of
    287 // |*buffer_num_bytes| output by |MojoBeginWriteData()|, and it must be a
    288 // multiple of the data pipe's element size.
    289 //
    290 // On failure, the two-phase write (if any) is ended (so the handle may become
    291 // writable again if there's space available) but no data written to |*buffer|
    292 // is "put into" the data pipe.
    293 //
    294 // |options| may be null for default options.
    295 //
    296 // Returns:
    297 //   |MOJO_RESULT_OK| on success.
    298 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
    299 //       |data_pipe_producer_handle| is not a handle to a data pipe producer or
    300 //       |num_bytes_written| is invalid (greater than the maximum value provided
    301 //       by |MojoBeginWriteData()| or not a multiple of the element size).
    302 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer is not in a
    303 //       two-phase write (e.g., |MojoBeginWriteData()| was not called or
    304 //       |MojoEndWriteData()| has already been called).
    305 MOJO_SYSTEM_EXPORT MojoResult
    306 MojoEndWriteData(MojoHandle data_pipe_producer_handle,
    307                  uint32_t num_bytes_written,
    308                  const struct MojoEndWriteDataOptions* options);
    309 
    310 // Reads data from the data pipe consumer given by |data_pipe_consumer_handle|.
    311 // May also be used to discard data or query the amount of data available.
    312 //
    313 // If |options->flags| has neither |MOJO_READ_DATA_FLAG_DISCARD| nor
    314 // |MOJO_READ_DATA_FLAG_QUERY| set, this tries to read up to |*num_bytes| (which
    315 // must be a multiple of the data pipe's element size) bytes of data to
    316 // |elements| and set |*num_bytes| to the amount actually read. If flags has
    317 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set, it will either read exactly
    318 // |*num_bytes| bytes of data or none. Additionally, if flags has
    319 // |MOJO_READ_DATA_FLAG_PEEK| set, the data read will remain in the pipe and be
    320 // available to future reads.
    321 //
    322 // If flags has |MOJO_READ_DATA_FLAG_DISCARD| set, it discards up to
    323 // |*num_bytes| (which again must be a multiple of the element size) bytes of
    324 // data, setting |*num_bytes| to the amount actually discarded. If flags has
    325 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE|, it will either discard exactly
    326 // |*num_bytes| bytes of data or none. In this case, |MOJO_READ_DATA_FLAG_QUERY|
    327 // must not be set, and |elements| is ignored (and should typically be set to
    328 // null).
    329 //
    330 // If flags has |MOJO_READ_DATA_FLAG_QUERY| set, it queries the amount of data
    331 // available, setting |*num_bytes| to the number of bytes available. In this
    332 // case, |MOJO_READ_DATA_FLAG_DISCARD| must not be set, and
    333 // |MOJO_READ_DATA_FLAG_ALL_OR_NONE| is ignored, as are |elements| and the input
    334 // value of |*num_bytes|.
    335 //
    336 // |options| may be null for default options.
    337 //
    338 // Returns:
    339 //   |MOJO_RESULT_OK| on success (see above for a description of the different
    340 //       operations).
    341 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
    342 //       |data_pipe_consumer_handle| is invalid, the combination of flags in
    343 //       |options->flags| is invalid, or |*options| itself is invalid).
    344 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer handle has been
    345 //       closed and data (or the required amount of data) was not available to
    346 //       be read or discarded.
    347 //   |MOJO_RESULT_OUT_OF_RANGE| if |options->flags| has
    348 //       |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set and the required amount of data
    349 //       is not available to be read or discarded and the producer is still
    350 //       open.
    351 //   |MOJO_RESULT_BUSY| if there is a two-phase read ongoing with
    352 //       |data_pipe_consumer_handle| (i.e., |MojoBeginReadData()| has been
    353 //       called, but not yet the matching |MojoEndReadData()|).
    354 //   |MOJO_RESULT_SHOULD_WAIT| if there is no data to be read or discarded (and
    355 //       the producer is still open) and |options->flags| does *not* have
    356 //       |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set.
    357 MOJO_SYSTEM_EXPORT MojoResult
    358 MojoReadData(MojoHandle data_pipe_consumer_handle,
    359              const struct MojoReadDataOptions* options,
    360              void* elements,
    361              uint32_t* num_bytes);
    362 
    363 // Begins a two-phase read from the data pipe consumer given by
    364 // |data_pipe_consumer_handle|. On success, |*buffer| will be a pointer from
    365 // which the caller can read up to |*buffer_num_bytes| bytes of data.
    366 //
    367 // During a two-phase read, |data_pipe_consumer_handle| is *not* readable.
    368 // If another caller tries to read from it by calling |MojoReadData()| or
    369 // |MojoBeginReadData()|, their request will fail with |MOJO_RESULT_BUSY|.
    370 //
    371 // Once the caller has finished reading data from |*buffer|, |MojoEndReadData()|
    372 // must be called to indicate the number of bytes read and to complete the
    373 // two-phase read operation.
    374 //
    375 // |options| may be null for default options.
    376 //
    377 // Returns:
    378 //   |MOJO_RESULT_OK| on success.
    379 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
    380 //       |data_pipe_consumer_handle| is not a handle to a data pipe consumer,
    381 //       or |*options| is invalid.)
    382 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe producer handle has been
    383 //       closed.
    384 //   |MOJO_RESULT_BUSY| if there is already a two-phase read ongoing with
    385 //       |data_pipe_consumer_handle| (i.e., |MojoBeginReadData()| has been
    386 //       called, but not yet the matching |MojoEndReadData()|).
    387 //   |MOJO_RESULT_SHOULD_WAIT| if no data can currently be read (and the
    388 //       producer is still open).
    389 MOJO_SYSTEM_EXPORT MojoResult
    390 MojoBeginReadData(MojoHandle data_pipe_consumer_handle,
    391                   const struct MojoBeginReadDataOptions* options,
    392                   const void** buffer,
    393                   uint32_t* buffer_num_bytes);
    394 
    395 // Ends a two-phase read from the data pipe consumer given by
    396 // |data_pipe_consumer_handle| that was begun by a call to |MojoBeginReadData()|
    397 // on the same handle. |num_bytes_read| should indicate the amount of data
    398 // actually read; it must be less than or equal to the value of
    399 // |*buffer_num_bytes| output by |MojoBeginReadData()| and must be a multiple of
    400 // the element size.
    401 //
    402 // On failure, the two-phase read (if any) is ended (so the handle may become
    403 // readable again) but no data is "removed" from the data pipe.
    404 //
    405 // |options| may be null for default options.
    406 //
    407 // Returns:
    408 //   |MOJO_RESULT_OK| on success.
    409 //   |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g.,
    410 //       |data_pipe_consumer_handle| is not a handle to a data pipe consumer or
    411 //       |num_bytes_written| is greater than the maximum value provided by
    412 //       |MojoBeginReadData()| or not a multiple of the element size).
    413 //   |MOJO_RESULT_FAILED_PRECONDITION| if the data pipe consumer is not in a
    414 //       two-phase read (e.g., |MojoBeginReadData()| was not called or
    415 //       |MojoEndReadData()| has already been called).
    416 MOJO_SYSTEM_EXPORT MojoResult
    417 MojoEndReadData(MojoHandle data_pipe_consumer_handle,
    418                 uint32_t num_bytes_read,
    419                 const struct MojoEndReadDataOptions* options);
    420 
    421 #ifdef __cplusplus
    422 }  // extern "C"
    423 #endif
    424 
    425 #endif  // MOJO_PUBLIC_C_SYSTEM_DATA_PIPE_H_
    426