libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
async.hpp
1#pragma once
2
3#include <atomic>
4
5#include "libxr_cb.hpp"
6#include "libxr_def.hpp"
7#include "semaphore.hpp"
8#include "thread.hpp"
9
10namespace LibXR
11{
12
21class ASync
22{
23 public:
28 enum class Status : uint32_t
29 {
30 READY = 0,
31 BUSY = 1,
32 DONE = UINT32_MAX
33 };
34
44 ASync(size_t stack_depth, Thread::Priority priority);
45
59 static void ThreadFun(ASync *async)
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 }
70
71 std::atomic<Status> status_ = Status::READY;
72
85 [[nodiscard]] Status GetStatus()
86 {
87 Status cur = status_.load();
88 if (cur != Status::DONE)
89 {
90 return cur;
91 }
93 return Status::DONE;
94 }
95
96 using Job = LibXR::Callback<ASync *>;
97
110 ErrorCode AssignJob(Job job);
111
128 void AssignJobFromCallback(Job job, bool in_isr)
129 {
130 job_ = job;
131 status_.store(Status::BUSY);
132 sem_.PostFromCallback(in_isr);
133 }
134
137
139};
140
141} // namespace LibXR
异步任务处理类。 Asynchronous task processing class.
Definition async.hpp:22
std::atomic< Status > status_
当前异步任务状态
Definition async.hpp:71
Status GetStatus()
获取当前异步任务的状态。 Retrieves the current status of the asynchronous task.
Definition async.hpp:85
void AssignJobFromCallback(Job job, bool in_isr)
在回调环境中分配任务,并通知任务线程执行。 Assigns a job from a callback environment and notifies the task thread.
Definition async.hpp:128
Job job_
存储分配的异步任务回调。 Stores the assigned asynchronous job callback.
Definition async.hpp:135
ErrorCode AssignJob(Job job)
分配一个异步任务并准备执行。 Assigns an asynchronous job and prepares for execution.
Definition async.cpp:13
Thread thread_handle_
处理异步任务的线程。 Thread handling asynchronous tasks.
Definition async.hpp:138
ASync(size_t stack_depth, Thread::Priority priority)
构造 ASync 对象并初始化任务线程。 Constructs an ASync object and initializes the task thread.
Definition async.cpp:8
Semaphore sem_
控制任务执行的信号量。 Semaphore controlling task execution.
Definition async.hpp:136
static void ThreadFun(ASync *async)
任务线程函数,等待信号量并执行任务。 Task thread function that waits for a semaphore and executes the assigned job.
Definition async.hpp:59
Status
异步任务的状态枚举。 Enumeration of asynchronous task statuses.
Definition async.hpp:29
@ DONE
任务已完成。 Task is completed.
@ READY
任务已准备就绪。 Task is ready.
@ BUSY
任务正在执行中。 Task is currently running.
提供一个通用的回调包装,支持动态参数传递。 Provides a generic callback wrapper, supporting dynamic argument passing.
Definition libxr_cb.hpp:124
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
Definition libxr_cb.hpp:207
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
Definition semaphore.cpp:48
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:25
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:14
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:21
LibXR 命名空间
Definition ch32_gpio.hpp:9