From f0d3c8ab2bfee787567f0cb93dc7de7a8a57253a Mon Sep 17 00:00:00 2001 From: ligd Date: Mon, 29 Jul 2024 17:07:46 +0800 Subject: [PATCH] usleep: use div_const to optimize the usleep Signed-off-by: ligd --- include/nuttx/clock.h | 12 ++++++++++++ libs/libc/time/lib_gettimeofday.c | 2 +- libs/libc/unistd/lib_usleep.c | 5 +++-- sched/sched/sched_cpuload_oneshot.c | 4 ++-- sched/signal/sig_ppoll.c | 4 ++-- sched/signal/sig_pselect.c | 2 +- sched/signal/sig_usleep.c | 4 ++-- 7 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h index e8d7579903..ffd8004d2a 100644 --- a/include/nuttx/clock.h +++ b/include/nuttx/clock.h @@ -206,10 +206,22 @@ #define TICK2HSEC(tick) div_const_roundnearest(tick, TICK_PER_HSEC) #define TICK2SEC(tick) div_const_roundnearest(tick, TICK_PER_SEC) +/* MSEC2SEC */ + +#define MSEC2SEC(usec) div_const(msec, MSEC_PER_SEC) + /* USEC2SEC */ #define USEC2SEC(usec) div_const(usec, USEC_PER_SEC) +/* NSEC2USEC */ + +#define NSEC2USEC(nsec) div_const(nsec, NSEC_PER_USEC) + +/* NSEC2MSEC */ + +#define NSEC2MSEC(nsec) div_const(nsec, NSEC_PER_MSEC) + #if defined(CONFIG_DEBUG_SCHED) && defined(CONFIG_SYSTEM_TIME64) && \ !defined(CONFIG_SCHED_TICKLESS) /* Initial system timer ticks value close to maximum 32-bit value, to test diff --git a/libs/libc/time/lib_gettimeofday.c b/libs/libc/time/lib_gettimeofday.c index 78ad7e7a7a..fa815fde23 100644 --- a/libs/libc/time/lib_gettimeofday.c +++ b/libs/libc/time/lib_gettimeofday.c @@ -78,7 +78,7 @@ int gettimeofday(FAR struct timeval *tv, FAR struct timezone *tz) /* Convert the struct timespec to a struct timeval */ tv->tv_sec = ts.tv_sec; - tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; + tv->tv_usec = NSEC2USEC(ts.tv_nsec); } return ret; diff --git a/libs/libc/unistd/lib_usleep.c b/libs/libc/unistd/lib_usleep.c index a3f78ba453..06a45c4291 100644 --- a/libs/libc/unistd/lib_usleep.c +++ b/libs/libc/unistd/lib_usleep.c @@ -27,6 +27,7 @@ #include #include #include +#include /**************************************************************************** * Public Functions @@ -101,9 +102,9 @@ int usleep(useconds_t usec) { /* Let clock_nanosleep() do all of the work. */ - sec = usec / 1000000; + sec = USEC2SEC(usec); rqtp.tv_sec = sec; - rqtp.tv_nsec = (usec - (sec * 1000000)) * 1000; + rqtp.tv_nsec = (usec - (sec * USEC_PER_SEC)) * NSEC_PER_USEC; ret = clock_nanosleep(CLOCK_REALTIME, 0, &rqtp, NULL); } diff --git a/sched/sched/sched_cpuload_oneshot.c b/sched/sched/sched_cpuload_oneshot.c index d6728ad4f0..cb87fa97a8 100644 --- a/sched/sched/sched_cpuload_oneshot.c +++ b/sched/sched/sched_cpuload_oneshot.c @@ -193,7 +193,7 @@ static void nxsched_oneshot_start(void) /* Then re-start the oneshot timer */ - secs = usecs / 1000000; + secs = USEC2SEC(usecs); usecs -= 100000 * secs; ts.tv_sec = secs; @@ -304,7 +304,7 @@ void nxsched_oneshot_extclk(FAR struct oneshot_lowerhalf_s *lower) } else { - g_sched_oneshot.maxdelay = ts.tv_nsec / 1000; + g_sched_oneshot.maxdelay = NSEC2USEC(ts.tv_nsec); } tmrinfo("madelay = %ld usec\n", (long)g_sched_oneshot.maxdelay); diff --git a/sched/signal/sig_ppoll.c b/sched/signal/sig_ppoll.c index 04670954df..c320bc1967 100644 --- a/sched/signal/sig_ppoll.c +++ b/sched/signal/sig_ppoll.c @@ -117,8 +117,8 @@ int ppoll(FAR struct pollfd *fds, nfds_t nfds, if (timeout_ts) { - timeout = timeout_ts->tv_sec * 1000 + - timeout_ts->tv_nsec / 1000000; + timeout = timeout_ts->tv_sec * MSEC_PER_SEC + + NSEC2MSEC(timeout_ts->tv_nsec); } ret = poll(fds, nfds, timeout); diff --git a/sched/signal/sig_pselect.c b/sched/signal/sig_pselect.c index 30a8607577..c660e379d1 100644 --- a/sched/signal/sig_pselect.c +++ b/sched/signal/sig_pselect.c @@ -116,7 +116,7 @@ int pselect(int nfds, FAR fd_set *readfds, FAR fd_set *writefds, if (timeout) { timeval_buf.tv_sec = timeout->tv_sec; - timeval_buf.tv_usec = timeout->tv_nsec / 1000; + timeval_buf.tv_usec = NSEC2USEC(timeout->tv_nsec); timeval = &timeval_buf; } diff --git a/sched/signal/sig_usleep.c b/sched/signal/sig_usleep.c index da426623fe..99825f5e9f 100644 --- a/sched/signal/sig_usleep.c +++ b/sched/signal/sig_usleep.c @@ -77,9 +77,9 @@ int nxsig_usleep(useconds_t usec) { /* Let nxsig_nanosleep() do all of the work. */ - sec = usec / 1000000; + sec = USEC2SEC(usec); rqtp.tv_sec = sec; - rqtp.tv_nsec = (usec - (sec * 1000000)) * 1000; + rqtp.tv_nsec = (usec - (sec * USEC_PER_SEC)) * NSEC_PER_USEC; ret = nxsig_nanosleep(&rqtp, NULL); }