mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
#include <kernel/syscalls.h>
|
|
#include <time.h>
|
|
#include <kernel/vmm.h>
|
|
#include <kernel/timer.h>
|
|
#include <arch/cpu.h>
|
|
|
|
static void timeout(context_t *, dpcarg_t arg) {
|
|
thread_t *thread = arg;
|
|
sched_wakeup(thread, 0);
|
|
}
|
|
|
|
syscallret_t syscall_nanosleep(context_t *, timespec_t *utime, timespec_t *remaining) {
|
|
syscallret_t ret = {
|
|
.ret = -1
|
|
};
|
|
|
|
timespec_t time;
|
|
ret.errno = usercopy_fromuser(&time, utime, sizeof(timespec_t));
|
|
if (ret.errno)
|
|
return ret;
|
|
|
|
if (time.ns > 999999999 || time.ns < 0) {
|
|
ret.errno = EINVAL;
|
|
return ret;
|
|
}
|
|
|
|
timerentry_t sleepentry;
|
|
sched_prepare_sleep(true);
|
|
|
|
sched_target_cpu(current_cpu());
|
|
timer_insert(current_cpu()->timer, &sleepentry, timeout, current_thread(), time.s * 1000000 + time.ns / 1000, false);
|
|
|
|
ret.errno = sched_yield() == SCHED_WAKEUP_REASON_INTERRUPTED ? EINTR : 0;
|
|
|
|
if (ret.errno && remaining) {
|
|
uintmax_t remainingus = timer_remove(current_cpu()->timer, &sleepentry);
|
|
time.ns = (remainingus % 1000000) * 1000;
|
|
time.s = (remainingus / 1000000);
|
|
ret.errno = usercopy_touser(remaining, &time, sizeof(timespec_t)) ? EFAULT : EINTR;
|
|
}
|
|
|
|
sched_target_cpu(NULL);
|
|
|
|
ret.ret = 0;
|
|
return ret;
|
|
}
|