Runtime support layer for gen_statem, built against existing public API (channel + scheduler::spawn), sibling to gen_server. No macro: per review, build the primitives first and hand-write the Switch example to evaluate whether a statem! macro earns its place before committing to one. - src/statem.rs: Machine trait (on_start/handle), Resolution<S> with From<S>, Cx (on_unhandled), Reply<T> move-only reply handle, StatemRef (send/call), spawn + inbox loop. Real time only; Postpone + Cx timeout arming are in the type surface but not yet acted on (chunks 2-3). - examples/statem_switch.rs: the RFC Switch machine hand-written against the primitives, tagged USER vs MACRO to mark what a macro would generate. Asserts the RFC end-state (flips=1, enters=3). - tests/statem.rs: call/cast round-trip, enter-on-start/transition-not-stay, panicking-handler -> Down. Reply<T> included (the call helper needs a handle type; keeps the example true to the RFC surface) but isolated and trivially removable if we drop it.
101 lines
3.5 KiB
Rust
101 lines
3.5 KiB
Rust
//! # smarm — Silly Marks Abstract Rust Machine
|
|
//!
|
|
//! Erlang-style green-thread actor concurrency for Rust.
|
|
//!
|
|
//! Multi-threaded: N scheduler OS threads (default: one per CPU) share a
|
|
//! single global run queue behind a `Mutex`. Actors communicate by sending
|
|
//! `Send` messages over channels; every actor has a supervisor. Synchronisation
|
|
//! primitives — `Mutex<T>` with mandatory lock timeouts, channel `recv`,
|
|
//! `sleep`, and epoll-backed `wait_readable`/`wait_writable` — all park the
|
|
//! green thread, never the OS thread.
|
|
//!
|
|
//! See `LOOM.md` for the design intent and the deferred-for-later list.
|
|
|
|
pub mod stack;
|
|
pub mod context;
|
|
pub mod preempt;
|
|
pub mod pid;
|
|
pub mod actor;
|
|
pub mod channel;
|
|
pub mod scheduler;
|
|
pub mod supervisor;
|
|
pub mod timer;
|
|
pub mod io;
|
|
pub mod mutex;
|
|
pub mod monitor;
|
|
pub mod registry;
|
|
pub mod pg;
|
|
pub mod link;
|
|
pub mod gen_server;
|
|
pub mod statem;
|
|
pub mod introspect;
|
|
#[cfg(feature = "observer")]
|
|
pub mod observer;
|
|
pub mod runtime;
|
|
pub(crate) mod raw_mutex;
|
|
pub(crate) mod slot_state;
|
|
pub(crate) mod sync_shim;
|
|
#[doc(hidden)] // pub only so benches/rq_micro.rs can drive the raw structures
|
|
pub mod run_queue;
|
|
pub mod trace;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Global allocator
|
|
// ---------------------------------------------------------------------------
|
|
|
|
#[global_allocator]
|
|
static ALLOCATOR: preempt::PreemptingAllocator = preempt::PreemptingAllocator;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Public API re-exports
|
|
// ---------------------------------------------------------------------------
|
|
|
|
pub use channel::{
|
|
channel, select, select_timeout, try_select, try_select_timeout, Receiver, RecvError,
|
|
RecvTimeoutError, Selectable, Sender,
|
|
};
|
|
pub use gen_server::{
|
|
call, cast, shutdown, whereis_server, CallError, CallTimeoutError, CastError, GenServer,
|
|
NamedServerBuilder, ServerBuilder, ServerCtx, ServerName, ServerRef, TimerHandle, Watcher,
|
|
};
|
|
pub use statem::{
|
|
CallError as StatemCallError, Cx, Machine, Reply, Resolution, SendError as StatemSendError,
|
|
StatemRef,
|
|
};
|
|
pub use introspect::{
|
|
actor_info, snapshot, tree, tree_from, ActorInfo, ActorState, RuntimeSnapshot, RuntimeTree,
|
|
TreeNode, SNAPSHOT_FORMAT_VERSION,
|
|
};
|
|
#[cfg(feature = "observer")]
|
|
pub use observer::{ObserverReply, ObserverRequest};
|
|
pub use link::{link, trap_exit, unlink, ExitSignal};
|
|
pub use monitor::{demonitor, monitor, Down, DownReason, Monitor, MonitorId};
|
|
pub use mutex::{LockTimeout, Mutex, MutexGuard};
|
|
pub use pid::{Addressable, Erased, Name, Pid, RawPid};
|
|
pub use pg::{dispatch, join, leave, members, members_as, pick, pick_as, Incarnation, Member, NodeId};
|
|
pub use registry::{
|
|
install, lookup_as, register, send, send_dyn, send_to, unregister, whereis, RegisterError,
|
|
SendError,
|
|
};
|
|
pub use runtime::{init, Config, Runtime};
|
|
pub use scheduler::{
|
|
block_on_io, cancel_timer, request_stop, run, self_pid, send_after, send_after_named, sleep,
|
|
spawn, spawn_addr, spawn_under, wait_readable, wait_readable_timeout, wait_writable,
|
|
wait_writable_timeout, yield_now, FdArm, JoinError, JoinHandle,
|
|
};
|
|
pub use supervisor::{ChildSpec, OneForOne, Restart, Signal, Strategy};
|
|
pub use timer::TimerId;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// check!()
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Voluntarily check whether this actor's timeslice has expired, yielding
|
|
/// if so.
|
|
#[macro_export]
|
|
macro_rules! check {
|
|
() => {
|
|
$crate::preempt::maybe_preempt()
|
|
};
|
|
}
|