106 Commits

Author SHA1 Message Date
Andrew Kelley
e84cda0ebf
Merge pull request #16622 from jacobly0/cbe-asm-compat
CBE: fix regressions and get new targets passing behavior tests
2023-07-31 15:34:32 -07:00
Andrew Kelley
1b9ffa0824 Revert "std.Thread: refining stack size from platform minimum, changes more targetted towards platform like Linux/musl (#15791)"
This reverts commit 41502c6aa53a3da31b276c23c4db74db7d04796b.
2023-07-31 10:54:35 -07:00
Jacob Young
817fa3af86 std: cleanup asm usage
After fixing some issues with inline assembly in the C backend, the std
cleanups have the side effect of making these functions compatible with
the backend, allowing it to be used on linux without linking libc.
2023-07-31 01:58:10 -04:00
Jim Price
584b062a30
Fix counting in SingleThreadedRwLock's tryLockShared (#16560)
Additionally we add RwLock to Thread.zig's list of tests
2023-07-26 08:19:52 -05:00
Luuk de Gram
87b8a0567b
default to single-threaded for WebAssembly
When targeting WebAssembly, we default to building a single-threaded build
as threads are still experimental. The user however can enable a multi-
threaded build by specifying '-fno-single-threaded'. It's a compile-error
to enable this flag, but not also enable shared-memory.
2023-06-26 20:00:58 +02:00
Luuk de Gram
e06ab1b010
std: implement detach for WASI-threads
When a thread is detached from the main thread, we automatically
cleanup any allocated memory. For this we first reset the stack-pointer
to the original stack-pointer of the main-thread so we can safely clear
the memory which also contains the thread's stack.
2023-06-26 20:00:58 +02:00
Luuk de Gram
622b7c4746
free allocated memory upon call join
When `join` detects a thread has completed, it will free the allocated
memory of the thread. For this we must first copy the allocator. This is
required as the allocated memory holds a reference to the original
allocator. If we free the memory, we would end up with UB as the
allocator would free itself.
2023-06-26 20:00:57 +02:00
Luuk de Gram
834609038c
std: implement join for WASI-threads
We now reset the Thread ID to 0 and wake up the main thread listening
for the thread to finish. We use inline assembly as we cannot use
the stack to set the thread ID as it could possibly clobber any
of the memory.

Currently, we leak the memory that was allocated for the thread.
We need to implement a way where we can clean up the memory without
using the stack (as the stack is stored inside this same memory).
2023-06-26 20:00:57 +02:00
Luuk de Gram
10bf58b2db
store allocator & remove global assembly
We now store the original allocator that was used to allocate the
memory required for the thread. This allocator can then be used
in any cleanup functionality to ensure the memory is freed correctly.

Secondly, we now use a function to set the stack pointer instead of
generating a function using global assembly. This is a lot cleaner
and more readable.
2023-06-26 20:00:57 +02:00
Luuk de Gram
a97dbdfa0b
std: implement Thread spawn for WASI
This implements a first version to spawn a WASI-thread. For a new thread
to be created, we calculate the size required to store TLS, the new stack,
and metadata. This size is then allocated using a user-provided allocator.

After a new thread is spawn, the HOST will call into our bootstrap procedure.
This bootstrap procedure will then initialize the TLS segment and set the
newly spawned thread's TID. It will also set the stack pointer to the newly
created stack to ensure we do not clobber the main thread's stack.

When bootstrapping the thread is completed, we will call the user's
function on this new thread.
2023-06-26 20:00:57 +02:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Andrew Kelley
96acc204b2
Merge pull request #16052 from mikdusan/bsd-getdents
fix bad return types for BSDs getdents and debitrot openbsd
2023-06-17 13:01:52 -07:00
Motiejus Jakštys
d41111d7ef mem: rename align*Generic to mem.align*
Anecdote 1: The generic version is way more popular than the non-generic
one in Zig codebase:

     git grep -w alignForward | wc -l
    56
     git grep -w alignForwardGeneric | wc -l
    149

     git grep -w alignBackward | wc -l
    6
     git grep -w alignBackwardGeneric | wc -l
    15

Anecdote 2: In my project (turbonss) that does much arithmetic and
alignment I exclusively use the Generic functions.

