| 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 #ifndef MOJO_PUBLIC_C_INCLUDE_MOJO_ENVIRONMENT_ASYNC_WAITER_H_ | |
| 6 #define MOJO_PUBLIC_C_INCLUDE_MOJO_ENVIRONMENT_ASYNC_WAITER_H_ | |
| 7 | |
| 8 #include <mojo/macros.h> | |
| 9 #include <mojo/result.h> | |
| 10 #include <mojo/system/handle.h> | |
| 11 #include <mojo/system/time.h> | |
| 12 #include <stdint.h> | |
| 13 | |
| 14 typedef uint64_t MojoAsyncWaitID; | |
| 15 MOJO_STATIC_ASSERT(sizeof(uintptr_t) <= sizeof(uint64_t), | |
| 16 "uintptr_t larger than uint64_t!"); | |
| 17 | |
| 18 typedef void (*MojoAsyncWaitCallback)(void* closure, MojoResult result); | |
| 19 | |
| 20 // Functions for asynchronously waiting (and cancelling asynchronous waits) on a | |
| 21 // handle. | |
| 22 // | |
| 23 // Thread-safety: | |
| 24 // - |CancelWait(wait_id)| may only be called on the same thread as the | |
| 25 // |AsyncWait()| that provided |wait_id| was called on. | |
| 26 // - A given |MojoAsyncWaiter|'s functions may only be called on the thread(s) | |
| 27 // that it is defined to be valid on (typically including the thread on | |
| 28 // which the |MojoAsyncWaiter| was provided). E.g., a library may require | |
| 29 // initialization with a single |MojoAsyncWaiter| and stipulate that it only | |
| 30 // be used on threads on which that |MojoAsyncWaiter| is valid. | |
| 31 // - If a |MojoAsyncWaiter| is valid on multiple threads, then its functions | |
| 32 // must be thread-safe (subject to the first restriction above). | |
| 33 struct MojoAsyncWaiter { | |
| 34 // Arranges for |callback| to be called on the current thread at some future | |
| 35 // when |handle| satisfies |signals| or it is known that it will never satisfy | |
| 36 // |signals| (with the same behavior as |MojoWait()|). | |
| 37 // | |
| 38 // |callback| will not be called in the nested context of |AsyncWait()|, but | |
| 39 // only, e.g., from some run loop. |callback| is provided with the |closure| | |
| 40 // argument as well as the result of the wait. For each call to |AsyncWait()|, | |
| 41 // |callback| will be called at most once. | |
| 42 // | |
| 43 // |handle| must not be closed or transferred (via |MojoWriteMessage()|; this | |
| 44 // is equivalent to closing the handle) until either the callback has been | |
| 45 // executed or the async wait has been cancelled using the returned (nonzero) | |
| 46 // |MojoAsyncWaitID| (see |CancelWait()|). Otherwise, an invalid (or, worse, | |
| 47 // re-used) handle may be waited on by the implementation of this | |
| 48 // |MojoAsyncWaiter|. | |
| 49 // | |
| 50 // Note that once the callback has been called, the returned |MojoAsyncWaitID| | |
| 51 // becomes invalid. | |
| 52 MojoAsyncWaitID (*AsyncWait)(MojoHandle handle, | |
| 53 MojoHandleSignals signals, | |
| 54 MojoDeadline deadline, | |
| 55 MojoAsyncWaitCallback callback, | |
| 56 void* closure); | |
| 57 | |
| 58 // Cancels an outstanding async wait (specified by |wait_id|) initiated by | |
| 59 // |AsyncWait()|. This may only be called from the same thread on which the | |
| 60 // corresponding |AsyncWait()| was called, and may only be called if the | |
| 61 // callback to |AsyncWait()| has not been called. | |
| 62 // | |
| 63 // Once this has been called, the callback provided to |AsyncWait()| will not | |
| 64 // be called. Moreover, it is then immediately safe to close or transfer the | |
| 65 // handle provided to |AsyncWait()|. (I.e., the implementation of this | |
| 66 // |MojoAsyncWaiter| will no longer wait on, or do anything else with, the | |
| 67 // handle.) | |
| 68 void (*CancelWait)(MojoAsyncWaitID wait_id); | |
| 69 }; | |
| 70 | |
| 71 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_ENVIRONMENT_ASYNC_WAITER_H_ | |
| OLD | NEW |