Commit graph

99 commits

Author SHA1 Message Date
mlugg
d00e05f186
all: update to std.builtin.Type.Pointer.Size field renames
This was done by regex substitution with `sed`. I then manually went
over the entire diff and fixed any incorrect changes.

This diff also changes a lot of `callconv(.C)` to `callconv(.c)`, since
my regex happened to also trigger here. I opted to leave these changes
in, since they *are* a correct migration, even if they're not the one I
was trying to do!
2025-01-16 12:46:29 +00:00
Andrew Kelley
d210f733f8 std.Progress: fix data race
In end(), the freelist pointer is owned so the bare store would be ok.
However, there is a load in start() that can happen at the same time, if
another start() and end() pair grabs that same index.

I don't think this fixes #21663 actually because even if the data race
corrupts the value for `next`, the cmpxchg protects the value from being
stored there.
2024-10-23 13:47:44 -07:00
mlugg
0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.

This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
2024-08-28 08:39:59 +01:00
Alex Rønne Petersen
2cced8903e
std.posix: Consider invalid signal numbers to sigaction() to be programmer error.
The set of signals that cannot have their action changed is documented in POSIX,
and any additional, non-standard signals are documented by the specific OS. I
see no valid reason why EINVAL should be considered an unpredictable error here.
2024-07-21 22:38:09 +02:00
Andrew Kelley
e8c4e79499 std.c reorganization
It is now composed of these main sections:
* Declarations that are shared among all operating systems.
* Declarations that have the same name, but different type signatures
  depending on the operating system. Often multiple operating systems
  share the same type signatures however.
* Declarations that are specific to a single operating system.
  - These are imported one per line so you can see where they come from,
    protected by a comptime block to prevent accessing the wrong one.

Closes #19352 by changing the convention to making types `void` and
functions `{}`, so that it becomes possible to update `@hasDecl` sites
to use `@TypeOf(f) != void` or `T != void`. Happily, this ended up
removing some duplicate logic and update some bitrotted feature
detection checks.

A handful of types have been modified to gain namespacing and type
safety. This is a breaking change.

Oh, and the last usage of `usingnamespace` site is eliminated.
2024-07-19 00:30:32 -07:00
Jacob Young
14caccb477 Progress: avoid race on global_progress.draw_buffer 2024-07-16 05:14:38 -04:00
Andrew Kelley
f33395ce6a std.Progress: add getIpcFd and have_ipc API
This makes advanced use cases possible such as a long-lived child
process whose progress node gets re-attached to a different parent.
2024-07-14 21:38:16 -07:00
Andrew Kelley
9363e995fc std.Progress: slightly better atomic memcpy
Let's at least do aligned usize loads/stores where possible.
2024-07-10 00:28:44 -04:00
Jacob Young
1824bee579 Progress: suppress tsan races
This removes the undefined behavior, but not the actual races.

Closes #20477
2024-07-09 16:40:28 -04:00
Jacob Young
65ced4a334 Compilation: put supported codegen backends on a separate thread
(There are no supported backends.)
2024-07-08 11:00:38 -04:00
Andrew Kelley
20e20f9238 std.Progress.Node: add none init value 2024-06-16 19:27:49 -07:00
Andrew Kelley
fad223d92e std.Progress: use a recursive mutex for stderr 2024-06-12 17:43:49 -07:00
Andrew Kelley
4b776ae441 std.Progress: fix race assertion failure
A node may be freed during the execution of this loop, causing there to
be a parent reference to a nonexistent node. Without this assignment,
this would lead to the map entry containing stale data. By assigning
none, the child node with the bad parent pointer will be harmlessly
omitted from the tree.

Closes #20262
2024-06-11 15:24:57 -07:00
Ryan Liptak
337f09e932 Add File.getOrEnableAnsiEscapeSupport and use it
On Windows, the console mode flag `ENABLE_VIRTUAL_TERMINAL_PROCESSING` determines whether or not ANSI escape codes are parsed/acted on. On the newer Windows Terminal, this flag is set by default, but on the older Windows Console, it is not set by default, but *can* be enabled (since Windows 10 RS1 from June 2016).

