class SingleConsumerQueue

Allows you to queue sequential commands safely from any thread/fiber, which are then processed by a single worker thread/fiber.

Public

Constructors

SingleConsumerQueue

SingleConsumerQueue()

Methods

~SingleConsumerQueue

~SingleConsumerQueue() noexcept

GetThreadId

u32 GetThreadId() const

Returns the thread id on which the queue commands are being processed on.

Only valid after RunUntilShutdown() is called.

GetSchedulerThread

const SPtr<SchedulerThread> &GetSchedulerThread() const

Returns the scheduler thread that the queue is executing commands on.

May be null if not scheduler is associated with the thread.

PostCommand

void PostCommand(Function<void ()> &&callback, const char *debugName = nullptr, bool waitUntilComplete = false, const String &extraInformation = StringUtility::kBlank)

Posts a command for execution on the queue.

Optionally blocks the calling fiber/thread until the command completes. Thread safe.

PostRequestShutdownCommand

void PostRequestShutdownCommand(bool waitUntilComplete)

Posts a special command that requests shutdown.

Optionally blocks the calling fiber/thread until the command completes. Thread safe.

RunUntilIdle

bool RunUntilIdle(TimePoint startTime = Clock::now(), Milliseconds timeout = 10ms)

Processes all currently queued commands and then returns.

Optionally if timeout is specified, it returns if the duration is reached even if not all commands have been processed.

startTime
Start time used for checking when the timeout happens compared to current time. Only relevant if timeout is specified.
timeout
If non-zero, the method will return even if not all commands have been processed, but the timeout was reached.

Returns: True if timeout was reached, false if all commands were processed.

RunUntilShutdown

void RunUntilShutdown()

Runs an infinite loop that processes commands until shutdown is requested via RequestShutdown().

You must call this method or the other overload once on a thread or fiber that will be processing the commands. Note that if running this on the fiber, a busy queue can completely block other tasks from running on the fiber thread - consider using ScheduleRunUntilShutdown() instead.

ScheduleRunUntilShutdown

void ScheduleRunUntilShutdown(Scheduler &scheduler, bool runOnCallingThread, Milliseconds yieldInterval = 10ms, bool blockUntilDone = false)

Schedules a task that processes commands until shutdown is requested via RequestShutdown().

Optionally yields the fiber at specified intervals, allowing other tasks on the thread to execute.

scheduler
Scheduler on which to post the task on.
runOnCallingThread
If true, the task will run on the calling thread, otherwise an arbitrary thread assigned by the scheduler.
yieldInterval
If non-zero, the scheduler task will end after this interval is reached, and it will be re-queued with the scheduler. This puts it back in the scheduler's task queue, allowing other tasks to run.
blockUntilDone
If true, the calling fiber will be blocked until shutdown is requested.

CancelAll

void CancelAll()

Cancels all currently queued commands.

Thread safe.

IsEmpty

bool IsEmpty()

Returns true if no commands are queued.

Thread safe.

Private

Fields

mThreadId

u32 mThreadId

mSchedulerThread

SPtr<SchedulerThread> mSchedulerThread

mCommandQueue

Queue<QueuedCommand> * mCommandQueue

mEmptyCommandQueues

Stack<Queue<QueuedCommand> *> mEmptyCommandQueues

List of empty queues for reuse.

mIsShutdownRequested

bool mIsShutdownRequested

mCommandQueueMutex

Mutex mCommandQueueMutex

mCommandAddedSignal

Signal mCommandAddedSignal

mCommandCompletedSignalEvent

SignalEvent mCommandCompletedSignalEvent