Viet-Trung Luu | fd103f2 | 2016-03-02 10:10:23 -0800 | [diff] [blame] | 1 | # Mojo handles (objects) |
| 2 | |
Viet-Trung Luu | 377be0b | 2016-03-02 14:08:32 -0800 | [diff] [blame] | 3 | Mojo handles are analogous to Unix file descriptors or Windows `HANDLE`s. That |
| 4 | is, they are basically-opaque integers that a program can use to refer to system |
| 5 | resources. Like their Unix and Windows equivalents, a Mojo handle value only has |
| 6 | meaning within a given process. |
| 7 | |
| 8 | That 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. |