The new `File.getOrEnableAnsiEscapeSupport` function will get the current status of ANSI escape code support, but will also attempt to enable `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on Windows if necessary which will provide better/more consistent results for things like `std.Progress` and `std.io.tty`.

This type of change was not done previously due to a mistaken assumption (on my part) that the console mode would persist after the run of a program. However, it turns out that the console mode is always reset to the default for each program run in a console session.
2024-06-02 16:46:21 -07:00
Andrew Kelley
85eb5a3069 std.Progress: fix line upper bound calculation
closes #20161

problem introduced in e09963d854
2024-06-02 17:22:15 -04:00
Ryan Liptak
2cf8e73781 Progress: Emit \r\n on Windows, include new line bytes in line_upper_bound_len
The \r\n is necessary to get the progress tree to work properly in the old console when ENABLE_VIRTUAL_TERMINAL_PROCESSING and DISABLE_NEWLINE_AUTO_RETURN are set.

The line_upper_bound_len fix addresses part of #20161
2024-06-02 13:04:25 -04:00
Andrew Kelley
e09963d854 std.Progress: keep the cursor at the beginning
This changes the terminal display to keep the cursor at the top left of
the progress display, so that unlocked stderr writes, perhaps by child
processes, don't get eaten by the clear.
2024-05-31 19:00:14 -04:00
Andrew Kelley
c564a16a01 std.Progress: IPC fixes
Reduce node_storage_buffer_len from 200 to 83. This makes messages over
the pipe fit in a single packet (4096 bytes). There is now a comptime
assert to ensure this. In practice this is plenty of storage because
typical terminal heights are significantly less than 83 rows.

Handling of split reads is fixed; instead of using a global
`remaining_read_trash_bytes`, the value is stored in the "saved
metadata" for the IPC node.

Saved metadata is split into two arrays so that the "find" operation can
quickly scan over fds for a match, looking at 332 bytes maximum, and
only reading the memory for the other data upon match. More typical
number of bytes read for this operation would be 0 (no child processes),
4 (1 child process), or 64 (16 child processes reporting progress).

Removed an align(4) that was leftover from an older design.

This also includes part of Jacob Young's not-yet-landed patch that
implements `writevNonblock`.
2024-05-31 04:06:12 -04:00
Ryan Liptak
d750a78b2c std.Progress: Fix Windows console API implementation
3a3d2187f9 unintentionally broke some of the Windows console API implementation.

- The 'marker' character was no longer being written at all
- The ANSI escape codes for syncing were being written unconditionally
2024-05-28 22:46:12 -04:00
Andrew Kelley
3a3d2187f9 std.Progress: better Windows support
* Merge a bunch of related state together into TerminalMode. Windows
  sometimes follows the same path as posix via ansi_escape_codes,
  sometimes not.
* Use a different thread entry point for Windows API but share the same
  entry point on Windows when the terminal is in ansi_escape_codes mode.
* Only clear the terminal when the stderr lock is held.
* Don't try to clear the terminal when nothing has been written yet.
* Don't try to clear the terminal in IPC mode.
* Fix size detection logic bug under error conditions.
2024-05-28 12:31:10 -07:00
Ryan Liptak
40afac40b8 std.Progress: Use Windows console API calls when ANSI escape codes are not supported 2024-05-28 10:41:07 -07:00
Andrew Kelley
65a0e14e4f std.Progress: relax some of the atomic orderings
Generates better machine code, particularly on ARM
2024-05-27 20:56:49 -07:00
Andrew Kelley
aca7feb8fa std.Progress: fix race condition with setIpcFd
The update thread was sometimes reading the special state and then
incorrectly getting 0 for the file descriptor, making it hang since it
tried to read from stdin.
2024-05-27 20:56:49 -07:00
Andrew Kelley
44389253c2 fix zig translate-c creating root progress node twice 2024-05-27 20:56:49 -07:00
Andrew Kelley
2367a1ff84 std.Progress: handle short writes 2024-05-27 20:56:49 -07:00
Andrew Kelley
dcf9cae256 std.Progress: handle big-endian targets
We cannot rely on host endianness because the parent or child process
may be executing inside QEMU.
2024-05-27 20:56:49 -07:00
Andrew Kelley
eea7e5e554 std.Progress: adjust the timings a little bit
Slightly slower refresh rate. It's still updating very quickly.

Lower the initial delay so that CLI applications feel more responsive.
Even though the application is doing work for the full 500ms until
something is displayed on the screen, it feels nicer to get the progress
earlier.
2024-05-27 20:56:49 -07:00
Andrew Kelley
64c6a5092c std.Progress: elide root node if empty
when the root progress node has a zero length name, the sub-tree is
flattened one layer, reducing visual noise, as well as bytes written to
the terminal.
2024-05-27 20:56:48 -07:00
Andrew Kelley
dc3a192ae8 std.Progress: count newlines more accurately
Split newline_count into written_newline_count and
accumulated_newline_count. This handle the case when the tryLock() fails
to obtain the lock, because in such case there would not be any newlines
written to the terminal but the system would incorrectly think there
were. Now, written_newline_count is only adjusted when the write() call
succeeds.

Furthermore, write() call failure is handled by exiting the update
thread.
2024-05-27 20:56:48 -07:00
Andrew Kelley
6145819c0b std.Progress: handle when terminal write buffer too small 2024-05-27 20:56:48 -07:00
Andrew Kelley
52ffdec74b std.Progress: keep cursor on newline
Don't truncate trailing newline. This better handles stray writes to
stderr that are not std.Progress-aware, such as from non-zig child
processes.

This commit also makes `Node.start` and `Node.end` bail out early with a
comptime branch when it is known the target will not be spawning an
update thread.
2024-05-27 20:56:48 -07:00
Andrew Kelley
0ca2b4e0f1 std.Progress: use std.log.debug rather than warn
when the errors could possibly be spammed many times
2024-05-27 20:56:48 -07:00
Andrew Kelley
ea7d8ec147 std.Progress: smaller type for parents and robustify
Switch Node.Parent, Node.Index, and Node.OptionalIndex to be backed by
u8 rather than u16. This works fine since we use 200 as the preallocated
node buffer. This has the nice property that scanning the entire parents
array for allocated nodes fits in 4 cache lines, even if we bumped the
200 up to 254 (leaving room for the two special states).

The thread that reads progress updates from the pipe now handles short
reads by ignoring messages that are sent in multiple reads.

When checking the terminal size, if there is a failure, fall back to a
conservative guess of 80x25 rather than panicking. A debug message is
also emitted which would be displayed only in a debug build.
2024-05-27 20:56:48 -07:00
Andrew Kelley
11f894702b std.Progress: avoid scrolling the PS1 off the terminal 2024-05-27 20:56:48 -07:00
Andrew Kelley
52ed54d1e7 std.Progress: truncate IPC data exceeding preallocated buffers
This accomplishes 2 things simultaneously:

1. Don't trust child process data; if the data is outside the expected
   range, ignore the data.
2. If there is too much data to fit in the preallocated buffers, drop
   the data.
2024-05-27 20:56:48 -07:00
Andrew Kelley
807b613f71 std.Progress: move more global preallocations to thread memory
Same idea as previous commit
2024-05-27 20:56:48 -07:00
Andrew Kelley
7fe72d560d std.Progress: move global preallocations to thread memory
Instead of making static buffers configurable, let's pick strong
defaults and then use the update thread's stack memory to store the
preallocations. The thread uses a fairly shallow stack so this memory is
otherwise unused. This also makes the data section of the executable
smaller since it runtime allocates the memory when a `std.Progress`
instance is allocated, and in the case that the process is not connected
to a terminal, it never allocates the memory.
2024-05-27 20:56:48 -07:00
Andrew Kelley
e8907f9e9c std.Progress: correct the top level doc comments 2024-05-27 20:56:48 -07:00
Jacob Young
d77f5e7aaa Progress: fix compile errors on windows
Works for `zig build-exe`, IPC still not implemented yet.
2024-05-27 20:56:48 -07:00
Andrew Kelley
ca03c9c512 std.Progress: fix race condition with IPC nodes
It stored some metadata into the canonical node storage data but that is
a race condition because another thread recycles those nodes.

Also, keep the parent name for empty child root node names.
2024-05-27 20:56:48 -07:00
Andrew Kelley
516366f78f std.Progress: skip printing root node when it is empty 2024-05-27 20:56:48 -07:00
Andrew Kelley
70e39c1a20 std.Progress: fixes
* bump default statically allocated resources
* debug help when multiple instances of std.Progress are initialized
* only handle sigwinch on supported operating systems
* handle when reading from the pipe returns 0 bytes
* avoid printing more lines than rows
2024-05-27 20:56:48 -07:00
Andrew Kelley
f97c2f28fd update the codebase for the new std.Progress API 2024-05-27 20:56:48 -07:00
Andrew Kelley
f6873c6b00 std.Progress: fix using saved IPC data
also fix handling of BrokenPipe

also fix continuing wrong loop in error conditions
2024-05-27 20:56:48 -07:00
Andrew Kelley
3a768bd6ce std.Progress: save a copy of IPC data
so that the previous message can be used when the pipe is empty.

prevents flickering
2024-05-27 20:56:48 -07:00
Andrew Kelley
df46f5af69 std.Progress: include subtrees from child processes 2024-05-27 20:56:48 -07:00
Andrew Kelley
f07116404a std.Progress: child process sends updates via IPC 2024-05-27 20:56:48 -07:00
Andrew Kelley
ed36470af1 std.Progress: truncate trailing newline 2024-05-27 20:56:48 -07:00
Andrew Kelley
67e08e7b3c fix clearing and sibling iteration 2024-05-27 20:56:48 -07:00
Andrew Kelley
582acdf721 keep the cursor at the end instead of beginning 2024-05-27 20:56:48 -07:00