raz / commit 32c0abb2

commit
32c0abb2f2ef620c83bc21a787dcbc3ddbb988d6
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
47d44dac
Report 2026-10.24: Sys's datagrams served, ports, timeouts, files passed, memory shared; Thread with C

The interfaces first, for review: listenUdp, receive, reply,
localPort, connectTcpWithin, sendFds, receiveFds, memFile, mapFile;
Thread drops link static and uses pthread_create in a program with C.

patch · log

3 files changed, +48 -7

doc/raz-lib.md (history) modified +41 -6

@@ -11,11 +11,11 @@
1111 | Module | What |
1212 |---|---|
1313 | `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 |
1515 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
1616 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
1717 | `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` |
1919 | `Mem` | `Allocator`, the interface of every allocator, and pages from the system |
2020 | `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 |
2121 | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator |
@@ -105,9 +105,11 @@
105105 A `Lock` is for short sections: a thread that finds it taken tries
106106 again, giving up the processor now and then. A `Mutex` sleeps. Each of
107107 `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.
111113
112114 ```
113115 module Out;
@@ -183,6 +185,7 @@
183185 tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
184186 end;
185187 Status* = (running, exited, killed, stopped);
188+ Peer* = record ... end; (* whom a datagram came from *)
186189
187190 proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
188191 proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
@@ -250,6 +253,16 @@
250253 proc listenTcp*(addr: []U8; port: INT): INT;
251254 proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
252255 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 *)
253266 ```
254267
255268 `info` follows a symbolic link: `size`, `dir` and `file` are those of
@@ -344,7 +357,29 @@
344357 `listen` and `listenTcp` are the other side: a server's socket, on
345358 `path`, which must not be there yet, or on `port` at `addr`,
346359 `"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.
348383
349384 `readyOf` waits on several file descriptors at once, as a program does
350385 that listens to a server and a terminal both; a descriptor at its end,

doc/raz.md (history) modified +1 -1

@@ -13,7 +13,7 @@
1313 This report defines the language. It is short on purpose. Whatever it
1414 doesn't allow, raz doesn't have.
1515
16-This is version 2026-10.23 of the report; section 11 says where each
16+This is version 2026-10.24 of the report; section 11 says where each
1717 version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose,
1818 for a language anyone can build again. They break programs written
1919 for 2026-10.10 and before: for 2026-10.9, say, the report of compiler

doc/report-history.md (history) modified +6 -0

@@ -128,3 +128,9 @@
128128 - **2026-10.23.** `Math` gains `bits` and `fromBits`, an `F64`'s bits
129129 and back, and `mul`, the 128-bit product of two `U64`s, for reading
130130 and writing reals exactly (volume 2).
131+- **2026-10.24.** `Sys` gains serving datagrams, `listenUdp`,
132+ `receive` and `reply`; a socket's own port, `localPort`; connecting
133+ with a timeout, `connectTcpWithin`; passing files over a Unix socket,
134+ `sendFds` and `receiveFds`; and memory shared through a file,
135+ `memFile` and `mapFile`. `Thread` works in a program that uses C,
136+ and no longer says `link static` (volume 2).