Files
c3c/lib/std/threads/os/thread_posix.c3
Manu Linares 04eb6fc451 CI: Major refactor, expanded coverage, and ~80% speedup (#2736)
* netbsd fail on error

- Group each BSD test in its own subshell. Without this, if an error
occurs, it exits, but we don't know "where" it failed
- added *usesh: true* to BSD for faster copying into the virtualbox VM

- Some test_suite_runners wheren't correctly printing. added
--no-terminal
- For testing I've changed *build-msvc* from Debug to RelWithDebInfo

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* separate runs

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* fix typo

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* Split msvc-debug into separate workflow

Run msvc-debug only on maintainer pushes, keep CI fast and still catches
errors.

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* Fix build and test errors on NetBSD

* Set proper sizes for various types used for threads
* Add num_cpu function
* Add sysctl constants
* Allow testproject to build on NetBSD
* Tweaks to the linker code so that it adds correct flags and finds dynamic linker on NetBSD

* bsd: force system linker test

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* system linker on openbsd

remove lld from netbsd

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* use cc linker in openbsd, and re-add lld for netbsd

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* openbsd: fixes

- implement num_cpu() for OPENBSD
- testproject: add openbsd
- linker.c: fix for openbsd
- bsd refactor main.yml and more fixes

* openbsd: fix for unit tests and test_suite_runner

openbsd now passes all tests except the 'dynlib-test' which yields
'relocation error' when running `cc test.c -L. -ladd -Wl,-rpath=.`

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* openbsd: guard/disable unit test test_ct_intlog2()

* build-msys2-clang: add all tests that build-msvc has

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* Fix Windows import library generation by adding /IMPLIB support and properly declaring static_lib_name()

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* build-msys2-mingw: add all tests that build-msvc has and enable it

configure CMake to link LLVM and LLD statically

* Revert "Revert windows"

This reverts commit 197f82d829.

* win-llvm 21.1.8

uses the RelWithDebInfo+Assertions

* refactor(CI): Centralize test logic and fix Docker execution

This commit refactors the GitHub Actions workflow to improve
maintainability and correct a flaw in the Docker build.

- Ensures Docker based tests run *inside* the generated container, not
on the host. This also fixes environment isolation and user permission
errors.
- Centralizes all common test steps (examples, libs, unit tests, etc)
into a single, auto-detecting Bash script: `scripts/tools/ci_tests.sh`.
- Centralize all release artifact packaging into a single
`scripts/tools/package_build.sh`
- fixes using correct paths for test_suite_runner when running on
windows under bash.

- This significantly reduces YAML duplication, making `main.yml` more
readable and easier to maintain.

This one supersedes pull/2677, and includes the fixes for:

- fix openbsd and netbsd
- added win-llvm 21.1.8 and fixed CMakeLists.txt
- added full unit-tests and suite for build-msys2-mingw and
build-msys2-clang

* macos: add/fix build for llvm (19,20,21)

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* ci: run tests in temp dir to avoid workspace pollution

Executes ci_tests.sh in a disposable temporary directory by copying
resources/ and test/ folders, preventing build artifacts from cluttering
the source tree.

This makes it easy to run `scripts/tools/ci_tests.sh` locally on any
platform.

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* add ilammy/msvc-dev-cmd@ and Ninja

we can simplify the windows runner and run everything in bash

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* windows: drain subprocess stderr before join to avoid test_suite_runner hang

* move last test to `ci_tests.sh`

thus completing the centralization of tests in `ci_tests.sh`

* Fix Windows CI cache

Updates the cache key to include hashes of CMakeLists.txt and the
workflow file.

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* `package_build.sh` runs in temp dir to avoid workspace pollution

- add cleanup

* nix: use `ci_tests.sh`

* remove annoying build-mac warnings

This now checks if package already installed trying to prevent
annoying warnings in the github ci:

Fixes:
```
build-mac (Release, 19)curl 8.17.0 is already installed and up-to-date.
To reinstall 8.17.0, run: brew reinstall curl
```

* refactor test_suite_runner

- less allocations, cleaner code, more maintainable.
- now can be killed and the temp dirs are correctly cleaned up, and the
output is correct.

* main.yml: bsd: change to using rsync, is twice as fast.

* default to `--wincrt=dynamic` when `--sanitize=address` on windows

Default to dynamic, as static ASan is removed in LLVM 21+ for Windows
https://github.com/llvm/llvm-project/pull/107899

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>

* Formatting (indent with tab, align with space). Fix K&R braces. Style: keep all cases multiline if one is.

---------

Signed-off-by: Manuel Barrio Linares <mbarriolinares@gmail.com>
Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
Co-authored-by: Alex Garrett <limit.ordinal17@gmail.com>
2026-01-14 01:16:06 +01:00

265 lines
6.2 KiB
Plaintext

module std::thread::os @if(env::POSIX);
import std::os::posix, std::time, libc;
import libc::errno;
import std::thread;
struct NativeMutex
{
Pthread_mutex_t mutex;
bool initialized;
}
alias NativeTimedMutex = NativeMutex;
alias NativeConditionVariable = Pthread_cond_t;
struct NativeThread
{
inline Pthread_t pthread;
ThreadFn thread_fn;
void* arg;
}
alias NativeOnceFlag = Pthread_once_t;
<*
@require !self.is_initialized() : "Mutex is already initialized"
@ensure self.is_initialized()
*>
fn void? NativeMutex.init(&self, MutexType type)
{
Pthread_mutexattr_t attr;
if (posix::pthread_mutexattr_init(&attr)) return thread::INIT_FAILED?;
defer posix::pthread_mutexattr_destroy(&attr);
// TODO: make a fine grained error instead
if (type.recursive)
{
if (posix::pthread_mutexattr_settype(&attr, posix::PTHREAD_MUTEX_RECURSIVE)) return thread::INIT_FAILED?;
}
else
{
$if env::COMPILER_SAFE_MODE:
if (posix::pthread_mutexattr_settype(&attr, posix::PTHREAD_MUTEX_ERRORCHECK)) return thread::INIT_FAILED?;
$endif
}
if (posix::pthread_mutex_init(&self.mutex, &attr)) return thread::INIT_FAILED?;
self.initialized = true;
}
fn bool NativeMutex.is_initialized(&self)
{
return self.initialized;
}
<*
@require self.is_initialized() : "Mutex was not initialized"
@ensure !self.is_initialized()
*>
fn void NativeMutex.destroy(&self)
{
if (Errno err = posix::pthread_mutex_destroy(&self.mutex)) abort("Error destroying mutex: %d", err);
*self = {};
}
<*
@require self.is_initialized() : "Mutex was not initialized"
*>
fn void NativeMutex.lock(&self)
{
switch (posix::pthread_mutex_lock(&self.mutex))
{
case errno::EINVAL: unreachable("Mutex invalid");
case errno::EDEADLK: abort("Mutex deadlock");
case errno::OK: return;
default: unreachable("Unexpected error in lock");
}
}
<*
@require self.is_initialized() : "Mutex was not initialized"
*>
fn void? NativeMutex.lock_timeout(&self, ulong ms)
{
/* Try to acquire the lock and, if we fail, sleep for 5ms. */
Errno result;
while ((result = posix::pthread_mutex_trylock(&self.mutex)) == errno::EBUSY)
{
if (!ms) break;
ulong sleep = min(5, ms);
if (!libc::nanosleep(&&time::ms(ms).to_timespec(), null)) return thread::LOCK_TIMEOUT?;
ms -= sleep;
}
switch (result)
{
case errno::OK:
return;
case errno::EBUSY:
case errno::ETIMEDOUT:
return thread::LOCK_TIMEOUT?;
default:
return unreachable("Invalid lock %d", result);
}
}
<*
@require self.is_initialized() : "Mutex was not initialized"
*>
fn bool NativeMutex.try_lock(&self)
{
return !posix::pthread_mutex_trylock(&self.mutex);
}
<*
@require self.is_initialized() : "Mutex was not initialized"
*>
fn void NativeMutex.unlock(&self)
{
if (Errno err = posix::pthread_mutex_unlock(&self.mutex)) abort("Failed to unlock mutex: %d", err);
}
fn void? NativeConditionVariable.init(&cond)
{
if (posix::pthread_cond_init(cond, null)) return thread::INIT_FAILED?;
}
fn void NativeConditionVariable.destroy(&cond)
{
if (Errno err = posix::pthread_cond_destroy(cond)) abort("Failed to destroy pthread_cond %d", err);
}
fn void NativeConditionVariable.signal(&cond)
{
if (Errno err = posix::pthread_cond_signal(cond)) abort("Failed to signal %d", err);
}
fn void NativeConditionVariable.broadcast(&cond)
{
if (Errno err = posix::pthread_cond_broadcast(cond)) abort("Failed to broadcast %d", err);
}
<*
@require mtx.is_initialized()
*>
fn void NativeConditionVariable.wait(&cond, NativeMutex* mtx)
{
if (Errno err = posix::pthread_cond_wait(cond, &mtx.mutex)) abort("Failed to wait %d", err);
}
<*
@require mtx.is_initialized()
@return? thread::WAIT_TIMEOUT
*>
fn void? NativeConditionVariable.wait_timeout(&cond, NativeMutex* mtx, ulong ms)
{
Time time = time::now() + time::ms(ms);
return cond.wait_until(mtx, time) @inline;
}
<*
@require mtx.is_initialized()
@return? thread::WAIT_TIMEOUT
*>
fn void? NativeConditionVariable.wait_timeout_duration(&cond, NativeMutex* mtx, Duration duration)
{
if (duration < time::DURATION_ZERO) return thread::WAIT_TIMEOUT?;
Time time = time::now() + duration;
return cond.wait_until(mtx, time) @inline;
}
<*
@require mtx.is_initialized()
@return? thread::WAIT_TIMEOUT
*>
fn void? NativeConditionVariable.wait_until(&cond, NativeMutex* mtx, Time time)
{
switch (posix::pthread_cond_timedwait(cond, &mtx.mutex, &&time.to_timespec()))
{
case errno::ETIMEDOUT:
return thread::WAIT_TIMEOUT?;
case errno::OK:
return;
default:
$if(env::OPENBSD):
// TODO: Investigate why this doesn't work correctly on openbsd.
return thread::WAIT_TIMEOUT?;
$else
abort("pthread_cond_timedwait failed, invalid value");
$endif
}
}
tlocal NativeThread current_thread @private;
fn void* callback(void* arg) @private
{
NativeThread* thread = arg;
current_thread = *thread;
return (void*)(iptr)thread.thread_fn(thread.arg);
}
fn void? NativeThread.create(&thread, ThreadFn thread_fn, void* arg)
{
thread.thread_fn = thread_fn;
thread.arg = arg;
if (posix::pthread_create(&thread.pthread, null, &callback, thread) != 0)
{
return thread::INIT_FAILED?;
}
}
fn void NativeThread.detach(thread)
{
if (Errno errno = posix::pthread_detach(thread.pthread)) abort("Thread detach failed: %d", errno);
}
fn void native_thread_exit(int result)
{
posix::pthread_exit((void*)(iptr)result);
}
fn NativeThread native_thread_current()
{
return current_thread;
}
fn bool NativeThread.equals(thread, NativeThread other)
{
return (bool)posix::pthread_equal(thread.pthread, other.pthread);
}
<*
@return "the return value of the thread"
@return? thread::THREAD_NOT_FOUND : "If the thread was not running"
*>
fn int? NativeThread.join(thread) @maydiscard
{
void *pres;
switch (posix::pthread_join(thread.pthread, &pres))
{
case errno::OK: return (int)(iptr)pres;
case errno::EINVAL: unreachable("Thread is not joinable.");
case errno::EDEADLK: unreachable("Thread join from current thread.");
case errno::ESRCH: return thread::THREAD_NOT_FOUND~;
default: unreachable("Thread join returned unexpected result");
}
}
fn void NativeOnceFlag.call_once(&flag, OnceFn func)
{
posix::pthread_once(flag, func);
}
fn void native_thread_yield()
{
posix::sched_yield();
}
fn void? native_sleep_nano(NanoDuration nano)
{
if (nano <= time::NANO_DURATION_ZERO) return;
if (libc::nanosleep(&&nano.to_timespec(), null)) return thread::INTERRUPTED?;
}