From b05bce309031ba2fe7c7cd044af95e4ceb410fcb Mon Sep 17 00:00:00 2001 From: Jiuzhu Dong Date: Fri, 30 Jul 2021 17:07:51 +0800 Subject: [PATCH] tzset: avoid taking semaphore in interrupt for non-kernel mode Change-Id: Ic72819eddb38621751e462fe32e9e433288fb33c Signed-off-by: Jiuzhu Dong --- libs/libc/time/lib_localtime.c | 40 +++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c index e94b6239fcd..fa01389a5d2 100644 --- a/libs/libc/time/lib_localtime.c +++ b/libs/libc/time/lib_localtime.c @@ -1817,19 +1817,28 @@ static FAR struct tm *localsub(FAR const time_t *timep, static FAR struct tm *gmtsub(FAR const time_t *timep, int_fast32_t offset, FAR struct tm *tmp) { - tz_semtake(&g_gmt_sem); - if (!g_gmt_isset) { - g_gmt_ptr = lib_malloc(sizeof *g_gmt_ptr); - if (g_gmt_ptr != NULL) +#ifndef __KERNEL__ + if (up_interrupt_context()) { - gmtload(g_gmt_ptr); - g_gmt_isset = 1; + return NULL; } - } +#endif - tz_semgive(&g_gmt_sem); + tz_semtake(&g_gmt_sem); + if (!g_gmt_isset) + { + g_gmt_ptr = lib_malloc(sizeof *g_gmt_ptr); + if (g_gmt_ptr != NULL) + { + gmtload(g_gmt_ptr); + g_gmt_isset = 1; + } + } + + tz_semgive(&g_gmt_sem); + } tmp->tm_zone = GMT; return timesub(timep, offset, g_gmt_ptr, tmp); @@ -2528,9 +2537,20 @@ void tzset(void) { FAR const char *name; - tz_semtake(&g_lcl_sem); - name = getenv("TZ"); + if (g_lcl_isset > 0 && name && strcmp(g_lcl_tzname, name) == 0) + { + return; + } + +#ifndef __KERNEL__ + if (up_interrupt_context()) + { + return; + } +#endif + + tz_semtake(&g_lcl_sem); if (name == NULL) { tzsetwall();