1. 更新定时器避免重入

This commit is contained in:
coffee 2026-04-21 21:32:08 +08:00
parent 029be9ede5
commit 9cd7eb2499
2 changed files with 9 additions and 1 deletions

View File

@ -63,6 +63,7 @@ typedef struct TimeRegisterInfo {
HTimerLen_t heapSize; ///< 堆当前大小 HTimerLen_t heapSize; ///< 堆当前大小
HTimerLen_t len; ///< 定时器个数 HTimerLen_t len; ///< 定时器个数
volatile uint8_t schedu; ///< 需要重新调度 (原子操作) volatile uint8_t schedu; ///< 需要重新调度 (原子操作)
uint8_t run : 1; ///< 运行中
} TimeRegisterInfo; } TimeRegisterInfo;
///< 初始化毫秒定时器, 需要传递获取毫秒的函数 ///< 初始化毫秒定时器, 需要传递获取毫秒的函数

View File

@ -15,7 +15,7 @@
// ==================== 原子操作支持 ==================== // ==================== 原子操作支持 ====================
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
// STM32/GD32/ESP32 都支持 GCC 扩展 // 支持 GCC 扩展
// 使用 __atomic_* 系列函数,提供明确的内存序语义 // 使用 __atomic_* 系列函数,提供明确的内存序语义
static inline int htimer_cas(volatile uint8_t *ptr, uint8_t oldv, uint8_t newv) { static inline int htimer_cas(volatile uint8_t *ptr, uint8_t oldv, uint8_t newv) {
return __atomic_compare_exchange_n(ptr, &oldv, newv, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE); return __atomic_compare_exchange_n(ptr, &oldv, newv, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
@ -334,6 +334,11 @@ void HTimerRun(uint8_t id)
} }
TimeRegisterInfo *reg = &sInfo.info[id]; TimeRegisterInfo *reg = &sInfo.info[id];
if (reg->run) {
return ;
}
reg->run = 1;
uint32_t now = GetCurrentMs(); uint32_t now = GetCurrentMs();
// schedu 阶段:只有 HTimerRun 修改堆,单线程安全 // schedu 阶段:只有 HTimerRun 修改堆,单线程安全
@ -415,6 +420,8 @@ void HTimerRun(uint8_t id)
HTIMER_ATOMIC_OR(&reg->schedu, 1); HTIMER_ATOMIC_OR(&reg->schedu, 1);
} }
} }
reg->run = 0;
} }
HTimer_t HTimerAdd(uint8_t id, uint32_t ms, HTimerCallType call, eHTimerFlags flags) HTimer_t HTimerAdd(uint8_t id, uint32_t ms, HTimerCallType call, eHTimerFlags flags)