Commit Graph

368 Commits

Author SHA1 Message Date
Andrew Kelley f4140f6e8b std.Io.Threaded: fix batchWait impl 2026-01-30 12:10:01 -08:00
Andrew Kelley afd91234a1 std.Io.Threaded: fix error set 2026-01-30 12:10:01 -08:00
Andrew Kelley d579375a7a std.Io.operate: fix bogus catch
this used to have a different error set. just goes to show you how
useful switching on error set is even when there is only 1 prong
2026-01-30 12:10:01 -08:00
Andrew Kelley a1b3d9e447 std.Io.Threaded: delete dead code 2026-01-30 12:10:01 -08:00
Jacob Young dea653fdb9 Io: add ring to Batch API 2026-01-30 12:10:01 -08:00
Andrew Kelley 5456f953fa std.Io.Threaded: super broken Windows impl of batch
this is a cry for help
2026-01-30 12:10:01 -08:00
Andrew Kelley 45bc4b4e36 std.Io: exploring a different batch API proposal 2026-01-30 12:10:01 -08:00
Andrew Kelley 3ef1266582 std.Io.Threaded: set poll_buffer_len to 32
reasoning is that polling with large amount of operations will be rarely
done with std.Io.Threaded. However this still provides the opportunity
to provide concurrency for any real world use cases that need it.
2026-01-30 12:10:01 -08:00
Andrew Kelley 24b3c4cec1 std.Io.Threaded.operate: handle poll buffer exceeded 2026-01-30 12:10:01 -08:00
Andrew Kelley ae2d71b65e std.Io.Threaded.operate: handle cancelation and poll errors 2026-01-30 12:10:01 -08:00
Andrew Kelley f391adc3af std.Io: simplify operate function
- no timeout
- no n_wait
- infallible
2026-01-30 12:10:01 -08:00
Andrew Kelley 6d22f7b4d7 std.Io: proof-of-concept "operations" API
This commit shows a proof-of-concept direction for std.Io.VTable to go,
which is to have general support for batching, timeouts, and
non-blocking.

I'm not sure if this is a good idea or not so I'm putting it up for
scrutiny.

This commit introduces `std.Io.operate`, `std.Io.Operation`, and
implements it experimentally for `FileReadStreaming`.

In `std.Io.Threaded`, the implementation is based on poll().

This commit shows how it can be used in `std.process.run` to collect
both stdout and stderr in a single-threaded program using
`std.Threaded.Io`.

It also demonstrates how to upgrade code that was previously using
`std.Io.poll` (*not* integrated with the interface!) using concurrency.
This may not be ideal since it makes the build runner no longer support
single-threaded mode. There is still a needed abstraction for
conveniently reading multiple File streams concurrently without
io.concurrent, but this commit demonstrates that such an API can be
built on top of the new `std.Io.operate` functionality.
2026-01-30 12:10:01 -08:00
Jacob Young b5174455f8 Io.Threaded: fix UAF-induced crashes during asynchronous operations
When `NtReadFile` returns `SUCCESS`, the APC routine still runs when
next alertable, which was previously clobbering an out of scope `done`.
Instead of adding an extra syscall to the success path, avoid all APC
side effects, allowing instant completions to return immediately.
2026-01-30 12:10:01 -08:00
Andrew Kelley 29f44952c1 std.Io.Threaded: fix NtDelayExecution delay interval 2026-01-30 12:10:01 -08:00
Andrew Kelley 9240881806 std.Io.Threaded: handle some more error codes from NtReadFile 2026-01-30 12:10:01 -08:00
Andrew Kelley b470800265 std.Io.Threaded: avoid extra fields of Thread
As mlugg pointed out those race when a thread finishes an operation just
after it is canceled and then that thread to picks up another task,
resulting in these fields being potentially overwritten.

