OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef MOJO_PUBLIC_SYSTEM_CORE_H_ | 5 #ifndef MOJO_PUBLIC_SYSTEM_CORE_H_ |
6 #define MOJO_PUBLIC_SYSTEM_CORE_H_ | 6 #define MOJO_PUBLIC_SYSTEM_CORE_H_ |
7 | 7 |
| 8 // Note: This header should be compilable as C. |
| 9 |
8 #include <stdint.h> | 10 #include <stdint.h> |
9 | 11 |
| 12 // Types ----------------------------------------------------------------------- |
| 13 |
| 14 // TODO(vtl): Notes: Use of undefined flags will lead to undefined behavior |
| 15 // (typically they'll be ignored), not necessarily an error. |
| 16 |
| 17 // Handles to Mojo objects. |
10 typedef uint32_t MojoHandle; | 18 typedef uint32_t MojoHandle; |
11 | 19 |
12 #ifdef __cplusplus | 20 // Result codes for Mojo operations. Non-negative values are success codes; |
| 21 // negative values are error/failure codes. |
| 22 typedef int32_t MojoResult; |
| 23 |
| 24 // Used to specify deadlines (timeouts), in microseconds. Note that |
| 25 // |MOJO_DEADLINE_INDEFINITE| (-1) means "forever". |
| 26 typedef uint64_t MojoDeadline; |
| 27 |
| 28 // Used to specify the state of a handle to wait on (e.g., the ability to read |
| 29 // or write to it). |
| 30 typedef uint32_t MojoWaitFlags; |
| 31 |
| 32 // Used to specify different modes to |MojoWriteMessage()|. |
| 33 typedef uint32_t MojoWriteMessageFlags; |
| 34 |
| 35 // Used to specify different modes to |MojoReadMessage()|. |
| 36 typedef uint32_t MojoReadMessageFlags; |
| 37 |
| 38 // Constants ------------------------------------------------------------------- |
| 39 |
| 40 // |MojoHandle|: |
| 41 // |MOJO_HANDLE_INVALID| - A value that is never a valid handle. |
| 42 #ifdef __cplusplus |
| 43 const MojoHandle MOJO_HANDLE_INVALID = 0; |
| 44 #else |
| 45 #define MOJO_HANDLE_INVALID ((MojoHandle) 0) |
| 46 #endif |
| 47 |
| 48 // |MojoResult|: |
| 49 // |MOJO_RESULT_OK| - Not an error; returned on success. Note that positive |
| 50 // |MojoResult|s may also be used to indicate success. |
| 51 // |MOJO_RESULT_CANCELLED| - Operation was cancelled, typically by the |
| 52 // caller. |
| 53 // |MOJO_RESULT_UNKNOWN| - Unknown error (e.g., if not enough information is |
| 54 // available for a more specific error). |
| 55 // |MOJO_RESULT_INVALID_ARGUMENT| - Caller specified an invalid argument. |
| 56 // This differs from |MOJO_RESULT_FAILED_PRECONDITION| in that the former |
| 57 // indicates arguments that are invalid regardless of the state of the |
| 58 // system. |
| 59 // |MOJO_RESULT_DEADLINE_EXCEEDED| - Deadline expired before the operation |
| 60 // could complete. |
| 61 // |MOJO_RESULT_NOT_FOUND| - Some requested entity was not found (i.e., does |
| 62 // not exist). |
| 63 // |MOJO_RESULT_ALREADY_EXISTS| - Some entity or condition that we attempted |
| 64 // to create already exists. |
| 65 // |MOJO_RESULT_PERMISSION_DENIED| - The caller does not have permission to |
| 66 // for the operation (use |MOJO_RESULT_RESOURCE_EXHAUSTED| for rejections |
| 67 // caused by exhausting some resource instead). |
| 68 // |MOJO_RESULT_RESOURCE_EXHAUSTED| - Some resource required for the call |
| 69 // (possibly some quota) has been exhausted. |
| 70 // |MOJO_RESULT_FAILED_PRECONDITION| - The system is not in a state required |
| 71 // for the operation (use this if the caller must do something to rectify |
| 72 // the state before retrying). |
| 73 // |MOJO_RESULT_ABORTED| - The operation was aborted by the system, possibly |
| 74 // due to a concurrency issue (use this if the caller may retry at a |
| 75 // higher level). |
| 76 // |MOJO_RESULT_OUT_OF_RANGE| - The operation was attempted past the valid |
| 77 // range. Unlike |MOJO_RESULT_INVALID_ARGUMENT|, this indicates that the |
| 78 // operation may be/become valid depending on the system state. (This |
| 79 // error is similar to |MOJO_RESULT_FAILED_PRECONDITION|, but is more |
| 80 // specific.) |
| 81 // |MOJO_RESULT_UNIMPLEMENTED| - The operation is not implemented, supported, |
| 82 // or enabled. |
| 83 // |MOJO_RESULT_INTERNAL| - Internal error: this should never happen and |
| 84 // indicates that some invariant expected by the system has been broken. |
| 85 // |MOJO_RESULT_UNAVAILABLE| - The operation is (temporarily) currently |
| 86 // unavailable. The caller may simply retry the operation (possibly with |
| 87 // a backoff). |
| 88 // |MOJO_RESULT_DATA_LOSS| - Unrecoverable data loss or corruption. |
| 89 // |
| 90 // Note that positive values are also available as success codes. |
| 91 // |
| 92 // The codes from |MOJO_RESULT_OK| to |MOJO_RESULT_DATA_LOSS| come from |
| 93 // Google3's canonical error codes. |
| 94 #ifdef __cplusplus |
| 95 const MojoResult MOJO_RESULT_OK = 0; |
| 96 const MojoResult MOJO_RESULT_CANCELLED = -1; |
| 97 const MojoResult MOJO_RESULT_UNKNOWN = -2; |
| 98 const MojoResult MOJO_RESULT_INVALID_ARGUMENT = -3; |
| 99 const MojoResult MOJO_RESULT_DEADLINE_EXCEEDED = -4; |
| 100 const MojoResult MOJO_RESULT_NOT_FOUND = -5; |
| 101 const MojoResult MOJO_RESULT_ALREADY_EXISTS = -6; |
| 102 const MojoResult MOJO_RESULT_PERMISSION_DENIED = -7; |
| 103 const MojoResult MOJO_RESULT_RESOURCE_EXHAUSTED = -8; |
| 104 const MojoResult MOJO_RESULT_FAILED_PRECONDITION = -9; |
| 105 const MojoResult MOJO_RESULT_ABORTED = -10; |
| 106 const MojoResult MOJO_RESULT_OUT_OF_RANGE = -11; |
| 107 const MojoResult MOJO_RESULT_UNIMPLEMENTED = -12; |
| 108 const MojoResult MOJO_RESULT_INTERNAL = -13; |
| 109 const MojoResult MOJO_RESULT_UNAVAILABLE = -14; |
| 110 const MojoResult MOJO_RESULT_DATA_LOSS = -15; |
| 111 #else |
| 112 #define MOJO_RESULT_OK ((MojoResult) 0) |
| 113 #define MOJO_RESULT_CANCELLED ((MojoResult) -1) |
| 114 #define MOJO_RESULT_UNKNOWN ((MojoResult) -2) |
| 115 #define MOJO_RESULT_INVALID_ARGUMENT ((MojoResult) -3) |
| 116 #define MOJO_RESULT_DEADLINE_EXCEEDED ((MojoResult) -4) |
| 117 #define MOJO_RESULT_NOT_FOUND ((MojoResult) -5) |
| 118 #define MOJO_RESULT_ALREADY_EXISTS ((MojoResult) -6) |
| 119 #define MOJO_RESULT_PERMISSION_DENIED ((MojoResult) -7) |
| 120 #define MOJO_RESULT_RESOURCE_EXHAUSTED ((MojoResult) -8) |
| 121 #define MOJO_RESULT_FAILED_PRECONDITION ((MojoResult) -9) |
| 122 #define MOJO_RESULT_ABORTED ((MojoResult) -10) |
| 123 #define MOJO_RESULT_OUT_OF_RANGE ((MojoResult) -11) |
| 124 #define MOJO_RESULT_UNIMPLEMENTED ((MojoResult) -12) |
| 125 #define MOJO_RESULT_INTERNAL ((MojoResult) -13) |
| 126 #define MOJO_RESULT_UNAVAILABLE ((MojoResult) -14) |
| 127 #define MOJO_RESULT_DATA_LOSS ((MojoResult) -15) |
| 128 #endif |
| 129 |
| 130 // |MojoDeadline|: |
| 131 // |MOJO_DEADLINE_INDEFINITE| |
| 132 #ifdef __cplusplus |
| 133 const MojoDeadline MOJO_DEADLINE_INDEFINITE = static_cast<MojoDeadline>(-1); |
| 134 #else |
| 135 #define MOJO_DEADLINE_INDEFINITE = ((MojoDeadline) -1); |
| 136 #endif |
| 137 |
| 138 // |MojoWaitFlags|: |
| 139 // |MOJO_WAIT_FLAG_NONE| - No flags. |MojoWait()|, etc. will return |
| 140 // |MOJO_RESULT_FAILED_PRECONDITION| if you attempt to wait on this. |
| 141 // |MOJO_WAIT_FLAG_READABLE| - Can read (e.g., a message) from the handle. |
| 142 // |MOJO_WAIT_FLAG_WRITABLE| - Can write (e.g., a message) to the handle. |
| 143 // |MOJO_WAIT_FLAG_EVERYTHING| - All flags. |
| 144 #ifdef __cplusplus |
| 145 const MojoWaitFlags MOJO_WAIT_FLAG_NONE = 0; |
| 146 const MojoWaitFlags MOJO_WAIT_FLAG_READABLE = 1 << 0; |
| 147 const MojoWaitFlags MOJO_WAIT_FLAG_WRITABLE = 1 << 1; |
| 148 const MojoWaitFlags MOJO_WAIT_FLAG_EVERYTHING = ~0; |
| 149 #else |
| 150 #define MOJO_WAIT_FLAG_NONE ((MojoWaitFlags) 0) |
| 151 #define MOJO_WAIT_FLAG_READABLE ((MojoWaitFlags) 1 << 0) |
| 152 #define MOJO_WAIT_FLAG_WRITABLE ((MojoWaitFlags) 1 << 1) |
| 153 #define MOJO_WAIT_FLAG_EVERYTHING (~((MojoWaitFlags) 0)) |
| 154 #endif |
| 155 |
| 156 // |MojoWriteMessageFlags|: |
| 157 // |MOJO_WRITE_MESSAGE_FLAG_NONE| - No flags; default mode. |
| 158 #ifdef __cplusplus |
| 159 const MojoWriteMessageFlags MOJO_WRITE_MESSAGE_FLAG_NONE = 0; |
| 160 #else |
| 161 #define MOJO_WRITE_MESSAGE_FLAG_NONE ((MojoWriteMessageFlags) 0) |
| 162 #endif |
| 163 |
| 164 // |MojoReadMessageFlags|: |
| 165 // |MOJO_READ_MESSAGE_FLAG_NONE| - No flags; default mode. |
| 166 // |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| - If the message is unable to be read |
| 167 // for whatever reason (e.g., the caller-supplied buffer is too small), |
| 168 // discard the message (i.e., simply dequeue it). |
| 169 #ifdef __cplusplus |
| 170 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_NONE = 0; |
| 171 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_MAY_DISCARD = 1 << 0; |
| 172 #else |
| 173 #define MOJO_READ_MESSAGE_FLAG_NONE ((MojoReadMessageFlags) 0) |
| 174 #define MOJO_READ_MESSAGE_FLAG_MAY_DISCARD ((MojoReadMessageFlags) 1 << 0) |
| 175 #endif |
| 176 |
| 177 // Functions ------------------------------------------------------------------- |
| 178 |
| 179 #ifdef __cplusplus |
| 180 extern "C" { |
| 181 #endif |
| 182 |
| 183 // Closes the given |handle|. |
| 184 // |
| 185 // Returns: |
| 186 // |MOJO_RESULT_OK| on success. |
| 187 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle. |
| 188 // |
| 189 // Concurrent operations on |handle| may succeed (or fail as usual) if they |
| 190 // happen before the close, be cancelled with result |MOJO_RESULT_CANCELLED| if |
| 191 // they properly overlap (this is likely the case with |MojoWait()|, etc.), or |
| 192 // fail with |MOJO_RESULT_INVALID_ARGUMENT| if they happen after. |
| 193 MojoResult MojoClose(MojoHandle handle); |
| 194 |
| 195 // Waits on the given handle until the state indicated by |flags| is satisfied |
| 196 // or until |deadline| has passed. |
| 197 // |
| 198 // Returns: |
| 199 // |MOJO_RESULT_OK| if some flag in |flags| was satisfied (or is already |
| 200 // satisfied). |
| 201 // |MOJO_RESULT_INVALID_ARGUMENT| if |handle| is not a valid handle (e.g., if |
| 202 // it has already been closed). |
| 203 // |MOJO_RESULT_DEADLINE_EXCEEDED| if the deadline has passed without any of |
| 204 // the flags being satisfied. |
| 205 // |MOJO_RESULT_FAILED_PRECONDITION| if it is or becomes impossible that any |
| 206 // flag in |flags| will ever be satisfied. |
| 207 // |
| 208 // If there are multiple waiters (on different threads, obviously) waiting on |
| 209 // the same handle and flag and that flag becomes set, all waiters will be |
| 210 // awoken. |
| 211 MojoResult MojoWait(MojoHandle handle, |
| 212 MojoWaitFlags flags, |
| 213 MojoDeadline deadline); |
| 214 |
| 215 // Waits on |handles[0]|, ..., |handles[num_handles-1]| for at least one of them |
| 216 // to satisfy the state indicated by |flags[0]|, ..., |flags[num_handles-1]|, |
| 217 // respectively, or until |deadline| has passed. |
| 218 // |
| 219 // Returns: |
| 220 // The index |i| (from 0 to |num_handles-1|) if |handle[i]| satisfies |
| 221 // |flags[i]|. |
| 222 // |MOJO_RESULT_INVALID_ARGUMENT| if some |handle[i]| is not a valid handle |
| 223 // (e.g., if it has already been closed). |
| 224 // |MOJO_RESULT_DEADLINE_EXCEEDED| if the deadline has passed without any of |
| 225 // handles satisfying any of its flags. |
| 226 // |MOJO_RESULT_FAILED_PRECONDITION| if it is or becomes impossible that SOME |
| 227 // |handle[i]| will ever satisfy any of its flags |flags[i]|. |
| 228 MojoResult MojoWaitMany(const MojoHandle* handles, |
| 229 const MojoWaitFlags* flags, |
| 230 uint32_t num_handles, |
| 231 MojoDeadline deadline); |
| 232 |
| 233 // TODO(vtl): flags? other params (e.g., queue sizes, max message sizes?) |
| 234 MojoResult MojoCreateMessagePipe(MojoHandle* handle_0, MojoHandle* handle_1); |
| 235 |
| 236 MojoResult MojoWriteMessage(MojoHandle handle, |
| 237 const void* bytes, uint32_t num_bytes, |
| 238 const MojoHandle* handles, uint32_t num_handles, |
| 239 MojoWriteMessageFlags flags); |
| 240 |
| 241 MojoResult MojoReadMessage(MojoHandle handle, |
| 242 void* bytes, uint32_t* num_bytes, |
| 243 MojoHandle* handles, uint32_t* num_handles, |
| 244 MojoReadMessageFlags flags); |
| 245 |
| 246 #ifdef __cplusplus |
| 247 } // extern "C" |
| 248 #endif |
| 249 |
| 250 // C++ wrapper functions ------------------------------------------------------- |
| 251 |
| 252 #ifdef __cplusplus |
| 253 |
13 namespace mojo { | 254 namespace mojo { |
14 | 255 |
| 256 // Used to assert things at compile time. (Use our own copy instead of |
| 257 // Chromium's, since we can't depend on Chromium.) |
| 258 template <bool> struct CompileAssert {}; |
| 259 #define MOJO_COMPILE_ASSERT(expr, msg) \ |
| 260 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] |
| 261 |
15 struct Handle { MojoHandle value; }; | 262 struct Handle { MojoHandle value; }; |
16 | 263 |
| 264 const Handle kInvalidHandle = { MOJO_HANDLE_INVALID }; |
| 265 |
| 266 // A |mojo::Handle| must take no extra space, since we'll treat arrays of them |
| 267 // as if they were arrays of |MojoHandle|s. |
| 268 MOJO_COMPILE_ASSERT(sizeof(Handle) == sizeof(MojoHandle), |
| 269 bad_size_for_cplusplus_handle); |
| 270 |
| 271 inline MojoResult Close(Handle handle) { |
| 272 return MojoClose(handle.value); |
| 273 } |
| 274 |
| 275 inline MojoResult Wait(Handle handle, |
| 276 MojoWaitFlags flags, |
| 277 MojoDeadline deadline) { |
| 278 return MojoWait(handle.value, flags, deadline); |
| 279 } |
| 280 |
| 281 inline MojoResult WaitMany(const Handle* handles, |
| 282 const MojoWaitFlags* flags, |
| 283 uint32_t num_handles, |
| 284 MojoDeadline deadline) { |
| 285 return MojoWaitMany(&handles[0].value, flags, num_handles, deadline); |
| 286 } |
| 287 |
| 288 inline MojoResult CreateMessagePipe(Handle* handle_0, Handle* handle_1) { |
| 289 return MojoCreateMessagePipe(&handle_0->value, &handle_1->value); |
| 290 } |
| 291 |
| 292 inline MojoResult WriteMessage(Handle handle, |
| 293 const void* bytes, uint32_t num_bytes, |
| 294 const Handle* handles, uint32_t num_handles, |
| 295 MojoWriteMessageFlags flags) { |
| 296 return MojoWriteMessage(handle.value, |
| 297 bytes, num_bytes, |
| 298 &handles[0].value, num_handles, |
| 299 flags); |
| 300 } |
| 301 |
| 302 inline MojoResult ReadMessage(Handle handle, |
| 303 void* bytes, uint32_t* num_bytes, |
| 304 Handle* handles, uint32_t* num_handles, |
| 305 MojoReadMessageFlags flags) { |
| 306 return MojoReadMessage(handle.value, |
| 307 bytes, num_bytes, |
| 308 &handles[0].value, num_handles, |
| 309 flags); |
| 310 } |
| 311 |
17 } // namespace mojo | 312 } // namespace mojo |
18 #endif | 313 #endif |
19 | 314 |
20 #endif // MOJO_PUBLIC_SYSTEM_CORE_H_ | 315 #endif // MOJO_PUBLIC_SYSTEM_CORE_H_ |
OLD | NEW |