Commit Graph

14170 Commits

Author SHA1 Message Date
Matthew Lugg e57c557ad4 std.Io.Threaded: hugely improve Windows and NetBSD support
The most interesting thing here is the replacement of the pthread futex
implementation with an implementation based on thread park/unpark APIs.
Thread parking tends to be the primitive provided by systems which do
not have a futex primitive, such as NetBSD, so this implementation is
far more efficient than the pthread one. It is also useful on Windows,
where `RtlWaitOnAddress` is itself a userland implementation based on
thread park/unpark; we can implement it ourselves including support for
features which Windows' implementation lacks, such as cancelation and
waking a number of waiters with 1<n<infinity.

Compared to the pthread implementation, this thread-parking-based one
also supports full robust cancelation. Thread parking also turns out to
be useful for implementing `sleep`, so is now used for that on Windows
and NetBSD.

This commit also introduces proper cancelation support for most Windows
operations. The most notable omission right now is DNS lookups through
`GetAddrInfoEx`, just because they're a little more work due to having
a unique cancelation mechanism---but the machinery is all there, so I'll
finish gluing it together soon.

As of this commit, there are very few parts of `Io.Threaded` which do
not support full robust cancelation. The only ones which actually really
matter (because they could block for a prolonged period of time) are DNS
lookups on Windows (as discussed above) and futex waits on WASM.
2026-01-03 15:45:10 +00:00
Matthew Lugg a1d4120fd9 std.Io.Threaded: update to new internal syscall API 2026-01-03 15:45:10 +00:00
Matthew Lugg 0da5d5f150 std.Io.Threaded: rework cancelation and groups
The goal of this internal refactor is to fix some bugs in cancelation
and allow group tasks to clean up their own resources eagerly. The
latter will become a guarantee of the `std.Io` interface, which is
important so that groups can be used to "detach" tasks.

This commit changes the API which POSIX system calls use internally (the
functions formerly called `beginSyscall` etc), but does not update the
usage sites yet.
2026-01-03 15:45:07 +00:00
Matthew Lugg 04226193cc Revert "Use mmap std.heap.page_allocator impl when compiling for Wasm + libc"
This reverts commit c9fa8e46df.

