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

WritePort class for handling write operations. More...

#include <libxr_rw.hpp>

Collaboration diagram for LibXR::WritePort:
[legend]

Data Structures

class  Stream
 WritePort 的流式写入操作器,支持链式 << 操作和批量提交。 More...
 

Public Types

enum class  BusyState : uint32_t {
  LOCKED , BLOCK_WAITING = 1 , BLOCK_CLAIMED , BLOCK_DETACHED = 3 ,
  IDLE = UINT32_MAX
}
 

Public Member Functions

 WritePort (size_t queue_size=3, size_t buffer_size=128)
 构造一个新的 WritePort 对象。 Constructs a new WritePort object.
 
size_t EmptySize ()
 获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.
 
size_t Size ()
 获取当前数据队列的已使用大小。 Gets the used size of the current data queue.
 
bool Writable ()
 判断端口是否可写。 Checks whether the port is writable.
 
WritePortoperator= (WriteFun fun)
 赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.
 
void Finish (bool in_isr, ErrorCode ans, WriteInfoBlock &info)
 更新写入操作的状态。 Updates the status of the write operation.
 
void MarkAsRunning (WriteOperation &op)
 标记写入操作为运行中。 Marks the write operation as running.
 
ErrorCode operator() (ConstRawData data, WriteOperation &op, bool in_isr=false)
 执行写入操作。 Performs a write operation.
 
void Reset ()
 Resets the WritePort.
 
