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

合并通知并串行执行服务函数 / Coalesce notifications and serialize service execution More...

#include <serialized_service.hpp>

Public Member Functions

template<typename Handler >
bool Invoke (uint32_t events, Handler &&handler) noexcept
 发布事件并尝试执行服务 / Publish events and try to run the service
 
 SerializedService (const SerializedService &)=delete
 
SerializedServiceoperator= (const SerializedService &)=delete
 

Private Attributes

std::atomic< uint32_t > state_ {0U}
 

Static Private Attributes

static constexpr uint32_t OWNER_BIT = 1U << 31U
 
static constexpr uint32_t EVENT_MASK = ~OWNER_BIT
 

Detailed Description

合并通知并串行执行服务函数 / Coalesce notifications and serialize service execution

每次调用先把事件合并进一个 32 位状态字,再竞争同一状态字中的唯一 owner 位。未取得 owner 的调用立即返回;owner 原子取走事件快照并执行 handler,直到没有待处理事件。 handler 执行期间再次调用 Invoke() 只会留下下一轮事件,不会递归执行 handler。 Each invocation first merges events into one 32-bit state word and then competes for the sole owner bit in that same word. A caller that does not acquire ownership returns immediately. The owner atomically consumes event snapshots and runs the handler until no work remains. Reentrant Invoke() calls only schedule another round; they never invoke the handler recursively.

Note
低 31 位是可合并的 level-triggered 事件;最高位保留给 owner。事件不记录 发生次数或 payload。所有入口必须提供同一个逻辑 handler;实际 handler 在取得 owner 的调用上下文中执行。 Events are coalesced level-triggered notifications and do not preserve occurrence counts or payloads. The low 31 bits are available for events and the high bit is reserved for ownership. Every entry must provide the same logical handler; execution uses the context of the caller that acquires ownership.
Warning
handler 不得抛出异常;可能由 ISR 取得 owner 时,handler 还必须有界且 非阻塞。The handler must not throw. If an ISR may acquire ownership, the handler must also be bounded and non-blocking.

Definition at line 33 of file serialized_service.hpp.

Member Function Documentation

◆ Invoke()

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

发布事件并尝试执行服务 / Publish events and try to run the service

Template Parameters
Handler可调用对象类型,签名为 void(uint32_t) / Callable type with the signature void(uint32_t)
Parameters
events要合并的低 31 位非零事件位图 / Nonzero low-31-bit event mask to merge
handler处理每轮事件快照的服务函数 / Service function handling each snapshot
Returns
当前调用取得过 owner 时返回 true;事件交给已有 owner 时返回 false / True if this invocation acquired ownership; false if an existing owner will service it

Definition at line 48 of file serialized_service.hpp.

49 {
50 if ((events == 0U) || ((events & OWNER_BIT) != 0U))
51 {
52 return false;
53 }
54
55 uint32_t observed = state_.fetch_or(events, std::memory_order_release) | events;
56
57 while ((observed & OWNER_BIT) == 0U)
58 {
59 // Another owner may have consumed this invocation's event and released the state
60 // before our CAS. An empty state means there is no remaining work to claim.
61 if ((observed & EVENT_MASK) == 0U)
62 {
63 return false;
64 }
65
66 const uint32_t desired = observed | OWNER_BIT;
67 if (state_.compare_exchange_weak(observed, desired, std::memory_order_acquire,
68 std::memory_order_relaxed))
69 {
70 break;
71 }
72 }
73
74 if ((observed & OWNER_BIT) != 0U)
75 {
76 return false;
77 }
78
79 while (true)
80 {
81 const uint32_t snapshot =
82 state_.exchange(OWNER_BIT, std::memory_order_acq_rel) & EVENT_MASK;
83 if (snapshot != 0U)
84 {
85 handler(snapshot);
86 }
87
88 uint32_t expected = OWNER_BIT;
89 if (state_.compare_exchange_strong(expected, 0U, std::memory_order_release,
90 std::memory_order_acquire))
91 {
92 return true;
93 }
94 // A publisher ORed new events while OWNER_BIT was set. Keep ownership and consume
95 // the next snapshot; release cannot race past an already-published event.
96 }
97 }

Field Documentation

◆ EVENT_MASK

uint32_t LibXR::SerializedService::EVENT_MASK = ~OWNER_BIT
staticconstexprprivate

Definition at line 104 of file serialized_service.hpp.

◆ OWNER_BIT

uint32_t LibXR::SerializedService::OWNER_BIT = 1U << 31U
staticconstexprprivate

Definition at line 103 of file serialized_service.hpp.

◆ state_

std::atomic<uint32_t> LibXR::SerializedService::state_ {0U}
private

Definition at line 106 of file serialized_service.hpp.

106{0U};

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