🆕 usleep()、nanosleep()

This commit is contained in:
fslongjin
2022-07-12 12:01:51 +08:00
parent 4208c56074
commit 676260c537
21 changed files with 277 additions and 15 deletions

View File

@ -9,7 +9,7 @@ libc_sub_dirs += sysdeps/x86_64
endif
libc: unistd.o fcntl.o malloc.o errno.o printf.o stdlib.o ctype.o string.o dirent.o
libc: unistd.o fcntl.o malloc.o errno.o printf.o stdlib.o ctype.o string.o dirent.o time.o
@list='$(libc_sub_dirs)'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
@ -42,4 +42,7 @@ string.o: string.c
gcc $(CFLAGS) -c string.c -o string.o
dirent.o: dirent.c
gcc $(CFLAGS) -c dirent.c -o dirent.o
gcc $(CFLAGS) -c dirent.c -o dirent.o
time.o: time.c
gcc $(CFLAGS) -c time.c -o time.o

32
user/libs/libc/time.c Normal file
View File

@ -0,0 +1,32 @@
#include "time.h"
#include "errno.h"
#include "unistd.h"
#include <libsystem/syscall.h>
/**
* @brief 休眠指定时间
*
* @param rqtp 指定休眠的时间
* @param rmtp 返回的剩余休眠时间
* @return int
*/
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
return syscall_invoke(SYS_NANOSLEEP, (uint64_t)rqtp, (uint64_t)rmtp, 0, 0, 0, 0, 0, 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);
}

38
user/libs/libc/time.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include "stddef.h"
// 操作系统定义时间以ns为单位
#define CLOCKS_PER_SEC 1000000
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
long int __tm_gmtoff; /* Seconds east of UTC. */
const char *__tm_zone; /* Timezone abbreviation. */
};
struct timespec
{
long int tv_sec; // 秒
long int tv_nsec; // 纳秒
};
/**
* @brief 休眠指定时间
*
* @param rqtp 指定休眠的时间
* @param rmtp 返回的剩余休眠时间
* @return int
*/
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);

View File

@ -89,3 +89,11 @@ int64_t chdir(char *dest_path);
* @return int
*/
int execv(const char* path, char * const argv[]);
/**
* @brief 睡眠指定时间
*
* @param usec 微秒
* @return int
*/
extern int usleep(useconds_t usec);

View File

@ -22,6 +22,7 @@
#define SYS_WAIT4 15 // 等待进程退出
#define SYS_EXIT 16 // 进程退出
#define SYS_MKDIR 17 // 创建文件夹
#define SYS_NANOSLEEP 18 // 纳秒级休眠
/**
* @brief 用户态系统调用函数