3#include "libxr_def.hpp"
11void Topic::EnsureDomainRegistry()
16 {
return static_cast<int>(a) -
static_cast<int>(b); });
32 if (topic->
data_.mutex)
34 topic->
data_.mutex->Lock();
38 LockState expected = LockState::UNLOCKED;
39 if (!topic->
data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
50 if (topic->
data_.mutex)
52 topic->
data_.mutex->Unlock();
56 topic->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
62 if (topic->
data_.mutex)
68 LockState expected = LockState::UNLOCKED;
69 if (!topic->
data_.busy.compare_exchange_strong(expected, LockState::LOCKED))
80 if (topic->
data_.mutex)
86 topic->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
92 ASSERT(name !=
nullptr);
94 EnsureDomainRegistry();
100 if (domain !=
nullptr)
107 [](
const uint32_t& a,
const uint32_t& b)
108 {
return static_cast<int>(a) -
static_cast<int>(b); });
115 if (cb.PayloadSize() != 0)
119 ASSERT(
block_->
data_.max_length == cb.PayloadSize());
123 ASSERT(
block_->
data_.max_length <= cb.PayloadSize());
135 bool cache,
bool check_length)
137 ASSERT(name !=
nullptr);
139 if (domain ==
nullptr)
141 domain = EnsureDefaultDomain();
150 ASSERT(topic->data_.max_length == max_length);
151 ASSERT(topic->data_.check_length == check_length);
153 if (multi_publisher && !topic->data_.mutex)
173 block_->
data_.busy.store(LockState::USE_MUTEX, std::memory_order_release);
178 block_->
data_.busy.store(LockState::UNLOCKED, std::memory_order_release);
194 ASSERT(name !=
nullptr);
196 if (domain ==
nullptr)
229 topic =
Find(name, domain);
230 if (topic ==
nullptr)
232 if (timeout != UINT32_MAX &&
239 }
while (topic ==
nullptr);
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
微秒时间戳 / Microsecond timestamp
互斥锁类,提供线程同步机制 (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
主题域(Domain)管理器,用于组织多个主题。Domain manager for organizing multiple topics.
Domain(const char *name)
构造函数,初始化或查找指定名称的主题域。Constructor initializing or looking up a domain by name.
RBTree< uint32_t >::Node< RBTree< uint32_t > > * node_
指向该域的根节点。Pointer to the root node of the domain.
static Domain * def_domain_
默认的主题域,所有未指定域的主题都会归入此域 Default domain where all topics without a specified domain are assigned
static void Unlock(TopicHandle topic)
解锁主题。Unlock the topic.
static void Lock(TopicHandle topic)
锁定主题,防止其被多个订阅者同时访问。Lock the topic to prevent it from being accessed by multiple subscribers at the sa...
static void UnlockFromCallback(TopicHandle topic)
从回调中解锁主题。Unlock the topic from a callback.
static void LockFromCallback(TopicHandle topic)
从回调中锁定主题,防止其被多个订阅者同时访问。Lock the topic from a callback to prevent it from being accessed by multiple s...
void EnableCache()
启用主题的缓存功能 Enables caching for the topic
static TopicHandle WaitTopic(const char *name, uint32_t timeout=UINT32_MAX, Domain *domain=nullptr)
等待主题的创建并返回其句柄 Waits for a topic to be created and returns its handle
uint32_t GetKey() const
获取主题的键值 Gets the key value of the topic
TopicHandle block_
主题句柄,指向当前主题的内存块 Topic handle pointing to the memory block of the current topic
void RegisterCallback(Callback &cb)
注册回调函数 Registers a callback function
static TopicHandle Find(const char *name, Domain *domain=nullptr)
在指定域中查找主题 Finds a topic in the specified domain
static RBTree< uint32_t > * domain_
主题域的红黑树结构,存储不同的主题 Red-Black Tree structure for storing different topics in the domain
Topic()
默认构造函数,创建一个空的 Topic 实例 Default constructor, creates an empty Topic instance
constexpr size_t CACHE_LINE_SIZE
缓存行大小 / Cache line size
存储主题运行状态和静态配置。Stores topic runtime state and static configuration.