blob: b0b9459d7d301f155f92b94abbc63af3dc97c2ce [file] [log] [blame] [view]
Viet-Trung Luufd103f22016-03-02 10:10:23 -08001# Mojo handles (objects)
2
Viet-Trung Luu377be0b2016-03-02 14:08:32 -08003Mojo handles are analogous to Unix file descriptors or Windows `HANDLE`s. That
4is, they are basically-opaque integers that a program can use to refer to system
5resources. Like their Unix and Windows equivalents, a Mojo handle value only has
6meaning within a given process.
7
8That said, there are some differences:
9* There is a single value, 0, that is guaranteed to never be a valid Mojo
10 handle. This is unlike Unix, where all negative file descriptors are usually
11 taken to be invalid, even if -1 is often taken to be the "canonical" invalid
12 file descriptor value.
13* The allocation of Mojo handle values is not specified. This is unlike Unix,
14 where file descriptors are allocated sequentially, with the lowest (positive)
15 unused value allocated.
16* Unlike Windows, there are no "pseudohandles". That is, there is no Mojo handle
17 value whose meaning is context dependent (within the same process).
18* In general, Mojo handles need not be duplicatable, whereas their Unix and
19 Windows equivalents can universally be duplicated.
20* Mojo handles can be sent across [message pipes](message_pipes.md). Unlike
21 sending file descriptors over Unix domain sockets (using `SCM_RIGHTS`), this
22 is done with transfer semantics: after a message with attached Mojo handles is
23 sent, the Mojo handle values become invalid in the sending process. (Even if
24 the receiving process is the same as the sending process, the received Mojo
25 handle values will probably be different from the values that were sent.)
26* Mojo handles have a well-defined life-cycle, and are only invalidated by
27 either transfer across a message pipe (as above) or by closing them. Unlike
28 Unix's overloaded `close()` (which may fail due to data loss, i.e., inability
29 to flush), closing a (valid) Mojo handle never fails.