From 9901f95d78a689c2ab347d6bf12de85570ed16dc Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 4 Sep 2023 12:58:25 +0300 Subject: [PATCH] Kernel: Add basic nanosleep, only millisecond percision --- kernel/include/kernel/Process.h | 1 + kernel/kernel/Process.cpp | 7 +++++++ kernel/kernel/Syscall.cpp | 3 +++ libc/include/sys/syscall.h | 1 + libc/time.cpp | 5 +++++ 5 files changed, 17 insertions(+) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 2a808fa409..a77c97594d 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -68,6 +68,7 @@ namespace Kernel BAN::ErrorOr sys_wait(pid_t pid, int* stat_loc, int options); BAN::ErrorOr sys_sleep(int seconds); + BAN::ErrorOr sys_nanosleep(const timespec* rqtp, timespec* rmtp); BAN::ErrorOr sys_setenvp(char** envp); diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 9bafe0cfa5..99d4bbebe1 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -529,6 +529,13 @@ namespace Kernel return 0; } + BAN::ErrorOr Process::sys_nanosleep(const timespec* rqtp, timespec* rmtp) + { + (void)rmtp; + SystemTimer::get().sleep(rqtp->tv_sec * 1000 + BAN::Math::div_round_up(rqtp->tv_nsec, 1'000'000)); + return 0; + } + BAN::ErrorOr Process::sys_setenvp(char** envp) { LockGuard _(m_lock); diff --git a/kernel/kernel/Syscall.cpp b/kernel/kernel/Syscall.cpp index c919c39dfe..a6e147301f 100644 --- a/kernel/kernel/Syscall.cpp +++ b/kernel/kernel/Syscall.cpp @@ -182,6 +182,9 @@ namespace Kernel case SYS_FCNTL: ret = Process::current().sys_fcntl((int)arg1, (int)arg2, (int)arg3); break; + case SYS_NANOSLEEP: + ret = Process::current().sys_nanosleep((const timespec*)arg1, (timespec*)arg2); + break; default: dwarnln("Unknown syscall {}", syscall); break; diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h index ae177acdd8..a3f0ba53b0 100644 --- a/libc/include/sys/syscall.h +++ b/libc/include/sys/syscall.h @@ -51,6 +51,7 @@ __BEGIN_DECLS #define SYS_GET_PGID 44 #define SYS_SET_PGID 45 #define SYS_FCNTL 46 +#define SYS_NANOSLEEP 47 __END_DECLS diff --git a/libc/time.cpp b/libc/time.cpp index e98ff75ee2..9d62dbc160 100644 --- a/libc/time.cpp +++ b/libc/time.cpp @@ -5,4 +5,9 @@ int clock_gettime(clockid_t clock_id, struct timespec* tp) { return syscall(SYS_CLOCK_GETTIME, clock_id, tp); +} + +int nanosleep(const struct timespec* rqtp, struct timespec* rmtp) +{ + return syscall(SYS_NANOSLEEP, rqtp, rmtp); } \ No newline at end of file