sched_thread.c: fix first schedule time

pull/37/head
yuqing 2018-07-19 17:43:15 +08:00
parent d0729c2540
commit 142d557c8f
4 changed files with 79 additions and 5 deletions

View File

@ -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

View File

@ -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;
}
/*

View File

@ -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:

View File

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#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(&current_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;
}