libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
async.cpp
1#include "async.hpp"
2
3#include "timer.hpp"
4
5using namespace LibXR;
6
7ASync::ASync(size_t stack_depth, Thread::Priority priority)
8{
9#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
10 UNUSED(stack_depth);
11 UNUSED(priority);
12 auto timer = Timer::CreateTask<ASync*>(
13 [](ASync* async)
14 {
15 if (async->pending_.exchange(0U, std::memory_order_acquire) != 0U)
16 {
17 async->RunJob();
18 }
19 },
20 this, 1);
21 Timer::Start(timer);
22 Timer::Add(timer);
23#else
24 thread_handle_.Create(this, ThreadFun, "async_job", stack_depth, priority);
25#endif
26}
27
29
31{
32 Status expected = Status::READY;
33 if (!status_.compare_exchange_strong(expected, Status::BUSY, std::memory_order_acquire,
34 std::memory_order_relaxed))
35 {
36 return ErrorCode::BUSY;
37 }
38
39 job_ = job;
40#ifdef LIBXR_NOT_SUPPORT_MUTI_THREAD
41 UNUSED(in_isr);
42 pending_.store(1U, std::memory_order_release);
43#else
44 sem_.PostFromCallback(in_isr);
45#endif
46 return ErrorCode::OK;
47}
48
49void ASync::RunJob()
50{
51 job_.Run(false, this);
52 status_.store(Status::DONE, std::memory_order_release);
53}
54
56{
57 while (true)
58 {
59 if (async->sem_.Wait() == ErrorCode::OK)
60 {
61 async->RunJob();
62 }
63 }
64}
单任务异步执行器 / Single-job asynchronous executor.
Definition async.hpp:33
std::atomic< Status > status_
当前异步任务状态
Definition async.hpp:72
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
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
Definition semaphore.cpp:96
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:49
Priority
线程优先级枚举 Enumeration for thread priorities
Definition thread.hpp:26
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:74
static TimerHandle CreateTask(void(*fun)(ArgType), ArgType arg, uint32_t cycle)
创建定时任务 Creates a periodic task
Definition timer.hpp:67
static void Start(TimerHandle handle)
启动定时任务 Starts a periodic task
Definition timer.cpp:7
static void Add(TimerHandle handle)
添加定时任务 Adds a periodic task
Definition timer.cpp:27
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ BUSY
忙碌 / Busy
@ OK
操作成功 / Operation successful