smol event-listener and async-lock (Rust)
The smol ecosystem's answer to "one primitive for tasks and threads": an Event that is a Vyukov-style eventcount — a FIFO intrusive list of listeners, each of which may be woken by a task Waker or a thread Unparker — and async-lock, which builds Mutex, RwLock, Semaphore, Barrier and OnceCell as a plain atomic state word plus one or more Events, so every lock has both .await and _blocking entry points over the same queue.
| Kind | libraries (event-listener 5.4.2, async-lock 3.4.2, plus their helpers event-listener-strategy and parking) |
| Language | Rust (no_std + alloc capable) |
| License | Apache-2.0 OR MIT |
| Repository | smol-rs/event-listener at 77004ac77dce (2026-07-27); smol-rs/async-lock at 23e181d5cfb9 (2026-06-27); smol-rs/event-listener-strategy at be0f536e7a18; smol-rs/parking at cedb1884e050 |
| Primitives covered | Event, EventListener, listener!, Notification (notify, additional, relaxed, tags); async_lock::{Mutex, RwLock, Semaphore, Barrier, OnceCell}; Strategy (Blocking, NonBlocking); parking::Parker |
| Parking substrate | a per-Event intrusive doubly-linked list of listener links under a std::sync::Mutex (or a critical-section cell, or a spinlock without either); each link holds a Task = Waker | Unparker; threads park on parking::Parker (Mutex<()> + Condvar + 3-state word) |
| Fairness | Event: FIFO notification; Mutex: barging with eventual fairness after 500 µs of starvation; Semaphore: barging; RwLock: write-preferring |
| Source read | event-listener: src/lib.rs, src/intrusive.rs, src/notify.rs, examples/mutex.rs; async-lock: src/lib.rs, src/mutex.rs, src/semaphore.rs, src/rwlock/raw.rs, src/rwlock.rs, src/once_cell.rs, src/barrier.rs; event-listener-strategy/src/lib.rs; parking/src/lib.rs |
Overview
What it solves
event-listener exists to turn any non-blocking data structure into a blocking and an async one with the same code. The crate opens (lib.rs L1–L10):
Notify async tasks or threads.
This is a synchronization primitive similar to eventcounts invented by Dmitry Vyukov.
You can use this crate to turn non-blocking data structures into async or blocking data structures. See a simple mutex implementation that exposes an async and a blocking interface for acquiring locks.
The pattern is the eventcount loop: check the condition, listen(), check again, then wait — .await in a task or .wait() on a thread (lib.rs L44–L61). Event itself states its contract (L155–L173):
If there are no active listeners at the time a notification is sent, it simply gets lost. […]
If a notified listener is dropped without receiving a notification, dropping will notify another active listener. Whether one additional listener will be notified depends on what kind of notification was delivered.
Listeners are registered and notified in the first-in first-out fashion, ensuring fairness.
Design philosophy
Separate the state from the wait queue. async-lock keeps each lock's state in its own AtomicUsize and uses Event only as a wait queue that carries no data. The uncontended paths never touch the Event's list; the contended paths are the eventcount loop. That makes event-listener the "base" of async-lock (and of async-channel, async-io, … in the smol stack): one audited wait-queue implementation, many lock-free state machines on top.
Threads and tasks are peers. A listener records whichever waker its current waiter has. async-lock's own crate docs then tell users not to reach for it unless they must (async-lock lib.rs L11–L30):
In general, you should consider using
std::synctypes over types from this crate.There are two primary use cases for types from this crate:
- You need to use a synchronization primitive in a
no_stdenvironment.- You need to hold a lock across an
.awaitpoint. […]In contrast,
async-lock's notification system usesstd::sync::Mutexunder the hood if thestdfeature is enabled, and will fall back to a significantly slower strategy if it is not. So, there are few cases whereasync-lockis a win for performance overstd::sync.
How it works
Event is a single AtomicPtr<Inner<T>>, null until first use, then a leaked Arc<Inner> installed by CAS (lib.rs L174–L181, inner() L469–L500). Inner holds an AtomicUsize notified and the list (L131–L144). The list (intrusive.rs L21–L48) is { head, tail, next, len, notified }, where next points at the first unnotified listener — the boundary that makes notify(n) and notify_additional(n) cheap to distinguish.
listen()heap-allocates anInnerListener(Box::pin) holding anArcto theInnerand aLink { state: Cell<State>, prev, next }, and links it at the tail under the list lock (lib.rsL305–L318,intrusive.rsL180–L212). Thelistener!macro does the same with a pinnedStackSlot, avoiding the allocation (lib.rsL1043–L1051).- A link's
StateisCreated,Task(Task),Notified { additional, tag }orNotifiedTaken(L1207–L1222);TaskisWaker(Waker)orUnparker(parking::Unparker)(L1287–L1313). notify(n)runs the notification's fence, then locks the list and walks fromnext: for a plain notification it first subtracts the already-notified count — "notifies at least the specified number of listeners" — whileadditionalalways notifiesnmore (intrusive.rsL347–L387). Each chosen link becomesNotifiedand, if it held aTask, that task is woken while the list lock is held.- Polling a listener calls
registerunder the lock: ifNotified, it removes itself and returns the tag; otherwise it stores the current waker (will_wakeshort-circuits a re-store) (L233–L274).
async_lock::Mutex (mutex.rs L20–L54) is a state: AtomicUsize — bit 0 = locked, the remaining bits count starving waiters (in units of 2) — and lock_ops: Event. try_lock is compare_exchange(0, 1, Acquire, Acquire) (L164); unlock is fetch_sub(1, Release) followed by lock_ops.notify(1) (L203–L207). The slow path AcquireSlow is described under fairness. Semaphore is count: AtomicUsize + event; acquire loops try_acquire / listen / wait, release is fetch_add(1) + notify(1) (semaphore.rs L289–L310, L381–L385). RwLock is a Mutex<()> that writers hold, a state word (WRITER_BIT = 1, readers in units of 2) and two Events, no_readers and no_writer (rwlock/raw.rs L22–L48). OnceCell is a 3-state word plus active_initializers and passive_waiters events (once_cell.rs L89–L105); Barrier is a Mutex<State { count, generation_id }> plus an Event.
Every future is written once as an EventListenerFuture with poll_with_strategy; Strategy::NonBlocking polls the listener with the task's Context, Strategy::Blocking calls listener.wait() (event-listener-strategy L453–L525). lock_blocking() is self.lock().wait() (mutex.rs L143); acquire_blocking(), read_blocking(), wait_blocking() likewise.
Analysis
State word and fast path
Mutex: oneusize. Lock:CAS(0 → 1, Acquire). Unlock:fetch_sub(1, Release)thenEvent::notify(1).notifyis not guarded by a waiter check: at this SHA it runsfull_fence()(on x86 alock noton a stack word, cheaper thanmfence,notify.rsL579–L610), allocates theEvent'sInneron first use, and takes the list mutex (lib.rsL448–L456,intrusive.rsL223–L227). Thenotifiedatomic the list maintains (update_notified, L390–L399) is read only byis_notified(). So an uncontended unlock costs a fence plus an OS-mutex lock/unlock, the same order of cost as tokio'sreleaseand the reason the crate defers tostd::sync.Semaphore:countCAS loop down,fetch_add+notify(1)up.RwLock: readers CASstate + 2whileWRITER_BITis clear; the last reader'sfetch_subnotifiesno_readers;write_unlockclears the bit, notifiesno_writer, and unlocks the writer mutex (L240–L276).
Waiting: spin, yield, park
No primitive spins on the state word. A task returns Pending after registering its Waker. A thread in wait() borrows a thread-local (Parker, Unparker) pair, registers the Unparker, and parks (lib.rs L1096–L1171). parking::Parker is the familiar EMPTY/PARKED/NOTIFIED word over a Mutex<()> and Condvar; unpark swaps in NOTIFIED unconditionally ("we must write NOTIFIED even if state is already NOTIFIED") and briefly takes the lock before notify_one so a thread between PARKED and cvar.wait cannot miss it (parking/src/lib.rs L326–L438). With neither std nor critical-section, the list lock itself is a test-and-test-and-set spinlock with spin_loop() that the crate calls "dangerous to rely on" (intrusive.rs L424–L493).
The parking substrate
Each Event owns its queue; there is no global table. Waiter records are the Links embedded in the listener, which lives either in a Box (listen(), one allocation per wait) or on the waiter's stack (listener!). Because Inner is reference-counted and each listener holds an Arc, a listener may outlive the borrow of the Event. The list is guarded by std::sync::Mutex (poisoning ignored), by critical_section::Mutex<RefCell<…>> when that feature is on (embedded targets), or by the spinlock — three backends behind one with_inner (intrusive.rs L109–L177).
NOTE
Inner's field comment still says "On no_std platforms, this is a more traditional Vec of listeners, with an atomic queue used as a backup for high contention" (lib.rs L138–L143). At this SHA the intrusive.rs module is the only backend, mounted as mod sys by a #[path] attribute; the comment describes the removed no_std implementation.
Fairness, handoff and starvation
Event is FIFO; the locks on top are not. A notification marks the oldest unnotified listener, but it hands over no ownership: the woken waiter must win a CAS on the lock's own state, and a newcomer can take it first.
Mutex: barging with a 500 µs eventual-fairness bound. The doc: "The locking mechanism uses eventual fairness to ensure locking will be fair on average without sacrificing performance. This is done by forcing a fair lock whenever a lock operation is starved for longer than 0.5 milliseconds" (mutex.rs L20–L24). AcquireSlow::poll_with_strategy (L520–L628) runs two loops:
- Unfair loop. Listen, then
CAS(0 → 1). A result of1means locked and nobody starving — keep waiting. A result> 1means someone is starving: re-notify(1)("we probably received a notification that was meant for a starved task") and switch loops. After each wakeup,start.elapsed() > Duration::from_micros(500)also switches. - Fair loop.
state.fetch_add(2)registers the starver. Newcomers'CAS(0 → 1)now always fails (state ≥ 2), so only starvers compete:CAS(2 → 3)on first entry,fetch_or(1)after a wakeup; a starver that sees the lock free but loses the race notifies the next listener "to be fair".take_mutex(called on success and fromDrop) subtracts the 2 (L501–L512).
The timer needs std::time::Instant; on no_std or Wasm the 500 µs exit is compiled out and only the "somebody is starved" path remains. This is parking_lot's eventual fairness idea transplanted to async — which parking_lot itself has since removed at HEAD. Compare tokio's strict FIFO (tokio-sync).
Semaphore barges with no bound: a woken waiter whose try_acquire loses re-listen()s at the tail (semaphore.rs L289–L310; the strategy sets the listener to None once it fires, event-listener-strategy L474–L490). RwLock is "write-preferring, which means writers are never starved": a writer takes the writer mutex, then fetch_or(WRITER_BIT) stops new readers, and waits on no_readers (rwlock.rs L1–L6, raw.rs L455–L475).
Cancellation. Dropping a listener calls remove(listener, propagate = true) (lib.rs L1071–L1079); if the link had been Notified, the list re-notifies one listener with the same additional flag and tag (intrusive.rs L326–L340) — the notification is not lost. discard() is the explicit opt-out (lib.rs L1174–L1181). AcquireSlow's PinnedDrop decrements the starvation count, so a cancelled starver does not leave newcomers locked out (mutex.rs L469–L474). A waiter that wins the CAS and is then dropped holds a guard whose Drop unlocks — permits live in the state word, never in the queue, so nothing can be stranded there.
Condition variables and predicates
No condition variable. Event is the condition-variable half — a wait queue without a mutex — and the eventcount loop is how callers express a predicate: check, listen(), re-check, wait. The re-check after listen() closes the lost-wakeup window because registration and notify serialize on the list lock; Event::listen's doc additionally promises "a SeqCst fence after registering a listener" (at this SHA the code relies on the list lock for it; the only explicit fence is in notify), and notify issues full_fence before notifying unless the notification is relaxed(). Tags (Event::<T>::with_tag) let a notifier pass a value to exactly the listeners it wakes.
Footprint and layout
Event: one pointer (8 bytes), lazily pointing at anArc<Inner>— two refcounts,notified, the list mutex,head/tail/next,len,notified.async_lock::Mutex<T>:AtomicUsize+Event= 16 bytes +T;Semaphorethe same;RwLock<T>=Mutex<()>+ twoEvents + a state word = 40 bytes +T(field arithmetic, not measured).- Per contended wait: one
Boxfromlisten()in everyasync-lockslow path (they calllisten(), notlistener!), carrying anArcclone and aLink(stateCellwith a 16-byteWaker, two pointers).
Portability
std (std::sync::Mutex list lock, parking for threads), no_std + critical-section, or no_std + spinlock; portable-atomic for targets without native atomics; loom for model checking. Blocking waits (wait, *_blocking) are compiled out on target_family = "wasm", where no thread can park. parking uses std Mutex/Condvar, so the thread side inherits the platform's std lock (futex on Linux).
Async and fiber integration
This crate pair is the clearest thread/task duality in the catalog: one queue, two kinds of waker, chosen per wait, not per primitive.
- A link stores
Task::Wakerwhen polled from a future andTask::Unparkerwhenwait()ed from a thread;notifycallswaker.wake()orunparker.unpark()accordingly (lib.rsL1287–L1313).parking::Unparkeralso converts into astd::task::Waker(impl Wake for Inner), so a thread can be woken through the async path too. async-lockwrites every acquire as one state machine over aStrategy; the blocking variant is that machine driven bylistener.wait(). The examples ship a hand-written mutex exposinglock()andlock_async()in a dozen lines overTryLock+Event(examples/mutex.rs).- Crossing from a thread into an event loop is just
notify: the waker it calls belongs to whatever executor polled the listener (smol'sasync-ioreactor or any other);event-listenerknows nothing of loops. - Weak spots for a fiber registry:
notifywakes under the list lock, so a waker that re-enters the sameEvent(or is slow) extends the critical section; andTaskRef::will_wakereturnsfalsefor twoUnparkers ("TODO: Use unreleasedwill_unparkAPI"), so a thread re-registers its unparker on every spurious return. Folly'sfibers::Batonsolves the same duality inside a single word instead of a per-object list.
Strengths
- One wait queue serves tasks and threads, selected per waiter — the model a
WaitWordregistry needs. - State and queue separated: lock semantics are a few atomics; all parking subtlety lives in one crate.
- Notifications are never lost to cancellation: dropped notified listeners propagate, with
discard()as the explicit escape. notifyvsnotify_additionalgives both idempotent "make sure n are awake" and counting semantics on one list.- Bounded starvation in
Mutexwithout giving up barging throughput. - Runs
no_std, withcritical-sectionfor embedded.
Weaknesses
- Every
notifytakes the list lock and a full fence even with no listeners; every unlock of anasync_lock::Mutextherefore costs an OS-mutex round trip. - Wakers run under the list lock.
- A
Boxper contended wait inasync-lock. Semaphorecan starve a waiter; onlyMutexhas the fairness switch, and its timer disappears withoutstd.- Thread waiters park on
Mutex+Condvar, not on the state word; staleno_stddocumentation inInner.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
Eventcount (Event) separate from lock state | "turn non-blocking data structures into async or blocking data structures" | every primitive is a check–listen–check loop; wakeups carry no ownership |
Link stores Waker or Unparker | one queue for tasks and threads; _blocking APIs for free | thread waits park on a Mutex+Condvar parker |
Lazily allocated Arc<Inner> behind an AtomicPtr | const fn new; Event is one word | first notify/listen allocates; an extra indirection on every operation |
next pointer = first unnotified listener | notify(n) is idempotent, notify_additional(n) counts | callers must pick the right one (the classic lost-vs-extra wakeup choice) |
| Notified-but-dropped listener re-notifies | cancellation-safe by default | discard() needed when the notification should die |
Mutex barges, then goes fair after 500 µs | throughput of barging, bounded starvation | needs Instant; no_std loses the bound; Semaphore has no equivalent |
Three list-lock backends (std, critical-section, spin) | no_std support | the spinlock fallback is documented as dangerous |
Sources
smol-rs/event-listenerat77004ac77dce,smol-rs/async-lockat23e181d5cfb9,smol-rs/event-listener-strategyatbe0f536e7a18andsmol-rs/parkingatcedb1884e050: the files in the metadata table.- Dmitry Vyukov, eventcounts — the design
event-listenercites as its origin (cited from the crate docs, not re-read).
NOTE
All four clones are single-commit checkouts, so no history is cited. The statement that parking_lot removed eventual fairness at HEAD comes from the catalog's parking-lot page, not from these crates. Byte sizes are field arithmetic for a 64-bit target.