Skip to content

Commit c3dab99

Browse files
authored
Merge pull request #4880 from mysterywolf/tz
[libc][time] 修复若干问题
2 parents 7de647c + 2589d24 commit c3dab99

File tree

6 files changed

+81
-62
lines changed

6 files changed

+81
-62
lines changed

components/libc/compilers/common/sys/time.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
extern "C" {
2020
#endif
2121

22+
/* timezone */
2223
#define DST_NONE 0 /* not on dst */
2324
#define DST_USA 1 /* USA style dst */
2425
#define DST_AUST 2 /* Australian style dst */
@@ -31,12 +32,21 @@ extern "C" {
3132
#define DST_TUR 9 /* Turkey */
3233
#define DST_AUSTALT 10 /* Australian style with shift in 1986 */
3334

34-
#ifndef _TIMEVAL_DEFINED
35-
#define _TIMEVAL_DEFINED
35+
struct timezone {
36+
int tz_minuteswest; /* minutes west of Greenwich */
37+
int tz_dsttime; /* type of dst correction */
38+
};
39+
40+
void rt_tz_set(rt_int8_t tz);
41+
rt_int8_t rt_tz_get(void);
42+
rt_int8_t rt_tz_is_dst(void);
43+
3644
/*
3745
* Structure returned by gettimeofday(2) system call,
3846
* and used in other calls.
3947
*/
48+
#ifndef _TIMEVAL_DEFINED
49+
#define _TIMEVAL_DEFINED
4050
#if !(defined(_WIN32))
4151
struct timeval {
4252
long tv_sec; /* seconds */
@@ -45,18 +55,6 @@ struct timeval {
4555
#endif
4656
#endif /* _TIMEVAL_DEFINED */
4757

48-
#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) && !(defined(__ICCARM__) && (__VER__ >= 8010001)) && !defined(_WIN32)
49-
struct timespec {
50-
time_t tv_sec; /* seconds */
51-
long tv_nsec; /* and nanoseconds */
52-
};
53-
#endif
54-
55-
struct timezone {
56-
int tz_minuteswest; /* minutes west of Greenwich */
57-
int tz_dsttime; /* type of dst correction */
58-
};
59-
6058
int stime(const time_t *t);
6159
time_t timegm(struct tm * const t);
6260
int gettimeofday(struct timeval *tv, struct timezone *tz);
@@ -67,6 +65,14 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r);
6765

6866
#ifdef RT_USING_POSIX
6967
#include <sys/types.h>
68+
69+
#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) && !(defined(__ICCARM__) && (__VER__ >= 8010001)) && !defined(_WIN32)
70+
struct timespec {
71+
time_t tv_sec; /* seconds */
72+
long tv_nsec; /* and nanoseconds */
73+
};
74+
#endif
75+
7076
/* posix clock and timer */
7177
#define MILLISECOND_PER_SECOND 1000UL
7278
#define MICROSECOND_PER_SECOND 1000000UL
@@ -96,15 +102,9 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r);
96102
int clock_getres (clockid_t clockid, struct timespec *res);
97103
int clock_gettime (clockid_t clockid, struct timespec *tp);
98104
int clock_settime (clockid_t clockid, const struct timespec *tp);
99-
int clock_time_to_tick(const struct timespec *time);
105+
int rt_timespec_to_tick(const struct timespec *time);
100106
#endif /* RT_USING_POSIX */
101107

102-
103-
/* timezone APIs (Not standard LIBC APIs) */
104-
void rt_tz_set(rt_int8_t tz);
105-
rt_int8_t rt_tz_get(void);
106-
rt_int8_t rt_tz_is_dst(void);
107-
108108
#ifdef __cplusplus
109109
}
110110
#endif

components/libc/compilers/common/time.c

Lines changed: 56 additions & 37 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
@@ -465,38 +465,47 @@ RTM_EXPORT(difftime);
465465
RTM_EXPORT(strftime);
466466

467467
#ifdef RT_USING_POSIX
468-
static struct timeval _timevalue;
469-
static int clock_time_system_init()
468+
469+
#ifdef RT_USING_RTC
470+
static volatile struct timeval _timevalue;
471+
static int _rt_clock_time_system_init()
470472
{
471-
time_t time;
473+
register rt_base_t level;
474+
time_t time = 0;
472475
rt_tick_t tick;
473476
rt_device_t device;
474477

475-
time = 0;
476-
477-
#ifdef RT_USING_RTC
478478
device = rt_device_find("rtc");
479479
if (device != RT_NULL)
480480
{
481481
/* get realtime seconds */
482-
rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
482+
if(rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time) == RT_EOK)
483+
{
484+
level = rt_hw_interrupt_disable();
485+
tick = rt_tick_get(); /* get tick */
486+
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
487+
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
488+
rt_hw_interrupt_enable(level);
489+
return 0;
490+
}
483491
}
484-
#else
485-
LOG_W("Cannot find a RTC device to provide time!");
486-
#endif
487-
488-
/* get tick */
489-
tick = rt_tick_get();
490492

491-
_timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
492-
_timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
493+
level = rt_hw_interrupt_disable();
494+
_timevalue.tv_usec = 0;
495+
_timevalue.tv_sec = 0;
496+
rt_hw_interrupt_enable(level);
493497

494-
return 0;
498+
return -1;
495499
}
496-
INIT_COMPONENT_EXPORT(clock_time_system_init);
500+
INIT_COMPONENT_EXPORT(_rt_clock_time_system_init);
501+
#endif /* RT_USING_RTC */
497502

