diff --git a/ww/eventloop/base/hlog.c b/ww/eventloop/base/hlog.c index 40ec804b..003a9109 100644 --- a/ww/eventloop/base/hlog.c +++ b/ww/eventloop/base/hlog.c @@ -62,8 +62,10 @@ static void logger_init(logger_t* logger) { logger_t* logger_create() { // init gmtoff here time_t ts = time(NULL); - struct tm* local_tm = localtime(&ts); - int local_hour = local_tm->tm_hour; + static _Thread_local struct tm local_tm; + localtime_r(&ts,&local_tm); + + int local_hour = local_tm.tm_hour; struct tm* gmt_tm = gmtime(&ts); int gmt_hour = gmt_tm->tm_hour; s_gmtoff = (local_hour - gmt_hour) * SECONDS_PER_HOUR; @@ -203,12 +205,13 @@ const char* logger_get_cur_file(logger_t* logger) { #endif static void logfile_name(const char* filepath, time_t ts, char* buf, int len) { - struct tm* tm = localtime(&ts); + static _Thread_local struct tm tm; + localtime_r(&ts,&tm); snprintf(buf, len, "%s.%04d%02d%02d.log", filepath, - tm->tm_year+1900, - tm->tm_mon+1, - tm->tm_mday); + tm.tm_year+1900, + tm.tm_mon+1, + tm.tm_mday); } #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop @@ -315,16 +318,16 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) { us = tm.wMilliseconds * 1000; #else struct timeval tv; - struct tm* tm = NULL; + static _Thread_local struct tm tm; gettimeofday(&tv, NULL); time_t tt = tv.tv_sec; - tm = localtime(&tt); - year = tm->tm_year + 1900; - month = tm->tm_mon + 1; - day = tm->tm_mday; - hour = tm->tm_hour; - min = tm->tm_min; - sec = tm->tm_sec; + localtime_r(&tt,&tm); + year = tm.tm_year + 1900; + month = tm.tm_mon + 1; + day = tm.tm_mday; + hour = tm.tm_hour; + min = tm.tm_min; + sec = tm.tm_sec; us = tv.tv_usec; #endif diff --git a/ww/eventloop/base/htime.c b/ww/eventloop/base/htime.c index 6f1542ea..16d79108 100644 --- a/ww/eventloop/base/htime.c +++ b/ww/eventloop/base/htime.c @@ -1,4 +1,5 @@ #include "htime.h" +#include static const char* s_weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; @@ -73,14 +74,15 @@ datetime_t datetime_now() { } datetime_t datetime_localtime(time_t seconds) { - struct tm* tm = localtime(&seconds); + static _Thread_local struct tm tm; + localtime_r(&seconds,&tm); datetime_t dt; - dt.year = tm->tm_year + 1900; - dt.month = tm->tm_mon + 1; - dt.day = tm->tm_mday; - dt.hour = tm->tm_hour; - dt.min = tm->tm_min; - dt.sec = tm->tm_sec; + dt.year = tm.tm_year + 1900; + dt.month = tm.tm_mon + 1; + dt.day = tm.tm_mday; + dt.hour = tm.tm_hour; + dt.min = tm.tm_min; + dt.sec = tm.tm_sec; return dt; } @@ -88,8 +90,10 @@ time_t datetime_mktime(datetime_t* dt) { struct tm tm; time_t ts; time(&ts); - struct tm* ptm = localtime(&ts); - memcpy(&tm, ptm, sizeof(struct tm)); + static _Thread_local struct tm ptm; + localtime_r(&ts,&ptm); + + memcpy(&tm, &ptm, sizeof(struct tm)); tm.tm_year = dt->year - 1900; tm.tm_mon = dt->month - 1; tm.tm_mday = dt->day; @@ -225,10 +229,10 @@ time_t cron_next_timeout(int minute, int hour, int day, int week, int month) { MONTHLY, YEARLY, } period_type = MINUTELY; - struct tm tm; time_t tt; time(&tt); - tm = *localtime(&tt); + static _Thread_local struct tm tm; + localtime_r(&tt,&tm); time_t tt_round = 0; tm.tm_sec = 0;