| | @@ -11,11 +11,11 @@ |
| 11 | 11 | | Module | What | |
| 12 | 12 | |---|---| |
| 13 | 13 | | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | |
| 14 | | -| `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; and sockets. One file per target, and `Sys.raz` over the C library for the others | |
| 14 | +| `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | |
| 15 | 15 | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | |
| 16 | 16 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 17 | 17 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| 18 | | -| `Thread` | threads for a program without C: `spawn`, `join`, and `Mutex`; it says `link static` | |
| 18 | +| `Thread` | threads, `spawn` and `join`, and `Mutex`, in any program; in one that uses C, made by C's `pthread_create` | |
| 19 | 19 | | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | |
| 20 | 20 | | `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program | |
| 21 | 21 | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | |
| | @@ -105,9 +105,11 @@ |
| 105 | 105 | A `Lock` is for short sections: a thread that finds it taken tries |
| 106 | 106 | again, giving up the processor now and then. A `Mutex` sleeps. Each of |
| 107 | 107 | `Thread`'s threads runs on a stack of its own, with a page below it |
| 108 | | -that can't be touched, so that running off the stack stops the program. |
| 109 | | -A program that uses C makes its threads with C's `pthread_create`, and |
| 110 | | -so can't use `Thread`. |
| 108 | +that can't be touched, so that running off the stack stops the program |
| 109 | +with a message, as in the main thread. In a program that uses C, |
| 110 | +`Thread` makes its threads with C's `pthread_create`, so that C's |
| 111 | +library knows of them; in one that doesn't, with the system's own |
| 112 | +calls. The program is the same either way. |
| 111 | 113 | |
| 112 | 114 | ``` |
| 113 | 115 | module Out; |
| | @@ -183,6 +185,7 @@ |
| 183 | 185 | tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) |
| 184 | 186 | end; |
| 185 | 187 | Status* = (running, exited, killed, stopped); |
| 188 | + Peer* = record ... end; (* whom a datagram came from *) |
| 186 | 189 | |
| 187 | 190 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 188 | 191 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| | @@ -250,6 +253,16 @@ |
| 250 | 253 | proc listenTcp*(addr: []U8; port: INT): INT; |
| 251 | 254 | proc accept*(fd: INT): INT; (* the next connection, waiting for one *) |
| 252 | 255 | proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) |
| 256 | +proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) |
| 257 | +proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *) |
| 258 | +proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) |
| 259 | +proc reply*(fd: INT; to: Peer; s: []U8): INT; (* s, as one datagram, to to *) |
| 260 | +proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *) |
| 261 | +proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *) |
| 262 | +proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *) |
| 263 | + |
| 264 | +proc memFile*(size: INT): INT; (* a file of size zero bytes, in memory, with no name: its fd *) |
| 265 | +proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *) |
| 253 | 266 | ``` |
| 254 | 267 | |
| 255 | 268 | `info` follows a symbolic link: `size`, `dir` and `file` are those of |
| | @@ -344,7 +357,29 @@ |
| 344 | 357 | `listen` and `listenTcp` are the other side: a server's socket, on |
| 345 | 358 | `path`, which must not be there yet, or on `port` at `addr`, |
| 346 | 359 | `"0.0.0.0"` or `"::"` for every address the machine has; `accept` |
| 347 | | -gives each connection that comes, as a socket of its own. |
| 360 | +gives each connection that comes, as a socket of its own. A server |
| 361 | +started on port 0 gets a port the system chooses, which `localPort` |
| 362 | +says, as a test does that must not take a port in use. |
| 363 | +`connectTcp` waits as long as the system does, minutes for a host |
| 364 | +that never answers; `connectTcpWithin` gives up after `ms` |
| 365 | +milliseconds, with -110 (ETIMEDOUT). |
| 366 | + |
| 367 | +`listenUdp` is the server's side of `connectUdp`: a socket of |
| 368 | +datagrams from anyone, as a name server has. `receive` gives the next |
| 369 | +datagram, at most `LEN(buf)` bytes of it, the rest lost, and a `Peer` |
| 370 | +that `reply` sends an answer to. |
| 371 | + |
| 372 | +**Passing files and sharing memory.** A program can give another an |
| 373 | +open file over a Unix socket: `sendFds` sends `s`, at least one byte, |
| 374 | +and with it `fds`, at most 16, each a file the other program then has |
| 375 | +open too; `receiveFds` reads into `buf` as `read` does, puts the fds |
| 376 | +that came with it in `fds`, as many as there is room for, closing the |
| 377 | +rest, and says how many bytes and how many fds. Each is closed in a |
| 378 | +program exec starts. `memFile` makes a file that lives in memory, of |
| 379 | +`size` zeroed bytes, and `mapFile` maps it, or any file, so that |
| 380 | +what one program writes there another sees at once: so a program hands |
| 381 | +an X server or a Wayland compositor its pixels, by `sendFds` of a |
| 382 | +`memFile` both map, rather than by copying them down a socket. |
| 348 | 383 | |
| 349 | 384 | `readyOf` waits on several file descriptors at once, as a program does |
| 350 | 385 | that listens to a server and a terminal both; a descriptor at its end, |