Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 23 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ jobs:
pull-requests: read
with:
# The test legs below are skipped on release PRs; the unit tests run
# here instead, in the stable environment a consumer gets.
# here instead, in the environment a consumer gets.
task: test
env: stable
env: default
mojo-compiler: "==1.3.0.dev2026111805"

test:
needs: release
if: ${{ !cancelled() && needs.release.outputs.release_only != 'true' }}
name: ${{ matrix.env == 'default' && 'nightly' || matrix.env }} / ${{ matrix.os }}
name: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ${{ fromJSON((github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && '["ubuntu-latest", "macos-latest"]' || '["ubuntu-latest"]') }}
env: [default, stable]
steps:
- uses: actions/checkout@v4

Expand All @@ -58,53 +58,41 @@ jobs:
# it hashes source dependencies after it, so a newer pixi rejects the
# committed lock with "lock file not up-to-date".
pixi-version: v0.78.0
environments: ${{ matrix.env }}

# Every step below goes through a pixi *task* rather than calling `mojo`
# directly, and that is load-bearing now rather than tidiness: the tasks
# spell `-I src -I $THREADS_COMPAT`, and `$THREADS_COMPAT` is set by the
# matching feature's `[activation.env]` in pixi.toml. Mojo 1.1.0 and the
# 26.7 nightlies accept the same `comptime Cell = Atomic[…]` line, so
# both point at plain `compat` today; the indirection is what lets a
# future divergence split that directory per toolchain without touching
# a task or a step. A step that invoked `mojo build … -I src` by hand
# would fail to find `threads_compat` on both legs.
#
# The nightly leg is also the regression test for the whole reason that
# split exists: `[feature.nightly.dependencies]` floors `modular` on the
# 26.7 line, well past `26.6.0.dev2026090705`, and the intrinsics this
# repo used before could not compile there at all
# (modular/modular#7094).
# spell `-I src -I $THREADS_COMPAT`, and `$THREADS_COMPAT` is set by
# `[activation.env]` in pixi.toml — plain `compat` today; the
# indirection is what lets a future divergence split that directory per
# toolchain without touching a task or a step. A step that invoked
# `mojo build … -I src` by hand would fail to find `threads_compat`.
#
# Linux is the interesting leg: pthread_setaffinity_np is a real pin
# there (a no-op on macOS), and the pthread symbols come out of libc
# rather than libSystem. Both are exercised by the suite.
- name: Unit tests
run: pixi run -e ${{ matrix.env }} test
run: pixi run test

# Lints `src`, `tests` and this leg's half of `compat` — not all of
# `compat`, because `--lsp` type-checks what it lints and the other
# half's `Cell` is by construction the spelling this compiler rejects.
# Between the two legs both halves are covered.
# Lints `src`, `tests` and the `compat` directory `$THREADS_COMPAT`
# names — only that one, because `--lsp` type-checks what it lints, and
# should `compat` split per toolchain again the other half's `Cell` would
# be by construction the spelling this compiler rejects.
- name: Lint (mojolint --lsp)
run: pixi run -e ${{ matrix.env }} lint
run: pixi run lint

# `mojo format` is stricter about `compat/` than mojolint is: it only
# parses, so it can check both halves on either toolchain.
# `mojo format` covers all of `compat/`: it only parses, so it would
# check every half of a split directory on any toolchain.
- name: Format check
run: pixi run -e ${{ matrix.env }} format-check
run: pixi run format-check

- name: Demo CLI builds and runs
run: |
pixi run -e ${{ matrix.env }} cli
pixi run cli
./build/threads-mojo info
./build/threads-mojo bench --elements 4000000

# The sanitizer gate. `tests/stress_threads.mojo` is built with a sanitizer
# and run in a loop; any finding in the output fails the job. Stable only,
# deliberately: nightly here would be a second toolchain's sanitizer runtime
# to keep green for no extra signal, and this repo's history with nightly +
# pixi-build source deps under pixi 0.78 is not encouraging.
# and run in a loop; any finding in the output fails the job.
#
# Which sanitizer runs where is decided by the toolchain rather than by
# preference — see the two comments below. Between them the two platforms
Expand All @@ -124,13 +112,12 @@ jobs:
- uses: prefix-dev/setup-pixi@v0.10.2
with:
pixi-version: v0.78.0
environments: stable

# Without a sanitizer first, so that a plain wrong answer is
# distinguishable from a race — and so a sanitizer-only failure is known
# to be about ordering rather than arithmetic.
- name: Stress (no sanitizer)
run: pixi run -e stable stress
run: pixi run stress

# macOS only. On linux-64 with Mojo 1.0.0 a `--sanitize thread` binary
# links and then aborts before `main`: the runtime's bundled TCMalloc
Expand All @@ -140,7 +127,7 @@ jobs:
# does not help, so it is the toolchain rather than this code.
- name: Stress under ThreadSanitizer
if: runner.os == 'macOS'
run: pixi run -e stable stress-tsan
run: pixi run stress-tsan

# Linux only, and the mirror image: `--sanitize address` does not link on
# osx-arm64 with this toolchain (undefined
Expand All @@ -149,7 +136,7 @@ jobs:
# included.
- name: Stress under AddressSanitizer
if: runner.os == 'Linux'
run: pixi run -e stable stress-asan
run: pixi run stress-asan

# Imports: no cycles between modules, and each name imported from the
# module that defines it (github.com/magmalake/customs).
Expand Down
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Mojo code: `parallelize` was removed from `std.algorithm`, and no thread pool
is reachable from `std`. If you want a second core, you call `pthread_create`
yourself. This tin is that call, done once, carefully, and tested.

Works on **Mojo 1.2.0 (stable) and current nightly**, on **linux-64** and
**osx-arm64** — all four combinations in CI.
Works on **Mojo 1.3.0.dev2026111805** (the nightly every magmalake tin is
pinned to), on **linux-64** and **osx-arm64** — both in CI.

## Install

Expand All @@ -30,7 +30,7 @@ Working with a coding agent? `npx skills add mojoshelf/mojoshelf --skill mojoshe

That resolves the tin from [mojoshelf](https://mojoshelf.org) and adds it — along with the tins it depends on — as **pixi git source dependencies**. magmalake tins are not published to a conda channel, so `pixi add threads-mojo` will not find them.

As a dependency declaration, or for a nightly consumer:
As a dependency declaration:

```toml
# pixi.toml
Expand Down Expand Up @@ -304,8 +304,8 @@ section. That is a silent correctness bug rather than a compile error. Use

## Why this does not just re-export `std.atomic`

It does, now — but it cannot do so from a single file, because the two
toolchains this tin targets disagree about what `Atomic` is:
It does, now — but not from a single file, because two toolchains this tin
has had to support disagree about what `Atomic` is:

| toolchain | declaration | the only spelling it accepts |
|---|---|---|
Expand Down Expand Up @@ -436,11 +436,10 @@ much as anything:
## Tests

```console
$ pixi run test # nightly
$ pixi run -e stable test # Mojo 1.2.0
$ pixi run test # Mojo 1.3.0.dev2026111805
```

The tests run on each environment, on both platforms. The load-bearing ones are the
The tests run on both platforms. The load-bearing ones are the
races, each written so a broken primitive produces a *wrong number* rather than
a flake:

Expand All @@ -454,7 +453,7 @@ a flake:
- **Allocator under threads** — 8 workers building and dropping Mojo `List`s
and `String`s concurrently, 64 tasks × 20 rounds each. Every worker in this
library rests on the Mojo runtime allocator being usable from several threads
at once; this test is the evidence, and it passes on all four CI legs.
at once; this test is the evidence, and it passes on both CI legs.
- **`WorkerPool`** — nine tests: workers that tick a shared counter until
stopped and then join; `request_stop()` racing the spawns; the 16 indices
each claimed exactly once (a duplicate leaves one slot at 2 and another at
Expand All @@ -478,9 +477,9 @@ that happens to interleave benignly on this machine, this run, still passes.
So the suite has a second half that is a *detector* rather than an assertion.

```console
$ pixi run -e stable stress # no sanitizer, 300 rounds
$ pixi run -e stable stress-tsan # --sanitize thread, zero tolerance
$ pixi run -e stable stress-asan # --sanitize address, + LeakSanitizer
$ pixi run stress # no sanitizer, 300 rounds
$ pixi run stress-tsan # --sanitize thread, zero tolerance
$ pixi run stress-asan # --sanitize address, + LeakSanitizer
```

`tests/stress_threads.mojo` runs the typed and opaque `parallel_for` at 1, 2,
Expand Down Expand Up @@ -530,7 +529,7 @@ The threading primitives here are distilled from
`ThreadHandle` shape, its opaque `pthread_mutex_t` blob, the heap-allocated
worker context that must outlive the join, and its hard-won note that the
scheduler's stop flag really wanted to be an atomic. The code is rewritten for
current Mojo rather than copied: flare pins `mojo==1.0.0b1`, and this tin has
to compile on both 1.0.0 and nightly.
current Mojo rather than copied: flare pins `mojo==1.0.0b1`, and this tin is
pinned to a Mojo 1.3 nightly.

Licensed under the Apache License, Version 2.0 — see `LICENSE` and `NOTICE`.
7 changes: 4 additions & 3 deletions compat/threads_compat.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ spell differently, and nothing else. `threads.atomic` explains the mechanism in
full; the short version is that a type cannot be chosen by a `comptime if` at
module scope, so the choice is made by which directory is on the include path.

Right now there is only one such directory, because Mojo 1.2.0 and the 1.3.0.dev
nightlies agree on every spelling here. While Mojo 1.0.0 was still supported
Right now there is only one such directory, because this tin supports one
toolchain, Mojo 1.3.0.dev2026111805. While Mojo 1.0.0 was still supported
there were two — `compat/stable` and `compat/nightly` — differing in the one
line below, and the moment the toolchains diverge again they split back apart.
line below, and the moment two supported toolchains diverge they split back
apart.
"""

from std.atomic import Atomic
Expand Down
Loading
Loading