498503
int clock_getres(clockid_t clockid, struct timespec *res)
499504
{
505+
#ifndef RT_USING_RTC
506+
LOG_W("Cannot find a RTC device to save time!");
507+
return -1;
508+
#else
500509
int ret = 0;
501510

502511
if (res == RT_NULL)
@@ -526,11 +535,16 @@ int clock_getres(clockid_t clockid, struct timespec *res)
526535
}
527536

528537
return ret;
538+
#endif /* RT_USING_RTC */
529539
}
530540
RTM_EXPORT(clock_getres);
531541

532542
int clock_gettime(clockid_t clockid, struct timespec *tp)
533543
{
544+
#ifndef RT_USING_RTC
545+
LOG_W("Cannot find a RTC device to save time!");
546+
return -1;
547+
#else
534548
int ret = 0;
535549

536550
if (tp == RT_NULL)
@@ -543,11 +557,14 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
543557
{
544558
case CLOCK_REALTIME:
545559
{
546-
/* get tick */
547-
int tick = rt_tick_get();
560+
int tick;
561+
register rt_base_t level;
548562

563+
level = rt_hw_interrupt_disable();
564+
tick = rt_tick_get(); /* get tick */
549565
tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
550566
tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
567+
rt_hw_interrupt_enable(level);
551568
}
552569
break;
553570

@@ -571,50 +588,54 @@ int clock_gettime(clockid_t clockid, struct timespec *tp)
571588
}
572589

573590
return ret;
591+
#endif /* RT_USING_RTC */
574592
}
575593
RTM_EXPORT(clock_gettime);
576594

577595
int clock_settime(clockid_t clockid, const struct timespec *tp)
578596
{
597+
#ifndef RT_USING_RTC
598+
LOG_W("Cannot find a RTC device to save time!");
599+
return -1;
600+
#else
601+
register rt_base_t level;
579602
int second;
580603
rt_tick_t tick;
581604
rt_device_t device;
582605

583606
if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
584607
{
585608
rt_set_errno(EINVAL);
586-
587609
return -1;
588610
}
589611

590612
/* get second */
591613
second = tp->tv_sec;
592-
/* get tick */
593-
tick = rt_tick_get();
594614

615+
level = rt_hw_interrupt_disable();
616+
tick = rt_tick_get(); /* get tick */
595617
/* update timevalue */
596618
_timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
597619
_timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
620+
rt_hw_interrupt_enable(level);
598621

599-
#ifdef RT_USING_RTC
600622
/* update for RTC device */
601623
device = rt_device_find("rtc");
602624
if (device != RT_NULL)
603625
{
604626
/* set realtime seconds */
605-
rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
627+
if(rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second) == RT_EOK)
628+
{
629+
return 0;
630+
}
606631
}
607-
else
608-
#else
609-
LOG_W("Cannot find a RTC device to save time!");
610-
#endif
611-
return -1;
612632

613-
return 0;
633+
return -1;
634+
#endif /* RT_USING_RTC */
614635
}
615636
RTM_EXPORT(clock_settime);
616637

617-
int clock_time_to_tick(const struct timespec *time)
638+
int rt_timespec_to_tick(const struct timespec *time)
618639
{
619640
int tick;
620641
int nsecond, second;
@@ -645,19 +666,17 @@ int clock_time_to_tick(const struct timespec *time)
645666

646667
return tick;
647668
}
648-
RTM_EXPORT(clock_time_to_tick);
669+
RTM_EXPORT(rt_timespec_to_tick);
649670

650671
#endif /* RT_USING_POSIX */
651672

652673

653-
/* timezone APIs (Not standard LIBC APIs) */
674+
/* timezone */
654675
#ifndef RT_LIBC_DEFAULT_TIMEZONE
655676
#define RT_LIBC_DEFAULT_TIMEZONE 8
656677
#endif
657678

658-
#include <rthw.h>
659-
660-
volatile static rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
679+
static volatile rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE;
661680

662681
void rt_tz_set(rt_int8_t tz)
663682
{

components/libc/pthreads/mqueue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ ssize_t mq_timedreceive(mqd_t mqdes,
244244
return -1;
245245
}
246246

247-
tick = clock_time_to_tick(abs_timeout);
247+
tick = rt_timespec_to_tick(abs_timeout);
248248

249249
result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, tick);
250250
if (result == RT_EOK)

components/libc/pthreads/pthread_cond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond,
222222
int timeout;
223223
rt_err_t result;
224224

225-
timeout = clock_time_to_tick(abstime);
225+
timeout = rt_timespec_to_tick(abstime);
226226
result = _pthread_cond_timedwait(cond, mutex, timeout);
227227
if (result == RT_EOK)
228228
return 0;

components/libc/pthreads/semaphore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
319319
return EINVAL;
320320

321321
/* calculate os tick */
322-
tick = clock_time_to_tick(abs_timeout);
322+
tick = rt_timespec_to_tick(abs_timeout);
323323

324324
result = rt_sem_take(sem->sem, tick);
325325
if (result == -RT_ETIMEOUT)

components/libc/signal/posix_signal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int sigtimedwait(const sigset_t *set, siginfo_t *info,
8383

8484
if (timeout)
8585
{
86-
tick = clock_time_to_tick(timeout);
86+
tick = rt_timespec_to_tick(timeout);
8787
}
8888

8989
ret = rt_signal_wait(set, info, tick);

0 commit comments

Comments
 (0)