Functions and constants functions
Free functions and constants of Core.
On this page
- Functions
- CreateTaskScheduler
- CreateDispatcherScheduler
- CreateLocalScheduler
- GetTaskScheduler
- GetCurrentTask
- GetTask
- SuspendTask
- ResumeTask
- YieldTask
- SetTaskPriority
- ConvertThreadToTask
- ConvertTaskToThread
- CancelTask
- IsCancellationRequested
- EnableTimers
- DisableTimers
- SleepTask
- SuspendTaskFor
- ScheduleTimedTask
- AsyncIo::EnableAsyncIo
- AsyncIo::DisableAsyncIo
- AsyncIo::AssociateHandleWithIoCompletionPort
- AsyncIo::SubmitIo
- AsyncIo::CancelIo
- AsyncIo::CancelAllIo
- Constants
- SchedulerVersion
- DefaultTaskQueueCapacity
- MaximumTaskQueueCapacity
- Use
#include <ThinkMeta.ConcurrentTasks/Core.h>- Namespace
ThinkMeta::ConcurrentTasks- Platforms
- Windows, Linux
Functions
CreateTaskScheduler
Creates a scheduler.
Syntax
ITaskScheduler* CreateTaskScheduler(
SchedulerKind kind,
const SchedulerInfo* info
) noexcept;Parameters
kindSchedulerKind- The scheduler implementation; see
SchedulerKind. infoconst SchedulerInfo*- The scheduler's parameters; required. The core copies the struct, so it need only be valid during the call.
Return value
Type: ITaskScheduler*
The scheduler, or nullptr if info is null, its version is unknown, kind needs a field that is not set, SchedulerInfo::taskQueueCapacity is too large, or the queues cannot be allocated.
See also
CreateDispatcherScheduler
Creates a dispatcher scheduler bound to the calling thread.
Syntax
IDispatcherScheduler* CreateDispatcherScheduler(
DispatcherWakeProc wake,
void* context
) noexcept;Parameters
wakeDispatcherWakeProc- Hands tasks that can run to the thread's event loop; required.
contextvoid*- Passed unchanged to
wake; must outlive the scheduler.
Return value
Type: IDispatcherScheduler*
The scheduler, or nullptr if wake is null or memory ran out.
Remarks
The calling thread is bound, not converted: outside the scheduler's tasks it stays an ordinary thread, and GetTaskScheduler works only inside them.
See also
CreateLocalScheduler
Creates a local scheduler, whose tasks run inside ILocalScheduler::Run.
Syntax
ILocalScheduler* CreateLocalScheduler(const SchedulerInfo* info) noexcept;Parameters
infoconst SchedulerInfo*- The scheduler's parameters; required. Of these,
SchedulerInfo::version,SchedulerInfo::taskQueueCapacityandSchedulerInfo::taskStackSizeapply; the other fields are ignored. The core copies the struct.
Return value
Type: ILocalScheduler*
The scheduler, or nullptr if info is null, its version is unknown, SchedulerInfo::taskQueueCapacity is too large, or memory ran out.
Remarks
Create it on the thread that will call ILocalScheduler::Run. The capacity bounds the runnable tasks per queue, as with the SchedulerKind::Default scheduler: new tasks per priority, and resumed or yielded tasks per priority; filling a queue is invalid use.
See also
GetTaskScheduler
Returns the scheduler running the calling task.
Syntax
ITaskScheduler* GetTaskScheduler() noexcept;Return value
Type: ITaskScheduler*
The scheduler; owned by its creator, not by the caller.
Remarks
Only in a task or a converted thread (ConvertThreadToTask).
GetCurrentTask
Returns the handle of the calling task.
Syntax
TaskHandle GetCurrentTask() noexcept;Return value
Type: TaskHandle
The calling task's handle, valid until the task exits; on a converted thread until ConvertTaskToThread.
Remarks
Only in a task or a converted thread (ConvertThreadToTask); on any other thread the result is undefined and the call may crash.
See also
GetTask
Returns the task object behind a handle.
Syntax
ITask* GetTask(TaskHandle task) noexcept;Parameters
taskTaskHandle- A task's handle (a
SuspendedTaskconverts to it);nullptrstands for the calling task.
Return value
Type: ITask*
The ITask the handle runs; owned by whoever added the task.
See also
SuspendTask
Suspends the calling task.
Syntax
void SuspendTask(ISuspendCallback* callback) noexcept;Parameters
callbackISuspendCallback*- Receives the task's handle once the task is off the processor; required, and must stay valid until
ISuspendCallback::OnSuspendedhas returned.
Remarks
The core calls back into ISuspendCallback::OnSuspended with the task's handle once the task is off the processor, so the waiter can publish it.
See also
ResumeTask
Makes a suspended task runnable again.
Syntax
void ResumeTask(SuspendedTask suspendedTask) noexcept;Parameters
suspendedTaskSuspendedTask- Handle received by
ISuspendCallback::OnSuspended;nullptris ignored.
Remarks
The task runs on the scheduler that suspended it. Callable from any task or OS thread, also from a completion routine on the core's I/O thread; never blocks. Resume each suspension exactly once.
See also
YieldTask
Gives up the processor without suspending the calling task.
Syntax
void YieldTask(YieldFlags yieldFlags = YieldFlags::Default) noexcept;Parameters
yieldFlagsYieldFlags- How to yield; see
YieldFlags.Default:YieldFlags::Default
See also
SetTaskPriority
Sets the priority of the calling task.
Syntax
void SetTaskPriority(TaskPriority priority) noexcept;Parameters
priorityTaskPriorityTaskPriority::NormalorTaskPriority::High;TaskPriority::Defaultis not meaningful here.
See also
ConvertThreadToTask
Lets the calling OS thread act as a task.
Syntax
void ConvertThreadToTask() noexcept;Remarks
The thread can then use the synchronization primitives. ConvertTaskToThread reverses it. Once per thread: a second call before ConvertTaskToThread is invalid use (the thread's task state would be nested, and the first one lost).
ConvertTaskToThread
Reverses ConvertThreadToTask on the calling thread.
Syntax
void ConvertTaskToThread() noexcept;Remarks
Releases the thread's task state. Call it on the same thread, once no other task can still resume this thread's task.
CancelTask
Asks a task to cancel.
Syntax
bool CancelTask(ITask* task) noexcept;Parameters
taskITask*- The task; it must return storage from
ITask::GetControlData.
Return value
Type: bool
true if the request is new; false if the task was asked already or has no ITask::GetControlData.
Remarks
Cooperative: a running task is never stopped from outside; it sees the request through IsCancellationRequested and ends itself. A task that has not started yet - added, or waiting to start by time - exits without running (INotifyTaskStateChange::OnTaskExited). A SleepTask ends early, and a SuspendTaskFor ends with WaitStatus::Cancelled if its ISuspendCallback::OnWithdraw takes the task back; a later one returns at once. The request stays until the task has exited; asked while the task is not added at all, it applies to the next run, which then exits without running. Asked again for a standing request, it acts again, e.g. on a start by time added since, and returns false. Callable from any task or thread, but not from ISuspendCallback::OnWithdraw; the task object must stay valid during the call. Never blocks.
See also
IsCancellationRequested
Returns whether the calling task is asked to cancel.
Syntax
bool IsCancellationRequested() noexcept;Return value
Type: bool
true after a CancelTask for the calling task; false otherwise, also outside a task and in a task without ITask::GetControlData.
Remarks
Cheap enough to ask in a loop.
See also
EnableTimers
Starts the core's timer thread.
Syntax
bool EnableTimers() noexcept;Return value
Type: bool
true if timers are enabled; false only if the thread could not be started.
Remarks
The timer thread starts tasks added by ITaskScheduler::AddTimedTask, wakes sleeping tasks and ends waits that time out. Calls are counted like AsyncIo::EnableAsyncIo.
See also
DisableTimers
Takes back one EnableTimers.
Syntax
bool DisableTimers() noexcept;Return value
Type: bool
true on success; false if timers were not enabled.
Remarks
The last one ends what is still waiting: tasks not yet started exit without running (INotifyTaskStateChange::OnTaskExited, on their scheduler, which must still run), sleeping tasks wake, and waits with a timeout time out. No other thread may use timers meanwhile.
SleepTask
Suspends the calling task for a while.
Syntax
Parameters
Return value
Type: bool
true after the sleep; false at once if timers are not enabled, and early if the task is cancelled (CancelTask).
Remarks
Its thread runs other tasks meanwhile. Only a cancel cuts the sleep short; to wait for something with a time limit, wait on it with a timeout.
See also
SuspendTaskFor
Suspends the calling task with a timeout; the wait also ends when the task is cancelled.
Syntax
WaitStatus SuspendTaskFor(ISuspendCallback* callback, Duration timeout) noexcept;Parameters
callbackISuspendCallback*- Receives the task's handle (
ISuspendCallback::OnSuspended) and, on timeout or cancel, theISuspendCallback::OnWithdrawcall; required, and must stay valid until this call returns. timeoutDuration- Longest time to stay suspended;
Duration::Infinitewaits without a timeout.
Return value
Type: WaitStatus
WaitStatus::Signaled if a ResumeTask ended the wait; WaitStatus::TimedOut or WaitStatus::Cancelled if the core ended it: then ISuspendCallback::OnWithdraw on callback took the task back from where ISuspendCallback::OnSuspended had published it. WaitStatus::Cancelled at once, without suspending, if the task was asked to cancel before.
Remarks
Like SuspendTask, but the wait ends after timeout at the latest, or on CancelTask for a task with ITask::GetControlData. The waiter's state (the callback) is never touched again once the task runs. Without EnableTimers, or with Duration::Infinite, it waits without a timeout.
ScheduleTimedTask
Starts a timed task now instead of later.
Syntax
bool ScheduleTimedTask(ITask* task) noexcept;Parameters
taskITask*- A task added by
ITaskScheduler::AddTimedTask.
Return value
Type: bool
true if the task was queued now; false if it was not waiting to start.
Remarks
Never blocks. To take the start back, cancel the task (CancelTask).
See also
AsyncIo::EnableAsyncIo
Starts the core's asynchronous I/O and the thread that completes it.
Syntax
bool EnableAsyncIo() noexcept;Return value
Type: bool
true if asynchronous I/O is enabled; false only if it could not be started.
Remarks
Windows: an I/O completion port. Linux: an io_uring, which some environments block (the default seccomp profile of Docker, kernels built without it); there it returns false, and the schedulers and primitives work as before. Calls are counted: only the first one starts it, so a library and the application can both enable asynchronous I/O.
See also
AsyncIo::DisableAsyncIo
Takes back one EnableAsyncIo.
Syntax
bool DisableAsyncIo() noexcept;Return value
Type: bool
true on success; false if asynchronous I/O was not enabled.
Remarks
The last one stops the asynchronous I/O and its thread, and by then no operation may be pending.
AsyncIo::AssociateHandleWithIoCompletionPortWindows only
Associates a file or socket handle with the core's completion port.
Syntax
bool AssociateHandleWithIoCompletionPort(
HANDLE handle,
IoCompletionRoutine completionRoutine
) noexcept;Parameters
handleHANDLE- A file or socket handle opened for overlapped I/O (
FILE_FLAG_OVERLAPPED); borrowed, it stays associated until it is closed. completionRoutineIoCompletionRoutine- Called for every completion packet of
handle; required.
Return value
Type: bool
true on success; false if it failed (GetLastError has the reason; ERROR_NOT_READY when EnableAsyncIo has not been called).
Remarks
The routine is stored as the handle's completion key, so the core needs no knowledge of the caller's types.
See also
AsyncIo::SubmitIoLinux only
Starts the operation a request describes.
Syntax
Parameters
requestIoRequest*- The request; required. It belongs to the operation until its routine has been called.
Return value
Type: bool
true if the operation was started: its routine will be called exactly once. false if not, with errno set: ENODEV when EnableAsyncIo has not been called, EAGAIN or EBUSY when the kernel takes nothing more for the moment (try again later); the routine is not called.
Remarks
Callable from any thread.
See also
AsyncIo::CancelIoLinux only
Asks the kernel to end a submitted operation early.
Syntax
Parameters
Return value
Type: bool
true if the request to cancel was passed on; false with errno set otherwise, as for SubmitIo.
Remarks
Does not wait. The operation still completes through its routine, with -ECANCELED or, if it had already finished, with its own result; a request that has completed already is not affected.
AsyncIo::CancelAllIoLinux only
Asks the kernel to end every submitted operation on a file or socket early.
Syntax
bool CancelAllIo(std::int32_t fd) noexcept;Parameters
fdstd::int32_t- The file or socket; must still be open.
Return value
Type: bool
true if the request to cancel was passed on; false with errno set otherwise: as for CancelIo, EINVAL for a negative fd, and EOPNOTSUPP when the kernel cannot cancel by file (before Linux 5.19).
Remarks
The counterpart of CancelIoEx without an OVERLAPPED. Does not wait. Covers the operations submitted before the call; each still completes through its routine, with -ECANCELED or its own result. Unlike Windows, true does not tell whether any operation was pending. Call it before closing fd: closing does not end the operations, and the number may then be given to another file.
See also
Constants
SchedulerVersion
ABI version this header describes.
Syntax
inline constexpr std::uint32_t SchedulerVersion = 2;Remarks
Passed in SchedulerInfo::version; the core accepts only its own version.
See also
DefaultTaskQueueCapacity
Default task queue capacity.
Syntax
inline constexpr std::uint32_t DefaultTaskQueueCapacity = 4096;Remarks
Used when SchedulerInfo::taskQueueCapacity is 0.
MaximumTaskQueueCapacity
Largest task queue capacity CreateTaskScheduler accepts.
Syntax
inline constexpr std::uint32_t MaximumTaskQueueCapacity = std::uint32_t{1} << 20;Generated from ThinkMeta.ConcurrentTasks.API @ c7663c4 (2026-11-19).