This updates fileReadStreaming on Windows to handle being alerted, and
then manage its own cancelation of the file I/O.
2026-01-30 12:10:01 -08:00
Andrew Kelley 84303546ba std.Io.Threaded: don't pass null to NtDelayExecution
Windows returns ACCESS_VIOLATION if you do that.
2026-01-30 12:10:01 -08:00
Andrew Kelley 36940c3953 std.Io.Threaded: implement APC cancelation
specifically the call to NtCancelIoFileEx
2026-01-30 12:10:01 -08:00
Andrew Kelley 0553525fe6 std.Io.Threaded: introduce Thread.InterruptMethod
implements APC cancelation except for the actual call to NtCancelIoFileEx
2026-01-30 12:10:01 -08:00
Andrew Kelley 1b157499ef std.Io.Threaded: add some temporary, choice panics 2026-01-30 12:10:01 -08:00
Andrew Kelley 4f948bb13e std: back out the flags field of Io.File
For now, let us refrain from putting the sync mode into the Io.File
struct, and document that to do concurrent batch operations, any Windows
file handles must be in asynchronous mode. The consequences for
violating this requirement is neither illegal behavior, nor an error,
but that concurrency is lost. In other words, deadlock might occur. This
prevents the addition of flags field.

partial revert of 2faf14200f58ee72ec3a13e894d765f59e6483a9
2026-01-30 12:10:00 -08:00
Andrew Kelley 59ebd82ebf std.Io: give File a nonblocking bit on Windows
This tracks whether it is a file opened in synchronous mode, or
something that supports APC.

This will be needed in order to know whether concurrent batch operations
on the file should return error.ConcurrencyUnavailable, or use APC to
complete the batch.

