From 5a04c1c65684faf1fa6fa93e1344791dd758e70e Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 2 Dec 2020 13:57:28 +0800 Subject: [PATCH] timer_modify check new_expires --- src/fast_timer.c | 5 +++++ src/fast_timer.h | 2 +- src/locked_timer.c | 4 ++++ src/locked_timer.h | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/fast_timer.c b/src/fast_timer.c index e41640a..bd1f74b 100644 --- a/src/fast_timer.c +++ b/src/fast_timer.c @@ -96,6 +96,11 @@ int fast_timer_modify(FastTimer *timer, FastTimerEntry *entry, const int64_t new_expires) { int result; + + if (new_expires <= timer->current_time) { + return ETIMEDOUT; + } + if (new_expires > entry->expires) { entry->rehash = TIMER_GET_SLOT_INDEX(timer, new_expires) != TIMER_GET_SLOT_INDEX(timer, entry->expires); diff --git a/src/fast_timer.h b/src/fast_timer.h index 292894f..e7ee7c5 100644 --- a/src/fast_timer.h +++ b/src/fast_timer.h @@ -36,7 +36,7 @@ typedef struct fast_timer_slot { typedef struct fast_timer { int slot_count; //time wheel slot count int64_t base_time; //base time for slot 0 - int64_t current_time; + volatile int64_t current_time; FastTimerSlot *slots; } FastTimer; diff --git a/src/locked_timer.c b/src/locked_timer.c index f1a847c..909c667 100644 --- a/src/locked_timer.c +++ b/src/locked_timer.c @@ -240,6 +240,10 @@ int locked_timer_modify(LockedTimer *timer, LockedTimerEntry *entry, int result; int slot_index; + if (new_expires <= timer->current_time) { + return ETIMEDOUT; + } + if (new_expires > entry->expires) { if ((result=check_entry_status(timer, entry, &slot_index)) != 0) { return result; diff --git a/src/locked_timer.h b/src/locked_timer.h index 8545732..e5243a6 100644 --- a/src/locked_timer.h +++ b/src/locked_timer.h @@ -58,7 +58,7 @@ typedef struct locked_timer { int slot_count; //time wheel slot count LockedTimerSharedLocks entry_shares; //shared locks for entry int64_t base_time; //base time for slot 0 - int64_t current_time; + volatile int64_t current_time; LockedTimerSlot *slots; } LockedTimer;