Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Message pipes | 1 # Message pipes |
| 2 | 2 |
| 3 Message pipes are the primary communication mechanism between Mojo programs. A | 3 Message pipes are the primary communication mechanism between Mojo programs. A |
| 4 *message pipe* is a point-to-point (with each side being called a message pipe | 4 *message pipe* is a point-to-point (with each side being called a message pipe |
| 5 *endpoint*), bidirectional message passing mechanism, where messages may contain | 5 *endpoint*), bidirectional message passing mechanism, where messages may contain |
| 6 both data and Mojo handles. | 6 both data and Mojo handles. |
| 7 | 7 |
| 8 It's important to understand that a message pipe is a "transport": it provides a | 8 It's important to understand that a message pipe is a "transport": it provides a |
| 9 way for data and handles to be sent between Mojo programs. The system is unaware | 9 way for data and handles to be sent between Mojo programs. The system is unaware |
| 10 of the meaning of the data or of the handles (other than their intrinsic | 10 of the meaning of the data or of the handles (other than their intrinsic |
| 11 properties). | 11 properties). |
| 12 | 12 |
| 13 That said, Mojo provides a *standard* way of communicating over message pipes, | 13 That said, Mojo provides a *standard* way of communicating over message pipes, |
| 14 namely via a standardized protocol together with [Mojom](mojom.md) IDL files. | 14 namely via a standardized protocol together with [Mojom IDL](mojom_idl.md) |
| 15 files. | |
| 15 | 16 |
| 16 ## Messages | 17 ## Messages |
| 17 | 18 |
| 18 A *message* consists of two things: | 19 A *message* consists of two things: |
| 19 * a finite sequence of bytes, and | 20 * a finite sequence of bytes, and |
| 20 * a finite sequence of [Mojo handles](handles.md). | 21 * a finite sequence of [Mojo handles](handles.md). |
| 21 | 22 |
| 22 Both of these are determined when the message is sent (or *written*). Messages | 23 Both of these are determined when the message is sent (or *written*). Messages |
| 23 are *framed* in the sense that they are "atomic" units: they are sent and | 24 are *framed* in the sense that they are "atomic" units: they are sent and |
| 24 received (or *read*) as entire units, not just by Mojo programs themselves but | 25 received (or *read*) as entire units, not just by Mojo programs themselves but |
| 25 by the system, which is aware of and enforces the message boundaries. | 26 by the system, which is aware of and enforces the message boundaries. |
| 26 | 27 |
| 27 (Note on terminology: We'll use "send" and "write" interchangeably, and | 28 (Note on terminology: We'll use "send" and "write" interchangeably, and |
| 28 similarly for "receive" and "read". "Write" and "read" correspond more closely | 29 similarly for "receive" and "read". "Write" and "read" correspond more closely |
| 29 to the names usually given to the basic Mojo operations, e.g., | 30 to the names usually given to the basic Mojo operations, e.g., |
| 30 `MojoWriteMessage()` and `MojoReadMessage()`. | 31 `MojoWriteMessage()` and `MojoReadMessage()`. |
| 31 | 32 |
| 32 ## Asynchronous operation and queueing | 33 ## Asynchronous operation and queueing |
| 33 | 34 |
| 34 Message pipes are *asynchronous* in the sense that sent messages do not have | 35 Message pipes are *asynchronous* in the sense that sent messages do not have |
| 35 intrinsic response messages mediated/enforced by the system. (This is different | 36 intrinsic response messages mediated/enforced by the system. (This is different |
| 36 from saying that message write/read are asynchronous operations: these | 37 from saying that message write/read are asynchronous operations: these |
| 37 operations are actually synchronous and complete "immediately". However, note | 38 operations are actually synchronous and complete "immediately". However, note |
| 38 that reading a message is "nonblocking" in the sense that it will fail if a | 39 that reading a message is "nonblocking" in the sense that it will fail if a |
| 39 message is not available to be read. Thus a message must be waited for, and the | 40 message is not available to be read. Thus a message must be waited for, and the |
| 40 combined wait-then-read may form an asynchronous pattern.) | 41 combined wait-then-read may form an asynchronous pattern.) |
|
jln (very slow on Chromium)
2016/07/26 23:20:23
didn't you mean "may form a synchronous pattern" h
vardhan
2016/07/26 23:36:44
yeah, i think synchronous pattern is right
| |
| 41 | 42 |
| 42 To allow message writes to complete immediately, messages are queued. Indeed, | 43 To allow message writes to complete immediately, messages are queued. Indeed, |
| 43 one can understand a message pipe as a pair of queues, one in each direction. | 44 one can understand a message pipe as a pair of queues, one in each direction. |
| 44 Each endpoint has opposite notions of incoming and outgoing queues (recall that | 45 Each endpoint has opposite notions of incoming and outgoing queues (recall that |
| 45 message pipes have a pair of endpoints). | 46 message pipes have a pair of endpoints). |
| 46 | 47 |
| 47 Writing a message to an endpoint then simply entails enqueueing that message on | 48 Writing a message to an endpoint then simply entails enqueueing that message on |
| 48 that endpoint's outgoing queue (which is the *peer* endpoint's incoming queue). | 49 that endpoint's outgoing queue (which is the *peer* endpoint's incoming queue). |
| 49 Reading a message from an endpoint is just dequeueing a message from that | 50 Reading a message from an endpoint is just dequeueing a message from that |
| 50 endpoint's incoming queue (which is the peer endpoint's outgoing queue). | 51 endpoint's incoming queue (which is the peer endpoint's outgoing queue). |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 75 | 76 |
| 76 ## "Synchronous" operation | 77 ## "Synchronous" operation |
| 77 | 78 |
| 78 Though message pipes are asynchronous as discussed above, they may be used in a | 79 Though message pipes are asynchronous as discussed above, they may be used in a |
| 79 synchronous fashion: immediately after sending a *request* message, one can then | 80 synchronous fashion: immediately after sending a *request* message, one can then |
| 80 just block and wait for the *response* message (and then read it and process | 81 just block and wait for the *response* message (and then read it and process |
| 81 it). Of course, this requires that the protocol support this: | 82 it). Of course, this requires that the protocol support this: |
| 82 * Message pipes must be used in a "directional" way: there must be fixed request | 83 * Message pipes must be used in a "directional" way: there must be fixed request |
| 83 and response directions or, equivalently, one endpoint belongs to the *client* | 84 and response directions or, equivalently, one endpoint belongs to the *client* |
| 84 and the other to the *server* (or *impl*). (Historical note: This is the case | 85 and the other to the *server* (or *impl*). (Historical note: This is the case |
| 85 for the current Mojom protocol, but not in previous versions.) The issue here | 86 for the current [Mojom protocol](mojom_protocol.md), but not in previous |
| 86 is that without this, the sender of the request messages may have to process | 87 versions.) The issue here is that without this, the sender of the request |
| 87 incoming request messages from its peer. | 88 messages may have to process incoming request messages from its peer. |
| 88 * Request messages must have unique response messages. (In the Mojom protocol, | 89 * Request messages must have unique response messages. (In the Mojom protocol, |
| 89 request messages have optional unique responses. For messages without | 90 request messages have optional unique responses. For messages without |
| 90 responses, one can just proceed immediately without waiting. However, without | 91 responses, one can just proceed immediately without waiting. However, without |
| 91 response messages there may be flow control issues; again, see above.) The | 92 response messages there may be flow control issues; again, see above.) The |
| 92 important point is that for each request message, there is a well-defined | 93 important point is that for each request message, there is a well-defined |
| 93 number of response messages for each request and not arbitrary "callback" | 94 number of response messages for each request and not arbitrary "callback" |
| 94 messages. | 95 messages. |
| 95 * The sending of a response message must not depend on a future action of the | 96 * The sending of a response message must not depend on a future action of the |
| 96 client. (This is a higher-level semantic that is not enforced by the Mojom | 97 client. (This is a higher-level semantic that is not enforced by the Mojom |
| 97 protocol. E.g., one may define a Mojom interface in which the response to a | 98 protocol. E.g., one may define a Mojom interface in which the response to a |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 109 result in deadlock. (E.g., this is the usual programming model for the | 110 result in deadlock. (E.g., this is the usual programming model for the |
| 110 standard Mojom C++ bindings.) | 111 standard Mojom C++ bindings.) |
| 111 * Even when blocking is permissible, it may not be desirable to do so: | 112 * Even when blocking is permissible, it may not be desirable to do so: |
| 112 advancement of the program then relies on trusting the server to be | 113 advancement of the program then relies on trusting the server to be |
| 113 responsive and send responses in a timely fashion. | 114 responsive and send responses in a timely fashion. |
| 114 * Mixing asynchronous and synchronous operation is problematic: one cannot send | 115 * Mixing asynchronous and synchronous operation is problematic: one cannot send |
| 115 a request and synchronously wait for a response while responses to other | 116 a request and synchronously wait for a response while responses to other |
| 116 messages are still pending. (Theoretically, one could buffer such other | 117 messages are still pending. (Theoretically, one could buffer such other |
| 117 responses until the response to particular request is received, and process | 118 responses until the response to particular request is received, and process |
| 118 those other responses later, but this would be dubious at best.) | 119 those other responses later, but this would be dubious at best.) |
| OLD | NEW |