Browse Source

[libc] 优化nanosleep函数

Meco Man 3 years ago
parent
commit
29c19f7ffd

+ 4 - 1
components/libc/compilers/common/sys/time.h

@@ -79,6 +79,10 @@ char* ctime(const time_t* tim_p);
 time_t time(time_t* t);
 time_t time(time_t* t);
 #endif
 #endif
 
 
+#ifdef RT_USING_POSIX_DELAY
+int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
+#endif /* RT_USING_POSIX_DELAY */
+
 #ifdef RT_USING_POSIX_CLOCK
 #ifdef RT_USING_POSIX_CLOCK
 /* POSIX clock and timer */
 /* POSIX clock and timer */
 #define MILLISECOND_PER_SECOND  1000UL
 #define MILLISECOND_PER_SECOND  1000UL
@@ -110,7 +114,6 @@ int clock_getres  (clockid_t clockid, struct timespec *res);
 int clock_gettime (clockid_t clockid, struct timespec *tp);
 int clock_gettime (clockid_t clockid, struct timespec *tp);
 int clock_settime (clockid_t clockid, const struct timespec *tp);
 int clock_settime (clockid_t clockid, const struct timespec *tp);
 int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp);
 int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp);
-int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
 int rt_timespec_to_tick(const struct timespec *time);
 int rt_timespec_to_tick(const struct timespec *time);
 #endif /* RT_USING_POSIX_CLOCK */
 #endif /* RT_USING_POSIX_CLOCK */
 
 

+ 11 - 28
components/libc/compilers/common/time.c

@@ -488,8 +488,18 @@ RTM_EXPORT(settimeofday);
 RTM_EXPORT(difftime);
 RTM_EXPORT(difftime);
 RTM_EXPORT(strftime);
 RTM_EXPORT(strftime);
 
 
-#ifdef RT_USING_POSIX_CLOCK
+#ifdef RT_USING_POSIX_DELAY
 #include <delay.h>
 #include <delay.h>
+int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
+{
+    sleep(rqtp->tv_sec);
+    ndelay(rqtp->tv_nsec);
+    return 0;
+}
+RTM_EXPORT(nanosleep);
+#endif /* RT_USING_POSIX_DELAY */
+
+#ifdef RT_USING_POSIX_CLOCK
 #ifdef RT_USING_RTC
 #ifdef RT_USING_RTC
 static volatile struct timeval _timevalue;
 static volatile struct timeval _timevalue;
 static int _rt_clock_time_system_init()
 static int _rt_clock_time_system_init()
@@ -670,33 +680,6 @@ int clock_settime(clockid_t clockid, const struct timespec *tp)
 }
 }
 RTM_EXPORT(clock_settime);
 RTM_EXPORT(clock_settime);
 
 
-int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
-{
-    uint32_t time_ms = rqtp->tv_sec * 1000;
-    uint32_t time_us = rqtp->tv_nsec / 1000;
-
-    time_ms += time_us / 1000 ;
-    time_us = time_us % 1000;
-
-    if (rt_thread_self() != RT_NULL)
-    {
-        rt_thread_mdelay(time_ms);
-    }
-    else  /* scheduler has not run yet */
-    {
-        while(time_ms > 0)
-        {
-            udelay(1000u);
-            time_ms -= 1;
-        }
-    }
-
-    udelay(time_us);
-
-    return 0;
-}
-RTM_EXPORT(nanosleep);
-
 int rt_timespec_to_tick(const struct timespec *time)
 int rt_timespec_to_tick(const struct timespec *time)
 {
 {
     int tick;
     int tick;