This patch also switches to using NtCreateFile directly in
std.Io.Threaded for dirCreateFile, as well as NtReadFile for
fileReadStreaming, making it handle files opened in synchronous mode as
well as files opened in asynchronous mode.
2026-01-30 12:10:00 -08:00
Andrew Kelley 6a3226c43c std.Io: add net.Socket.createPair
and remove the following from std.posix:
- socketpair
- fcntl
2026-01-30 02:07:26 -08:00
rpkak b9819fce69 Io.Threaded: limit copy_file_range len to prevent EOVERFLOW 2026-01-30 08:19:50 +01:00
Andrew Kelley 377bb8f237 Merge pull request 'std: migrate getcwd to Io' (#31047) from getcwd into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31047
2026-01-30 06:38:08 +01:00
Brandon Black ecb9ddf267 Threaded.sleepPosix: fix libc error handling
Confusingly, the POSIX spec for clock_nanosleep() says it returns
*positive* error values directly and does not touch `errno`. Not
detecting EINTR properly here was breaking the cancellation of
threads blocked in this call when linking libc.
2026-01-30 06:19:31 +01:00
Andrew Kelley b1d1806fef std.process: currentDir -> currentPath
In Zig standard library, Dir means an open directory handle. path
represents a file system identifier string. This function is better
named after "current path" than "current dir". "get" and "working" are
superfluous.
2026-01-29 18:47:58 -08:00
Andrew Kelley 0a37ad2ec4 std.Io.File: handle DISK_FULL on windows 2026-01-29 18:40:56 -08:00
Andrew Kelley 3cc5dda756 fix RtlGetCurrentDirectory_U parameter
it's the byte length not number of wchars
2026-01-29 18:40:56 -08:00
Andrew Kelley 649aaf4814 std: migrate getcwd to Io
progress towards #30150
2026-01-29 18:40:55 -08:00
Andrew Kelley 9b415761dd std.os.windows: delete unused APIs
Intention is to go through std.Io for these things.
2026-01-30 03:39:46 +01:00
Andrew Kelley 18c6abc0ba std: finish moving os.windows.ReadLink logic to Io.Threaded
- remove error.SharingViolation from all error sets since it has the
  same meaning as FileBusy
- add error.FileBusy to CreateFileAtomicError and ReadLinkError
- update dirReadLinkWindows to use NtCreateFile and NtFsControlFile and
  integrate with cancelation properly.
- move windows CTL_CODE constants to the proper namespace
- delete os.windows.ReadLink
2026-01-28 17:02:17 -08:00
Andrew Kelley 5774248041 std.os.windows: delete unused CreateSymbolicLink 2026-01-28 15:33:04 -08:00
Andrew Kelley 1b235540c1 Revert "Io.Threaded: remove WSA_FLAG_OVERLAPPED from socket call"
The stated reason for this commit was cancelation didn't work. On
further review, we know why cancelation didn't work, and recent
enhancements on master branch make it easy to make it work.

Meanwhile, not using overlapped means that multiple threads cannot use
the same open socket handle. I also added line comments to explain the
choice in this revert commit.

This reverts commit fd3657bf8c.

closes #31011
reopens #30865
2026-01-26 14:16:28 -08:00
Andrew Kelley 5eb55ba866 Merge pull request 'compiler: use Io.MemoryMap' (#30961) from use-mmap into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30961
2026-01-26 22:58:01 +01:00
Andrew Kelley 5c42193b17 std.Io.Threaded: rework cancelation
Now it can handle sync cancelation and alertable cancelation on Windows.

Also fix the API of NtCancelIoFileEx
2026-01-23 12:59:36 -08:00
Andrew Kelley 499ba5d55c compiler: use Io.MemoryMap
Also make setLength return error.OperationUnsupported when it cannot be
done atomically.
2026-01-22 21:25:53 -08:00
Andrew Kelley 1badb2a840 std.Io.Threaded: dirCreateFileWindows uses NtCreateFile directly 2026-01-22 18:08:13 -08:00
Chadwain Holness fd3657bf8c Io.Threaded: remove WSA_FLAG_OVERLAPPED from socket call 2026-01-22 03:39:00 +01:00
Matthew Lugg 35a191ec1c std.Io.Threaded: fix futex timeout race handling 2026-01-21 16:41:55 +01:00
Alex Rønne Petersen a3ea3bd31d std: NetBSD doesn't have a reliable F_GETPATH
It can fail arbitrarily with ENOENT if the kernel happens to not have the FD in
its name cache. That makes it useless for our purposes.

closes https://codeberg.org/ziglang/zig/issues/30843
2026-01-21 16:41:51 +01:00
Andrew Kelley d2dda0737b std.Io.Threaded: sendmmsg can return EACCES 2026-01-18 21:40:15 +01:00
Andrew Kelley 7b21fd7244 std.Io.Threaded: fix memory mapping on windows
- set protection flags properly
- handle when mapping fails after unmapping
2026-01-15 14:18:21 -08:00
Andrew Kelley d2585e68a7 std.Io.Threaded: use NtExtendSection in fileMemoryMapSetLength
is this a good idea? not sure yet. reverting in the next commit
2026-01-15 14:18:21 -08:00
Andrew Kelley de347dd6a9 std.Io.Threaded: add missing free in MemoryMap.setLength 2026-01-15 14:18:21 -08:00
Andrew Kelley 7ff6bcc111 std.Io.Threaded: track unaligned mapping length on Windows 2026-01-15 14:18:21 -08:00
Andrew Kelley b2776d097d std.Io.Threaded: implement MemoryMap.setLength for Windows 2026-01-15 14:18:20 -08:00
Andrew Kelley 8527ec9c7b std.Io.Threaded: implement mmap fallback for Windows 2026-01-15 14:18:20 -08:00
Andrew Kelley 2b01764d86 std: fix handling of EBADF error code
In the context of read/write it's ambiguous, means file was opened
without read/write respectively.
2026-01-15 14:18:20 -08:00
Andrew Kelley cb37a5c1fb std.Io.Threaded: fix fileReadPositional error.IsDir for wasi 2026-01-15 14:18:20 -08:00