Architecture

This guide shows how the MoonBit packages, the C runtime and the JavaScript addon of luna_thread fit together, and which integer codes cross the foreign function interface. The package pages describe each MoonBit package in detail.

Layers

The specification names two realization paths:

MoonBit facade -> MoonBit C FFI  -> C wrapper  -> C runtime
MoonBit facade -> MoonBit JS FFI -> JS wrapper -> N-API addon -> C runtime

The first path exists. The second stops at the MoonBit side: backend/js names the JavaScript target, and the addon in js/ exports only a runtimeName function.

The MoonBit packages depend on each other without cycles:

PackageImports
sharednothing but the core library
planshared
workflowplan, shared
backend/jsplan, shared
backend/nativeplan, shared, workflow
corebackend/native, plan, shared, workflow
  • shared holds the vocabulary every other package uses: backend targets, execution modes, ordering guarantees, execution policies and their validation, runtime capability tables, and integer-coded mirrors of the C request structs.
  • plan describes one data-parallel operation over a typed input domain and checks it against the v1 subset.
  • workflow describes a task graph whose nodes may be compute plans or synchronisation steps bound to capabilities, and checks its structure.
  • backend/native translates plans into typed native requests, calls the C kernels, and marshals workflows to the C scheduler.
  • core, the root package, re-exports the common entry points with defaults and routes submissions to the native backend.

The C runtime

The runtime is one C file with one header:

FileRole
native/include/luna_thread_runtime.hPublic C API: status codes, request structs, workflow types.
native/src/runtime.cKernels, validation and the workflow scheduler.
luna_thread/backend/native/ffi_runtime_impl.cThe same source, compiled as a MoonBit native stub; it differs only in its #include path.
luna_thread/backend/native/ffi_runtime_bridge.cluna_mbt_* wrappers that turn MoonBit arguments into request structs.
luna_thread/backend/native/ffi_stub.cDeclarations of the luna_mbt_* wrappers.
native/tests/smoke.cA C smoke test of the kernels and of their failure statuses.

The two copies of the runtime must be kept identical by hand.

There are two builds. moon compiles the stubs with the system C compiler and no OpenMP flags, so the #pragma omp loops run on the calling thread, while the workflow scheduler still starts POSIX threads. CMake (make native-configure, make native-build) builds native/ as a static library with OpenMP when LUNA_THREAD_ENABLE_OPENMP is on, together with the smoke test. The native backend design explains both execution models.

Codes across the interface

All enumerations cross the C interface as Int. The MoonBit side maps them in backend/native (and shared for statuses).

Status codes

CodeC nameMoonBit NativeStatus
0LUNA_THREAD_STATUS_OKOk
1INVALID_ARGUMENTInvalidArgument
2UNSUPPORTED_BACKENDUnsupportedBackend
3UNSUPPORTED_MODEUnsupportedMode
4UNSUPPORTED_VALUE_TYPEUnsupportedValueType
5UNSUPPORTED_REDUCTION_KERNELUnsupportedReductionKernel
6OVERFLOWOverflow
7NULL_POINTERNullPointer
8UNSUPPORTED_RUNTIME_PRIMITIVEnone
9RUNTIME_BROKENnone
10CHANNEL_EMPTYnone
11CHANNEL_CLOSEDnone
12MUTEX_PROTOCOL_ERRORnone
13CONDVAR_PROTOCOL_ERRORnone
14BARRIER_BROKENnone

Codes 8 to 14 come only from the workflow runtime; WorkflowResult::status carries them as raw integers.

Workflow codes

CodeNode kindCapability kindEdge kindRuntime state
0ComputeOwnedBufferDataDependencySubmitted
1SpawnSharedReadViewControlDependencyRunning
2JoinAtomicCellOwnershipTransferCompleted
3SendMutexSynchronizationDependencyFailed
4RecvCondvarRejected
5LockRwLock
6UnlockSemaphore
7WaitBarrier
8SignalChannel
9BarrierOpaque
10ReadShared
11WriteShared

Value types are 0 for I32 and 1 for I64; reduction kernels are 0 for Sum, 1 for Min and 2 for Max.

The JavaScript addon

js/ is a node-gyp project (make js-install, make js-build) whose C source registers one function, runtimeName, returning "luna_thread_addon". It does not link the C runtime yet.

The specification

doc/attachments/moonbit_parallel_spec/main.typ is the normative English specification and main.zh_CN.typ its Chinese mirror; make docs compiles both. The specification defines the workflow calculus, ownership transfer, the ABI layouts and the conformance obligations. The implementation covers the data model and validation of Part I and the native realization of a subset of it; the package design pages state which parts. docs/spec-review-report-zh.md records review findings in Chinese.

Repository layout

PathContents
luna_thread/The MoonBit module (moon.mod) and its packages.
native/The C runtime, its header, CMake build and smoke test.
js/The Node.js addon scaffold.
doc/This manual, its translations and the specification.
scripts/check-env.shChecks the MoonBit, C, Node.js and Typst toolchains.
MakefileShortcuts for checking, building the native runtime and the addon, and compiling the specification.