libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::SerializedService Class Reference

事件合并与回调串行化服务 / Event-coalescing serialized-callback service. More...

#include <serialized_service.hpp>

Public Member Functions

template<typename Handler >
bool Invoke (uint32_t events, bool in_isr, Handler &&handler) noexcept
 发布事件并尝试取得执行权 / Publish events and attempt to claim execution.
 
void Publish (uint32_t events) noexcept
 为已有或确定会出现的执行者发布事件 / Publish for an active or guaranteed executor.
 
 SerializedService (const SerializedService &)=delete
 
SerializedService & operator= (const SerializedService &)=delete
 

Detailed Description

事件合并与回调串行化服务 / Event-coalescing serialized-callback service.

该服务用一个原子字保存执行权和低 31 位事件。Invoke() 发布事件并尝试取得执行权; 只有取得执行权的调用执行 handler,执行期间到达的事件由同一调用继续处理。未取得执行 权的调用只发布事件,其 handler 不会保存或稍后调用。事件位表示状态需要重新检查,不 表示发生次数。

This service stores execution ownership and low-31-bit events in one atomic word. Invoke() publishes events and attempts to claim execution. Only the caller that claims execution runs the handler; events arriving during execution are processed before ownership is released. A losing caller's handler is never retained or called later. Event bits request a state re-check and do not count occurrences.

Publish() 只记录事件,不负责唤醒或调度。service 以及 handler 访问的数据必须在所有活动 调用结束前保持有效;handler 不得抛出异常,也不得等待当前 service 所保护的进度。原子 操作的上下文限制、MMIO、DMA 同步和后端生命周期由调用方负责。

Publish() records an event but does not wake or schedule execution. The service and all data accessed by its handler must outlive every active call. Handlers must not throw or wait for progress protected by this service. Context restrictions for atomics, MMIO, DMA synchronization, and backend lifetime remain the caller's responsibility.

Definition at line 34 of file serialized_service.hpp.

Member Function Documentation

◆ Invoke()

template<typename Handler >
bool LibXR::SerializedService::Invoke ( uint32_t events,
bool in_isr,
Handler && handler )
inlinenoexcept

发布事件并尝试取得执行权 / Publish events and attempt to claim execution.

Parameters
events非零低 31 位事件 / Nonzero event bits below bit 31.
in_isr当前调用上下文;仅传给取得执行权的调用 / Context of this call; used only if it claims execution.
handler签名 void(uint32_t, bool) / Callable as void(uint32_t, bool).
Returns
当前调用执行了 handler 则为 true;否则仅发布事件并返回 false / True if this call ran the handler; false if it only published events.

Definition at line 49 of file serialized_service.hpp.

50 {
51 ASSERT(IsEventMask(events));
52
53 uint32_t observed = state_.fetch_or(events, std::memory_order_release) | events;
54 while ((observed & OWNER_BIT) == 0U)
55 {
56 if (state_.compare_exchange_weak(observed, OWNER_BIT, std::memory_order_acquire,
57 std::memory_order_relaxed))
58 {
59 Handler& handler_ref = handler;
60 return Drain(handler_ref, in_isr, observed & EVENT_MASK);
61 }
62 }
63 return false;
64 }

◆ Publish()

void LibXR::SerializedService::Publish ( uint32_t events)
inlinenoexcept

为已有或确定会出现的执行者发布事件 / Publish for an active or guaranteed executor.

Parameters
events低 31 位事件;零值不操作 / Low-31-bit events; zero is a no-op.
Precondition
已有执行者或保证后续 Invoke;本方法本身不调度执行 / An executor or a later Invoke is guaranteed; this method does not schedule execution.

Definition at line 74 of file serialized_service.hpp.

75 {
76 if (events == 0U)
77 {
78 return;
79 }
80 ASSERT(IsEventMask(events));
81 state_.fetch_or(events, std::memory_order_release);
82 }

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