libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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
32class ASync
33{
34 public:
39 enum class Status : uint32_t
40 {
41 READY = 0,
42 BUSY = 1,
43 DONE = UINT32_MAX
44 };
45
55 ASync(size_t stack_depth, Thread::Priority priority);
56
70 static void ThreadFun(ASync* async);
71
72 std::atomic<Status> status_ = Status::READY;
73
86 [[nodiscard]] Status GetStatus()
87 {
88 Status cur = status_.load(std::memory_order_acquire);
89 if (cur != Status::DONE)
90 {
91 return cur;
92 }
93 // 只允许一个观察者取走 DONE;迟到的观察者不能覆盖下一次提交的 BUSY。
94 // Consume DONE once; a late observer must not overwrite the next submission's BUSY.
95 if (status_.compare_exchange_strong(cur, Status::READY, std::memory_order_acq_rel,
96 std::memory_order_acquire))
97 {
98 return Status::DONE;
99 }
100 return cur;
101 }
102
103 using Job = LibXR::Callback<ASync*>;
104
114 ErrorCode AssignJob(Job job);
115
126 ErrorCode AssignJobFromCallback(Job job, bool in_isr);
127
130
132
133 private:
134 void RunJob();
135
136#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
137 // BUSY 在写入 job_ 前置位;独立标记只在任务完整写入后发布。
138 // BUSY precedes the job_ write; publish this flag only after the complete job is
139 // stored.
140 std::atomic<uint32_t> pending_{0U};
141#endif
142};
143
144} // namespace LibXR
单任务异步执行器 / Single-job asynchronous executor.
Definition async.hpp:33
std::atomic< Status > status_
当前异步任务状态
Definition async.hpp:72
Status GetStatus()
获取当前异步任务的状态。 Retrieves the current status of the asynchronous task.
Definition async.hpp:86
Job job_
存储分配的异步任务回调 / Stores the assigned asynchronous job callback.
Definition async.hpp:128
ErrorCode AssignJob(Job job)
提交任务 / Submit a job.
Definition async.cpp:28
Thread thread_handle_
处理异步任务的线程 / Thread handling asynchronous tasks.
Definition async.hpp:131
ASync(size_t stack_depth, Thread::Priority priority)
构造 ASync 并初始化线程或软件 Timer。 Constructs an ASync and initializes its worker or software Timer.
Definition async.cpp:7
Semaphore sem_
控制任务执行的信号量 / Semaphore controlling task execution.
Definition async.hpp:129
static void ThreadFun(ASync *async)
任务线程函数,等待信号量并执行任务。 Task thread function that waits for a semaphore and executes the assigned job.
Definition async.cpp:55
Status
异步任务的状态枚举。 Enumeration of asynchronous task statuses.
Definition async.hpp:40
@ DONE
任务已完成,尚未取走状态 / Completed, awaiting acknowledgment.
@ READY
任务已准备就绪 / Task is ready.
@ BUSY
任务已接收或正在执行 / Job accepted or running.
ErrorCode AssignJobFromCallback(Job job, bool in_isr)
从回调上下文提交任务 / Submit a job from callback context.
Definition async.cpp:30
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
Definition libxr_cb.hpp:143
信号量类,实现线程同步机制 Semaphore class implementing thread synchronization
Definition semaphore.hpp:23
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition thread.hpp:19
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:26
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes