Skip to content

Utils_README

SweerItTer edited this page Feb 21, 2026 · 4 revisions

Utils 通用工具模块 API 文档

概述

Utils 模块提供通用的工具类,包括线程管理、内存管理、队列、日志、设备监控等功能。

模块组成

线程管理

文档 说明
AsyncThreadPool Utils_AsyncThreadPool.md 异步线程池
ThreadPauser Utils_ThreadPauser.md 线程暂停器
ThreadUtils Utils_ThreadUtils.md 线程工具

内存管理

文档 说明
FixedSizePool Utils_FixedSizePool.md 固定大小对象池
ObjectsPool Utils_ObjectsPool.md 对象池
SharedBufferState Utils_SharedBufferState.md 共享缓冲区状态

队列

文档 说明
SafeQueue Utils_SafeQueue.md 线程安全队列
OrderedQueue Utils_OrderedQueue.md 有序队列
ConcurrentQueue Utils_ConcurrentQueue.md 并发队列

文件描述符

文档 说明
FdWrapper Utils_FdWrapper.md 文件描述符包装器
FenceWatcher Utils_FenceWatcher.md Fence 监控器

日志和监控

文档 说明
Logger Utils_Logger.md 日志系统
CpuMonitor Sys_CpuMonitor.md CPU 监控
MemoryMonitor Sys_MemoryMonitor.md 内存监控
UdevMonitor Utils_UdevMonitor.md 设备监控

其他工具

文档 说明
ProgressBar Utils_ProgressBar.md 进度条
SimpleVariant Utils_SimpleVariant.md 简单变体
Types Utils_Types.md 类型定义

功能特性

线程管理

  • 异步任务执行
  • 线程池管理
  • 线程暂停和恢复

内存管理

  • 对象池管理
  • 缓冲区状态管理
  • 引用计数管理

队列

  • 线程安全队列
  • 阻塞和非阻塞模式
  • 超时等待

设备监控

  • 设备热插拔监控
  • 设备事件回调
  • 设备发现

日志系统

  • 多级别日志输出
  • 日志文件管理
  • 线程安全

使用示例

线程池

// 创建线程池
AsyncThreadPool pool(4);

// 提交任务
auto future = pool.submit([](int a, int b) {
    return a + b;
}, 10, 20);

// 获取结果
int result = future.get();
printf("Result: %d\n", result);

内存池

// 创建内存池,分配 16 字节大小的内存块
FixedSizePool pool(16);

// 分配内存
void* ptr = pool.allocate();
if (ptr) {
    // 使用内存
    int* value = new(ptr) int(42);  // 在分配的内存上构造对象
    *value = 42;
    printf("Value: %d\n", *value);
    
    // 销毁对象
    value->~int();
    
    // 释放内存
    pool.deallocate(ptr);
}

共享缓冲区

// 创建共享缓冲区
auto shared_state = std::make_shared<SharedBufferState>(dma_fd, 2);

// 设置有效
shared_state->setValid(true);

// 检查有效性
if (shared_state->getValid()) {
    int fd = shared_state->fd();
    process_buffer(fd);
}

安全队列

// 创建队列
SafeQueue<int> queue;

// 入队
queue.push(42);

// 出队(阻塞)
int item = queue.pop();

// 出队(非阻塞)
auto item = queue.tryPop();
if (item) {
    printf("Got item: %d\n", *item);
}

// 出队(超时)
auto item = queue.tryPopFor(std::chrono::seconds(1));

日志系统

// 设置日志级别
Logger::instance().setLevel(LogLevel::INFO);

// 设置日志文件
Logger::instance().setLogFile("/var/log/myapp.log");

// 记录日志
LOG_INFO("Application started");
LOG_DEBUG("Debugging information: value=%d", 42);
LOG_WARNING("Warning: low memory");
LOG_ERROR("Error: failed to connect");

设备监控

// 监控 video4linux 设备
UdevMonitor monitor("video4linux");

monitor.setCallback([](const UdevMonitor::DeviceInfo& info) {
    if (info.event == UdevMonitor::EventType::ADD) {
        printf("Device added: %s\n", info.devnode.c_str());
    } else if (info.event == UdevMonitor::EventType::REMOVE) {
        printf("Device removed: %s\n", info.devnode.c_str());
    }
});

monitor.start();

线程安全

  • AsyncThreadPool: 线程安全,可以并发提交任务
  • FixedSizePool: 线程安全,使用线程本地缓存和互斥锁保护全局数据结构
  • SafeQueue: 线程安全,所有操作都是线程安全的
  • SharedBufferState: 线程安全,valid 标志使用原子操作
  • Logger: 线程安全,可以并发记录日志
  • UdevMonitor: 线程安全,回调在监控线程中调用

性能优化

  • 使用对象池减少内存分配开销
  • 使用线程池提高并行度
  • 使用移动语义减少拷贝
  • 使用条件变量提高效率

错误处理

  • 检查所有函数的返回值
  • 使用异常处理错误
  • 记录错误日志

相关模块

注意事项

  1. 线程安全: 确保线程安全的使用方式
  2. 资源管理: 使用 RAII 管理资源
  3. 性能: 合理选择数据结构和算法
  4. 异常处理: 检查返回值,处理异常
  5. 日志: 记录重要的调试信息
  6. 线程数: 根据硬件配置设置合理的线程数
  7. 队列大小: 根据需求设置合适的队列大小
  8. 对象池: 频繁分配的对象使用对象池

版本历史

  • v1.0 - 初始版本,提供基本的工具类

主页

API 文档

DMA 模块

DRM 模块

NET 模块

V4L2 模块

V4L2Param 模块

RGA 模块

MPP 模块

Sys 模块

Mouse 模块

Utils 模块

Clone this wiki locally