ddn.concurrent.hap

Thread-safe synchronous multicast notifications.

SharedHap synchronizes handler-list mutations and snapshot capture. Handlers run on the emitting thread and are responsible for the safety of the state they access. This module creates no threads and schedules no work. Ordinary thread-confined haps and registration bags live in ddn.hap.

Types 2

structSharedRegistration(Handler)

A thread-safe registration handle returned by SharedHap.register.

SharedRegistration represents exactly one attached handler on a SharedHap and provides explicit, idempotent detach. Both attached() and detach() take the internal lock and look the record up by its id, so the handle stays valid across handler-list splices and concurrent emits. It is non-copyable, move-only, and inert when default-initialised or moved-from.

Parameters

HandlerThe delegate handler type of the source hap.
Methods
bool attached() @safeDetermine whether this registration's handler is still attached.
bool detach() @safeDetach this registration from its source hap.
Constructors
this(shared(SharedState!Handler) state, size_t id)
structSharedHap(Args...)

A thread-safe multicast hap that invokes all attached handlers synchronously.

SharedHap provides the same attach/detach/emit semantics as Hap but with internal synchronization for safe use across threads. Every public method is a this This template, so the hap is usable both as a plain local (SharedHap!T) and as a field of a shared object (shared SharedHap!T) with no casts at call sites.

Handlers run outside the internal mutex, so other threads can inspect, mutate or emit the same hap during a callback. Concurrent emissions can invoke the same handler simultaneously: handler state needs its own synchronization, and application-level wait cycles can still deadlock.

The internal state is published with an atomic compare-and-swap on first use and state accessors read it with acquire semantics, so concurrent first use does not lose handlers to competing initialization.

Snapshot semantics: emit captures a snapshot of current handlers under the lock, then releases the lock before invoking handlers. Mutations during emit affect future snapshots, not the current one. A handler already captured in an in-flight snapshot may still run after detach or detachAll returns.

Handler model: handlers are void delegate(Args) @safe, not shared delegates. Handlers run on the emitting thread and must be thread-safe by construction; SharedHap protects only its own handler list, nothing the handlers themselves touch.

SharedHap is non-copyable and move-only. It uses reference-backed shared state so in-flight snapshots remain valid even if the public SharedHap value is moved or destroyed after snapshot capture. Moving the struct value while another thread holds a reference to it is the caller's race; only the heap state is protected.

Note

SharedHap intentionally provides no emitSafe (error-reporting

dispatch) counterpart. When that is needed, use Hap, which is single-threaded by design.

Parameters

ArgsArgument types passed to attached handlers.
Methods
bool hasHandlers(this This)() @safeDetermine whether at least one handler is attached.
size_t handlerCount(this This)() @safeGet the number of currently attached handlers.
void opOpAssign(string op : "~", this This)(Handler handler) @safeAttach a delegate handler with `~=` syntax.
void opOpAssign(string op : "~", this This)(FunctionHandler handler) @safeAttach a free-function handler with `~=` syntax.
void attach(this This)(Handler handler) @safeAttach a delegate handler.
void attach(this This)(FunctionHandler handler) @safeAttach a free-function handler.
void detach(this This)(Handler handler) @safeDetach the first matching delegate handler by delegate identity.
void detach(this This)(FunctionHandler handler) @safeDetach the first matching free-function handler.
void detachAll(this This)() @safeDetach all handlers.
SharedRegistration!Handler register(this This)(Handler handler) @safeRegister a delegate handler and return a detachable handle.
SharedRegistration!Handler register(this This)(FunctionHandler handler) @safeRegister a free-function handler and return a detachable handle.
void opCall(this This)(Args args) @safeEmit this hap by invoking all handlers captured at dispatch start.
void emit(this This)(Args args) @safeNamed alternative to `opCall`.