class SchedulerThread

Inherits: std::enable_shared_from_this<SchedulerThread>

Represents a single worker thread within Scheduler.

Public

Constructors

SchedulerThread

SchedulerThread(Scheduler *scheduler, Mode mode, u32 id)

Methods

GetThread

const Thread &GetThread() const

Returns the underlying thread object.

GetMessageQueue

SingleConsumerQueue &GetMessageQueue() const

Returns a message queue that may be used for posting messages to this thread.

Post

void Post(SchedulerTask &&task)

Queues a new task for execution on this thread.

GetCurrentFiber

Fiber *GetCurrentFiber() const

Returns the fiber currently being executed.

staticGet

static const SPtr<SchedulerThread> &Get()

Returns the scheduler thread bound to the current thread.

Fields

Id

const u32 Id

Unique identifier of the scheduler thread.

Private

Methods

Start

void Start()

Starts execution of the thread.

Must be called before enqueuing any work.

Stop

void Stop()

Stops execution of the thread.

This will block until all pending tasks have finished. Must be called before shutdown.

Wait

bool Wait(Lock &lock, const TimePoint *timeout, const Function<bool ()> &predicate)

Suspends execution of the current task until the task is woken up via a call to Enqueue() or timeout expires.

See Fiber::Wait() overloads for more information.

Wait

bool Wait(const TimePoint *timeout)

Suspends execution of the current task until the task is woken up via a call to Enqueue() or timeout expires.

See Fiber::Wait() overloads for more information.

WaitWithoutLocking

void WaitWithoutLocking(const TimePoint *timeout)

Suspends execution of the current task until the task is woken up via a call to Enqueue() or timeout expires.

See Fiber::Wait() overloads for more information. Mutex must be locked when this is called.

Enqueue

void Enqueue(Fiber *fiber)

Enqueues a suspended fiber to continue execution.

Enqueue

void Enqueue(SchedulerTask &&task)

Enqueues a new unstarted task.

Only safe to use if mode is SingleThreaded.

TryLockForEnqueue

bool TryLockForEnqueue()

Attempts to lock the object for task enqueuing.

Returns true if the lock was successful, after which caller must call EnqueueAndUnlock. You should use this instead of Enqueue() if the mode is MultiThreaded.

EnqueueAndUnlock

void EnqueueAndUnlock(SchedulerTask &&task)

Enqueues a task and unlocks the lock acquired via TryLock().

RunUntilShutdown

void RunUntilShutdown()

Processes all tasks and fibers until there are no more and shutdown flag is set.

TryStealTask

bool TryStealTask(SchedulerTask &outTask)

Attempts to steal a Task from another worker.

Returns true if a task was successfully stolen.

Run

void Run()

Processes all tasks until Stop() is called.

CreateWorkerFiber

Fiber *CreateWorkerFiber()

Creates a new fiber that calls Run().

SwitchExecutionToFiber

void SwitchExecutionToFiber(Fiber *other)

Switches execution to the provided fiber.

RunUntilIdle

void RunUntilIdle()

Executes all pending tasks and returns.

WaitForWork

void WaitForWork()

Waits until some work is available, first spinning and then potentially yielding the thread to the OS.

SpinForWork

void SpinForWork()

Attempts to steal work from another Worker, and keeps the thread awake for a short duration, hoping to find some work before we need to yield to OS.

UpdateWaitingFibers

void UpdateWaitingFibers()

Enqueues any fibers that have finished waiting.

WaitOnAddedSignal

void WaitOnAddedSignal(const Function<bool ()> &predicate)

Waits until the mAddedSignal is notified and predicate returns true.

Fields

mMode

const Mode mMode

mOwnerScheduler

Scheduler *const mOwnerScheduler

mMainFiber

UPtr<Fiber> mMainFiber

mCurrentFiber

Fiber * mCurrentFiber

mThread

Thread mThread

mRandomNumberGenerator

Random mRandomNumberGenerator

mMessageQueue

SingleConsumerQueue * mMessageQueue

mReadyOperationCount

std::atomic<u64> mReadyOperationCount

mBlockedFiberCount

u64 mBlockedFiberCount

mPendingTasks

Deque<SchedulerTask> mPendingTasks

mReadyFibers

Deque<Fiber *> mReadyFibers

mWaitingFibers

WaitingFibers mWaitingFibers

mTriggerNotifyOnAdd

bool mTriggerNotifyOnAdd

mAddedSignal

ConditionVariable mAddedSignal

mMutex

Mutex mMutex

mFreeFibers

UnorderedSet<Fiber *> mFreeFibers

mAllFibers

TInlineArray<UPtr<Fiber>, 16> mAllFibers

mIsShutdownRequested

bool mIsShutdownRequested