lean4-htt/src/runtime
Joachim Breitner c36b0fb165
refactor: make CancelToken Promise-based (#13303)
This PR moves `IO.CancelToken` from `Init.System.IO` to its own file
`Init.System.CancelToken`, backed by `IO.Promise Unit` instead of
`IO.Ref Bool`. This enables non-polling cancellation propagation: the
token's underlying promise can be used directly with `IO.waitAny`, and
callbacks can be registered to fire when cancellation is requested.

The structure carries both the promise *and* a plain `IO.Ref Bool` flag,
set in lockstep by `set`. `isSet` reads the flag directly (used on hot
paths like `Core.checkInterrupted`); `task`/`onSet` go through the
promise. The avoids a ~0.4% regression that a pure-promise
representation introduced.

API additions:

- `CancelToken.task : Task (Option Unit)`. Returns the underlying
promise's `result?` task directly — the same task object on every call,
so further `Task.map`/`BaseIO.bindTask` dependencies can be safely
attached. Resolves with `some ()` when `set` is called, or `none` if the
token is dropped without ever being set.
- `CancelToken.onSet : BaseIO Unit → BaseIO Unit`. Registers a callback
that runs synchronously on the cancelling thread when `set` is called
(or immediately if the token is already set). Implemented via
`BaseIO.chainTask` on `result?`, so no fresh `Task.map` per call and no
GC hazard.

Runtime cleanup:

- Add `LEAN_TASK_STATE_{WAITING,RUNNING,FINISHED}` constants in `lean.h`
matching `IO.TaskState`.
- Factor `lean::promise_is_resolved` inline in `object.h`, replacing
three open-coded `lean_io_get_task_state_core(...) == 2` checks (in
`interrupt.cpp`, `uv/timer.cpp`, `uv/signal.cpp`).
- Drop the manual `inc_ref(g_cancel_tk)` in `check_interrupted`; the
token is owned by the enclosing `scope_cancel_tk` for the duration of
the call (documented).
- Replace the bare `lean_always_assert(g_task_manager)` in
`lean_promise_new` with an explicit `lean_internal_panic` carrying a
message that names `Promise.new`, identifies the typical trigger
(`initialize` blocks, transitively via `IO.CancelToken.new`), and
recommends lazy construction. Without this, users got an opaque "LEAN
ASSERTION VIOLATION ... Condition: g_task_manager" with no actionable
hint.

Behavioural notes documented inline:

- `new` cannot be called from `initialize` blocks (task manager not
running yet); construct lazily.
- `task` documents the dropped-promise case (`none`) and steers callers
to `onSet` for callback chaining.

A consumer of `onSet` for parent → child cancel-token propagation in
parallel tactic combinators is in #13428 (fixes #13300).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-28 21:50:54 +00:00
..
uv refactor: make CancelToken Promise-based (#13303) 2026-04-28 21:50:54 +00:00
alloc.cpp fix: heartbeats from realizeConst should be ignored (#7473) 2025-03-13 15:10:29 +00:00
alloc.h fix: heartbeats from realizeConst should be ignored (#7473) 2025-03-13 15:10:29 +00:00
allocprof.cpp
allocprof.h
apply.cpp fix: deallocation for closures in non default configurations (#11217) 2025-11-17 16:27:20 +00:00
apply.h fix: split libInit_shared out of libleanshared (#3421) 2024-02-22 19:16:32 +00:00
array_ref.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
buffer.h chore: update to c++20 (#12117) 2026-02-11 01:17:40 +00:00
byteslice.cpp feat: add useful functions in Parsec, add error variant and Std.Data.ByteSlice (#9599) 2025-09-11 14:53:41 +00:00
byteslice.h feat: add useful functions in Parsec, add error variant and Std.Data.ByteSlice (#9599) 2025-09-11 14:53:41 +00:00
CMakeLists.txt feat: make mimalloc security options available (#13401) 2026-04-14 13:22:07 +00:00
compact.cpp fix: file read buffer overflow (#13392) 2026-04-13 17:56:27 +00:00
compact.h fix: ensure padding bytes for lean::mpz objects in olean files are zero (#11485) 2025-12-09 10:59:15 +00:00
debug.cpp refactor: port shell option processing to Lean (v2) (#11434) 2025-12-02 17:41:51 +00:00
debug.h fix: split libInit_shared out of libleanshared (#3421) 2024-02-22 19:16:32 +00:00
exception.cpp doc: add link to reference manual in stack overflow message (#12157) 2026-01-26 07:56:48 +00:00
exception.h fix: split libInit_shared out of libleanshared (#3421) 2024-02-22 19:16:32 +00:00
flet.h
hash.cpp fix: avoid unaligned pointer dereference (#12318) 2026-02-10 20:40:24 +00:00
hash.h feat: define Int8 (#5790) 2024-10-25 06:06:40 +00:00
init_module.cpp feat: implement basic async IO with timers (#6505) 2025-01-13 18:11:04 +00:00
init_module.h fix: split libInit_shared out of libleanshared (#3421) 2024-02-22 19:16:32 +00:00
int.h feat: define ISize and basic operations on it (#5961) 2024-11-05 15:08:19 +00:00
interrupt.cpp refactor: make CancelToken Promise-based (#13303) 2026-04-28 21:50:54 +00:00
interrupt.h fix: Windows build (#4628) 2024-07-02 20:32:00 +00:00
io.cpp fix: two bugs in io.cpp (#13427) 2026-04-16 12:38:17 +00:00
io.h fix: handle NUL bytes in IO functions (#9616) 2025-08-01 06:12:53 +00:00
libuv.cpp feat: add signal handling support using libuv (#9258) 2025-09-15 13:09:50 +00:00
libuv.h feat: add signal handling support using libuv (#9258) 2025-09-15 13:09:50 +00:00
list_ref.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
memory.cpp feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
memory.h fix: split libInit_shared out of libleanshared (#3421) 2024-02-22 19:16:32 +00:00
mpn.cpp
mpn.h
mpz.cpp fix: file read buffer overflow (#13392) 2026-04-13 17:56:27 +00:00
mpz.h feat: optimized division without remainder for Int and Nat (#8089) 2025-04-29 07:23:35 +00:00
mutex.cpp refactor: don't use shared_timed_mutex when not required anymore (#12434) 2026-02-11 12:53:42 +00:00
mutex.h feat: Mutex, Condvar 2022-09-05 08:52:46 -07:00
object.cpp refactor: make CancelToken Promise-based (#13303) 2026-04-28 21:50:54 +00:00
object.h refactor: make CancelToken Promise-based (#13303) 2026-04-28 21:50:54 +00:00
object_ref.cpp
object_ref.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
option_ref.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
optional.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
pair_ref.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
platform.cpp feat: ignore lean -R if module name is in setup (#8874) 2025-06-23 17:55:52 +00:00
platform.h
process.cpp fix: avoid duplicate buffered writes when IO.Process.output exec fails (#13464) 2026-04-27 23:51:20 +00:00
process.h
sharecommon.cpp feat: support mpz in the shareCommon APIs (#7838) 2025-04-06 19:52:50 +00:00
sharecommon.h perf: use mimalloc with important C++ hash maps (#7868) 2025-04-11 16:23:33 +00:00
sstream.h
stack_overflow.cpp fix: Do not overwrite existing signal handlers (#5062) 2024-08-19 09:11:38 +00:00
stack_overflow.h
stackinfo.cpp fix: make the stack handling more robust to sanitizers and -O3 (#6143) 2024-11-22 15:10:20 +00:00
stackinfo.h fix: split libInit_shared out of libleanshared (#3421) 2024-02-22 19:16:32 +00:00
string_ref.h perf: mark move constructors and assignment operators as noexcept (#10784) 2025-10-22 14:21:51 +00:00
thread.cpp feat: increase default stack size from 8MB to 1GB (#12971) 2026-03-20 15:40:00 +00:00
thread.h fix: add missing release() and adopt_lock_t to single-threaded unique_lock stub (#13233) 2026-04-07 09:30:13 +00:00
utf8.cpp fix: validate UTF-8 at C++ -> Lean boundary (#3963) 2024-06-19 14:05:48 +00:00
utf8.h fix: validate UTF-8 at C++ -> Lean boundary (#3963) 2024-06-19 14:05:48 +00:00