Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // This file contains types/constants and functions specific to message pipes. | 5 // This file contains types/constants and functions specific to message pipes. |
| 6 // | 6 // |
| 7 // Note: This header should be compilable as C. | 7 // Note: This header should be compilable as C. |
| 8 | 8 |
| 9 #ifndef MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ | 9 #ifndef MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| 10 #define MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ | 10 #define MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| 11 | 11 |
| 12 #include <stdint.h> | 12 #include <stdint.h> |
| 13 | 13 |
| 14 #include "mojo/public/c/system/macros.h" | 14 #include "mojo/public/c/system/macros.h" |
| 15 #include "mojo/public/c/system/system_export.h" | 15 #include "mojo/public/c/system/system_export.h" |
| 16 #include "mojo/public/c/system/types.h" | 16 #include "mojo/public/c/system/types.h" |
| 17 | 17 |
| 18 // |MojoMessageHandle|: Used to refer to message objects created by | |
| 19 // |MojoAllocMessage()| and transferred by |MojoWriteMessageNew()| or | |
| 20 // |MojoReadMessageNew()|. | |
| 21 | |
| 22 typedef uintptr_t MojoMessageHandle; | |
| 23 | |
| 24 #ifdef __cplusplus | |
| 25 const MojoMessageHandle MOJO_MESSAGE_HANDLE_INVALID = 0; | |
| 26 #else | |
| 27 #define MOJO_MESSAGE_HANDLE_INVALID ((MojoMessageHandle)0) | |
| 28 #endif | |
| 29 | |
| 18 // |MojoCreateMessagePipeOptions|: Used to specify creation parameters for a | 30 // |MojoCreateMessagePipeOptions|: Used to specify creation parameters for a |
| 19 // message pipe to |MojoCreateMessagePipe()|. | 31 // message pipe to |MojoCreateMessagePipe()|. |
| 20 // |uint32_t struct_size|: Set to the size of the | 32 // |uint32_t struct_size|: Set to the size of the |
| 21 // |MojoCreateMessagePipeOptions| struct. (Used to allow for future | 33 // |MojoCreateMessagePipeOptions| struct. (Used to allow for future |
| 22 // extensions.) | 34 // extensions.) |
| 23 // |MojoCreateMessagePipeOptionsFlags flags|: Used to specify different modes | 35 // |MojoCreateMessagePipeOptionsFlags flags|: Used to specify different modes |
| 24 // of operation. | 36 // of operation. |
| 25 // |MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE|: No flags; default mode. | 37 // |MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE|: No flags; default mode. |
| 26 // |MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE|: The message pipe | 38 // |MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE|: The message pipe |
| 27 // can be sent over another pipe after it's been read, written or | 39 // can be sent over another pipe after it's been read, written or |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 typedef uint32_t MojoReadMessageFlags; | 84 typedef uint32_t MojoReadMessageFlags; |
| 73 | 85 |
| 74 #ifdef __cplusplus | 86 #ifdef __cplusplus |
| 75 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_NONE = 0; | 87 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_NONE = 0; |
| 76 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_MAY_DISCARD = 1 << 0; | 88 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_MAY_DISCARD = 1 << 0; |
| 77 #else | 89 #else |
| 78 #define MOJO_READ_MESSAGE_FLAG_NONE ((MojoReadMessageFlags)0) | 90 #define MOJO_READ_MESSAGE_FLAG_NONE ((MojoReadMessageFlags)0) |
| 79 #define MOJO_READ_MESSAGE_FLAG_MAY_DISCARD ((MojoReadMessageFlags)1 << 0) | 91 #define MOJO_READ_MESSAGE_FLAG_MAY_DISCARD ((MojoReadMessageFlags)1 << 0) |
| 80 #endif | 92 #endif |
| 81 | 93 |
| 94 // |MojoAllocMessageFlags|: Used to specify different options for | |
| 95 // |MojoAllocMessage()|. | |
| 96 // |MOJO_ALLOC_MESSAGE_FLAG_NONE| - No flags; default mode. | |
| 97 | |
| 98 typedef uint32_t MojoAllocMessageFlags; | |
| 99 | |
| 100 #ifdef __cplusplus | |
| 101 const MojoAllocMessageFlags MOJO_ALLOC_MESSAGE_FLAG_NONE = 0; | |
| 102 #else | |
| 103 #define MOJO_ALLOC_MESSAGE_FLAG_NONE ((MojoAllocMessageFlags)0) | |
| 104 #endif | |
| 105 | |
| 82 #ifdef __cplusplus | 106 #ifdef __cplusplus |
| 83 extern "C" { | 107 extern "C" { |
| 84 #endif | 108 #endif |
| 85 | 109 |
| 86 // Note: See the comment in functions.h about the meaning of the "optional" | 110 // Note: See the comment in functions.h about the meaning of the "optional" |
| 87 // label for pointer parameters. | 111 // label for pointer parameters. |
| 88 | 112 |
| 89 // Creates a message pipe, which is a bidirectional communication channel for | 113 // Creates a message pipe, which is a bidirectional communication channel for |
| 90 // framed data (i.e., messages). Messages can contain plain data and/or Mojo | 114 // framed data (i.e., messages). Messages can contain plain data and/or Mojo |
| 91 // handles. | 115 // handles. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 106 MojoHandle* message_pipe_handle0, // Out. | 130 MojoHandle* message_pipe_handle0, // Out. |
| 107 MojoHandle* message_pipe_handle1); // Out. | 131 MojoHandle* message_pipe_handle1); // Out. |
| 108 | 132 |
| 109 // Writes a message to the message pipe endpoint given by |message_pipe_handle|, | 133 // Writes a message to the message pipe endpoint given by |message_pipe_handle|, |
| 110 // with message data specified by |bytes| of size |num_bytes| and attached | 134 // with message data specified by |bytes| of size |num_bytes| and attached |
| 111 // handles specified by |handles| of count |num_handles|, and options specified | 135 // handles specified by |handles| of count |num_handles|, and options specified |
| 112 // by |flags|. If there is no message data, |bytes| may be null, in which case | 136 // by |flags|. If there is no message data, |bytes| may be null, in which case |
| 113 // |num_bytes| must be zero. If there are no attached handles, |handles| may be | 137 // |num_bytes| must be zero. If there are no attached handles, |handles| may be |
| 114 // null, in which case |num_handles| must be zero. | 138 // null, in which case |num_handles| must be zero. |
| 115 // | 139 // |
| 116 // If handles are attached, on success the handles will no longer be valid (the | 140 // If handles are attached, the handles will no longer be valid (on success the |
| 117 // receiver will receive equivalent, but logically different, handles). Handles | 141 // receiver will receive equivalent, but logically different, handles). Handles |
| 118 // to be sent should not be in simultaneous use (e.g., on another thread). | 142 // to be sent should not be in simultaneous use (e.g., on another thread). |
| 119 // | 143 // |
| 120 // Returns: | 144 // Returns: |
| 121 // |MOJO_RESULT_OK| on success (i.e., the message was enqueued). | 145 // |MOJO_RESULT_OK| on success (i.e., the message was enqueued). |
| 122 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., if | 146 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., if |
| 123 // |message_pipe_handle| is not a valid handle, or some of the | 147 // |message_pipe_handle| is not a valid handle, or some of the |
| 124 // requirements above are not satisfied). | 148 // requirements above are not satisfied). |
| 125 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if some system limit has been reached, or | 149 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if some system limit has been reached, or |
| 126 // the number of handles to send is too large (TODO(vtl): reconsider the | 150 // the number of handles to send is too large (TODO(vtl): reconsider the |
| 127 // latter case). | 151 // latter case). |
| 128 // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. | 152 // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. |
| 129 // Note that closing an endpoint is not necessarily synchronous (e.g., | 153 // Note that closing an endpoint is not necessarily synchronous (e.g., |
| 130 // across processes), so this function may succeed even if the other | 154 // across processes), so this function may succeed even if the other |
| 131 // endpoint has been closed (in which case the message would be dropped). | 155 // endpoint has been closed (in which case the message would be dropped). |
| 132 // |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|. | 156 // |MOJO_RESULT_UNIMPLEMENTED| if an unsupported flag was set in |*options|. |
| 133 // |MOJO_RESULT_BUSY| if some handle to be sent is currently in use. | 157 // |MOJO_RESULT_BUSY| if some handle to be sent is currently in use. |
| 134 // | 158 // |
| 135 // TODO(vtl): Add a notion of capacity for message pipes, and return | 159 // TODO(vtl): Add a notion of capacity for message pipes, and return |
| 136 // |MOJO_RESULT_SHOULD_WAIT| if the message pipe is full. | 160 // |MOJO_RESULT_SHOULD_WAIT| if the message pipe is full. |
| 137 MOJO_SYSTEM_EXPORT MojoResult | 161 MOJO_SYSTEM_EXPORT MojoResult |
| 138 MojoWriteMessage(MojoHandle message_pipe_handle, | 162 MojoWriteMessage(MojoHandle message_pipe_handle, |
| 139 const void* bytes, // Optional. | 163 const void* bytes, // Optional. |
| 140 uint32_t num_bytes, | 164 uint32_t num_bytes, |
| 141 const MojoHandle* handles, // Optional. | 165 const MojoHandle* handles, // Optional. |
| 142 uint32_t num_handles, | 166 uint32_t num_handles, |
| 143 MojoWriteMessageFlags flags); | 167 MojoWriteMessageFlags flags); |
| 144 | 168 |
| 169 // Writes a message to the message pipe endpoint given by |message_pipe_handle|. | |
| 170 // | |
| 171 // |message|: A message object allocated by |MojoAllocMessage()|. Ownership of | |
| 172 // the message is passed into Mojo. | |
| 173 // | |
| 174 // Returns results corresponding to |MojoWriteMessage()| above. | |
| 175 MOJO_SYSTEM_EXPORT MojoResult | |
| 176 MojoWriteMessageNew(MojoHandle message_pipe_handle, | |
| 177 MojoMessageHandle message, | |
| 178 MojoWriteMessageFlags); | |
| 179 | |
| 145 // Reads the next message from a message pipe, or indicates the size of the | 180 // Reads the next message from a message pipe, or indicates the size of the |
| 146 // message if it cannot fit in the provided buffers. The message will be read | 181 // message if it cannot fit in the provided buffers. The message will be read |
| 147 // in its entirety or not at all; if it is not, it will remain enqueued unless | 182 // in its entirety or not at all; if it is not, it will remain enqueued unless |
| 148 // the |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| flag was passed. At most one | 183 // the |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| flag was passed. At most one |
| 149 // message will be consumed from the queue, and the return value will indicate | 184 // message will be consumed from the queue, and the return value will indicate |
| 150 // whether a message was successfully read. | 185 // whether a message was successfully read. |
| 151 // | 186 // |
| 152 // |num_bytes| and |num_handles| are optional in/out parameters that on input | 187 // |num_bytes| and |num_handles| are optional in/out parameters that on input |
| 153 // must be set to the sizes of the |bytes| and |handles| arrays, and on output | 188 // must be set to the sizes of the |bytes| and |handles| arrays, and on output |
| 154 // will be set to the actual number of bytes or handles contained in the | 189 // will be set to the actual number of bytes or handles contained in the |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 174 // TODO(vtl): Reconsider the |MOJO_RESULT_RESOURCE_EXHAUSTED| error code; should | 209 // TODO(vtl): Reconsider the |MOJO_RESULT_RESOURCE_EXHAUSTED| error code; should |
| 175 // distinguish this from the hitting-system-limits case. | 210 // distinguish this from the hitting-system-limits case. |
| 176 MOJO_SYSTEM_EXPORT MojoResult | 211 MOJO_SYSTEM_EXPORT MojoResult |
| 177 MojoReadMessage(MojoHandle message_pipe_handle, | 212 MojoReadMessage(MojoHandle message_pipe_handle, |
| 178 void* bytes, // Optional out. | 213 void* bytes, // Optional out. |
| 179 uint32_t* num_bytes, // Optional in/out. | 214 uint32_t* num_bytes, // Optional in/out. |
| 180 MojoHandle* handles, // Optional out. | 215 MojoHandle* handles, // Optional out. |
| 181 uint32_t* num_handles, // Optional in/out. | 216 uint32_t* num_handles, // Optional in/out. |
| 182 MojoReadMessageFlags flags); | 217 MojoReadMessageFlags flags); |
| 183 | 218 |
| 219 // Reads the next message from a message pipe and returns an address to a | |
|
Sam McNally
2016/04/26 08:20:43
s/an address to//
Ken Rockot(use gerrit already)
2016/04/26 15:28:23
Done
| |
| 220 // message containing the message bytes. The returned message must eventually be | |
| 221 // freed using |MojoFreeMessage()|. | |
| 222 // | |
| 223 // Message payload can be accessed using |MojoGetMessageBuffer()|. | |
| 224 // | |
| 225 // |message_pipe_handle|, |num_bytes|, |handles|, |num_handles|, and |flags| | |
| 226 // correspond to their use in |MojoReadMessage()| above, with the | |
| 227 // exception that |num_bytes| is only an output argument. | |
| 228 // |message| must be non-null unless |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| is | |
| 229 // set in flags. | |
| 230 // | |
| 231 // Return values correspond to the return values for |MojoReadMessage()| above. | |
| 232 // On success (MOJO_RESULT_OK), |*message| will contain a handle to a message | |
| 233 // object which may be passed to |MojoGetMessageBuffer()|. The caller owns the | |
| 234 // message object and is responsible for freeing it via |MojoFreeMessage()|. | |
| 235 MOJO_SYSTEM_EXPORT MojoResult | |
| 236 MojoReadMessageNew(MojoHandle message_pipe_handle, | |
| 237 MojoMessageHandle* message, // Optional out. | |
| 238 uint32_t* num_bytes, // Optional out. | |
| 239 MojoHandle* handles, // Optional out. | |
| 240 uint32_t* num_handles, // Optional in/out. | |
| 241 MojoReadMessageFlags flags); | |
| 242 | |
| 184 // Fuses two message pipe endpoints together. Given two pipes: | 243 // Fuses two message pipe endpoints together. Given two pipes: |
| 185 // | 244 // |
| 186 // A <-> B and C <-> D | 245 // A <-> B and C <-> D |
| 187 // | 246 // |
| 188 // Fusing handle B and handle C results in a single pipe: | 247 // Fusing handle B and handle C results in a single pipe: |
| 189 // | 248 // |
| 190 // A <-> D | 249 // A <-> D |
| 191 // | 250 // |
| 192 // Handles B and C are ALWAYS closed. Any unread messages at C will eventually | 251 // Handles B and C are ALWAYS closed. Any unread messages at C will eventually |
| 193 // be delivered to A, and any unread messages at B will eventually be delivered | 252 // be delivered to A, and any unread messages at B will eventually be delivered |
| 194 // to D. | 253 // to D. |
| 195 // | 254 // |
| 196 // NOTE: A handle may only be fused if it is an open message pipe handle which | 255 // NOTE: A handle may only be fused if it is an open message pipe handle which |
| 197 // has not been written to. | 256 // has not been written to. |
| 198 // | 257 // |
| 199 // Returns: | 258 // Returns: |
| 200 // |MOJO_RESULT_OK| on success. | 259 // |MOJO_RESULT_OK| on success. |
| 201 // |MOJO_RESULT_FAILED_PRECONDITION| if both handles were valid message pipe | 260 // |MOJO_RESULT_FAILED_PRECONDITION| if both handles were valid message pipe |
| 202 // handles but could not be merged (e.g. one of them has been written to). | 261 // handles but could not be merged (e.g. one of them has been written to). |
| 203 // |MOJO_INVALID_ARGUMENT| if either handle is not a fusable message pipe | 262 // |MOJO_INVALID_ARGUMENT| if either handle is not a fusable message pipe |
| 204 // handle. | 263 // handle. |
| 205 MOJO_SYSTEM_EXPORT MojoResult | 264 MOJO_SYSTEM_EXPORT MojoResult |
| 206 MojoFuseMessagePipes(MojoHandle handle0, MojoHandle handle1); | 265 MojoFuseMessagePipes(MojoHandle handle0, MojoHandle handle1); |
| 207 | 266 |
| 267 // Allocates a new message whose ownership may be passed to | |
| 268 // |MojoWriteMessageNew()|. Use |MojoGetMessageBuffer()| to retrieve the address | |
| 269 // of the mutable message payload. | |
| 270 // | |
| 271 // |num_bytes|: The size of the message payload in bytes. | |
| 272 // |handles|: An array of handles to transfer in the message. This takes | |
| 273 // ownership of and invalidates all contained handles. Must be null if and | |
| 274 // only if |num_handles| is 0. | |
| 275 // |num_handles|: The number of handles contained in |handles|. | |
| 276 // |flags|: Must be |MOJO_CREATE_MESSAGE_FLAG_NONE|. | |
| 277 // |message|: The address of a handle to be filled with the allocated message's | |
| 278 // handle. Must be non-null. | |
| 279 // | |
| 280 // Returns: | |
| 281 // |MOJO_RESULT_OK| if the message was successfully allocated. In this case | |
| 282 // |*message| will be populated with a handle to an allocated message | |
| 283 // with a buffer large enough to hold |num_bytes| contiguous bytes. | |
| 284 // |MOJO_RESULT_INVALID_ARGUMENT| if one or more handles in |handles| was | |
| 285 // invalid, or |handles| was null with a non-zero |num_handles|. | |
| 286 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if allocation failed because either | |
| 287 // |num_bytes| or |num_handles| exceeds an implementation-defined maximum. | |
| 288 // |MOJO_RESULT_BUSY| if one or more handles in |handles| cannot be sent at | |
| 289 // the time of this call. | |
| 290 // | |
| 291 // Only upon successful message allocation will all handles in |handles| be | |
| 292 // transferred into the message and invalidated. | |
| 293 MOJO_SYSTEM_EXPORT MojoResult | |
| 294 MojoAllocMessage(uint32_t num_bytes, | |
| 295 const MojoHandle* handles, | |
| 296 uint32_t num_handles, | |
| 297 MojoAllocMessageFlags flags, | |
| 298 MojoMessageHandle* message); // Out | |
| 299 | |
| 300 // Frees a message allocated by |MojoAllocMessage()| or |MojoReadMessageNew()|. | |
| 301 // | |
| 302 // |message|: The message to free. This must correspond to a message previously | |
| 303 // allocated by |MojoAllocMessage()| or |MojoReadMessageNew()|. Note that if | |
| 304 // the message has already been passed to |MojoWriteMessageNew()| it should | |
| 305 // NOT also be freed with this API. | |
| 306 // | |
| 307 // Returns: | |
| 308 // |MOJO_RESULT_OK| if |message| was valid and has been freed. | |
| 309 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| was not a valid message. | |
| 310 MOJO_SYSTEM_EXPORT MojoResult MojoFreeMessage(MojoMessageHandle message); | |
| 311 | |
| 312 // Retrieves the address of mutable message bytes for a message allocated by | |
| 313 // either |MojoAllocMessage()| or |MojoReadMessageNew()|. | |
| 314 // | |
| 315 // Returns: | |
| 316 // |MOJO_RESULT_OK| if |message| is a valid message object. |*buffer| will | |
| 317 // be updated to point to mutable message bytes. | |
| 318 // |MOJO_RESULT_INVALID_ARGUMENT| if |message| is not a valid message object. | |
| 319 MOJO_SYSTEM_EXPORT MojoResult MojoGetMessageBuffer(MojoMessageHandle message, | |
| 320 void** buffer); // Out | |
| 208 | 321 |
| 209 #ifdef __cplusplus | 322 #ifdef __cplusplus |
| 210 } // extern "C" | 323 } // extern "C" |
| 211 #endif | 324 #endif |
| 212 | 325 |
| 213 #endif // MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ | 326 #endif // MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| OLD | NEW |