|
jam 0.0.1
A compacting generational garbage collector for C++26
|
The weak guest API is jam.vm.Weak. It exposes the collector hooks through a Java JAR and a small JNI library. A finalizer is a JVM Runnable. It may enter whatever guest context it needs; jam-vm has no context IDs or routing table. As long as some caller pumps the queue, callbacks from every producer can run. In Native Image, the same API connects directly to the collector and the queue belongs to the current isolate.
The thc owner can lower weak primitives to this API and wrap Haskell finalizers in runnables that enter thc. The collector owns reachability, retirement and at-most-once claims. The runnable owns entering and executing the guest code.
Planned support for lifted Haskell weak keys is tracked in issue #7. The API below provides ordinary weak associations.
Run the source-tree commands below from vm/.
The same API JAR supplies jam.vm.Lifted:
These are language calls outside collection. resolve() reports an already available replacement without evaluating it. project(n) reports an existing reference-valued projection without forcing or boxing it. Null means unavailable; it does not distinguish a terminal constructor from an unresolved thunk. The language owns that distinction, field numbering, publication and cycle handling.
Store replaceable references as Lifted. Both computations and terminal constructors implement it, so replacement remains type-correct. A language Int constructor can hold its primitive integer directly, like I#; it need not wrap java.lang.Integer. Primitive fields are not turned into lifted references by projection. Ordinary foreign Java objects need an explicit language representation if they are to occupy these slots.
This interface neither rewrites Java slots during GC nor changes ordinary Weak registrations. A language weak wrapper can resolve before registration or install a bootstrap finalizer against an unresolved thunk. The bootstrap uses the existing claim/completion protocol to retain its captures while it installs and publishes a replacement association. The full lifecycle and THC adoption remain tracked in issue #7. The provider smoke test exercises re-registration through nested GC on both HotSpot and Native Image; it is not a complete language weak implementation.
Use a JDK 25 installation and the platform C compiler:
This does not rebuild jam or HotSpot. It writes:
| Output | Purpose |
|---|---|
| build/bridge/jam-vm.jar | Public jam.vm.Weak class; automatic module name jam.vm |
| build/bridge/jam-vm-sources.jar | API sources for the consumer's IDE |
| build/bridge/lib/ | JNI shim: libjam_bridge.dylib, libjam_bridge.so or jam_bridge.dll |
| build/bridge/include/jam_vm_Weak.h | JNI declarations generated by javac |
| build/bridge/api/ | Javadoc for the exported methods |
The shim resolves the JVM_JamWeak* exports in the running VM. It does not link thc to the collector's C++ modules. A stock JVM without these exports reports a linkage error; a patched JVM running another collector reports UnsupportedOperationException. Call Weak.checkAvailable() when initializing the integration so a missing collector is reported before registering work.
Add the JAR to the host class path and its native directory to the library path:
For module-path use, enable native access for jam.vm instead. Load the API from a shared host class loader: the JVM associates a loaded native library with its loader. The JAR and shim are a pair for the pinned Jam-enabled JDK, not an independently versioned VM extension protocol. Current packages support macOS arm64, Linux x86_64 and Windows x86_64; consult their platform requirements. On Windows, separate class-path entries with ; and use bin/ as the native library directory in a packaged JDK.
First, install the weak association:
key must be nonnull. value is the object returned by a successful dereference. finalizer is a Runnable, or null when none is needed. Captures of the key or value inside the runnable participate in the conditional graph; the registry does not turn them into independent strong roots.
Now, you can dereference it:
The returned value is an ordinary strong Java reference. Null means either a retired/unknown registration or an active null value. Use a nonnull guest value or box null if that distinction matters. For a GHC weak value represented by a managed closure, the closure itself should supply the nonnull representation.
A guest weak-handle wrapper should contain only the token. Storing its key, value or finalizer as ordinary strong fields in the wrapper would change the reachability problem. Tokens are monotonic IDs local to a JVM or native isolate, never reused. Do not persist them across runs or treat them as security capabilities. Dropping a wrapper does not cancel its finalizer.
Any caller can drain currently pending work:
The pump claims one runnable at a time, calls run() on its own thread, and releases the collector root in a finally block. The call to run() happens after leaving the VM entry and its heap lock. There is no guest callback inside the GC safepoint.
Callbacks can allocate, trigger collection or pump recursively. Different threads may also pump concurrently: each claim is atomic with respect to other pumps and explicit finalization. Finalizers have no promised execution order. The pump returns when a poll observes the queue empty; it neither requests GC nor waits for future work.
If a runnable throws, its claim is completed and the exception propagates to the caller. Remaining callbacks stay queued for a subsequent pump. The caller chooses whether to catch that exception and continue pumping. Jam-vm does not retry a callback that has already been claimed.
There is currently no wakeup notification. Arrange for a host thread or event loop to call pump() periodically. Context identity, attachment and shutdown remain inside the installed runnable and its owner, invisible to this API.
Explicit finalization shares the same at-most-once claim as the pump:
This retires an active or queued association before returning its runnable. Dereference remains null even if the finalizer resurrects the key. An absent, already-running, completed or unknown registration returns null. An association without a finalizer is still retired.
Weak.take(long[] tokenOut) exposes the pump's nonblocking claim operation for callers that need to schedule execution elsewhere. It returns a runnable and writes its token to element zero, or returns null and writes zero when empty. The array must have at least one element.
Every successful take or finalizeNow claim must eventually be completed. For asynchronous execution, call complete after the runnable actually finishes, not immediately after submitting it. If submission fails, release the claim as part of handling that failure. Pending and running finalizers stay rooted across GC; losing a returned runnable without completing it leaks that retained graph. Repeated completion is harmless.
Use the Jam-enabled GraalVM for compiled Truffle execution. The packaged runtime includes the API at lib/jam/jam-vm.jar and its native libraries under lib/jam/. Keep thc's patched Truffle libraries on its normal runtime class path.
thc currently enables compact object headers by default. Build its launchers with -Pthc.compactObjectHeaders=false for Jam, which requires ordinary headers. Set JAVA_HOME to the packaged GraalVM and add these JVM options when launching thc:
This selects the collector. THC's weak primitive lowering and finalizer pump integration shipped in THC #1200. See supported configurations for qualification limits.
The optional Candidate API is packaged in vm-2026.10.09-0e36293 for macOS arm64, Linux x86_64 and Windows x86_64. Focused HotSpot/GraalVM and Native Image probes passed on all three platforms; full supplier CI passed all fifteen jobs. This qualifies the supplier API, not arbitrary language scheduler behavior. Use the matching SDK and provider from the release's platform manifest. Issue #36 records the packaging work, and #38 records the subsequent compiler repair and qualified delivery.
jam.vm.Candidate is an optional API for a language runtime that has already captured a suspended computation in a heap object. It does not capture stacks, wake threads, or execute guest code. The owner contains the saved execution and pending operation; the collector need not understand either.
Call Candidate.arm(owner, waitGeneration) while ordinary roots still hold the owner. Publish the returned ticket and wait generation in an owner-free inventory before dropping those roots. A carrier stack, thread attachment or wake callback that still retains the owner prevents selection. Reference.reachabilityFence can keep the owner alive through inventory publication.
Minor collections retain every registered owner. A major first finishes ordinary strong tracing and the live-key weak closure, then selects all unreachable armed owners before tracing any of them. It traces that batch and closes weak associations again before retiring dead weak keys. Mutually unreachable suspended owners are therefore selected together, while owners reached through ordinary live weak associations are not selected.
Candidate.poll(ticket, waitGeneration) claims a selected owner. For ordinary wakeup or cancellation, Candidate.disarm(ticket, waitGeneration) can claim an armed or selected owner instead. Both return null for stale generations or an existing claimant. A successful claim stays rooted across further collections until Candidate.complete(ticket, waitGeneration) releases it. Complete only after installing an ordinary strong execution root or finishing terminal cleanup; a failed scheduling attempt must retain and retry ownership. A null claim cannot undo an already committed guest operation. Abandoning a claim leaks its retained root rather than losing the suspended execution.
The normal-time service scans once initially, then snapshots Candidate.epoch() before scanning its synchronized inventory. Record that snapshot only after a successful scan. The epoch is published after compaction and reference repair; a selection during the scan causes another pass. At Long.MAX_VALUE it saturates, so scan on every poll. A failed collection attempt does not consume a pending publication. This API does not trigger collection or promise progress without a major collection.
Tickets belong to one JVM or isolate. Slot generations prevent stale ticket reuse; the positive caller-owned wait generation distinguishes logical operations. The native registry is shared by HotSpot and Native Image. Hosts use the existing weak-root, weak-close, preparation and finish phases; heaps without candidates keep their existing weak-close path.
This source API requires a provider with the Candidate hooks. Compiling its Java class against an older runtime does not add those hooks. JNI reports missing optional hooks only when Candidate is used, leaving the existing Weak API usable. See the Java API documentation for failure and ownership contracts.