Chez concurrency pt.1: real OS-thread futures + blocking promises (shared heap)

future/future-call run the body on a native thread (fork-thread) over the SAME
heap — JVM semantics, not Janet's isolated-heap snapshot. deref blocks on a
mutex+condition latch; timed (deref f ms val) uses an absolute deadline.
promise is a real blocking promise (deref parks until deliver), replacing the
Janet non-blocking atom shim. future?/future-done?/future-cancelled?/future-cancel
/realized? are native (the overlay versions read Janet map keys); re-asserted in
post-prelude over the overlay. pmap/pcalls/pvalues (overlay, over future) light
up for free.

Thread-safety this forces:
- atoms get a per-atom mutex; swap!/swap-vals! are a JVM-style CAS loop (f runs
  outside the lock, so a watch/validator can deref the same atom); reset!/
  compare-and-set! are atomic.
- the dynamic binding stack becomes a Chez thread-parameter, so each future/thread
  has its own; Chez inherits it at fork, giving binding conveyance (the shim also
  installs an explicit snapshot).
- Thread/sleep really sleeps now (a worker sleeping doesn't block the parent).

Re-minted the seed: future-call now resolves at compile time, so pmap compiles to
a var-deref instead of the host-static-call fallback that crashed. image.ss
unchanged.

Corpus: the 2 snapshot cases now match the JVM (shared) not Janet (isolated) —
allowlisted on both Chez gates; the two racy future-cancel cases allowlisted;
"promise undelivered" (blocks on JVM/Chez, profile :bucket :timeout) skipped like
:throws. Zero-Janet corpus 2544 -> 2569, 0 new divergences, floor raised. Full
Janet gate + JVM cert green.

jolt-byjr
This commit is contained in:
Yogthos 2026-06-20 13:11:31 -04:00
parent a23095b502
commit ec30c9e405
10 changed files with 330 additions and 42 deletions

View file

@ -945,7 +945,7 @@
(guard (e (#t #f))
(def-var! "clojure.core" "take-nth" (letrec ((take-nth (case-lambda ((n) (let fnrec177 ((n n)) (lambda (rf) (let fnrec178 ((rf rf)) (let* ((iv (jolt-invoke (var-deref "clojure.core" "volatile!") -1.0))) (case-lambda (() (let fnrec179 () (jolt-invoke rf ))) ((result) (let fnrec180 ((result result)) (jolt-invoke rf result))) ((result input) (let fnrec181 ((result result) (input input)) (let* ((i (jolt-invoke (var-deref "clojure.core" "vswap!") iv jolt-inc))) (if (jolt-zero? (remainder i n)) (jolt-invoke rf result input) result)))))))))) ((n coll) (let fnrec182 ((n n) (coll coll)) (jolt-invoke (var-deref "clojure.core" "make-lazy-seq") (lambda () (let fnrec183 () (jolt-invoke (var-deref "clojure.core" "coll->cells") (let* ((temp__15__auto (jolt-seq coll))) (if (jolt-truthy? temp__15__auto) (let* ((s temp__15__auto)) (jolt-cons (jolt-first s) (jolt-invoke take-nth n (jolt-drop n s)))) jolt-nil))))))))))) take-nth)))
(guard (e (#t #f))
(def-var! "clojure.core" "pmap" (letrec ((pmap (case-lambda ((f coll) (let fnrec184 ((f f) (coll coll)) (jolt-map (var-deref "clojure.core" "deref") (jolt-invoke (var-deref "clojure.core" "doall") (jolt-map (lambda (x) (let fnrec185 ((x x)) (host-static-call "clojure.core" "future-call" (lambda () (let fnrec186 () (jolt-invoke f x)))))) coll))))) ((f coll . colls) (let fnrec187 ((f f) (coll coll) (colls (list->cseq colls))) (jolt-invoke pmap (lambda (xs) (let fnrec188 ((xs xs)) (jolt-apply f xs))) (jolt-apply jolt-map jolt-vector coll colls))))))) pmap)))
(def-var! "clojure.core" "pmap" (letrec ((pmap (case-lambda ((f coll) (let fnrec184 ((f f) (coll coll)) (jolt-map (var-deref "clojure.core" "deref") (jolt-invoke (var-deref "clojure.core" "doall") (jolt-map (lambda (x) (let fnrec185 ((x x)) (jolt-invoke (var-deref "clojure.core" "future-call") (lambda () (let fnrec186 () (jolt-invoke f x)))))) coll))))) ((f coll . colls) (let fnrec187 ((f f) (coll coll) (colls (list->cseq colls))) (jolt-invoke pmap (lambda (xs) (let fnrec188 ((xs xs)) (jolt-apply f xs))) (jolt-apply jolt-map jolt-vector coll colls))))))) pmap)))
(guard (e (#t #f))
(def-var! "clojure.core" "pcalls" (letrec ((pcalls (lambda fns (let fnrec189 ((fns (list->cseq fns))) (jolt-invoke (var-deref "clojure.core" "pmap") (lambda (f) (let fnrec190 ((f f)) (jolt-invoke f ))) fns))))) pcalls)))
(guard (e (#t #f))