Commit Graph

191 Commits

Author SHA1 Message Date
Andrew Kelley 13f13fe0a7 std.Io: add documentation to Batch 2026-01-30 20:21:07 -08:00
Jacob Young 3d3f22a14d Io.Batch: implement alternate API 2026-01-30 12:10:05 -08:00
Andrew Kelley f69891797a std: remove error.BrokenPipe from file reads, add error.EndOfStream
and make reading file streaming allowed to return 0 byte reads.
According to Microsoft documentation, on Windows it is possible to get
0-byte reads from pipes when 0-byte writes are made.
2026-01-30 12:10:02 -08:00
Andrew Kelley e1604c1923 std.Io.Threaded: batchWait and batchCancel for Windows 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 1a168f08b5 std.Io.File: introduce MultiReader
Concurrently read from multiple file streams, eliminating risk of
deadlocking.
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 72894900ec std.process.Child.collectOutput: change back to other impl
this one avoids calling poll() more than necessary
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 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
Andrew Kelley def22c2b63 std.Io: delete the poll API 2026-01-30 12:10:01 -08:00
mercenary fa988e88ed zstd.Decompress: smarter rebase when discarding (#30891)
The call to `rebase` in `discardIndirect` and `discardDirect` was inappropriate. As `rebase` expects the `capacity` parameter to exclude the sliding window, this call was asking for ANOTHER `d.window_len` bytes. This was impossible to fulfill with a buffer smaller than 2*`d.window_len`, and caused [#25764](https://github.com/ziglang/zig/issues/25764).

This PR adds a basic test to do a discard (which does trigger [#25764](https://github.com/ziglang/zig/issues/25764)), and rebases only as much as is required to make the discard succeed ([or no rebase at all](https://github.com/ziglang/zig/issues/25764#issuecomment-3484716253)). That means: ideally rebase to fit `limit`, or if the buffer is too small, as much as possible.

I must say, `discardDirect` does not make much sense to me, but I replaced it anyway. `rebaseForDiscard` works fine with `d.reader.buffer.len == 0`. Let me know if anything should be changed.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30891
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: mercenary <mercenary@noreply.codeberg.org>
Co-committed-by: mercenary <mercenary@noreply.codeberg.org>
2026-01-30 20:19:19 +01: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
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 649aaf4814 std: migrate getcwd to Io
progress towards #30150
2026-01-29 18:40:55 -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 c917f619f0 std: fix compilation failures on various targets 2026-01-15 14:18:20 -08:00
Andrew Kelley 4821898432 std.Io.File.MemoryMap API tuning
- remove file_size parameter from MemoryMap.write
- remove requirement for mapping length to be aligned
- align allocated fallback memory
- add unit test for std.Io.Threaded.disable_memory_mapping = true
- add unit test for MemoryMap.setLength
2026-01-15 14:18:20 -08:00
Andrew Kelley bed7bc37c4 std.File.MemoryMap updates
- change offset to u64
- make len non-optional
- make write take a file_size parameter
- std.Io.Threaded: introduce disable_memory_mapping flag to force it to
  take the fallback path.

Additionally:

- introduce BlockSize to File.Stat. On Windows, based on cached call to
  NtQuerySystemInformation. On unsupported OS's, set to 1.
- support File.NLink on Windows. this was available the whole time, we
  just didn't see the field at first.
- remove EBADF / INVALID_HANDLE from reading/writing file error sets
2026-01-15 14:18:20 -08:00
Andrew Kelley 5a7dc4b0fa std.Io: introduce File.MemoryMap
by defining the pointer contents to only be synchronized after explicit
sync points, makes it legal to have a fallback implementation based on
file operations while still supporting a handful of use cases for memory
mapping.

furthermore, it makes it legal for evented I/O implementations to use
evented file I/O for the sync points rather than memory mapping.

not yet done:
- implement checking the length when options.len is null
- some windows impl work
- some wasi impl work
- unit tests
- integration with compiler
2026-01-15 14:18:20 -08:00
Andrew Kelley 20baf049aa std.Io: add doc comments 2026-01-08 12:36:07 -08:00
Andrew Kelley 70af303a2b std.Io: move some decls around
This file has changed a lot since the previous release, and I resisted
the urge to do this until the conflicts would be minimized.
2026-01-08 12:35:57 -08:00
Andrew Kelley ce89006035 std.Io.Kqueue: fix bitrot 2026-01-07 18:00:36 -08:00
Andrew Kelley c0092f5394 std.Io: expose Kqueue and IoUring directly 2026-01-07 17:33:07 -08:00
Andrew Kelley ee574f665c std.Io.Dir: introduce renamePreserve and use it in File.Atomic.link
breaking change: the error for renaming over a non-empty directory now
returns error.DirNotEmpty rather than error.PathAlreadyExists.
2026-01-07 11:03:37 -08:00
Andrew Kelley a136890acc std.Io.random: clarify doc comments 2026-01-07 11:03:37 -08:00
Andrew Kelley 81a35a86ea std.Io: introduce random and randomSecure
and use a thread-local CSPRNG for the former.
2026-01-07 11:03:36 -08:00
Andrew Kelley 816565dd07 std: move entropy to Io 2026-01-07 11:03:36 -08:00
Andrew Kelley 81bfd28974 std.Io.Dir: rework atomic file 2026-01-05 20:28:58 -08:00
Andrew Kelley 69d07472a1 std lib tests passing on linux 2026-01-04 00:27:08 -08:00
Andrew Kelley 384bfc5f99 std.Progress: go through Io interface for parent IPC mechanism
and fix start code
2026-01-04 00:27:08 -08:00
Andrew Kelley b85524d0c8 std.process.Environ: fix contains function 2026-01-04 00:27:08 -08:00
Andrew Kelley 32af0f6154 std: move child process APIs to std.Io
this gets the build runner compiling again on linux

this work is incomplete; it only moves code around so that environment
variables can be wrangled properly. a future commit will need to audit
the cancelation and error handling of this moved logic.
2026-01-04 00:27:08 -08:00
Andrew Kelley d6a1e73142 std: start wrangling environment variables and process args
this commit is unfinished. It marks a spot where I wanted to start
moving child process stuff below the std.Io.VTable
2026-01-04 00:27:07 -08:00
Matthew Lugg f306a9f84a std: rebase fixups and cancelation changes
This commit includes some API changes which I agreed with Andrew as a
follow-up to the recent `Io.Group` changes:

* `Io.Group.await` *does* propagate cancelation to group tasks; it then
  waits for them to complete, and *also* returns `error.Canceled`. The
  assertion that group tasks handle `error.Canceled` "correctly" means
  this behavior is loosely analagous to how awaiting a future works. The
  important thing is that the semantics of `Group.await` and
  `Future.await` are similar, and `error.Canceled` will always be
  visible to the caller (assuming correct API usage).

* `Io.Group.awaitUncancelable` is removed.

* `Future.await` calls `recancel` only if the "child" task (the future
  being awaited) did not acknowledge cancelation. If it did, then it is
  assumed that the future will propagate `error.Canceled` through
  `await` as needed.
2026-01-03 15:45:11 +00:00
Matthew Lugg b8a09bcbd9 std.Io.Group: tweak documentation and vtable API 2026-01-03 15:45:11 +00:00
Ben Krieger 4ad8bc3413 Make Io.Mutex an extern struct 2025-12-31 16:40:34 -05: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
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
Lukas Lalinsky e8a2e6578a Add std.Io.net.Stream.shutdown 2025-12-29 02:20:37 +01: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 fa79d34674 std: add changing cur dir back
There's a good argument to not have this in the std lib but it's more
work to remove it than to leave it in, and this branch is already
20,000+ lines changed.
2025-12-23 22:15:12 -08:00
Andrew Kelley bd6acbf7da std.Io: minor cleanups to futex and event
mainly avoid an unnecessary `@ptrCast`
2025-12-23 22:15:12 -08:00
Andrew Kelley 86e9e32cf0 std.Io: fix missing try in waitTimeout 2025-12-23 22:15:12 -08:00
Andrew Kelley a8088306f6 std: rename other Dir "make" functions to "create" 2025-12-23 22:15:11 -08:00
Andrew Kelley 651ff9f9ee std.Io.Threaded: implement dirHardLink 2025-12-23 22:15:10 -08:00
Andrew Kelley 8632a28ca9 std: add support for realpath on file handle
and rename OperationNotSupported to OperationUnsupported
2025-12-23 22:15:10 -08:00
Andrew Kelley 608145c2f0 fix more fallout from locking stderr 2025-12-23 22:15:10 -08:00