Utils_FenceWatcher - SweerItTer/utilsCore GitHub Wiki
FenceWatcher 是 utilsCore Utils 模块的核心类,提供 Fence 同步对象监控功能,使用单例模式。
- 监控 Fence 同步对象
- 异步通知 Fence 完成
- 超时检测
- 线程安全操作
- GPU 同步
- DRM 显示同步
- RGA 处理同步
- 硬件加速同步
- 依赖: Linux epoll, eventfd
- 被依赖: DrmLayer, RGA 等模块
FenceWatcher 是 Fence 监控的封装类,提供:
- 单例模式
- Fence 监控
- 异步回调通知
- 超时检测
- 单例模式: 全局唯一实例
- 观察者模式: 回调通知 Fence 状态
- RAII: 自动管理资源
static FenceWatcher& instance();参数说明: 无
返回值: FenceWatcher 单例引用
所有权归属:
- 只读访问,不转移所有权
注意事项:
- 全局唯一实例
- 第一次调用时初始化
- 线程安全
使用例程:
auto& watcher = FenceWatcher::instance();void watchFence(int fence_fd, std::function<void()> callback, int timeout_ms = 1000);参数说明:
-
fence_fd(输入): Fence 文件描述符 -
callback(输入): 回调函数(无参数) -
timeout_ms(输入): 超时时间(毫秒),默认 1000ms
返回值: 无
所有权归属:
- fence_fd 由 FenceWatcher 管理(自动关闭)
- callback 由调用者提供
注意事项:
- fence_fd < 0 时会立即调用 callback
- callback 会在 Fence 触发或超时后调用
- FenceWatcher 会自动关闭 fence_fd
- 默认超时为 1000ms
- 线程安全
使用例程:
auto& watcher = FenceWatcher::instance();
// 监控 Fence
watcher.watchFence(fence_fd, []() {
printf("Fence signaled or timeout\n");
}, 1000); // 1 秒超时void shutdown();参数说明: 无
返回值: 无
所有权归属:
- 无所有权转移
注意事项:
- 停止监控线程
- 唤醒 epoll_wait
- 等待线程退出
- 可以安全重复调用
使用例程:
auto& watcher = FenceWatcher::instance();
// 关闭监控
watcher.shutdown();class FenceWatcher {
private:
int epoll_fd_ = -1; // epoll 文件描述符
int event_fd_ = -1; // eventfd 用于唤醒
std::unordered_map<int, FenceData> fd_callbacks_; // fence_fd -> 回调映射
std::mutex map_mutex_; // 保护 fd_callbacks_
std::thread loop_thread_; // 事件循环线程
std::atomic<bool> running_; // 运行标志
};
struct FenceData {
std::function<void()> callback; // 回调函数
std::chrono::steady_clock::time_point expire_time; // 过期时间
};void eventLoop() {
const int MAX_EVENTS = 16;
struct epoll_event events[MAX_EVENTS];
while (running_) {
int n = epoll_wait(epoll_fd_, events, MAX_EVENTS, 50);
auto now = std::chrono::steady_clock::now();
// 处理就绪事件
for (int i = 0; i < n; ++i) {
int fd = events[i].data.fd;
if (fd == event_fd_) {
// shutdown 唤醒
uint64_t dummy;
read(event_fd_, &dummy, sizeof(dummy));
continue;
}
triggerCallback(fd);
}
// 超时检查
std::vector<int> expired;
{
std::lock_guard<std::mutex> lock(map_mutex_);
for (auto& kv : fd_callbacks_) {
int fd = kv.first;
FenceData& data = kv.second;
if (now >= data.expire_time) {
expired.push_back(fd);
}
}
}
for (size_t i = 0; i < expired.size(); ++i) {
triggerCallback(expired[i]);
}
}
// 退出前清理剩余 fence
std::lock_guard<std::mutex> lock(map_mutex_);
for (auto& kv : fd_callbacks_) {
close(kv.first);
}
fd_callbacks_.clear();
}-
监控操作: 使用
std::mutex保护 fd_callbacks_ -
原子操作:
std::atomic<bool>保护运行标志 - epoll: 使用 epoll 实现事件驱动
- 可以并发调用 watchFence()
- 可以并发调用 shutdown()
- 回调在事件循环线程中调用
auto& watcher = FenceWatcher::instance();
// 监控 Fence
watcher.watchFence(fence_fd, []() {
printf("Fence signaled or timeout\n");
});auto& watcher = FenceWatcher::instance();
// 提交 DRM 带有 Fence
int fence_fd;
compositor->commit(fence_fd);
// 监控 Fence
watcher.watchFence(fence_fd, [layer]() {
printf("Display completed\n");
layer->onFenceSignaled();
});auto& watcher = FenceWatcher::instance();
// 提交 RGA 处理
int fence_fd;
rga_process_with_fence(input_fd, output_fd, &fence_fd);
// 监控 Fence
watcher.watchFence(fence_fd, []() {
printf("RGA processing completed\n");
});auto& watcher = FenceWatcher::instance();
// 使用 lambda 捕获上下文
FramePtr frame = ...;
watcher.watchFence(fence_fd, [frame]() {
printf("Frame %lu completed\n", frame->meta.timestamp_ns);
});// 应用退出前关闭监控
auto& watcher = FenceWatcher::instance();
watcher.shutdown();- 单例模式: 使用 instance() 获取实例
- fd 管理: FenceWatcher 会自动关闭 fence_fd
- 回调类型: 回调函数无参数,无法区分是触发还是超时
- 默认超时: 默认超时为 1000ms
- 线程安全: 所有操作都是线程安全的
- shutdown: 应用退出前应调用 shutdown()
- epoll: 使用 epoll 实现事件驱动
- Linux 专用: 仅支持 Linux 系统
- DrmLayer - DRM 图层
- RgaProcessor - RGA 处理器
- PlanesCompositor - Plane 组合器
- Utils 模块总览