Skip to content

Commit 19c5088

Browse files
committed
[libc][time] 修复posix相关函数没有进行临界区保护的问题
1 parent 4ce6b2f commit 19c5088

File tree

1 file changed

+18
-17
lines changed
  • components/libc/compilers/common

1 file changed

+18
-17
lines changed

components/libc/compilers/common/time.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "sys/time.h"
2525
#include <sys/errno.h>
2626
#include <rtthread.h>
27-
27+
#include <rthw.h>
2828
#ifdef RT_USING_DEVICE
2929
#include <rtdevice.h>
3030
#endif
@@ -470,6 +470,7 @@ RTM_EXPORT(strftime);
470470
static volatile struct timeval _timevalue;
471471
static int _rt_clock_time_system_init()
472472
{
473+
register rt_base_t level;
473474
time_t time = 0;
474475
rt_tick_t tick;
475476
rt_device_t device;
@@ -480,16 +481,20 @@ static int _rt_clock_time_system_init()
480481
/* get realtime seconds */
481482
if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
482483
{
483-
/* get tick */
484-
tick = rt_tick_get();
484+
level = rt_hw_interrupt_disable();
485+
tick = rt_tick_get(); /* get tick */
485486
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
486487
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
488+
rt_hw_interrupt_enable(level);
487489
return 0;
488490
}
489491
}
490492

493+
level = rt_hw_interrupt_disable();
491494
_timevalue.tv_usec = 0;
492495
_timevalue.tv_sec = 0;
496+
rt_hw_interrupt_enable(level);
497+
493498
return -1;
494499
}
495500
INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
@@ -552,11 +557,14 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
552557
{
553558
case CLOCK_REALTIME:
554559
{
555-
/* get tick */
556-
int tick = rt_tick_get();
560+
int tick;
561+
register rt_base_t level;
557562

563+
level = rt_hw_interrupt_disable();
564+
tick = rt_tick_get(); /* get tick */
558565
tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
559566
tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
567+
rt_hw_interrupt_enable(level);
560568
}
561569
break;
562570

@@ -590,6 +598,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
590598
LOG_W("Cannot find a RTC device to save time!");
591599
return -1;
592600
#else
601+
register rt_base_t level;
593602
int second;
594603
rt_tick_t tick;
595604
rt_device_t device;
@@ -602,12 +611,13 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
602611

603612
/* get second */
604613
second = tp->tv_sec;
605-
/* get tick */
606-
tick = rt_tick_get();
607614

615+
level = rt_hw_interrupt_disable();
616+
tick = rt_tick_get(); /* get tick */
608617
/* update timevalue */
609618
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
610619
_timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
620+
rt_hw_interrupt_enable(level);
611621

612622
/* update for RTC device */
613623
device = rt_device_find("rtc");
@@ -666,8 +676,6 @@ RTM_EXPORT(rt_timespec_to_tick);
666676
#define RT_LIBC_DEFAULT_TIMEZONE 8
667677
#endif
668678

669-
#include <rthw.h>
670-
671679
static volatile rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
672680

673681
void rt_tz_set(rt_int8_t tz)
@@ -680,14 +688,7 @@ void rt_tz_set(rt_int8_t tz)
680688

681689
rt_int8_t rt_tz_get(void)
682690
{
683-
rt_int8_t tz;
684-
register rt_base_t level;
685-
686-
level = rt_hw_interrupt_disable();
687-
tz = rt_current_timezone;
688-
rt_hw_interrupt_enable(level);
689-
690-
return tz;
691+
return rt_current_timezone;
691692
}
692693

693694
rt_int8_t rt_tz_is_dst(void)

0 commit comments

Comments
 (0)