From 62ff77d4932bb81f5acaf8092cbb97e68de2a9ba Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 22 Jul 2021 00:35:08 +0800 Subject: [libc][time] 重命名非标准libc函数 并 调整time.h定义位置 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/sys/time.h | 42 ++++++++++++++--------------- components/libc/compilers/common/time.c | 23 ++++++++++------ components/libc/pthreads/mqueue.c | 2 +- components/libc/pthreads/pthread_cond.c | 2 +- components/libc/pthreads/semaphore.c | 2 +- components/libc/signal/posix_signal.c | 2 +- 6 files changed, 40 insertions(+), 33 deletions(-) (limited to 'components') diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 4ca665c536..c2320429e0 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -19,6 +19,7 @@ extern "C" { #endif +/* timezone */ #define DST_NONE 0 /* not on dst */ #define DST_USA 1 /* USA style dst */ #define DST_AUST 2 /* Australian style dst */ @@ -31,12 +32,21 @@ extern "C" { #define DST_TUR 9 /* Turkey */ #define DST_AUSTALT 10 /* Australian style with shift in 1986 */ -#ifndef _TIMEVAL_DEFINED -#define _TIMEVAL_DEFINED +struct timezone { + int tz_minuteswest; /* minutes west of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +void rt_tz_set(rt_int8_t tz); +rt_int8_t rt_tz_get(void); +rt_int8_t rt_tz_is_dst(void); + /* * Structure returned by gettimeofday(2) system call, * and used in other calls. */ +#ifndef _TIMEVAL_DEFINED +#define _TIMEVAL_DEFINED #if !(defined(_WIN32)) struct timeval { long tv_sec; /* seconds */ @@ -45,18 +55,6 @@ struct timeval { #endif #endif /* _TIMEVAL_DEFINED */ -#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) && !(defined(__ICCARM__) && (__VER__ >= 8010001)) && !defined(_WIN32) -struct timespec { - time_t tv_sec; /* seconds */ - long tv_nsec; /* and nanoseconds */ -}; -#endif - -struct timezone { - int tz_minuteswest; /* minutes west of Greenwich */ - int tz_dsttime; /* type of dst correction */ -}; - int stime(const time_t *t); time_t timegm(struct tm * const t); int gettimeofday(struct timeval *tv, struct timezone *tz); @@ -67,6 +65,14 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r); #ifdef RT_USING_POSIX #include + +#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) && !(defined(__ICCARM__) && (__VER__ >= 8010001)) && !defined(_WIN32) +struct timespec { + time_t tv_sec; /* seconds */ + long tv_nsec; /* and nanoseconds */ +}; +#endif + /* posix clock and timer */ #define MILLISECOND_PER_SECOND 1000UL #define MICROSECOND_PER_SECOND 1000000UL @@ -96,15 +102,9 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r); int clock_getres (clockid_t clockid, struct timespec *res); int clock_gettime (clockid_t clockid, struct timespec *tp); int clock_settime (clockid_t clockid, const struct timespec *tp); -int clock_time_to_tick(const struct timespec *time); +int rt_timespec_to_tick(const struct timespec *time); #endif /* RT_USING_POSIX */ - -/* timezone APIs (Not standard LIBC APIs) */ -void rt_tz_set(rt_int8_t tz); -rt_int8_t rt_tz_get(void); -rt_int8_t rt_tz_is_dst(void); - #ifdef __cplusplus } #endif diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index 8f4856a330..6e4ec58950 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -465,8 +465,8 @@ RTM_EXPORT(difftime); RTM_EXPORT(strftime); #ifdef RT_USING_POSIX -static struct timeval _timevalue; -static int clock_time_system_init() +static volatile struct timeval _timevalue; +static int _rt_clock_time_system_init() { time_t time; rt_tick_t tick; @@ -493,7 +493,7 @@ static int clock_time_system_init() return 0; } -INIT_COMPONENT_EXPORT(clock_time_system_init); +INIT_COMPONENT_EXPORT(_rt_clock_time_system_init); int clock_getres(clockid_t clockid, struct timespec *res) { @@ -614,7 +614,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) } RTM_EXPORT(clock_settime); -int clock_time_to_tick(const struct timespec *time) +int rt_timespec_to_tick(const struct timespec *time) { int tick; int nsecond, second; @@ -645,19 +645,19 @@ int clock_time_to_tick(const struct timespec *time) return tick; } -RTM_EXPORT(clock_time_to_tick); +RTM_EXPORT(rt_timespec_to_tick); #endif /* RT_USING_POSIX */ -/* timezone APIs (Not standard LIBC APIs) */ +/* timezone */ #ifndef RT_LIBC_DEFAULT_TIMEZONE #define RT_LIBC_DEFAULT_TIMEZONE 8 #endif #include -volatile static rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE; +static volatile rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE; void rt_tz_set(rt_int8_t tz) { @@ -669,7 +669,14 @@ void rt_tz_set(rt_int8_t tz) rt_int8_t rt_tz_get(void) { - return rt_current_timezone; + rt_int8_t tz; + register rt_base_t level; + + level = rt_hw_interrupt_disable(); + tz = rt_current_timezone; + rt_hw_interrupt_enable(level); + + return tz; } rt_int8_t rt_tz_is_dst(void) diff --git a/components/libc/pthreads/mqueue.c b/components/libc/pthreads/mqueue.c index 1af0ab2919..ce2ff43e23 100644 --- a/components/libc/pthreads/mqueue.c +++ b/components/libc/pthreads/mqueue.c @@ -243,7 +243,7 @@ ssize_t mq_timedreceive(mqd_t mqdes, return -1; } - tick = clock_time_to_tick(abs_timeout); + tick = rt_timespec_to_tick(abs_timeout); result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, tick); if (result == RT_EOK) diff --git a/components/libc/pthreads/pthread_cond.c b/components/libc/pthreads/pthread_cond.c index 59c271bf6e..9bf49704ed 100644 --- a/components/libc/pthreads/pthread_cond.c +++ b/components/libc/pthreads/pthread_cond.c @@ -222,7 +222,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond, int timeout; rt_err_t result; - timeout = clock_time_to_tick(abstime); + timeout = rt_timespec_to_tick(abstime); result = _pthread_cond_timedwait(cond, mutex, timeout); if (result == RT_EOK) return 0; diff --git a/components/libc/pthreads/semaphore.c b/components/libc/pthreads/semaphore.c index 199b33bfe8..5e59ae850f 100644 --- a/components/libc/pthreads/semaphore.c +++ b/components/libc/pthreads/semaphore.c @@ -319,7 +319,7 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) return EINVAL; /* calculate os tick */ - tick = clock_time_to_tick(abs_timeout); + tick = rt_timespec_to_tick(abs_timeout); result = rt_sem_take(sem->sem, tick); if (result == -RT_ETIMEOUT) diff --git a/components/libc/signal/posix_signal.c b/components/libc/signal/posix_signal.c index 1cb561d428..d664feb24d 100644 --- a/components/libc/signal/posix_signal.c +++ b/components/libc/signal/posix_signal.c @@ -83,7 +83,7 @@ int sigtimedwait(const sigset_t *set, siginfo_t *info, if (timeout) { extern int clock_time_to_tick(const struct timespec *time); - tick = clock_time_to_tick(timeout); + tick = rt_timespec_to_tick(timeout); } #endif -- cgit v1.3.1 From 4ce6b2f5de05ad9a31448a5c1b5dd8b1863472d4 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 22 Jul 2021 01:19:49 +0800 Subject: [libc][time] 对posix相关函数 获取时间结果的判断逻辑做出调整 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/time.c | 59 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'components') diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index 6e4ec58950..45bedacf68 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -465,38 +465,42 @@ RTM_EXPORT(difftime); RTM_EXPORT(strftime); #ifdef RT_USING_POSIX + +#ifdef RT_USING_RTC static volatile struct timeval _timevalue; static int _rt_clock_time_system_init() { - time_t time; + time_t time = 0; rt_tick_t tick; rt_device_t device; - time = 0; - -#ifdef RT_USING_RTC device = rt_device_find("rtc"); if (device != RT_NULL) { /* get realtime seconds */ - rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time); + if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK) + { + /* get tick */ + tick = rt_tick_get(); + _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK; + _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1; + return 0; + } } -#else - LOG_W("Cannot find a RTC device to provide time!"); -#endif - - /* get tick */ - tick = rt_tick_get(); - - _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK; - _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1; - return 0; + _timevalue.tv_usec = 0; + _timevalue.tv_sec = 0; + return -1; } INIT_COMPONENT_EXPORT(_rt_clock_time_system_init); +#endif /* RT_USING_RTC */ int clock_getres(clockid_t clockid, struct timespec *res) { +#ifndef RT_USING_RTC + LOG_W("Cannot find a RTC device to save time!"); + return -1; +#else int ret = 0; if (res == RT_NULL) @@ -526,11 +530,16 @@ int clock_getres(clockid_t clockid, struct timespec *res) } return ret; +#endif /* RT_USING_RTC */ } RTM_EXPORT(clock_getres); int clock_gettime(clockid_t clockid, struct timespec *tp) { +#ifndef RT_USING_RTC + LOG_W("Cannot find a RTC device to save time!"); + return -1; +#else int ret = 0; if (tp == RT_NULL) @@ -571,11 +580,16 @@ int clock_gettime(clockid_t clockid, struct timespec *tp) } return ret; +#endif /* RT_USING_RTC */ } RTM_EXPORT(clock_gettime); int clock_settime(clockid_t clockid, const struct timespec *tp) { +#ifndef RT_USING_RTC + LOG_W("Cannot find a RTC device to save time!"); + return -1; +#else int second; rt_tick_t tick; rt_device_t device; @@ -583,7 +597,6 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL)) { rt_set_errno(EINVAL); - return -1; } @@ -596,21 +609,19 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK; _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1; -#ifdef RT_USING_RTC /* update for RTC device */ device = rt_device_find("rtc"); if (device != RT_NULL) { /* set realtime seconds */ - rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second); + if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK) + { + return 0; + } } - else -#else - LOG_W("Cannot find a RTC device to save time!"); -#endif - return -1; - return 0; + return -1; +#endif /* RT_USING_RTC */ } RTM_EXPORT(clock_settime); -- cgit v1.3.1 From 19c5088bb8b6cfdc9e3cecc63b6f9c116a799c49 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 22 Jul 2021 01:40:37 +0800 Subject: [libc][time] 修复posix相关函数没有进行临界区保护的问题 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/time.c | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'components') diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index 45bedacf68..50f6d4c71e 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -24,7 +24,7 @@ #include "sys/time.h" #include #include - +#include #ifdef RT_USING_DEVICE #include #endif @@ -470,6 +470,7 @@ RTM_EXPORT(strftime); static volatile struct timeval _timevalue; static int _rt_clock_time_system_init() { + register rt_base_t level; time_t time = 0; rt_tick_t tick; rt_device_t device; @@ -480,16 +481,20 @@ static int _rt_clock_time_system_init() /* get realtime seconds */ if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK) { - /* get tick */ - tick = rt_tick_get(); + level = rt_hw_interrupt_disable(); + tick = rt_tick_get(); /* get tick */ _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK; _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1; + rt_hw_interrupt_enable(level); return 0; } } + level = rt_hw_interrupt_disable(); _timevalue.tv_usec = 0; _timevalue.tv_sec = 0; + rt_hw_interrupt_enable(level); + return -1; } INIT_COMPONENT_EXPORT(_rt_clock_time_system_init); @@ -552,11 +557,14 @@ int clock_gettime(clockid_t clockid, struct timespec *tp) { case CLOCK_REALTIME: { - /* get tick */ - int tick = rt_tick_get(); + int tick; + register rt_base_t level; + level = rt_hw_interrupt_disable(); + tick = rt_tick_get(); /* get tick */ tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND; tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000; + rt_hw_interrupt_enable(level); } break; @@ -590,6 +598,7 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) LOG_W("Cannot find a RTC device to save time!"); return -1; #else + register rt_base_t level; int second; rt_tick_t tick; rt_device_t device; @@ -602,12 +611,13 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) /* get second */ second = tp->tv_sec; - /* get tick */ - tick = rt_tick_get(); + level = rt_hw_interrupt_disable(); + tick = rt_tick_get(); /* get tick */ /* update timevalue */ _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK; _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1; + rt_hw_interrupt_enable(level); /* update for RTC device */ device = rt_device_find("rtc"); @@ -666,8 +676,6 @@ RTM_EXPORT(rt_timespec_to_tick); #define RT_LIBC_DEFAULT_TIMEZONE 8 #endif -#include - static volatile rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE; void rt_tz_set(rt_int8_t tz) @@ -680,14 +688,7 @@ void rt_tz_set(rt_int8_t tz) rt_int8_t rt_tz_get(void) { - rt_int8_t tz; - register rt_base_t level; - - level = rt_hw_interrupt_disable(); - tz = rt_current_timezone; - rt_hw_interrupt_enable(level); - - return tz; + return rt_current_timezone; } rt_int8_t rt_tz_is_dst(void) -- cgit v1.3.1