diff options
| author | Meco Man <[email protected]> | 2021-02-12 01:30:41 +0800 |
|---|---|---|
| committer | Meco Man <[email protected]> | 2021-02-12 01:30:41 +0800 |
| commit | 291fe36139788cf31bd5c6cdb2ac3919dfa0b0cb (patch) | |
| tree | 3afe6c1bf30ff9b588dc81e70d419b80b242e716 /components/libc/pthreads/pthread.c | |
| parent | f0805a7973ffe62e459da7748cbada62bccf98b9 (diff) | |
[pthread][libc] move 'clock_time' to libc
Diffstat (limited to 'components/libc/pthreads/pthread.c')
| -rw-r--r-- | components/libc/pthreads/pthread.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c index 19a511b45b..cec76a9691 100644 --- a/components/libc/pthreads/pthread.c +++ b/components/libc/pthreads/pthread.c @@ -13,6 +13,7 @@ #include <rthw.h> #include <pthread.h> #include <sched.h> +#include <sys/time.h> #include "pthread_internal.h" RT_DEFINE_SPINLOCK(pth_lock); @@ -688,3 +689,35 @@ int pthread_cancel(pthread_t thread) } 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); |
