blob: 6a4192963fb3f9a3b99fb6da3d2366dc3e5395a5 [file] [log] [blame] [view]
Ken Rockot686e4132017-04-26 00:03:311# Mojo
[email protected]99e508a42013-12-04 01:15:092
rockotf59d2d62017-04-01 02:49:083[TOC]
scheibaad29cf2016-03-31 22:33:504
rockotf59d2d62017-04-01 02:49:085## Getting Started With Mojo
6
Ken Rockotab035122019-02-06 00:35:247To get started using Mojo in Chromium, the fastest path forward will likely be
8to read the Mojo sections of the
9[Intro to Mojo & Services](/docs/mojo_and_services.md) guide.
rockotf59d2d62017-04-01 02:49:0810
Ken Rockotab035122019-02-06 00:35:2411For more detailed reference material on the most commonly used features of Mojo,
12head directly to the [bindings](#Bindings-APIs) documentation for your language
13of choice or the more general
14[mojom Interface Definition Language (IDL)](/mojo/public/tools/bindings/README.md)
15documentation.
rockotf59d2d62017-04-01 02:49:0816
Ken Rockotab035122019-02-06 00:35:2417If you're looking for information on creating and/or connecting to services,
18you're in the wrong place! Mojo does not deal with services, it only facilitates
19interface definition, message passing, and other lower-level IPC primitives.
20Instead, you should take a look at some of the other available
21[Mojo & Services](/docs/README.md#Mojo-Services) documentation.
rockotf59d2d62017-04-01 02:49:0822
23## System Overview
24
Ken Rockotdba46db2018-07-04 18:41:0425Mojo is a collection of runtime libraries providing a platform-agnostic
rockotf59d2d62017-04-01 02:49:0826abstraction of common IPC primitives, a message IDL format, and a bindings
27library with code generation for multiple target languages to facilitate
28convenient message passing across arbitrary inter- and intra-process boundaries.
29
Ken Rockotdba46db2018-07-04 18:41:0430The documentation here is segmented according to the different libraries
Ken Rockotab035122019-02-06 00:35:2431comprising Mojo. Mojo is divided into cleanly-separated layers with the basic
32hierarchy of subcomponents as follows:
rockotf59d2d62017-04-01 02:49:0833
Ken Rockotab035122019-02-06 00:35:2434![Mojo Library Layering: Core on bottom, language bindings on top, public system support APIs in the middle](/docs/images/mojo_stack.png)
rockotf59d2d62017-04-01 02:49:0835
Ken Rockotdba46db2018-07-04 18:41:0436## Mojo Core
37In order to use any of the more interesting high-level support libraries like
38the System APIs or Bindings APIs, a process must first initialize Mojo Core.
39This is a one-time initialization which remains active for the remainder of the
40process's lifetime. There are two ways to initialize Mojo Core: via the Embedder
41API, or through a dynamically linked library.
rockotf59d2d62017-04-01 02:49:0842
Ken Rockotdba46db2018-07-04 18:41:0443### Embedding
44Many processes to be interconnected via Mojo are **embedders**, meaning that
45they statically link against the `//mojo/core/embedder` target and initialize
46Mojo support within each process by calling `mojo::core::Init()`. See
47[**Mojo Core Embedder API**](/mojo/core/embedder/README.md) for more details.
Ken Rockot7c05e3de2018-06-26 02:54:4548
Ken Rockotdba46db2018-07-04 18:41:0449This is a reasonable option when you can guarantee that all interconnected
50process binaries are linking against precisely the same revision of Mojo Core.
Ken Rockotab035122019-02-06 00:35:2451This includes Chromium itself as well as any developer tools and test
52executables built within the tree.
53
Ken Rockotdba46db2018-07-04 18:41:0454To support other scenarios, use dynamic linking.
Ken Rockot7c05e3de2018-06-26 02:54:4555
rockotf59d2d62017-04-01 02:49:0856## C System API
Ken Rockot7c05e3de2018-06-26 02:54:4557Once Mojo is initialized within a process, the public
Ken Rockot929282c2018-05-02 17:07:2958[**C System API**](/mojo/public/c/system/README.md) is usable on any thread for
Ken Rockotab035122019-02-06 00:35:2459the remainder of the process's lifetime. This encapsulates Mojo Core's stable
60ABI and comprises the total public API surface of the Mojo Core library.
Ken Rockotdba46db2018-07-04 18:41:0461
Ken Rockotab035122019-02-06 00:35:2462The C System library's only dependency (apart from the system libc and e.g.
63pthreads) is Mojo Core itself. As such, it's possible build a fully-featured
64multiprocess system using only Mojo Core and its exposed C API. It exposes the
65fundamental cross-platform capabilities to create and manipulate Mojo primitives
66like **message pipes**, **data pipes**, and **shared buffers**, as well as APIs
67to help bootstrap connections among processes.
68
69Despite this, it's rare for applications to use the C API directly. Instead this
70API acts as a stable foundation upon which several higher-level and more
71ergonomic Mojo libraries are built.
Ken Rockot7c05e3de2018-06-26 02:54:4572
73## Platform Support API
74Mojo provides a small collection of abstractions around platform-specific IPC
75primitives to facilitate bootstrapping Mojo IPC between two processes. See the
76[Platform API](/mojo/public/cpp/platform/README.md) documentation for details.
rockotf59d2d62017-04-01 02:49:0877
Ken Rockotab035122019-02-06 00:35:2478## Higher-Level System APIs
rockotf59d2d62017-04-01 02:49:0879There is a relatively small, higher-level system API for each supported
80language, built upon the low-level C API. Like the C API, direct usage of these
81system APIs is rare compared to the bindings APIs, but it is sometimes desirable
82or necessary.
83
Ken Rockotab035122019-02-06 00:35:2484These APIs provide wrappers around low-level [system API](#C-System-API)
85concepts, presenting interfaces that are more idiomatic for the target language:
rockotf59d2d62017-04-01 02:49:0886
Ken Rockotab035122019-02-06 00:35:2487- [**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)
rockotf59d2d62017-04-01 02:49:0890
Ken Rockotab035122019-02-06 00:35:2491## Bindings APIs
92The [**mojom Interface Definition Language (IDL)**](/mojo/public/tools/bindings/README.md)
93is used to generate interface bindings for various languages to send and receive
94mojom interface messages using Mojo message pipes. The generated code is
95supported by a language-specific bindings API:
rockotf59d2d62017-04-01 02:49:0896
Ken Rockotab035122019-02-06 00:35:2497- [**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)
rockotf59d2d62017-04-01 02:49:08100
Ken Rockotab035122019-02-06 00:35:24101Note that the C++ bindings see the broadest usage in Chromium and are thus
102naturally 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).
rockotf59d2d62017-04-01 02:49:08106
107## FAQ
108
109### Why not protobuf? Why a new thing?
110There are number of potentially decent answers to this question, but the
111deal-breaker is that a useful IPC mechanism must support transfer of native
112object handles (*e.g.* file descriptors) across process boundaries. Other
113non-new IPC things that do support this capability (*e.g.* D-Bus) have their own
114substantial deficiencies.
115
116### Are message pipes expensive?
117No. As an implementation detail, creating a message pipe is essentially
118generating two random numbers and stuffing them into a hash table, along with a
119few tiny heap allocations.
120
121### So really, can I create like, thousands of them?
122Yes! Nobody will mind. Create millions if you like. (OK but maybe don't.)
123
John Abd-El-Malekd5cc4a42017-07-13 23:43:42124### What are the performance characteristics of Mojo?
125Compared to the old IPC in Chrome, making a Mojo call is about 1/3 faster and uses
1261/3 fewer context switches. The full data is [available here](https://docs.google.com/document/d/1n7qYjQ5iy8xAkQVMYGqjIy_AXu2_JJtMoAcOOupO_jQ/edit).
127
rockotf59d2d62017-04-01 02:49:08128### Can I use in-process message pipes?
129Yes, and message pipe usage is identical regardless of whether the pipe actually
Ken Rockotab035122019-02-06 00:35:24130crosses a process boundary -- in fact the location of the other end of a pipe is
131intentionally obscured, in part for the sake of efficiency, and in part to
132discourage tight coupling of application logic to such details.
rockotf59d2d62017-04-01 02:49:08133
134Message pipes which don't cross a process boundary are efficient: sent messages
135are never copied, and a write on one end will synchronously modify the message
136queue on the other end. When working with generated C++ bindings, for example,
Oksana Zhuravlovad5888c92019-08-23 19:43:06137the 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
rockotf59d2d62017-04-01 02:49:08139`PostTask` to the `Binding`'s `TaskRunner` with the added -- but often small --
140costs of serialization, deserialization, validation, and some internal routing
141logic.
142
143### What about ____?
144
145Please post questions to
146[`[email protected]`](https://groups.google.com/a/chromium.org/forum/#!forum/chromium-mojo)!
147The list is quite responsive.
148