kernel, libc: Add an usleep() system call and use that to implement usleep() and sleep() in libc
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-01-22 15:00:20 +01:00
parent d5b1d72396
commit 7f8a8cdcaf
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 37 additions and 2 deletions

View File

@ -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
)

12
kernel/src/sys/usleep.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <sys/types.h>
Result<u64> sys_usleep(Registers*, SyscallArgs args)
{
useconds_t us = (useconds_t)args[0];
kernel_sleep(us / 1000);
return 0;
}

View File

@ -1,4 +1,4 @@
file(GLOB HEADERS include/*.h)
file(GLOB_RECURSE HEADERS include/*.h)
set(SOURCES
${HEADERS}

View File

@ -11,5 +11,6 @@
typedef int pid_t;
typedef __i64_t time_t;
typedef __u16_t mode_t;
typedef __u64_t useconds_t;
#endif

View File

@ -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

View File

@ -1,5 +1,7 @@
#include <bits/errno-return.h>
#include <stdarg.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <unistd.h>
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;
}
}

View File

@ -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
{