|
libxr
Embedded C++ framework for bare-metal, RTOS and Linux
|
单任务异步执行器 / Single-job asynchronous executor. More...
#include <async.hpp>
Public Types | |
| enum class | Status : uint32_t { READY = 0 , BUSY = 1 , DONE = UINT32_MAX } |
| 异步任务的状态枚举。 Enumeration of asynchronous task statuses. More... | |
| using | Job = LibXR::Callback<ASync*> |
Public Member Functions | |
| ASync (size_t stack_depth, Thread::Priority priority) | |
构造 ASync 并初始化线程或软件 Timer。 Constructs an ASync and initializes its worker or software Timer. | |
| Status | GetStatus () |
| 获取当前异步任务的状态。 Retrieves the current status of the asynchronous task. | |
| ErrorCode | AssignJob (Job job) |
| 提交任务 / Submit a job. | |
| ErrorCode | AssignJobFromCallback (Job job, bool in_isr) |
| 从回调上下文提交任务 / Submit a job from callback context. | |
Static Public Member Functions | |
| static void | ThreadFun (ASync *async) |
| 任务线程函数,等待信号量并执行任务。 Task thread function that waits for a semaphore and executes the assigned job. | |
Public Attributes | |
| std::atomic< Status > | status_ = Status::READY |
| 当前异步任务状态 | |
| Job | job_ |
| 存储分配的异步任务回调 / Stores the assigned asynchronous job callback. | |
| Semaphore | sem_ |
| 控制任务执行的信号量 / Semaphore controlling task execution. | |
| Thread | thread_handle_ |
| 处理异步任务的线程 / Thread handling asynchronous tasks. | |
单任务异步执行器 / Single-job asynchronous executor.
有线程时由工作线程执行;无多线程支持时,由软件 Timer 推进。 任务完成后保持 DONE,GetStatus 取走完成状态后恢复 READY。
Threaded systems use a worker; cooperative systems use a software Timer. Completion stays DONE until GetStatus acknowledges it and restores READY.
对象保留到工作线程退出或系统复位;回调数据保留到任务完成,任务不得抛异常。
Keep the object until its worker exits or the system resets. Keep callback data until completion. Jobs must not throw.
裸机从普通上下文推进 Timer;长任务会拖延其他回调,也不能等待同一 Timer 的其他任务。
Drive a bare-metal Timer in normal context. Long jobs delay other callbacks and cannot wait for work on the same Timer.
| using LibXR::ASync::Job = LibXR::Callback<ASync*> |
|
strong |
| ASync::ASync | ( | size_t | stack_depth, |
| Thread::Priority | priority ) |
构造 ASync 并初始化线程或软件 Timer。 Constructs an ASync and initializes its worker or software Timer.
| stack_depth | 线程栈深度,无线程后端忽略。 Worker stack depth; ignored by cooperative backends. |
| priority | 线程优先级,无线程后端忽略。 Worker priority; ignored by cooperative backends. |
Definition at line 7 of file async.cpp.
提交任务 / Submit a job.
| job | 预先创建的任务回调 / Previously created job callback. |
Definition at line 28 of file async.cpp.
从回调上下文提交任务 / Submit a job from callback context.
| job | 预先创建的任务回调 / Previously created job callback. |
| in_isr | 当前是否处于 ISR;线程后端据此选择通知方式。 Whether the caller is in an ISR; selects the worker notification path. |
Definition at line 30 of file async.cpp.
|
inlinenodiscard |
获取当前异步任务的状态。 Retrieves the current status of the asynchronous task.
如果任务尚未完成,则返回当前 status_。 如果任务已完成,则返回 DONE 并重置状态为 READY。 If the task is not yet completed, it returns the current status_. If the task is completed, it returns DONE and resets the status to READY.
Definition at line 86 of file async.hpp.
|
static |
任务线程函数,等待信号量并执行任务。 Task thread function that waits for a semaphore and executes the assigned job.
该函数作为 ASync 任务的主线程,持续等待 sem_ 释放后执行任务, 执行完成后更新 status_ 状态。 This function serves as the main thread for ASync, continuously waiting for sem_ to be released before executing a job, and updating status_ upon completion.
Definition at line 55 of file async.cpp.
| Job LibXR::ASync::job_ |
| Semaphore LibXR::ASync::sem_ |
| std::atomic<Status> LibXR::ASync::status_ = Status::READY |
| Thread LibXR::ASync::thread_handle_ |