From 291fe36139788cf31bd5c6cdb2ac3919dfa0b0cb Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 12 Feb 2021 01:30:41 +0800 Subject: [pthread][libc] move 'clock_time' to libc --- components/libc/compilers/common/sys/time.h | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 599b8b5262..34ca9427b9 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -10,6 +10,7 @@ #ifndef _SYS_TIME_H_ #define _SYS_TIME_H_ +#include #include #ifdef __cplusplus @@ -53,6 +54,38 @@ time_t timegm(struct tm * const t); int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); +#ifdef RT_USING_PTHREADS +/* posix clock and timer */ +#define MILLISECOND_PER_SECOND 1000UL +#define MICROSECOND_PER_SECOND 1000000UL +#define NANOSECOND_PER_SECOND 1000000000UL + +#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND) +#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND) +#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND) + +#ifndef CLOCK_REALTIME +#define CLOCK_REALTIME 1 +#endif + +#define CLOCK_CPUTIME_ID 2 + +#ifndef CLOCK_PROCESS_CPUTIME_ID +#define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID +#endif +#ifndef CLOCK_THREAD_CPUTIME_ID +#define CLOCK_THREAD_CPUTIME_ID CLOCK_CPUTIME_ID +#endif + +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC 4 +#endif + +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); +#endif /*RT_USING_PTHREADS*/ + #ifdef __cplusplus } #endif -- cgit v1.3.1 From e33e7822d7a7aaa027e511f59c5533664a02f31e Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 12 Feb 2021 02:58:44 +0800 Subject: add --- components/libc/compilers/common/sys/time.h | 1 + 1 file changed, 1 insertion(+) (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 34ca9427b9..4c522a524a 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -55,6 +55,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); #ifdef RT_USING_PTHREADS +#include /* posix clock and timer */ #define MILLISECOND_PER_SECOND 1000UL #define MICROSECOND_PER_SECOND 1000000UL -- cgit v1.3.1 From ac500f35d6a4517243e5a290e8a97b3a6c6bf3c1 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 12 Feb 2021 12:08:14 +0800 Subject: change RT_USING_PTHREADS to RT_USING_POSIX --- components/libc/compilers/common/sys/time.h | 4 ++-- components/libc/compilers/common/time.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 4c522a524a..6e74b901e5 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -54,7 +54,7 @@ time_t timegm(struct tm * const t); int gettimeofday(struct timeval *tv, struct timezone *tz); int settimeofday(const struct timeval *tv, const struct timezone *tz); -#ifdef RT_USING_PTHREADS +#ifdef RT_USING_POSIX #include /* posix clock and timer */ #define MILLISECOND_PER_SECOND 1000UL @@ -85,7 +85,7 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz); 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); -#endif /*RT_USING_PTHREADS*/ +#endif /* RT_USING_POSIX */ #ifdef __cplusplus } diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index d7048acc4a..c73bb0f26c 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -372,7 +372,7 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz) } RTM_EXPORT(settimeofday); -#ifdef RT_USING_PTHREADS +#ifdef RT_USING_POSIX static struct timeval _timevalue; static int clock_time_system_init() { @@ -512,4 +512,4 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) return 0; } RTM_EXPORT(clock_settime); -#endif /*RT_USING_PTHREADS*/ +#endif /* RT_USING_POSIX */ -- cgit v1.3.1 From 5fc59f88c1b4b145a6deeea5cd0af459e56fc4b6 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 12 Feb 2021 12:14:45 +0800 Subject: update --- components/libc/compilers/common/sys/time.h | 1 + 1 file changed, 1 insertion(+) (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index 6e74b901e5..df5b4fae93 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2020-09-07 Meco Man combine gcc armcc iccarm + * 2021-02-12 Meco Man move all definitions located in to this file */ #ifndef _SYS_TIME_H_ #define _SYS_TIME_H_ -- cgit v1.3.1 From 598aa86f750bdc96ffb427fc8507a426a99fc63f Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Fri, 12 Feb 2021 20:36:17 +0800 Subject: move clock_time_to_tick to time.c --- components/libc/compilers/common/sys/time.h | 1 + components/libc/compilers/common/time.c | 34 +++++++++++++++++++++++++++++ components/libc/pthreads/pthread.c | 33 ---------------------------- components/libc/pthreads/pthread_internal.h | 3 +-- 4 files changed, 36 insertions(+), 35 deletions(-) (limited to 'components/libc/compilers/common/sys') diff --git a/components/libc/compilers/common/sys/time.h b/components/libc/compilers/common/sys/time.h index df5b4fae93..e4d60d2d11 100644 --- a/components/libc/compilers/common/sys/time.h +++ b/components/libc/compilers/common/sys/time.h @@ -86,6 +86,7 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz); 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); #endif /* RT_USING_POSIX */ #ifdef __cplusplus diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index c73bb0f26c..c2fbd6f333 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -512,4 +512,38 @@ int clock_settime(clockid_t clockid, const struct timespec *tp) return 0; } RTM_EXPORT(clock_settime); + +int clock_time_to_tick(const struct timespec *time) +{ + int tick; + int nsecond, second; + struct timespec tp; + + RT_ASSERT(time != RT_NULL); + + tick = RT_WAITING_FOREVER; + if (time != NULL) + { + /* get current tp */ + clock_gettime(CLOCK_REALTIME, &tp); + + if ((time->tv_nsec - tp.tv_nsec) < 0) + { + nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec); + second = time->tv_sec - tp.tv_sec - 1; + } + else + { + nsecond = time->tv_nsec - tp.tv_nsec; + second = time->tv_sec - tp.tv_sec; + } + + tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND; + if (tick < 0) tick = 0; + } + + return tick; +} +RTM_EXPORT(clock_time_to_tick); + #endif /* RT_USING_POSIX */ diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c index cec76a9691..7430510bf2 100644 --- a/components/libc/pthreads/pthread.c +++ b/components/libc/pthreads/pthread.c @@ -688,36 +688,3 @@ int pthread_cancel(pthread_t thread) return 0; } RTM_EXPORT(pthread_cancel); - -int clock_time_to_tick(const struct timespec *time) -{ - int tick; - int nsecond, second; - struct timespec tp; - - RT_ASSERT(time != RT_NULL); - - tick = RT_WAITING_FOREVER; - if (time != NULL) - { - /* get current tp */ - clock_gettime(CLOCK_REALTIME, &tp); - - if ((time->tv_nsec - tp.tv_nsec) < 0) - { - nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec); - second = time->tv_sec - tp.tv_sec - 1; - } - else - { - nsecond = time->tv_nsec - tp.tv_nsec; - second = time->tv_sec - tp.tv_sec; - } - - tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND; - if (tick < 0) tick = 0; - } - - return tick; -} -RTM_EXPORT(clock_time_to_tick); diff --git a/components/libc/pthreads/pthread_internal.h b/components/libc/pthreads/pthread_internal.h index a6706ca091..240741e21a 100644 --- a/components/libc/pthreads/pthread_internal.h +++ b/components/libc/pthreads/pthread_internal.h @@ -13,6 +13,7 @@ #include #include +#include struct _pthread_cleanup { @@ -62,8 +63,6 @@ typedef struct _pthread_data _pthread_data_t; _pthread_data_t *_pthread_get_data(pthread_t thread); -int clock_time_to_tick(const struct timespec *time); - void posix_mq_system_init(void); void posix_sem_system_init(void); void pthread_key_system_init(void); -- cgit v1.3.1