diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index fe25dd0a..d8427116 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -22,6 +22,7 @@ set(SOURCES src/sys/console_write.cpp src/sys/clock_gettime.cpp src/sys/allocate_memory.cpp + src/sys/usleep.cpp src/InitRD.cpp src/ELF.cpp ) diff --git a/kernel/src/sys/usleep.cpp b/kernel/src/sys/usleep.cpp new file mode 100644 index 00000000..970b887b --- /dev/null +++ b/kernel/src/sys/usleep.cpp @@ -0,0 +1,12 @@ +#include "sys/Syscall.h" +#include "thread/Scheduler.h" +#include + +Result sys_usleep(Registers*, SyscallArgs args) +{ + useconds_t us = (useconds_t)args[0]; + + kernel_sleep(us / 1000); + + return 0; +} diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 9f686d94..567f5a08 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -1,4 +1,4 @@ -file(GLOB HEADERS include/*.h) +file(GLOB_RECURSE HEADERS include/*.h) set(SOURCES ${HEADERS} diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h index cf368ed9..c4adbd49 100644 --- a/libc/include/sys/types.h +++ b/libc/include/sys/types.h @@ -11,5 +11,6 @@ typedef int pid_t; typedef __i64_t time_t; typedef __u16_t mode_t; +typedef __u64_t useconds_t; #endif diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 218be84d..de7d321b 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -24,6 +24,12 @@ extern "C" /* Calls the operating system kernel for a specific service. */ long syscall(long num, ...); + /* Sleeps for X microseconds. */ + int usleep(useconds_t us); + + /* Sleeps for X seconds. */ + unsigned long sleep(unsigned long seconds); + #ifdef __cplusplus } #endif diff --git a/libc/src/unistd.cpp b/libc/src/unistd.cpp index 58b0c277..d83afe5b 100644 --- a/libc/src/unistd.cpp +++ b/libc/src/unistd.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include extern "C" long arch_invoke_syscall(long, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t); @@ -23,4 +25,16 @@ extern "C" return rc; } + + int usleep(useconds_t us) + { + long rc = syscall(SYS_usleep, us); + __errno_return(rc, int); + } + + unsigned long sleep(unsigned long seconds) + { + syscall(SYS_usleep, seconds * 1000000); + return 0; + } } diff --git a/luna/include/luna/Syscall.h b/luna/include/luna/Syscall.h index 96277f77..65b1adf4 100644 --- a/luna/include/luna/Syscall.h +++ b/luna/include/luna/Syscall.h @@ -1,6 +1,7 @@ #pragma once -#define enumerate_syscalls(_e) _e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) +#define enumerate_syscalls(_e) \ + _e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) enum Syscalls {