This commit was failing CI checks. This failure was unfortunately not
noticed before merge, due in part to the build runner bug fixed in the
last commit.
2026-01-03 14:52:17 +00:00
Matthew Lugg d024d9f005 std.Build: crashes in the test runner are fatal errors 2026-01-03 14:52:11 +00:00
Carl Åstholm c9fa8e46df Use mmap std.heap.page_allocator impl when compiling for Wasm + libc
When linking libc, it should be the libc that manages the heap. The main
Wasm memory might have been configured as non-growable, which makes
`WasmAllocator` a poor default and causes the common `DebugAllocator`
use case fail with OOM errors unless the user uses `std_options` to
override the default page allocator. Additionally, on Emscripten,
growing Wasm memory without notifying the JS glue code will cause array
buffers to get detached and lead to spurious crashes.
2026-01-02 16:55:11 -08:00
jedisct1 1bf29757d9 Merge pull request 'crypto.edwards25519: optimize rejectLowOrder' (#30650) from jedisct1/zig:ed25519rej into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30650
2026-01-02 23:37:12 +01:00
Meghan Denny 53ebfde6b4 std: use decl literals to improve endian ergonomics 2026-01-01 20:34:55 -08:00
hixuyuming 0c5f879b98 Fix: tryFindProgram fails when $PATH contains relative paths (#30619)
Fixes a issue in tryFindProgram where it would fail if the PATH environment variable contained relative paths, due to its incorrect assumption that the full_path argument is always absolute path.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30619
Co-authored-by: hixuyuming <hixuyuming@gmail.com>
Co-committed-by: hixuyuming <hixuyuming@gmail.com>
2026-01-02 02:51:16 +01:00
Frank Denis 1baa127c65 crypto.edwards25519: optimize rejectLowOrder
Reject low-order points by checking projective coordinates directly
instead of using affine coordinates.

Equivalent, but saves CPU cycles (~254 field multiplications total
before, 3 field multiplications after).
2026-01-01 23:56:20 +01:00
mercenary eab93d3574 libc: remove log/f, log2/f and log10/f
These symbols are already provided by compiler_rt
2026-01-01 15:25:40 +01:00
mercenary 814b1e9a58 libc: remove fmod, fmodf and fmodl
These symbols are already provided by compiler_rt
2026-01-01 15:18:44 +01:00
Ben Krieger 4ad8bc3413 Make Io.Mutex an extern struct 2025-12-31 16:40:34 -05:00
Steeve Morin 9d497d0d77 Update posix.getRandomBytesDevURandom to use linux.statx
Fixes #30631
2025-12-31 20:20:51 +01:00
Andrew Kelley e956948f99 std: remove fs.getAppDataDir with no replacement
This API is a bit too opinionated for the Zig standard library.
Applications should contain this logic instead.
2025-12-30 16:21:25 -08:00
PeterMcKinnis 96ba0ab930 fix lockStderr API calls in test_runner fuzz code 2025-12-30 15:09:17 -05:00
Andrew Kelley 1eb8fe7c61 Merge pull request 'bsd-futex' (#30626) from mikdusan/zig:bsd-futex into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30626
Reviewed-by: Andrew Kelley <andrewrk@noreply.codeberg.org>
2025-12-30 20:24:00 +01:00
mercenary bffd5c5112 libc: remove exp2 and exp2f
These symbols are already provided by compiler_rt
2025-12-30 18:27:19 +01:00
Andrew Kelley 2a02c7a0d5 std.Io.Group: async and concurrent support Cancelable results
Now, the return type of functions spawned with `Group.async` and
`Group.concurrent` may be anything that coerces to `Io.Cancelable!void`.

Before this commit, group tasks were the only exception to the rule
"error.Canceled should never be swallowed". Now, there is no exception,
and it is enforced with an assertion upon closure completion.

Finally, fixes a case of swallowing error.Canceled in the compiler,
solving a TODO.

There are three ways to handle `error.Canceled`. In order of most
common:
1. Propagate it
2. After receiving it, io.recancel() and then don't propagate it
3. Make it unreachable with io.swapCancelProtection()
2025-12-29 22:51:06 -08:00
Andrew Kelley 2adfd4d107 std.Io: fix and improve Group API
Rename `wait` to `await` to be consistent with Future API. The
convention here is that this set of functionality goes together:
* async/concurrent
* await/cancel

Also rename Select `wait` to `await` for the same reason.

`Group.await` now can return `error.Canceled`. Furthermore,
`Group.await` does not auto-propagate cancelation. Instead, users should
follow the pattern of `defer group.cancel(io);` after initialization,
and doing `try group.await(io);` at the end of the success path.
Advanced logic can choose to do something other than this pattern in the
event of cancelation.

Additionally, fixes a bug in `std.Io.Threaded` future await, in which it
swallowed an `error.Canceled`. Now if a task is canceled while awaiting
a future, after propagating the cancel request, it also recancels,
meaning that the awaiting task will properly detect its own cancelation
at the next cancelation point.

Furthermore, fixes a bug in the compiler where `error.Canceled` was
being swallowed in `dispatchPrelinkWork`.

Finally, fixes std.crypto code that inappropriately used
`catch unreachable` in response to cancelation without even so much as a
comment explaining why it was believed to be unreachable. Now, those
functions have `error.Canceled` in the error set and propagate
cancelation properly.

With this way of doing things, `Group.await` has a nice property: even if
all tasks in the group are CPU bound and without cancelation points, the
`Group.await` can still be canceled. In such case, the task that was
waiting for `await` wakes up with a chance to do some more resource
cleanup tasks, such as canceling more things, before entering the
deferred `Group.cancel` call at which point it has to suspend until the
canceled but uninterruptible CPU bound tasks complete.

closes #30601
2025-12-29 22:47:34 -08:00
Michael Dusan 4b26c49076 dragonfly: make test lib/std/std.zig pass 2025-12-29 22:29:56 -05:00
Michael Dusan 3c851ec396 dragonfly: Io.Threaded: use futex 2025-12-29 22:29:48 -05:00
Michael Dusan 09f06082f0 openbsd: make test lib/std/std.zig pass
* According to OpenBSD's getdents docs indicate the buffer must be
  greater or or equal to the block size associated with the file and to
  refer to stat(2).
* Use S_BLKSIZE, which is 512, instead of @sizeOf(std.c.dirent), which is 280.
* Oddly the other BSDs are not this picky.
2025-12-29 21:40:02 -05:00
Michael Dusan 1ea2d5692d openbsd: init Io.Threaded.argv0 for tests
Some tests (eg. std.process.openExecutable) require argv0 for OpenBSD,
otherwise error.OperationUnsupported is encountered.
2025-12-29 21:06:08 -05:00
Michael Dusan 51728f1053 openbsd: add timespec OMIT and NOW definitions 2025-12-29 21:06:08 -05:00
Michael Dusan ceb59b48b4 openbsd: Io.Threaded: use futex 2025-12-29 21:06:08 -05:00
Andrew Kelley f862762f09 std: fix Progress flickering
move some of the clearing logic from std.Io.Threaded to the Io interface
layer, thus allowing it to be skipped by advanced usage code via calling
the vtable functions directly.

then take advantage of this in std.Progress to avoid clearing the
terminal twice.

closes #30611
2025-12-29 12:49:43 -08:00
mercenary 1c05823ff8 libc: remove exp and expf
These symbols are already provided by compiler_rt
2025-12-29 15:55:18 +01:00
mercenary 5e4aefd2a6 libc: remove bcmp
This symbol is already provided by compiler_rt
2025-12-29 15:54:04 +01:00
Andrew Kelley 744e22b18a build_runner: re-order the help menu
closes #30615
2025-12-28 19:53:42 -08:00
Steven Casper 8d4a9119b8 Properly support passing a packed struct to byteSwapAllFields (#30571)
Reopening [#25698](https://github.com/ziglang/zig/pull/25698)
Closes [#25054](https://github.com/ziglang/zig/issues/25054)

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30571
Reviewed-by: Andrew Kelley <andrewrk@noreply.codeberg.org>
Co-authored-by: Steven Casper <sebastiancasper3@gmail.com>
Co-committed-by: Steven Casper <sebastiancasper3@gmail.com>
2025-12-29 02:36:11 +01:00
Lukas Lalinsky e8a2e6578a Add std.Io.net.Stream.shutdown 2025-12-29 02:20:37 +01:00
nyx-xyn f2f474fc78 std.Io.Dir: pass io to Walker.leave 2025-12-29 00:36:57 +01:00
Michael Dusan b335e52ed6 openbsd: fixup nullz handling in Io.Threaded 2025-12-28 20:20:43 +01:00
Michael Dusan 0ae629ee22 netbsd: use correct symbol for wait4 2025-12-28 20:20:43 +01:00
Andrew Kelley 6984992153 Merge pull request 'std.Io.File.Stat: make access time optional' (#30607) from atime into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30607
2025-12-27 23:35:51 +01:00
unerr 2e73288e63 Fix #30167: std.mem.window returns slices smaller or equal to size 2025-12-27 20:45:26 +01:00
Andrew Kelley 6bf9499c0c Merge pull request 'Optimize {write,take}Leb128' (#30012) from NicoElbers/zig:leb-perf into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30012
Reviewed-by: Andrew Kelley <andrewrk@noreply.codeberg.org>
2025-12-27 20:43:13 +01:00
Lucas Santos ef77cc0de1 Fixes doc comment of Futex.wait
On Windows, changing the value at the target address is not sufficient, there needs to be a wake call.
From [MSDN](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitonaddress):
> If the value at Address differs from the value at CompareAddress, the function returns immediately. If the values are the same, the function does not return until another thread in the same process signals that the value at Address has changed by calling WakeByAddressSingle or WakeByAddressAll or the timeout elapses, whichever comes first.
We could note that this behavior is only observed on Windows. However, if we want the function to have consistent behavior across all platforms, we should require users to call wake. On platforms where changing the value is sufficient to wake the waiting thread, an unblocking of the thread without a matching wake would just be considered a spurious wake.
2025-12-27 20:40:19 +01:00
Aaron Ang 9db3e23e80 std.math.big: require sufficient capacity for aliased params 2025-12-27 20:38:33 +01:00
Andrew Kelley 212968c574 std.Io.Threaded: handle missing atime from statx 2025-12-27 11:18:16 -08:00
Andrew Kelley c0809c9b68 std: add more timespec OMIT and NOW definitions 2025-12-27 11:08:56 -08:00
Andrew Kelley 10e72a8cad std.Io.Threaded: implement fileSetTimestamps on WASI 2025-12-27 11:01:16 -08:00
Andrew Kelley dd98188ce0 std.Io.Threaded: mostly implement fileSetTimestamps for Windows
it's still missing the case of setting to now (which was also not
implemented before)
2025-12-27 10:52:17 -08:00
Andrew Kelley a7c9d11b28 std.Io: make file access time optional
Some filesystems, such as ZFS, do not report atime. It's pretty useless
in general, so make it an optional field in File.Stat.

Also take the opportunity to make setting timestamps API more flexible
and match the APIs widely available, which have UTIME_OMIT and UTIME_NOW
constants that can be independently set for both fields.

This is needed to handle smoothly the case when atime is null.
2025-12-27 10:40:24 -08:00
Andrew Kelley d8460910b4 std.Io.Threaded: init stderr_writer in streaming mode 2025-12-26 19:58:56 -08:00
Andrew Kelley 2fb46b0ed9 std.Progress: recancel in start()
one must never swallow error.Canceled; if you can't handle it, either
temporarily disable it for the function, or recancel if you catch it.
2025-12-26 19:58:56 -08:00
Ryan Liptak 4f8aa8213d Fix for compile error in resinator 2025-12-26 19:58:56 -08:00
Andrew Kelley 252c4e57c6 std.Progress: fix compilation on Windows
by using std.Io.File.readStreaming rather than posix.read
2025-12-26 19:58:56 -08:00
Andrew Kelley 28810f5e3d std.Io.File: add readStreaming
I resisted adding this because it's generally better to create a
File.Reader instead, but we do need a simple wrapper around the vtable
function, and there are use cases for it such as std.Progress.
2025-12-26 19:58:56 -08:00