eve.rt.queue

Lock-free work queue for cross-thread task submission.

This module provides a thread-safe queue for submitting work items from any thread to be processed by a consumer thread. It is designed for use with the EVE runtime to enable cross-thread communication and work distribution.

Types 3

structWorkItem

A work item that can be queued for execution.

Work items wrap a delegate to be executed on the consumer thread.

Fields
void delegate() @safe taskThe delegate to execute.
string nameOptional name for debugging/logging purposes.
Methods
void execute() @safeExecute the work item.
bool isValid() const pure @safe nothrow @nogcCheck if the work item has a valid task.
Constructors
this(void delegate() @safe task, string name = null)Construct a work item from a delegate.
enumQueueStatus : ubyte

Status codes for queue operations.

SUCCESS = 0Operation completed successfully.
EMPTY = 1Queue is empty (for dequeue operations).
CLOSED = 2Queue has been closed.
FULL = 3Queue is full (for bounded queues).

A thread-safe work queue for cross-thread task submission.

The WorkQueue allows multiple producer threads to submit work items that will be consumed by a single consumer thread. It supports both blocking and non-blocking operations.

Example:

auto queue = new WorkQueue();

// Producer thread
queue.push(() @safe { writeln("Hello from consumer!"); });

// Consumer thread
WorkItem item;
if (queue.tryPop(item) == QueueStatus.SUCCESS) {
   item.execute();
}

Fields
WorkItem[] buffer
size_t head
size_t tail
size_t count
size_t capacity_
bool closed_
Mutex mutex
Condition notEmpty
Condition notFull
Methods
QueueStatus push(WorkItem item) @trustedPush a work item onto the queue, blocking if full.
QueueStatus push(void delegate() @safe task, string name = null) @trustedditto
QueueStatus tryPush(WorkItem item) @trustedTry to push a work item without blocking.
QueueStatus tryPush(void delegate() @safe task, string name = null) @trustedditto
QueueStatus pop(out WorkItem item) @trustedPop a work item from the queue, blocking if empty.
QueueStatus tryPop(out WorkItem item) @trustedTry to pop a work item without blocking.
void close() @trustedClose the queue, preventing further pushes.
bool isClosed() @property const @trustedCheck if the queue has been closed.
size_t length() @property @trustedGet the current number of items in the queue.
size_t capacity() @property const pure @safe nothrow @nogcGet the maximum capacity of the queue.
bool empty() @property @trustedCheck if the queue is empty.
bool isBounded() @property const pure @safe nothrow @nogcCheck if the queue is bounded.
void pushInternal(WorkItem item) @trusted
Constructors
this(size_t capacity = 0)Construct a work queue with the specified capacity.

Functions 2

fnWorkQueue workQueue(size_t capacity) @safeCreate a new bounded work queue.
fnWorkQueue workQueue() @safeCreate a new unbounded work queue.