Anecdote 3: we used only the Generic versions in the Macho Man's linker
workshop.
2023-06-17 12:49:13 -07:00
r00ster91
2593156068 migration: std.math.{min, min3, max, max3} -> @min & @max 2023-06-16 13:44:09 -07:00
Michael Dusan
40fc71bf71
openbsd: fix std.c.getdents and debitrot
- fix getdents return type usize → c_int
- special-case process.zig to use sysctl instead of sysctlbyname
- use struct/field pattern for sysctl HW_* constants
2023-06-15 14:48:20 -04:00
David CARLIER
41502c6aa5
std.Thread: refining stack size from platform minimum, changes more targetted towards platform like Linux/musl (#15791) 2023-05-25 14:32:17 -05:00
Linus Groh
94e30a756e std: fix a bunch of typos
The majority of these are in comments, some in doc comments which might
affect the generated documentation, and a few in parameter names -
nothing that should be breaking, however.
2023-04-30 18:16:04 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Jacob Young
3f1c51ca90 std: remove names from incorrectly named tests
Tests that only reference decls for the purpose of analyzing more tests
should be unnamed, otherwise trying to filter for just a referenced test
can become impossible depending on the names.
2023-04-21 23:08:48 -04:00
Andrew Kelley
a1f7c8d860
Merge pull request #14696 from r00ster91/thread
std.Thread: use dead code
2023-04-20 16:03:12 -07:00
Michael Dusan
490a411fd4 openbsd: fix thread name buffer size
OpenBSD 7.3 changed its implementation of
pthread_get_name_np/pthread_set_name_np to wrap new libc functions
getthrname/setthrname and lowered the max buffer size from 32 to 24.

This is not a backwards-compatible change because if we were to put in
comptime version logic to use size 32 when target < 7.3 the binaries
would be undefined when running on >= 7.3. It also could simply be that
OpenBSD has a policy to not support older binaries running on newer
releases? Regardless, the safest course is to simply use the smallest
known buffer size.

As an aside, this bug manifested as a "hung" std.Thread test because 7.3
pthread API never checks for error result when wrapping getthrname/setthrname.
This is not a problem in std.Thread when we use the correct max buffer
size because ERANGE/EINVAL become unreachable.
2023-04-12 15:36:03 -04:00
Andrew Kelley
5b90fa05a4 extract ThreadPool and WaitGroup from compiler to std lib 2023-03-15 10:48:12 -07:00
Jan Philipp Hafer
06b263825a std.os: add missing mmap errors
Man page for posix lists EMFILE, man page for linux ENFILE.
Also posix says "The mmap() function adds an extra reference to the file
associated with the file descriptor fildes which is not removed by a
subsequent close() on that file descriptor. This reference is removed
when there are no more mappings to the file."

It sounds counter-intuitive, that a process limit but no system limit can
be exceeeded.

As far as I understand, fildes is only used for file descriptor backed mmaps.
2023-03-08 13:00:06 -05:00
r00ster91
140ca67ea6 std.Thread: use pthread_getname_np on musl
Starting with version 1.2.3, musl now supports pthread_getname_np:
7a43f6fea9/WHATSNEW (L2293-L2329)
2023-02-26 00:15:46 +01:00
r00ster91
f10100cf9b std.Thread: drop is_gnu check
I believe the reason we had that check in the first place was because for both `pthread_setname_np` and `pthread_getname_np` man says:
```
CONFORMING TO
       These functions are nonstandard GNU extensions; hence the suffix "_np" (nonportable) in the names.
```
However, this `is_gnu` check was never consistently applied; it was
missing in `setName` in the Linux case. It is also missing on all other call sites for other platforms (macOS, iOS, et al.).
Though, that could be because it may only apply to Linux.
I think for a best-effort approach it is okay to drop this. It's probably less non-standard than man makes it out to be.
2023-02-24 20:58:09 +01:00
r00ster91
0b2ee09378 std.Thread.setName: use unused code
I noticed a comment saying that the intent of a code's author was unclear.
What happened is that the author forgot to put the check for whether the
thread is the calling thread (`self.getHandle() ==
std.c.pthread_self()`) in the `if (use_pthreads)`.
If the thread is the calling thread, we use `prctl` to set or get the
thread's name and it does not take a thread id because it knows the id
of the thread we're calling `getName` or `setName` from.
I have found a source saying that using `pthread_setname_np` on either the calling thread
or any other thread by thread id would work too (so we don't need to
call `prctl`) but I was not sure if that is the case on all systems
so we keep using `pthread_setname_np` if we have a
specific thread that is not the thread we're calling from, and `prctl`
otherwise.
2023-02-24 20:03:40 +01:00
Isaac Freund
23b7d28896 std: restrict mem.span() and mem.len() to sentinel terminated pointers
These functions are currently footgunny when working with pointers to
arrays and slices. They just return the stated length of the array/slice
without iterating and looking for the first sentinel, even if the
array/slice is a sentinel terminated type.

From looking at the quite small list of places in the standard
library/compiler that this change breaks existing code, the new code
looks to be more readable in all cases.

The usage of std.mem.span/len was totally unneeded in most of the cases
affected by this breaking change.

We could remove these functions entirely in favor of other existing
functions in std.mem such as std.mem.sliceTo(), but that would be a
somewhat nasty breaking change as std.mem.span() is very widely used for
converting sentinel terminated pointers to slices. It is however not at
all widely used for anything else.

Therefore I think it is better to break these few non-standard and
potentially incorrect usages of these functions now and at some later
time, if deemed worthwhile, finally remove these functions.

If we wait for at least a full release cycle so that everyone adapts to
this change first, updating for the removal could be a simple find and
replace without needing to worry about the semantics.
2023-01-29 15:07:06 -05:00
r00ster91
3dd8f43ad3 std.Thread: make Id smaller where possible 2023-01-16 14:20:57 +02:00
Veikka Tuominen
08b2d491bc update usages of @call 2022-12-13 13:14:20 +02:00
Ali Chraghi
f5f1f8c666 all: rename i386 to x86 2022-11-04 00:09:27 +03:30
Andrew Kelley
f16855b9d7 remove pointless discards 2022-09-12 18:13:24 -07:00
biexelar
59e33b447b
std.Thread: fix freeAndExit on x86_64-linux (#12693)
Previously, this function used incorrect registers for the munmap syscall, leading to detached threads not cleaning up.

closes #12690

Co-authored-by: bxlr <biexelar@diroot.org>
2022-08-30 22:31:47 -04:00
Andrew Kelley
afe6e69e4c std.Thread: fix mutable ref of temporary
The previous code is a compile error in stage2 as well as the
upcoming lang spec.
2022-07-04 16:53:41 -07:00
Ali Chraghi
0e6285c8fc math: make cast return optional instead of an error 2022-05-27 16:43:33 -04:00
Andrew Kelley
0fafc8cc44 std.Thread: insert a missing @alignCast
stage1 has a missing compile error for this situation.
2022-05-17 23:50:38 -07:00
Koakuma
fb0692334e target: Rename sparcv9 -> sparc64
Rename all references of sparcv9 to sparc64, to make Zig align more with
other projects. Also, added new function to convert glibc arch name to Zig
arch name, since it refers to the architecture as sparcv9.

This is based on the suggestion by @kubkon in PR 11847.
(https://github.com/ziglang/zig/pull/11487#pullrequestreview-963761757)
2022-05-13 16:43:59 -04:00
protty
18f3034629
std.Thread: ResetEvent improvements (#11523)
* std: start removing redundant ResetEvents

* src: fix other uses of std.Thread.ResetEvent

* src: add builtin.sanitize_thread for tsan detection

* atomic: add Atomic.fence for proper fencing with tsan

* Thread: remove the other ResetEvent's and rewrite the current one

* Thread: ResetEvent docs

* zig fmt + WaitGroup.reset() fix

* src: fix build issues for ResetEvent + tsan

* Thread: ResetEvent tests

* Thread: ResetEvent module doc

* Atomic: replace llvm *p memory constraint with *m

* panicking: handle spurious wakeups in futex.wait() when waiting for abort()

* zig fmt
2022-04-26 16:48:56 -05:00
protty
963ac60918
std.Thread: Mutex and Condition improvements (#11497)
* Thread: minor cleanups

* Thread: rewrite Mutex

* Thread: introduce Futex.Deadline

* Thread: Condition rewrite + cleanup

* Mutex: optimize lock fast path

* Condition: more docs

* Thread: more mutex + condition docs

* Thread: remove broken Condition test

* Thread: zig fmt

* address review comments + fix Thread.DummyMutex in GPA

* Atomic: disable bitRmw x86 inline asm for stage2

* GPA: typo mutex_init

* Thread: remove noalias on stuff

* Thread: comment typos + clarifications
2022-04-23 19:35:56 -05:00
David John
139b731d82 std: rename sched_yield to yield and move it to std.Thread 2022-02-27 15:34:02 -05:00
Endel Dreyer
cd9a6fed4f fix typo on Thread's getHandle docs 2022-02-26 10:48:37 +02:00
Veikka Tuominen
cf5009f9af
Merge pull request #10003 from viriuwu/nt-thread-name
std.Thread.getName/setName: rework windows implementation
2022-02-15 13:23:09 +02:00
viri
0bde55e881
std.Thread(windows): use NT internals for name fns 2022-02-15 01:20:54 -06:00
billzez
470c8ca48c
update RwLock to use static initialization (#10838) 2022-02-08 23:35:48 -05:00
Sage Hane
e288148f60
fs: Use OpenMode enum instead of read/write flags. 2022-01-29 15:52:08 +02:00
afranchuk
7c4c49ff07
Fix a bug in std.Thread.Condition and add a basic Condition test. (#10538)
* Fix FUTEX usage in std.Thread.Condition - It was using an old name.
2022-01-11 13:04:24 -05:00
Isaac Freund
9f9f215305
stage1, stage2: rename c_void to anyopaque (#10316)
zig fmt now replaces c_void with anyopaque to make updating
code easy.
2021-12-19 00:24:45 -05:00
Andrew Kelley
2a0adef583
Merge pull request #9910 from mikdusan/dragonfly
dragonfly: port Thread.setname/getname
2021-12-03 17:29:55 -08:00
Lee Cannon
1093b09a98
allocgate: renamed getAllocator function to allocator 2021-11-30 23:32:47 +00:00
Lee Cannon
85de022c56
allocgate: std Allocator interface refactor 2021-11-30 23:32:47 +00:00