Browse Source

内核态usleep

fslongjin 2 years ago
parent
commit
e7fb6df203
3 changed files with 32 additions and 1 deletions
  1. 8 0
      kernel/common/time.h
  2. 17 1
      kernel/time/sleep.c
  3. 7 0
      kernel/time/sleep.h

+ 8 - 0
kernel/common/time.h

@@ -37,6 +37,14 @@ struct timespec
  */
 extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
 
+/**
+ * @brief 睡眠指定时间
+ *
+ * @param usec 微秒
+ * @return int
+ */
+extern int usleep(useconds_t usec);
+
 /**
  * @brief 获取当前的CPU时间
  * 

+ 17 - 1
kernel/time/sleep.c

@@ -59,4 +59,20 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
     }
 
     return 0;
-}
+}
+
+/**
+ * @brief 睡眠指定时间
+ *
+ * @param usec 微秒
+ * @return int
+ */
+int usleep(useconds_t usec)
+{
+    struct timespec ts = {
+        tv_sec : (long int)(usec / 1000000),
+        tv_nsec : (long int)(usec % 1000000) * 1000UL
+    };
+
+    return nanosleep(&ts, NULL);
+}

+ 7 - 0
kernel/time/sleep.h

@@ -14,3 +14,10 @@
  */
 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
 
+/**
+ * @brief 睡眠指定时间
+ *
+ * @param usec 微秒
+ * @return int
+ */
+int usleep(useconds_t usec);