Skip to content

Commit 29c19f7

Browse files
mysterywolfBernardXiong
authored andcommitted
[libc] 优化nanosleep函数
1 parent 8565fe2 commit 29c19f7

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ char* ctime(const time_t* tim_p);
7979
time_t time(time_t* t);
8080
#endif
8181

82+
#ifdef RT_USING_POSIX_DELAY
83+
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
84+
#endif /* RT_USING_POSIX_DELAY */
85+
8286
#ifdef RT_USING_POSIX_CLOCK
8387
/* POSIX clock and timer */
8488
#define MILLISECOND_PER_SECOND 1000UL
@@ -110,7 +114,6 @@ int clock_getres (clockid_t clockid, struct timespec *res);
110114
int clock_gettime (clockid_t clockid, struct timespec *tp);
111115
int clock_settime (clockid_t clockid, const struct timespec *tp);
112116
int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp);
113-
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
114117
int rt_timespec_to_tick(const struct timespec *time);
115118
#endif /* RT_USING_POSIX_CLOCK */
116119

components/libc/compilers/common/time.c

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,18 @@ RTM_EXPORT(settimeofday);
488488
RTM_EXPORT(difftime);
489489
RTM_EXPORT(strftime);
490490

491-
#ifdef RT_USING_POSIX_CLOCK
491+
#ifdef RT_USING_POSIX_DELAY
492492
#include <delay.h>
493+
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
494+
{
495+
sleep(rqtp->tv_sec);
496+
ndelay(rqtp->tv_nsec);
497+
return 0;
498+
}
499+
RTM_EXPORT(nanosleep);
500+
#endif /* RT_USING_POSIX_DELAY */
501+
502+
#ifdef RT_USING_POSIX_CLOCK
493503
#ifdef RT_USING_RTC
494504
static volatile struct timeval _timevalue;
495505
static int _rt_clock_time_system_init()
@@ -670,33 +680,6 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
670680
}
671681
RTM_EXPORT(clock_settime);
672682

673-
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
674-
{
675-
uint32_t time_ms = rqtp->tv_sec * 1000;
676-
uint32_t time_us = rqtp->tv_nsec / 1000;
677-
678-
time_ms += time_us / 1000 ;
679-
time_us = time_us % 1000;
680-
681-
if (rt_thread_self() != RT_NULL)
682-
{
683-
rt_thread_mdelay(time_ms);
684-
}
685-
else /* scheduler has not run yet */
686-
{
687-
while(time_ms > 0)
688-
{
689-
udelay(1000u);
690-
time_ms -= 1;
691-
}
692-
}
693-
694-
udelay(time_us);
695-
696-
return 0;
697-
}
698-
RTM_EXPORT(nanosleep);
699-
700683
int rt_timespec_to_tick(const struct timespec *time)
701684
{
702685
int tick;

0 commit comments

Comments
 (0)