From 142d557c8fca2641ac78f5526581b06ef882b871 Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 19 Jul 2018 17:43:15 +0800 Subject: [PATCH] sched_thread.c: fix first schedule time --- HISTORY | 3 +- src/sched_thread.c | 19 +++++++++-- src/tests/Makefile | 2 +- src/tests/test_sched_thread.c | 60 +++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/tests/test_sched_thread.c diff --git a/HISTORY b/HISTORY index 660923f..43e1fb0 100644 --- a/HISTORY +++ b/HISTORY @@ -1,9 +1,10 @@ -Version 1.39 2018-07-16 +Version 1.39 2018-07-19 * add #@function REPLACE_VARS * #@set value can embed %{VARIABLE} * shared_func.h: add function starts_with and ends_with * common_blocked_queue.h: add function common_blocked_queue_try_pop + * sched_thread.c: fix first schedule time Version 1.38 2018-06-26 * connection_pool.c: set err_no to 0 when success diff --git a/src/sched_thread.c b/src/sched_thread.c index 6dcbc2e..ecd09de 100644 --- a/src/sched_thread.c +++ b/src/sched_thread.c @@ -44,6 +44,8 @@ static int sched_init_entries(ScheduleArray *pScheduleArray) time_t time_base; struct tm tm_current; struct tm tm_base; + int remain; + int interval; if (pScheduleArray->count < 0) { @@ -105,10 +107,21 @@ static int sched_init_entries(ScheduleArray *pScheduleArray) tm_base.tm_sec = 0; } time_base = mktime(&tm_base); + remain = g_current_time - time_base; + if (remain > 0) + { + interval = pEntry->interval - remain % pEntry->interval; + } + else if (remain < 0) + { + interval = (-1 * remain) % pEntry->interval; + } + else + { + interval = 0; + } - pEntry->next_call_time = g_current_time + \ - pEntry->interval - (g_current_time - \ - time_base) % pEntry->interval; + pEntry->next_call_time = g_current_time + interval; } /* diff --git a/src/tests/Makefile b/src/tests/Makefile index 1dde4cd..b901b44 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -6,7 +6,7 @@ LIB_PATH = -lfastcommon -lpthread ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \ test_id_generator test_ini_parser test_char_convert test_char_convert_loader \ - test_logger test_skiplist_set test_crc32 test_thourands_seperator + test_logger test_skiplist_set test_crc32 test_thourands_seperator test_sched_thread all: $(ALL_PRGS) .c: diff --git a/src/tests/test_sched_thread.c b/src/tests/test_sched_thread.c new file mode 100644 index 0000000..8473aa3 --- /dev/null +++ b/src/tests/test_sched_thread.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "fastcommon/logger.h" +#include "fastcommon/sched_thread.h" + +static int schedule_func(void* arg) +{ + static int count = 0; + logInfo("schedule count: %d", ++count); + return 0; +} + +int main(int argc, char *argv[]) +{ +#define SCHEDULE_ENTRIES_COUNT 2 + + ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_COUNT]; + ScheduleArray scheduleArray; + ScheduleEntry *pEntry; + pthread_t schedule_tid; + time_t current_time; + struct tm tm_base; + int second; + bool continue_flag = true; + + log_init(); + g_log_context.log_level = LOG_DEBUG; + + + pEntry = scheduleEntries; + memset(scheduleEntries, 0, sizeof(scheduleEntries)); + + logInfo("start..."); + + current_time = time(NULL); + localtime_r(¤t_time, &tm_base); + + second = (60 + (tm_base.tm_sec - 10)) % 60; + INIT_SCHEDULE_ENTRY((*pEntry), sched_generate_next_id(), + tm_base.tm_hour, tm_base.tm_min, second, 60, schedule_func, NULL); + pEntry++; + + scheduleArray.entries = scheduleEntries; + scheduleArray.count = pEntry - scheduleEntries; + sched_start(&scheduleArray, &schedule_tid, + 64 * 1024, (bool * volatile)&continue_flag); + + + sleep(600); + logInfo("done."); + + return 0; +}