libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ASync Class Reference

异步任务处理类。 Asynchronous task processing class. More...

#include <async.hpp>

Collaboration diagram for LibXR::ASync:
[legend]

Public Types

enum class  Status : uint8_t { READY , BUSY , DONE }
 异步任务的状态枚举。 Enumeration of asynchronous task statuses. More...
 
using Job = LibXR::Callback<ASync *>
 

Public Member Functions

 ASync (size_t stack_depth, Thread::Priority priority)
 构造 ASync 对象并初始化任务线程。 Constructs an ASync object and initializes the task thread.
 
Status GetStatus ()
 获取当前异步任务的状态。 Retrieves the current status of the asynchronous task.
 
ErrorCode AssignJob (Job job)
 分配一个异步任务并准备执行。 Assigns an asynchronous job and prepares for execution.
 
void AssignJobFromCallback (Job job, bool in_isr)
 在回调环境中分配任务,并通知任务线程执行。 Assigns a job from a callback environment and notifies the task thread.
 

Static Public Member Functions

static void ThreadFun (ASync *async)
 任务线程函数,等待信号量并执行任务。 Task thread function that waits for a semaphore and executes the assigned job.
 

Data Fields

std::atomic< Statusstatus_ = Status::READY
 当前异步任务状态
 
Job job_
 存储分配的异步任务回调。 Stores the assigned asynchronous job callback.
 
Semaphore sem_
 控制任务执行的信号量。 Semaphore controlling task execution.
 
Thread thread_handle_
 处理异步任务的线程。 Thread handling asynchronous tasks.
 

Detailed Description

异步任务处理类。 Asynchronous task processing class.

该类用于管理异步任务的执行,提供任务分配、状态管理以及线程执行功能。 This class manages the execution of asynchronous tasks, providing task assignment, status management, and thread execution functionalities.

Definition at line 21 of file async.hpp.

Member Typedef Documentation

◆ Job

Definition at line 96 of file async.hpp.

Member Enumeration Documentation

◆ Status

enum class LibXR::ASync::Status : uint8_t
strong

异步任务的状态枚举。 Enumeration of asynchronous task statuses.

Enumerator
READY 

任务已准备就绪。 Task is ready.

BUSY 

任务正在执行中。 Task is currently running.

DONE 

任务已完成。 Task is completed.

Definition at line 28 of file async.hpp.

29 {
30 READY,
31 BUSY,
32 DONE
33 };
@ DONE
任务已完成。 Task is completed.
@ READY
任务已准备就绪。 Task is ready.
@ BUSY
任务正在执行中。 Task is currently running.

Constructor & Destructor Documentation

◆ ASync()

ASync::ASync ( size_t stack_depth,
Thread::Priority priority )

构造 ASync 对象并初始化任务线程。 Constructs an ASync object and initializes the task thread.

Parameters
stack_depth线程栈深度。 Stack depth for the thread.
priority线程优先级。 Priority of the thread.

Definition at line 8 of file async.cpp.

9{
10 thread_handle_.Create(this, ThreadFun, "async_job", stack_depth, priority);
11}
Thread thread_handle_
处理异步任务的线程。 Thread handling asynchronous tasks.
Definition async.hpp:138
static void ThreadFun(ASync *async)
任务线程函数,等待信号量并执行任务。 Task thread function that waits for a semaphore and executes the assigned job.
Definition async.hpp:59
void Create(ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority)
创建新线程 Creates a new thread
Definition thread.hpp:64

Member Function Documentation

◆ AssignJob()

ErrorCode ASync::AssignJob ( Job job)

分配一个异步任务并准备执行。 Assigns an asynchronous job and prepares for execution.

该函数用于设置 job_ 回调,并将任务状态置为 BUSY。 This function sets the job_ callback and updates the task status to BUSY.

Parameters
job需要执行的回调任务。 The callback job to be executed.
Returns
返回 ErrorCode,指示操作是否成功。 Returns an ErrorCode indicating whether the operation was successful.

Definition at line 13 of file async.cpp.

14{
15 Status expected = Status::READY;
16 if (!status_.compare_exchange_strong(expected, Status::BUSY))
17 {
18 return ErrorCode::BUSY;
19 }
20
21 job_ = job;
22 sem_.Post();
23
24 return ErrorCode::OK;
25}
std::atomic< Status > status_
当前异步任务状态
Definition async.hpp:71
Job job_
存储分配的异步任务回调。 Stores the assigned asynchronous job callback.
Definition async.hpp:135
Status
异步任务的状态枚举。 Enumeration of asynchronous task statuses.
Definition async.hpp:29
Semaphore sem_
控制任务执行的信号量。 Semaphore controlling task execution.
Definition async.hpp:136
void Post()
释放(增加)信号量 Releases (increments) the semaphore
Definition semaphore.cpp:23

◆ AssignJobFromCallback()

void LibXR::ASync::AssignJobFromCallback ( Job job,
bool in_isr )
inline

在回调环境中分配任务,并通知任务线程执行。 Assigns a job from a callback environment and notifies the task thread.

该函数适用于中断上下文或回调环境, 直接修改 job_ 并设置状态为 BUSY,然后通过信号量通知任务线程。 This function is designed for use in an interrupt context or callback environment, directly modifying job_, setting the status to BUSY, and signaling the task thread via a semaphore.

Parameters
job需要执行的回调任务。 The callback job to be executed.
in_isr是否在中断上下文中调用。 Indicates whether the function is called within an interrupt service routine.

Definition at line 128 of file async.hpp.

129 {
130 job_ = job;
131 status_.store(Status::BUSY);
132 sem_.PostFromCallback(in_isr);
133 }
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
Definition semaphore.cpp:48

◆ GetStatus()

Status LibXR::ASync::GetStatus ( )
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.

Returns
返回当前任务状态。 Returns the current task status.

Definition at line 85 of file async.hpp.

86 {
87 Status cur = status_.load();
88 if (cur != Status::DONE)
89 {
90 return cur;
91 }
93 return Status::DONE;
94 }

◆ ThreadFun()

static void LibXR::ASync::ThreadFun ( ASync * async)
inlinestatic

任务线程函数,等待信号量并执行任务。 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.

Parameters
async指向 ASync 实例的指针。 Pointer to the ASync instance.

Definition at line 59 of file async.hpp.

60 {
61 while (true)
62 {
63 if (async->sem_.Wait() == ErrorCode::OK)
64 {
65 async->job_.Run(false, async);
66 async->status_ = Status::DONE;
67 }
68 }
69 }

Field Documentation

◆ job_

Job LibXR::ASync::job_

存储分配的异步任务回调。 Stores the assigned asynchronous job callback.

Definition at line 135 of file async.hpp.

◆ sem_

Semaphore LibXR::ASync::sem_

控制任务执行的信号量。 Semaphore controlling task execution.

Definition at line 136 of file async.hpp.

◆ status_

std::atomic<Status> LibXR::ASync::status_ = Status::READY

当前异步任务状态

Definition at line 71 of file async.hpp.

◆ thread_handle_

Thread LibXR::ASync::thread_handle_

处理异步任务的线程。 Thread handling asynchronous tasks.

Definition at line 138 of file async.hpp.


The documentation for this class was generated from the following files: