From 8f7ad8f9cee7a808a37b61e2dad2e4b63514cb4d 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 | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c index e94b6239fc..66e90c7ab9 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,8 +2537,14 @@ void tzset(void) { FAR const char *name; - tz_semtake(&g_lcl_sem); +#ifndef __KERNEL__ + if (up_interrupt_context()) + { + return; + } +#endif + tz_semtake(&g_lcl_sem); name = getenv("TZ"); if (name == NULL) {