Commit graph

138 commits

Author SHA1 Message Date
Andrew Kelley
c4f5dda135 std.Io.Threaded: re-introduce retry logic behind config 2025-12-01 19:17:52 -08:00
Andrew Kelley
de87bad4c3 std.Io.Threaded: don't solve the cancel race after all
Unfortunately, trying again until the cancellation request is
acknowledged has been observed to incur a large amount of overhead,
and usually strong cancellation guarantees are not needed, so the
race condition is not handled here. Users who want to avoid this
have this menu of options instead:
* Use no libc, in which case Zig std lib can avoid the race (tracking
  issue: https://codeberg.org/ziglang/zig/issues/30049)
* Use musl libc
* Use `std.Io.Evented`. But this is not implemented yet. Tracked by
  - https://codeberg.org/ziglang/zig/issues/30050
  - https://codeberg.org/ziglang/zig/issues/30051

glibc + threaded is the only problematic combination.
2025-12-01 19:17:52 -08:00
Andrew Kelley
144206856e std.Io.Threaded: fix compilation for riscv32-linux 2025-12-01 19:17:52 -08:00
Andrew Kelley
39ac40209b std.Io.Threaded: use musl's beautiful pthread_cancel semantics 2025-12-01 19:17:52 -08:00
Andrew Kelley
d60760d61e std.Io.Threaded: tune requestCancel
On a heavily loaded Linux 6.17.5, I observed a maximum of 20 attempts
not acknowledged before the timeout (including exponential backoff) was
sufficient, despite the heavy load.

The time wasted here sleeping is mitigated by the fact that, later on,
the system will likely wait for the canceled task, causing it to
indefinitely yield until the canceled task finishes, and the task must
acknowledge the cancel before it proceeds to that point.
2025-12-01 19:17:52 -08:00
Andrew Kelley
29e418cbfb std.Io.Threaded: fix the cancellation race
Now, before a syscall is entered, beginSyscall is called, which may
return error.Canceled. After syscall returns, whether error or success,
endSyscall is called. If the syscall returns EINTR then checkCancel is
called.

`cancelRequested` is removed from the std.Io VTable for now, with plans
to replace it with a more powerful API that allows protection against
cancellation requests.

closes #25751
2025-12-01 19:17:52 -08:00
Nikolay Govorov
a0289d0cce std.posix.accept: handle non-listening socket EINVAL 2025-11-29 19:57:59 +01:00
Zihad
e4be00f949 std.Io.Threaded: fix QueryPerformanceCounter usage
Co-authored-by: Andrew Kelley <andrew@ziglang.org>
2025-11-25 03:46:24 -08:00
Andrew Kelley
5816646aa0
Merge pull request #26037 from ziglang/build-runner-std.Io
build runner: update from std.Thread.Pool to std.Io
2025-11-25 02:49:37 -08:00
Ryan Liptak
53e615b920
Merge pull request #25993 from squeek502/windows-paths
Teach `std.fs.path` about the wonderful world of Windows paths
2025-11-24 15:27:24 -08:00
Andrew Kelley
a242292644 build runner: update from std.Thread.Pool to std.Io 2025-11-24 14:34:18 -08:00
Andrew Kelley
32dc46aae5 std.Io: add Group.concurrent
A function that participates in a group but guarantees allocation of one
unit of concurrency, or returns an error.
2025-11-24 14:33:16 -08:00
Andrew Kelley
eb038ffbc1 std.Io.Threaded: forward cancellation requests to awaited tasks 2025-11-21 19:54:41 -08:00
Andrew Kelley
b052afd24b std.Io.Threaded: import std.mem.Alignment 2025-11-21 19:54:41 -08:00
Andrew Kelley
cf744aa182 std.Io.Threaded: slightly different semantics
while still preserving the guarantee about async() being assigned a unit
of concurrency (or immediately running the task), this change:
* retains the error from calling getCpuCount()
* spawns all threads in detached mode, using WaitGroup to join them
* treats all workers the same regardless of whether they are processing
  concurrent or async tasks. one thread pool does all the work, while
  respecting async and concurrent limits.
2025-11-21 19:54:41 -08:00
Andrew Kelley
13b537d77c std.Io.Threaded: remove dead code 2025-11-21 19:54:41 -08:00
Andrew Kelley
aae85a4130 std.Io.Threaded: allow calling init in single-threaded mode 2025-11-21 19:54:41 -08:00
Loris Cro
69f9395b38 fix logic bug in groupAsync 2025-11-21 19:54:41 -08:00
Loris Cro
ff883dd6ce fix single-threaded builds 2025-11-21 19:54:41 -08:00
Loris Cro
8eaebf5939 Io.Threaded PoC reimplementation
This is a reimplementation of Io.Threaded that fixes the issues
highlighted in the recent Zulip discussion. It's poorly tested but it
does successfully run to completion the litmust test example that I
offered in the discussion.

This implementation has the following key design decisions:

- `t.cpu_count` is used as the threadpool size.
- `t.concurrency_limit` is used as the maximum number of
  "burst, one-shot" threads that can be spawned by `io.concurrent` past
  `t.cpu_count`.
- `t.available_thread_count` is the number of threads in the pool that
  is not currently busy with work (the bookkeeping happens in the worker
  function).
- `t.one_shot_thread_count` is the number of active threads that were
  spawned by `io.concurrent` past `t.cpu_count`.

In this implementation:

- `io.async` first tries to decrement `t.available_thread_count`. If
  there are no threads available, it tries to spawn a new one if possible,
  otherwise it runs the task immediately.
- `io.concurrent` first tries to use a thread in the pool same as
  `io.async`, but on failure (no available threads and pool size limit
  reached) it tries to spawn a new one-shot thread. One shot threads
  run a different main function that just executes one task, decrements
  the number of active one shot threads, and then exits.

A relevant future improvement is to have one-shot threads stay on for a
few seconds (and potentially pick up a new task) to amortize spawning
costs.
2025-11-21 19:54:41 -08:00
Ryan Liptak
59b8bed222 Teach fs.path about the wonderful world of Windows paths
Previously, fs.path handled a few of the Windows path types, but not all of them, and only a few of them correctly/consistently. This commit aims to make `std.fs.path` correct and consistent in handling all possible Win32 path types.

This commit also slightly nudges the codebase towards a separation of Win32 paths and NT paths, as NT paths are not actually distinguishable from Win32 paths from looking at their contents alone (i.e. `\Device\Foo` could be an NT path or a Win32 rooted path, no way to tell without external context). This commit formalizes `std.fs.path` being fully concerned with Win32 paths, and having no special detection/handling of NT paths.

Resources on Windows path types, and Win32 vs NT paths:

- https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html
- https://chrisdenton.github.io/omnipath/Overview.html
- https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file

API additions/changes/deprecations

- `std.os.windows.getWin32PathType` was added (it is analogous to `RtlDetermineDosPathNameType_U`), while `std.os.windows.getNamespacePrefix` and `std.os.windows.getUnprefixedPathType` were deleted. `getWin32PathType` forms the basis on which the updated `std.fs.path` functions operate.
- `std.fs.path.parsePath`, `std.fs.path.parsePathPosix`, and `std.fs.path.parsePathWindows` were added, while `std.fs.path.windowsParsePath` was deprecated. The new `parsePath` functions provide the "root" and the "kind" of a path, which is platform-specific. The now-deprecated `windowsParsePath` did not handle all possible path types, while the new `parsePathWindows` does.
- `std.fs.path.diskDesignator` has been deprecated in favor of `std.fs.path.parsePath`, and same deal with `diskDesignatorWindows` -> `parsePathWindows`
- `relativeWindows` is now a compile error when *not* targeting Windows, while `relativePosix` is now a compile error when targeting Windows. This is because those functions read/use the CWD path which will behave improperly when used from a system with different path semantics (e.g. calling `relativePosix` from a Windows system with a CWD like `C:\foo\bar` will give you a bogus result since that'd be treated as a single relative component when using POSIX semantics). This also allows `relativeWindows` to use Windows-specific APIs for getting the CWD and environment variables to cut down on allocations.
- `componentIterator`/`ComponentIterator.init` have been made infallible. These functions used to be able to error on UNC paths with an empty server component, and on paths that were assumed to be NT paths, but now:
  + We follow the lead of `RtlDetermineDosPathNameType_U`/`RtlGetFullPathName_U` in how it treats a UNC path with an empty server name (e.g. `\\\share`) and allow it, even if it'll be invalid at the time of usage
  + Now that `std.fs.path` assumes paths are Win32 paths and not NT paths, we don't have to worry about NT paths

Behavior changes

- `std.fs.path` generally: any combinations of mixed path separators for UNC paths are universally supported, e.g. `\/server/share`, `/\server\share`, `/\server/\\//share` are all seen as equivalent UNC paths
- `resolveWindows` handles all path types more appropriately/consistently.
  + `//` and `//foo` used to be treated as a relative path, but are now seen as UNC paths
  + If a rooted/drive-relative path cannot be resolved against anything more definite, the result will remain a rooted/drive-relative path.
  + I've created [a script to generate the results of a huge number of permutations of different path types](https://gist.github.com/squeek502/9eba7f19cad0d0d970ccafbc30f463bf) (the result of running the script is also included for anyone that'd like to vet the behavior).
- `dirnameWindows` now treats the drive-relative root as the dirname of a drive-relative path with a component, e.g. `dirname("C:foo")` is now `C:`, whereas before it would return null. `dirnameWindows` also handles local device paths appropriately now.
- `basenameWindows` now handles all path types more appropriately. The most notable change here is `//a` being treated as a partial UNC path now and therefore `basename` will return `""` for it, whereas before it would return `"a"`
- `relativeWindows` will now do its best to resolve against the most appropriate CWD for each path, e.g. relative for `D:foo` will look at the CWD to check if the drive letter matches, and if not, look at the special environment variable `=D:` to get the shell-defined CWD for that drive, and if that doesn't exist, then it'll resolve against `D:\`.

Implementation details

- `resolveWindows` previously looped through the paths twice to build up the relevant info before doing the actual resolution. Now, `resolveWindows` iterates backwards once and keeps track of which paths are actually relevant using a bit set, which also allows it to break from the loop when it's no longer possible for earlier paths to matter.
- A standalone test was added to test parts of `relativeWindows` since the CWD resolution logic depends on CWD information from the PEB and environment variables

Edge cases worth noting

- A strange piece of trivia that I found out while working on this is that it's technically possible to have a drive letter that it outside the intended A-Z range, or even outside the ASCII range entirely. Since we deal with both WTF-8 and WTF-16 paths, `path[0]`/`path[1]`/`path[2]` will not always refer to the same bits of information, so to get consistent behavior, some decision about how to deal with this edge case had to be made. I've made the choice to conform with how `RtlDetermineDosPathNameType_U` works, i.e. treat the first WTF-16 code unit as the drive letter. This means that when working with WTF-8, checking for drive-relative/drive-absolute paths is a bit more complicated. For more details, see the lengthy comment in `std.os.windows.getWin32PathType`
- `relativeWindows` will now almost always be able to return either a fully-qualified absolute path or a relative path, but there's one scenario where it may return a rooted path: when the CWD gotten from the PEB is not a drive-absolute or UNC path (if that's actually feasible/possible?). An alternative approach to this scenario might be to resolve against the `HOMEDRIVE` env var if available, and/or default to `C:\` as a last resort in order to guarantee the result of `relative` is never a rooted path.
- Partial UNC paths (e.g. `\\server` instead of `\\server\share`) are a bit awkward to handle, generally. Not entirely sure how best to handle them, so there may need to be another pass in the future to iron out any issues that arise. As of now the behavior is:
  + For `relative`, any part of a UNC disk designator is treated as the "root" and therefore isn't applicable for relative paths, e.g. calling `relative` with `\\server` and `\\server\share` will result in `\\server\share` rather than just `share` and if `relative` is called with `\\server\foo` and `\\server\bar` the result will be `\\server\bar` rather than `..\bar`
  + For `resolve`, any part of a UNC disk designator is also treated as the "root", but relative and rooted paths are still elligable for filling in missing portions of the disk designator, e.g. `resolve` with `\\server` and `foo` or `\foo` will result in `\\server\foo`

Fixes #25703
Closes #25702
2025-11-21 00:03:44 -08:00
rpkak
6b4f45f782 system specific errno 2025-11-20 15:03:23 -08:00
Benjamin Jurk
4b5351bc0d
update deprecated ArrayListUnmanaged usage (#25958) 2025-11-20 14:46:23 -08:00
Jan200101
bd832ed39a std.Io.Threaded: add missing statx masks
statx does not guarantee that the values requested by the mask be
present and those not requested be absent which is why this worked.
2025-11-19 20:13:25 +01:00
Alex Rønne Petersen
9ab7eec23e represent Mac Catalyst as aarch64-maccatalyst-none rather than aarch64-ios-macabi
Apple's own headers and tbd files prefer to think of Mac Catalyst as a distinct
OS target. Earlier, when DriverKit support was added to LLVM, it was represented
a distinct OS. So why Apple decided to only represent Mac Catalyst as an ABI in
the target triple is beyond me. But this isn't the first time they've ignored
established target triple norms (see: armv7k and aarch64_32) and it probably
won't be the last.

While doing this, I also audited all Darwin OS prongs throughout the codebase
and made sure they cover all the tags.
2025-11-14 11:33:35 +01:00
Carl Åstholm
cca2d09950 io: Correctly align async closure contexts
This fixes package fetching on Windows.

Previously, `Async/GroupClosure` allocations were only aligned for the
closure struct type, which resulted in panics when `context_alignment`
(or `result_alignment` for that matter) had a greater alignment.
2025-11-11 01:11:45 +01:00
Carl Åstholm
ed7f2588e4 io: Translate Windows Clock.real timestamps to the POSIX/Unix epoch
This fixes `std.http.Client` TLS certificate validation on Windows.
2025-11-10 22:12:39 +01:00
Linus Groh
afdd04356c std: serenity has preadv now
2a9154e77c
2025-11-02 14:45:32 -08:00
Alex Rønne Petersen
5db9eaa851
std.Io.Threaded: use ResetEventPosix on illumos
Like NetBSD, illumos has no futexes.

ref #25760
2025-11-01 16:18:56 +01:00
Jacob Young
1d80c9540a Threaded: fix safety crashes 2025-10-29 21:01:47 -04:00
Andrew Kelley
05b28409e7 std.Io.Threaded: install and cleanup signal handlers
rather than in start code. delete std.options.keep_sig_io and
std.options.keep_sig_pipe
2025-10-29 06:20:52 -07:00
Andrew Kelley
c40204a3e5 std.Io: add unit tests for Group and concurrent 2025-10-29 06:20:52 -07:00
Andrew Kelley
03fd132b1c std.Io: fix Group.wait unsoundness
Previously if a Group.wait was canceled, then a subsequent call to
wait() or cancel() would trip an assertion in the synchronization code.
2025-10-29 06:20:52 -07:00
Andrew Kelley
6c794ce7bc std.Io.Threaded.dirOpenFileWtf16: SHARING_VIOLATION
is the error code that needs the kernel bug workaround, not
ACCESS_DENIED.
2025-10-29 06:20:52 -07:00
Andrew Kelley
6f64c8b693 std.debug.SelfInfo.Windows: less invasive change
restores code closer to master branch in hopes of avoiding a regression
that was introduced when this was based on openSelfExe rather than
GetModuleFileNameExW.
2025-10-29 06:20:52 -07:00
Andrew Kelley
030b630829 std.Io.Threaded: fix EBADF error code on wasm32-wasi -lc when reading 2025-10-29 06:20:52 -07:00
Andrew Kelley
c4dc7d7c3d std.Io.Threaded: implement Unix sockets for Windows 2025-10-29 06:20:52 -07:00
Andrew Kelley
b39f3d294d std.Io.Threaded: implement dirMakeOpenPath for WASI
and fix error code when file operation occurs on director handle
2025-10-29 06:20:52 -07:00
Andrew Kelley
4114392369 std: fix definition of ws2_32.GetAddrInfoExW
There was a missing parameter.
2025-10-29 06:20:52 -07:00
Andrew Kelley
f5870b267e std.Io.Threaded: fix typo in panic message 2025-10-29 06:20:52 -07:00
Andrew Kelley
94b9874981 std.Io.Threaded: stub out netbsd mutex and condition 2025-10-29 06:20:52 -07:00
Andrew Kelley
a28d3059e6 std.Io.Threaded: implement ResetEvent in terms of pthreads
needed for NetBSD
2025-10-29 06:20:52 -07:00
Andrew Kelley
30448d92af std.Io.Threaded: fix netLookup for Windows
* respect address family option
* fix port number using wrong buffer
2025-10-29 06:20:52 -07:00
Andrew Kelley
135ec79f50 std.Io.File: fix stat for Windows 2025-10-29 06:20:51 -07:00
Andrew Kelley
cc4931b325 std.Io.Threaded: fix 32-bit overflow in lookupDns
be a little more careful with nanoseconds
2025-10-29 06:20:51 -07:00
Andrew Kelley
3de1b6c9a9 std.Io.Threaded: better clock lowering for BSDs 2025-10-29 06:20:51 -07:00
Andrew Kelley
8b269f7e18 std: make signal numbers into an enum
fixes start logic for checking whether IO/POLL exist
2025-10-29 06:20:51 -07:00
Andrew Kelley
cc751c01f1 std.Io.Threaded: correct clockToPosix for FreeBSD 2025-10-29 06:20:51 -07:00
Andrew Kelley
4e95c2eb1b std.Io.Threaded: implement futexes for freebsd 2025-10-29 06:20:51 -07:00
Andrew Kelley
0caf286a1a std.Io.Threaded: don't skip executing canceled group closures 2025-10-29 06:20:51 -07:00