RTU/mimo/plan/libtask优化.md

59 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Plan: libtask 优化
## 优化建议列表
基于《libtask模块分析.md》中 8 个缺点,按严重度排优先级:
| # | 优化项 | 严重度 | 改动量 | 说明 |
|---|--------|--------|--------|------|
| 1 | Timer TOCTOU 修复 | 中 | 2行 | stop/destroy 等 handler 执行完毕再返回 |
| 2 | 消息队列阻塞 send | 中 | ~40行 | 新增 `task_msg_queue_send_timeout`,满时阻塞等待 |
| 3 | 事件查询接口 | 低 | ~15行 | 新增 `task_event_query`,不等待查询当前事件位 |
| 4 | 清理死代码 | 低 | ~10行 | 删除未使用的 `#include <mqueue.h>`、注释掉的 `non_block` 等 |
| 5 | Timer SIGEV_THREAD 保留 | — | 0行 | 评估后不修改(回调仅做 task_event_sendmutex 安全) |
每个优化项独立、低风险、不改变现有 API 兼容性。
---
## 步骤
### T1: 保存 plan 到 mimo/plan/
### T2: 优化 #1 — Timer TOCTOU 修复
-`stru_task_timer` 新增 `int running` 字段
- `task_timer_sig_handler`: `fun()` 前设 `running=1`,后设 `running=0`
- `task_timer_stop`: 置 `active=0``while(running) usleep(100)` 等 handler 完成
- `task_timer_destroy`: 同 stop 的等待逻辑
### T3: 优化 #2 — 消息队列阻塞 send
- 新增 API: `int task_msg_queue_send_timeout(queue, msg, size, timeout_ms)`
- 逻辑: 满时 `pthread_cond_timedwait` → 有空位则写入 → signal 接收者
-`myTask.h` 中新增声明
### T4: 优化 #3 — 事件查询接口
- 新增 API: `uint32_t task_event_query(stru_task_event_t p_event)`
- 逻辑: lock → 读取 events → unlock → 返回(不清除)
-`myTask.h` 中新增声明
### T5: 优化 #4 — 清理死代码
- 删除未使用头文件: `#include <mqueue.h>`, `#include <sys/msg.h>`, `#include <signal.h>`, `#include <sched.h>`
- 删除 `stru_task_msg_queue` 中注释掉的 `int non_block`
### T6: 编译验证
- 执行 `./release/build.sh` 确保所有改动编译零错误零警告
### T7: 更新分析文档
- 更新 `mimo/工程/libtask模块分析.md` 的优缺点章节,反映优化结果
---
## 涉及文件
- **修改**: `src/public/libtask/src/myTask.c`(核心修改)
- **修改**: `release/inc/myTask.h`(新增 API 声明)
- **修改**: `mimo/工程/libtask模块分析.md`(同步优缺点)
## 验证
1. `./release/build.sh` 编译通过
2. 确认所有现有调用点app_sys.cpp 中的 task_timer_create + task_event_send不受影响