Expand description
§go-lib
Go-style concurrency for Rust: goroutines, channels, select, WaitGroup —
built on a port of the M:N scheduler from https://github.com/golang/go.
No async runtime is used: the scheduler, channels, and parking primitives
are ported from src/runtime/ in the Go repo. Mutexes and read-write locks
are taken straight from std::sync because their uncontended path is
just an atomic CAS — porting Go’s versions would be code without benefit.
See [runtime::syscall] for the shim that keeps std blocking calls
scheduler-safe.
§Public surface
go!/select!macros — spawn goroutines, multiplex channel opschan— buffered and unbuffered channelsnet— goroutine-awareTcpListener/TcpStream(v2.0)sync::WaitGroup— wait for a collection of goroutinessync::Cond— goroutine-aware condition variablesync::Mutex/sync::RwLock— re-exports ofstd::synccontext— cancellation and deadline propagationset_panic_handler— customise goroutine-panic behaviourset_gomaxprocs/gomaxprocs— runtime parallelism control
§Internals
See runtime for the scheduler (G/M/P, parking, work stealing, sysmon,
stack growth, async preemption, netpoll).
§v2.0 — new in this release
- Dynamic stack growth (Step 3): goroutines start with an 8 KiB stack
and grow automatically up to 1 GiB via SIGSEGV guard-page detection and
copystack(conservative pointer adjustment). - Async preemption (Step 4): sysmon sends
SIGURGto the M thread whose goroutine has run > 10 ms. The signal handler redirects execution to an assembly trampoline that saves all registers, callsasync_preempt2, and restores state on resume — a transparent, non-cooperative yield. - Netpoll / async I/O (Step 5):
epollon Linux,kqueueon macOS. Goroutines park onEAGAINand are re-enqueued when the fd is ready. See thenetmodule forTcpListener/TcpStream.
§Known limitations
§defer / recover / cross-goroutine panic
Goroutine panics are caught and routed to set_panic_handler; the
process does not abort. Go’s recover() (stopping panic propagation at a
call-stack boundary) has no direct Rust equivalent — use catch_unwind
inside the goroutine body when fine-grained recovery is needed.
§Race detector
The Go race detector is a compiler/runtime feature with no Rust equivalent
in this crate. Use cargo test --cfg loom with the [loom model checker]
for systematic concurrency testing.
§Unsafe conventions
The runtime modules (src/runtime/) are a direct port of Go’s C-adjacent
runtime code. Almost every function is unsafe fn because it operates on
raw goroutine pointers and mmap’d memory. Inner unsafe {} blocks are
omitted for brevity (suppressed via unsafe_op_in_unsafe_fn) — the caller’s
obligation is documented in each function’s # Safety section instead.
Modules§
- chan
- Channels — ported from
src/runtime/chan.go. - context
- Cancellation and deadline propagation — equivalent to Go’s
contextpackage. - net
- Goroutine-aware TCP networking (Step 5: netpoll integration).
- runtime
- Goroutine scheduler internals.
- select
selectgo— the runtime heart ofselect { }.- sync
- Synchronization primitives.
Macros§
Functions§
- gomaxprocs
- Return the current number of logical processors (GOMAXPROCS).
- gosched
- Yield the CPU, giving other goroutines a chance to run.
- run
- Initialise the go-lib scheduler and run
fas the first goroutine. - set_
gomaxprocs - Set the number of logical processors and return the previous value.
- set_
panic_ handler - Register a custom handler for goroutine panics.
- sleep
- Sleep the current goroutine for at least
d. - with_
syscall - Wrap a potentially-blocking operation so the go-lib scheduler can hand off this goroutine’s P to another M while the OS thread is in the kernel.