ErrorCode CommitWrite (ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
 提交写入操作。 Commits a write operation.
 

Data Fields

WriteFun write_fun_ = nullptr
 
LockFreeQueue< WriteInfoBlock > * queue_info_
 
LockFreeQueue< uint8_t > * queue_data_
 
std::atomic< BusyStatebusy_ {BusyState::IDLE}
 
ErrorCode block_result_ = ErrorCode::OK
 Final status for the current BLOCK write.
 

Detailed Description

WritePort class for handling write operations.

处理写入操作的WritePort类。

Definition at line 537 of file libxr_rw.hpp.

Member Enumeration Documentation

◆ BusyState

enum class LibXR::WritePort::BusyState : uint32_t
strong
Enumerator
LOCKED 

Submission path owns queue mutation. 提交路径占有写队列/元数据修改权。

BLOCK_WAITING 

One BLOCK waiter is armed and waiting for final completion. 一个 BLOCK 等待者已经挂起,等待最终完成。

BLOCK_CLAIMED 

Final wakeup belongs to the current waiter. 最终唤醒已归当前等待者所有。

BLOCK_DETACHED 

Waiter already timed out/reset; completion must not post. 等待者已超时/被分离,完成侧不能再 Post。

IDLE 

No active submitter and no armed BLOCK waiter. 没有活动提交者,也没有挂起中的 BLOCK 等待者。

Definition at line 553 of file libxr_rw.hpp.

554 {
555 LOCKED =
556 0,
557 BLOCK_WAITING = 1,
560 2,
561 BLOCK_DETACHED = 3,
563 IDLE = UINT32_MAX
565 };
@ BLOCK_CLAIMED
Final wakeup belongs to the current waiter. 最终唤醒已归当前等待者所有。
@ LOCKED
Submission path owns queue mutation. 提交路径占有写队列/元数据修改权。

Constructor & Destructor Documentation

◆ WritePort()

WritePort::WritePort ( size_t queue_size = 3,
size_t buffer_size = 128 )

构造一个新的 WritePort 对象。 Constructs a new WritePort object.

该构造函数初始化无锁操作队列 queue_info_ 和数据块队列 queue_data_。 This constructor initializes the lock-free operation queue queue_info_ and the data block queue queue_data_.

Parameters
queue_size队列的大小,默认为 3。 The size of the queue, default is 3.
block_size缓存的数据的最大大小,默认为 128。 The maximum size of cached data, default is 128.
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 265 of file libxr_rw.cpp.

266 : queue_info_(new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
268 queue_data_(buffer_size > 0 ? new (std::align_val_t(LIBXR_CACHE_LINE_SIZE))
269 LockFreeQueue<uint8_t>(buffer_size)
270 : nullptr)
271{
272}
无锁队列实现 / Lock-free queue implementation

Member Function Documentation

◆ CommitWrite()

ErrorCode WritePort::CommitWrite ( ConstRawData data,
WriteOperation & op,
bool pushed = false,
bool in_isr = false )

提交写入操作。 Commits a write operation.

Parameters
data写入的原始数据 / Raw data to be written
op写入操作对象,包含操作类型和同步机制。 Write operation object containing the operation type and synchronization
pushed数据是否已经推送到缓冲区 / Whether the data has been pushed to the buffer
in_isr指示是否在中断上下文中执行。 Indicates whether the operation is executed in an interrupt context.
Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 356 of file libxr_rw.cpp.

358{
359 if (!meta_pushed && queue_info_->EmptySize() < 1)
360 {
361 busy_.store(BusyState::IDLE, std::memory_order_release);
362 return ErrorCode::FULL;
363 }
364
366 if (!meta_pushed)
367 {
368 if (queue_data_->EmptySize() < data.size_)
369 {
370 busy_.store(BusyState::IDLE, std::memory_order_release);
371 return ErrorCode::FULL;
372 }
373
374 ans =
375 queue_data_->PushBatch(reinterpret_cast<const uint8_t*>(data.addr_), data.size_);
376 UNUSED(ans);
377 ASSERT(ans == ErrorCode::OK);
378
379 WriteInfoBlock info{data, op};
380 ans = queue_info_->Push(info);
381
382 ASSERT(ans == ErrorCode::OK);
383 }
384
385 op.MarkAsRunning();
386
387 if (op.type == WriteOperation::OperationType::BLOCK)
388 {
389 // BLOCK waiter must be armed before write_fun_() runs.
390 // 必须先挂起 BLOCK waiter,再调用 write_fun_()。
391 busy_.store(BusyState::BLOCK_WAITING, std::memory_order_release);
392 }
393
394 ans = write_fun_(*this, in_isr);
395
396 if (ans != ErrorCode::PENDING)
397 {
398 if (op.type == WriteOperation::OperationType::BLOCK)
399 {
400 busy_.store(BusyState::IDLE, std::memory_order_release);
401 return ans;
402 }
403
404 if (!meta_pushed)
405 {
406 busy_.store(BusyState::IDLE, std::memory_order_release);
407 }
408
409 if (op.type != WriteOperation::OperationType::BLOCK)
410 {
411 op.UpdateStatus(in_isr, ans);
412 }
413 return ErrorCode::OK;
414 }
415
416 if (op.type == WriteOperation::OperationType::BLOCK)
417 {
418 ASSERT(!in_isr);
419 auto wait_ans = op.data.sem_info.sem->Wait(op.data.sem_info.timeout);
420 if (wait_ans == ErrorCode::OK)
421 {
422 // BLOCK_CLAIMED is always released by the waiter itself.
423 // BLOCK_CLAIMED 始终由 waiter 自己释放。
424#ifdef LIBXR_DEBUG_BUILD
425 auto state = busy_.load(std::memory_order_acquire);
426 ASSERT(state == BusyState::BLOCK_CLAIMED);
427#endif
428 busy_.store(BusyState::IDLE, std::memory_order_release);
429 return block_result_;
430 }
431
432 // Timeout won before completion claimed the waiter.
433 // 超时先赢,完成侧还没 claim 当前 waiter。
435 if (busy_.compare_exchange_strong(expected, BusyState::BLOCK_DETACHED,
436 std::memory_order_acq_rel,
437 std::memory_order_acquire))
438 {
439 return ErrorCode::TIMEOUT;
440 }
441
442 if (expected != BusyState::BLOCK_CLAIMED)
443 {
444 ASSERT(expected == BusyState::BLOCK_DETACHED);
445 busy_.store(BusyState::IDLE, std::memory_order_release);
446 return ErrorCode::TIMEOUT;
447 }
448
449 // Timeout lost after completion had already claimed the waiter.
450 // 超时发生得太晚,完成侧已经 claim 了当前 waiter。
451 auto finish_wait_ans = op.data.sem_info.sem->Wait(UINT32_MAX);
452 UNUSED(finish_wait_ans);
453 ASSERT(finish_wait_ans == ErrorCode::OK);
454 busy_.store(BusyState::IDLE, std::memory_order_release);
455
456 return block_result_;
457 }
458
459 if (!meta_pushed)
460 {
461 busy_.store(BusyState::IDLE, std::memory_order_release);
462 }
463
464 return ErrorCode::OK;
465}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
union LibXR::Operation::@5 data
void UpdateStatus(bool in_isr, Status &&status)
Updates operation status based on type.
Definition libxr_rw.hpp:178
void MarkAsRunning()
标记操作为运行状态。 Marks the operation as running.
Definition libxr_rw.hpp:213
OperationType type
Definition libxr_rw.hpp:237
ErrorCode Wait(uint32_t timeout=UINT32_MAX)
等待(减少)信号量 Waits (decrements) the semaphore
Definition semaphore.cpp:53
ErrorCode block_result_
Final status for the current BLOCK write.
Definition libxr_rw.hpp:571
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
@ TIMEOUT
超时 | Timeout
@ FULL
已满 | Full
@ PENDING
等待中 | Pending
@ OK
操作成功 | Operation successful

◆ EmptySize()

size_t WritePort::EmptySize ( )

获取数据队列的剩余可用空间。 Gets the remaining available space in the data queue.

Returns
返回数据队列的空闲大小。 Returns the empty size of the data queue.

Definition at line 274 of file libxr_rw.cpp.

275{
276 ASSERT(queue_data_ != nullptr);
277 return queue_data_->EmptySize();
278}

◆ Finish()

void WritePort::Finish ( bool in_isr,
ErrorCode ans,
WriteInfoBlock & info )

更新写入操作的状态。 Updates the status of the write operation.

该函数在写入操作过程中更新 write_size_ 并调用 UpdateStatus 方法更新 op 的状态。 This function updates write_size_ and calls UpdateStatus on op during a write operation.

Parameters
in_isr指示是否在中断上下文中执行。 Indicates whether the operation is executed in an interrupt context.
ans错误码,用于指示操作的结果。 Error code indicating the result of the operation.
op需要更新状态的 WriteOperation 引用。 Reference to the WriteOperation whose status needs to be updated.

Definition at line 294 of file libxr_rw.cpp.

295{
296 if (info.op.type == WriteOperation::OperationType::BLOCK)
297 {
298 block_result_ = ans;
299
300 // Write completion claims BLOCK_WAITING and hands the wakeup to the waiter.
301 // 写完成从 BLOCK_WAITING claim 当前 waiter,并把唤醒交给它。
303 if (busy_.compare_exchange_strong(expected, BusyState::BLOCK_CLAIMED,
304 std::memory_order_acq_rel,
305 std::memory_order_acquire))
306 {
307 info.op.data.sem_info.sem->PostFromCallback(in_isr);
308 return;
309 }
310
311 ASSERT(expected == BusyState::BLOCK_DETACHED);
312 if (expected == BusyState::BLOCK_DETACHED)
313 {
314 expected = BusyState::BLOCK_DETACHED;
315 (void)busy_.compare_exchange_strong(expected, BusyState::IDLE,
316 std::memory_order_acq_rel,
317 std::memory_order_acquire);
318 }
319 return;
320 }
321
322 info.op.UpdateStatus(in_isr, ans);
323}
void PostFromCallback(bool in_isr)
从中断回调中释放(增加)信号量 Releases (increments) the semaphore from an ISR (Interrupt Service Routine)
WriteOperation op
Write operation instance. 写入操作实例。
Definition libxr_rw.hpp:371

◆ MarkAsRunning()

void WritePort::MarkAsRunning ( WriteOperation & op)

标记写入操作为运行中。 Marks the write operation as running.

该函数用于将 op 标记为运行状态,以指示当前正在进行写入操作。 This function marks op as running to indicate an ongoing write operation.

Parameters
op需要更新状态的 WriteOperation 引用。 Reference to the WriteOperation whose status needs to be updated.

Definition at line 325 of file libxr_rw.cpp.

325{ op.MarkAsRunning(); }

◆ operator()()

ErrorCode WritePort::operator() ( ConstRawData data,
WriteOperation & op,
bool in_isr = false )

执行写入操作。 Performs a write operation.

该函数检查端口是否可写,并根据 data.size_op 的类型执行不同的操作。 This function checks if the port is writable and performs different actions based on data.size_ and the type of op.

Parameters
data需要写入的原始数据。 Raw data to be written.
op写入操作对象,包含操作类型和同步机制。 Write operation object containing the operation type and synchronization mechanism.
in_isr指示是否在中断上下文中执行。 Indicates whether the operation is executed in an interrupt context.
Returns
返回操作的 ErrorCode,指示操作结果。 Returns an ErrorCode indicating the result of the operation.

Definition at line 327 of file libxr_rw.cpp.

328{
329 if (Writable())
330 {
331 if (data.size_ == 0)
332 {
333 if (op.type != WriteOperation::OperationType::BLOCK)
334 {
335 op.UpdateStatus(in_isr, ErrorCode::OK);
336 }
337 return ErrorCode::OK;
338 }
339
340 BusyState expected = BusyState::IDLE;
341 if (!busy_.compare_exchange_strong(expected, BusyState::LOCKED,
342 std::memory_order_acq_rel,
343 std::memory_order_acquire))
344 {
345 return ErrorCode::BUSY;
346 }
347
348 return CommitWrite(data, op, false, in_isr);
349 }
350 else
351 {
353 }
354}
bool Writable()
判断端口是否可写。 Checks whether the port is writable.
Definition libxr_rw.cpp:286
ErrorCode CommitWrite(ConstRawData data, WriteOperation &op, bool pushed=false, bool in_isr=false)
提交写入操作。 Commits a write operation.
Definition libxr_rw.cpp:356
@ BUSY
忙碌 | Busy
@ NOT_SUPPORT
不支持 | Not supported

◆ operator=()

WritePort & WritePort::operator= ( WriteFun fun)

赋值运算符重载,用于设置写入函数。 Overloaded assignment operator to set the write function.

该函数允许使用 WriteFun 类型的函数对象赋值给 WritePort,从而设置 write_fun_。 This function allows assigning a WriteFun function object to WritePort, setting write_fun_.

Parameters
fun要分配的写入函数。 The write function to be assigned.
Returns
返回自身的引用,以支持链式调用。 Returns a reference to itself for chaining.

Definition at line 288 of file libxr_rw.cpp.

289{
290 write_fun_ = fun;
291 return *this;
292}

◆ Reset()

void WritePort::Reset ( )

Resets the WritePort.

重置WritePort。

Definition at line 467 of file libxr_rw.cpp.

468{
469 ASSERT(queue_data_ != nullptr);
470 queue_data_->Reset();
471 queue_info_->Reset();
472
473 auto state = busy_.load(std::memory_order_acquire);
474 if (state == BusyState::BLOCK_WAITING)
475 {
476 // Reset detaches the BLOCK waiter instead of reopening the port directly.
477 // Reset 先分离 BLOCK waiter,不直接重开端口。
479 if (busy_.compare_exchange_strong(expected, BusyState::BLOCK_DETACHED,
480 std::memory_order_acq_rel,
481 std::memory_order_acquire))
482 {
483 return;
484 }
485
486 state = busy_.load(std::memory_order_acquire);
487 }
488
490 {
491 return;
492 }
493
495 busy_.store(BusyState::IDLE, std::memory_order_release);
496}
void Reset()
重置队列 / Resets the queue

◆ Size()

size_t WritePort::Size ( )

获取当前数据队列的已使用大小。 Gets the used size of the current data queue.

Returns
返回数据队列的已使用大小。 Returns the size of the data queue.

Definition at line 280 of file libxr_rw.cpp.

281{
282 ASSERT(queue_data_ != nullptr);
283 return queue_data_->Size();
284}
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue

◆ Writable()

bool WritePort::Writable ( )

判断端口是否可写。 Checks whether the port is writable.

Returns
如果 write_fun_ 不为空,则返回 true,否则返回 false。 Returns true if write_fun_ is not null, otherwise returns false.

Definition at line 286 of file libxr_rw.cpp.

286{ return write_fun_ != nullptr; }

Field Documentation

◆ block_result_

ErrorCode LibXR::WritePort::block_result_ = ErrorCode::OK

Final status for the current BLOCK write.

Definition at line 571 of file libxr_rw.hpp.

◆ busy_

std::atomic<BusyState> LibXR::WritePort::busy_ {BusyState::IDLE}

Definition at line 570 of file libxr_rw.hpp.

◆ queue_data_

LockFreeQueue<uint8_t>* LibXR::WritePort::queue_data_

Definition at line 569 of file libxr_rw.hpp.

◆ queue_info_

LockFreeQueue<WriteInfoBlock>* LibXR::WritePort::queue_info_

Definition at line 568 of file libxr_rw.hpp.

◆ write_fun_

WriteFun LibXR::WritePort::write_fun_ = nullptr

Definition at line 567 of file libxr_rw.hpp.


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