Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 1 | # Mojo |
[email protected] | 99e508a4 | 2013-12-04 01:15:09 | [diff] [blame] | 2 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 3 | [TOC] |
scheib | aad29cf | 2016-03-31 22:33:50 | [diff] [blame] | 4 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 5 | ## Getting Started With Mojo |
| 6 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 7 | To get started using Mojo in Chromium, the fastest path forward will likely be |
| 8 | to read the Mojo sections of the |
| 9 | [Intro to Mojo & Services](/docs/mojo_and_services.md) guide. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 10 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 11 | For more detailed reference material on the most commonly used features of Mojo, |
| 12 | head directly to the [bindings](#Bindings-APIs) documentation for your language |
| 13 | of choice or the more general |
| 14 | [mojom Interface Definition Language (IDL)](/mojo/public/tools/bindings/README.md) |
| 15 | documentation. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 16 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 17 | If you're looking for information on creating and/or connecting to services, |
| 18 | you're in the wrong place! Mojo does not deal with services, it only facilitates |
| 19 | interface definition, message passing, and other lower-level IPC primitives. |
| 20 | Instead, you should take a look at some of the other available |
| 21 | [Mojo & Services](/docs/README.md#Mojo-Services) documentation. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 22 | |
| 23 | ## System Overview |
| 24 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 25 | Mojo is a collection of runtime libraries providing a platform-agnostic |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 26 | abstraction of common IPC primitives, a message IDL format, and a bindings |
| 27 | library with code generation for multiple target languages to facilitate |
| 28 | convenient message passing across arbitrary inter- and intra-process boundaries. |
| 29 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 30 | The documentation here is segmented according to the different libraries |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 31 | comprising Mojo. Mojo is divided into cleanly-separated layers with the basic |
| 32 | hierarchy of subcomponents as follows: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 33 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 34 |  |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 35 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 36 | ## Mojo Core |
| 37 | In order to use any of the more interesting high-level support libraries like |
| 38 | the System APIs or Bindings APIs, a process must first initialize Mojo Core. |
| 39 | This is a one-time initialization which remains active for the remainder of the |
| 40 | process's lifetime. There are two ways to initialize Mojo Core: via the Embedder |
| 41 | API, or through a dynamically linked library. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 42 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 43 | ### Embedding |
| 44 | Many processes to be interconnected via Mojo are **embedders**, meaning that |
| 45 | they statically link against the `//mojo/core/embedder` target and initialize |
| 46 | Mojo support within each process by calling `mojo::core::Init()`. See |
| 47 | [**Mojo Core Embedder API**](/mojo/core/embedder/README.md) for more details. |
Ken Rockot | 7c05e3de | 2018-06-26 02:54:45 | [diff] [blame] | 48 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 49 | This is a reasonable option when you can guarantee that all interconnected |
| 50 | process binaries are linking against precisely the same revision of Mojo Core. |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 51 | This includes Chromium itself as well as any developer tools and test |
| 52 | executables built within the tree. |
| 53 | |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 54 | To support other scenarios, use dynamic linking. |
Ken Rockot | 7c05e3de | 2018-06-26 02:54:45 | [diff] [blame] | 55 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 56 | ## C System API |
Ken Rockot | 7c05e3de | 2018-06-26 02:54:45 | [diff] [blame] | 57 | Once Mojo is initialized within a process, the public |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 58 | [**C System API**](/mojo/public/c/system/README.md) is usable on any thread for |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 59 | the remainder of the process's lifetime. This encapsulates Mojo Core's stable |
| 60 | ABI and comprises the total public API surface of the Mojo Core library. |
Ken Rockot | dba46db | 2018-07-04 18:41:04 | [diff] [blame] | 61 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 62 | The C System library's only dependency (apart from the system libc and e.g. |
| 63 | pthreads) is Mojo Core itself. As such, it's possible build a fully-featured |
| 64 | multiprocess system using only Mojo Core and its exposed C API. It exposes the |
| 65 | fundamental cross-platform capabilities to create and manipulate Mojo primitives |
| 66 | like **message pipes**, **data pipes**, and **shared buffers**, as well as APIs |
| 67 | to help bootstrap connections among processes. |
| 68 | |
| 69 | Despite this, it's rare for applications to use the C API directly. Instead this |
| 70 | API acts as a stable foundation upon which several higher-level and more |
| 71 | ergonomic Mojo libraries are built. |
Ken Rockot | 7c05e3de | 2018-06-26 02:54:45 | [diff] [blame] | 72 | |
| 73 | ## Platform Support API |
| 74 | Mojo provides a small collection of abstractions around platform-specific IPC |
| 75 | primitives to facilitate bootstrapping Mojo IPC between two processes. See the |
| 76 | [Platform API](/mojo/public/cpp/platform/README.md) documentation for details. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 77 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 78 | ## Higher-Level System APIs |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 79 | There is a relatively small, higher-level system API for each supported |
| 80 | language, built upon the low-level C API. Like the C API, direct usage of these |
| 81 | system APIs is rare compared to the bindings APIs, but it is sometimes desirable |
| 82 | or necessary. |
| 83 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 84 | These APIs provide wrappers around low-level [system API](#C-System-API) |
| 85 | concepts, presenting interfaces that are more idiomatic for the target language: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 86 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 87 | - [**C++ System API**](/mojo/public/cpp/system/README.md) |
| 88 | - [**JavaScript System API**](/third_party/blink/renderer/core/mojo/README.md) |
| 89 | - [**Java System API**](/mojo/public/java/system/README.md) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 90 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 91 | ## Bindings APIs |
| 92 | The [**mojom Interface Definition Language (IDL)**](/mojo/public/tools/bindings/README.md) |
| 93 | is used to generate interface bindings for various languages to send and receive |
| 94 | mojom interface messages using Mojo message pipes. The generated code is |
| 95 | supported by a language-specific bindings API: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 96 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 97 | - [**C++ Bindings API**](/mojo/public/cpp/bindings/README.md) |
| 98 | - [**JavaScript Bindings API**](/mojo/public/js/README.md) |
| 99 | - [**Java Bindings API**](/mojo/public/java/bindings/README.md) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 100 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 101 | Note that the C++ bindings see the broadest usage in Chromium and are thus |
| 102 | naturally the most feature-rich, including support for things like |
| 103 | [associated interfaces](/mojo/public/cpp/bindings/README.md#Associated-Interfaces), |
| 104 | [synchronous calls](/mojo/public/cpp/bindings/README.md#Synchronous-Calls), and |
| 105 | [type-mapping](/mojo/public/cpp/bindings/README.md#Type-Mapping). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 106 | |
| 107 | ## FAQ |
| 108 | |
| 109 | ### Why not protobuf? Why a new thing? |
| 110 | There are number of potentially decent answers to this question, but the |
| 111 | deal-breaker is that a useful IPC mechanism must support transfer of native |
| 112 | object handles (*e.g.* file descriptors) across process boundaries. Other |
| 113 | non-new IPC things that do support this capability (*e.g.* D-Bus) have their own |
| 114 | substantial deficiencies. |
| 115 | |
| 116 | ### Are message pipes expensive? |
| 117 | No. As an implementation detail, creating a message pipe is essentially |
| 118 | generating two random numbers and stuffing them into a hash table, along with a |
| 119 | few tiny heap allocations. |
| 120 | |
| 121 | ### So really, can I create like, thousands of them? |
| 122 | Yes! Nobody will mind. Create millions if you like. (OK but maybe don't.) |
| 123 | |
John Abd-El-Malek | d5cc4a4 | 2017-07-13 23:43:42 | [diff] [blame] | 124 | ### What are the performance characteristics of Mojo? |
| 125 | Compared to the old IPC in Chrome, making a Mojo call is about 1/3 faster and uses |
| 126 | 1/3 fewer context switches. The full data is [available here](https://docs.google.com/document/d/1n7qYjQ5iy8xAkQVMYGqjIy_AXu2_JJtMoAcOOupO_jQ/edit). |
| 127 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 128 | ### Can I use in-process message pipes? |
| 129 | Yes, and message pipe usage is identical regardless of whether the pipe actually |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 130 | crosses a process boundary -- in fact the location of the other end of a pipe is |
| 131 | intentionally obscured, in part for the sake of efficiency, and in part to |
| 132 | discourage tight coupling of application logic to such details. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 133 | |
| 134 | Message pipes which don't cross a process boundary are efficient: sent messages |
| 135 | are never copied, and a write on one end will synchronously modify the message |
| 136 | queue on the other end. When working with generated C++ bindings, for example, |
Oksana Zhuravlova | d5888c9 | 2019-08-23 19:43:06 | [diff] [blame] | 137 | the net result is that a `Remote` on one thread sending a message to a |
| 138 | `Receiver` on another thread (or even the same thread) is effectively a |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 139 | `PostTask` to the `Binding`'s `TaskRunner` with the added -- but often small -- |
| 140 | costs of serialization, deserialization, validation, and some internal routing |
| 141 | logic. |
| 142 | |
| 143 | ### What about ____? |
| 144 | |
| 145 | Please post questions to |
| 146 | [`[email protected]`](https://groups.google.com/a/chromium.org/forum/#!forum/chromium-mojo)! |
| 147 | The list is quite responsive. |
| 148 | |