6#include "libxr_def.hpp"
13void Topic::EnsureDomainRegistry()
18 {
return (a > b) - (a < b); });
26 def_domain_ =
new Domain(
"libxr_def_domain");
34 if (topic->
data_.mutex)
36 topic->
data_.mutex->Lock();
40 LockState expected = LockState::UNLOCKED;
41 if (!topic->
data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
52 if (topic->
data_.mutex)
54 topic->
data_.mutex->Unlock();
58 topic->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
64 if (topic->
data_.mutex)
70 LockState expected = LockState::UNLOCKED;
71 if (!topic->
data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
82 if (topic->
data_.mutex)
88 topic->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
94 ASSERT(name !=
nullptr);
96 EnsureDomainRegistry();
102 if (domain !=
nullptr)
109 [](
const uint32_t& a,
const uint32_t& b) {
return (a > b) - (a < b); });
116Topic::Topic(
const char* name, TypeID::ID payload_type_id,
size_t payload_size,
117 size_t payload_alignment,
Domain* domain,
bool multi_publisher)
119 ASSERT(name !=
nullptr);
120 ASSERT(payload_type_id !=
nullptr);
121 ASSERT(payload_size != 0);
122 ASSERT(payload_alignment != 0);
123 ASSERT((payload_alignment & (payload_alignment - 1)) == 0);
125 if (domain ==
nullptr)
127 domain = EnsureDefaultDomain();
136 RequireTypeContract(topic, payload_type_id, payload_size, payload_alignment);
140 RequireMultiPublisher(topic);
148 block_->
data_.payload_type_id = payload_type_id;
149 block_->
data_.payload_size = payload_size;
150 block_->
data_.payload_alignment = payload_alignment;
151 block_->
data_.crc32 = crc32;
156 block_->
data_.busy.store(LockState::USE_MUTEX, std::memory_order_release);
160 block_->
data_.mutex =
nullptr;
161 block_->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
170void Topic::RequireTypeContract(TopicHandle topic, TypeID::ID payload_type_id,
171 size_t payload_size,
size_t payload_alignment)
173 const auto& data = topic->
data_;
174 REQUIRE(data.payload_type_id == payload_type_id && data.payload_size == payload_size &&
175 data.payload_alignment == payload_alignment);
178void Topic::RequireCallbackType(TopicHandle topic, TypeID::ID payload_type_id)
180 REQUIRE(topic->data_.payload_type_id == payload_type_id);
183void Topic::RequireMultiPublisher(TopicHandle topic)
185 REQUIRE(topic->data_.mutex !=
nullptr);
190 ASSERT(name !=
nullptr);
192 if (domain ==
nullptr)
194 if (def_domain_ ==
nullptr)
198 domain = def_domain_;
212 topic =
Find(name, domain);
213 if (topic ==
nullptr)
215 if (timeout != UINT32_MAX &&
222 }
while (topic ==
nullptr);
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
互斥锁类,提供线程同步机制 Mutex class providing thread synchronization mechanisms.
Key key
节点键值 / Key associated with the node.
红黑树的泛型数据节点,继承自 BaseNode Generic data node for Red-Black Tree, inheriting from BaseNode.
Data data_
存储的数据 / Stored data.
红黑树实现,支持泛型键和值,并提供线程安全操作 Red-Black Tree implementation supporting generic keys and values with thread-...
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 / Search for a node in the Red-Black Tree.
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 / Insert a new node into the tree.
static uint32_t GetTime()
获取当前系统时间(毫秒) Gets the current system time in milliseconds
static void Sleep(uint32_t milliseconds)
让线程进入休眠状态 Puts the thread to sleep
topic 所属的命名域 / Naming domain that groups topics
Domain(const char *name)
构造一个 topic 域 / Construct one topic domain
RBTree< uint32_t >::Node< RBTree< uint32_t > > * node_
static void Unlock(TopicHandle topic)
在普通上下文里释放一个 topic 发布路径 / Unlock one topic publish path in normal context
static void Lock(TopicHandle topic)
在普通上下文里锁住一个 topic 发布路径 / Lock one topic publish path in normal context
static void UnlockFromCallback(TopicHandle topic)
在回调或 ISR 路径里释放一个 topic 发布路径 / Unlock one topic publish path from callback or ISR context
static void LockFromCallback(TopicHandle topic)
在回调或 ISR 路径里锁住一个 topic 发布路径 / Lock one topic publish path from callback or ISR context
static TopicHandle WaitTopic(const char *name, uint32_t timeout=UINT32_MAX, Domain *domain=nullptr)
等待指定名称的 topic 出现 / Wait until a topic with the given name exists
uint32_t GetKey() const
读取 topic 键值 / Read the key value of this topic
static TopicHandle Find(const char *name, Domain *domain=nullptr)
按名称查找一个已存在 topic / Find one existing topic by name
Topic()
构造一个空 topic 视图 / Construct one empty topic view
LibXR 命名空间 / LibXR namespace.
topic 运行时状态块 / Runtime state block of one topic