poll: rework timeout

This commit is contained in:
Matheus
2025-11-02 21:32:04 -03:00
parent c9d401f488
commit 3b6b5363ad
5 changed files with 36 additions and 23 deletions
+17 -8
View File
@@ -5,6 +5,7 @@
#include <hashtable.h>
#include <kernel/poll.h>
#include <kernel/alloc.h>
#include <arch/cpu.h>
static mutex_t futexmutex;
static hashtable_t hashtable;
@@ -50,7 +51,14 @@ syscallret_t syscall_futex(context_t *, uint32_t *futexp, int op, uint32_t value
MUTEX_INIT(&futexmutex);
}
timespec_t timespec = tm ? *tm : (timespec_t){0};
timespec_t timespec = {0};
if (tm) {
ret.errno = usercopy_fromuser(&timespec, tm, sizeof(timespec_t));
if (ret.errno)
return ret;
}
uintmax_t us = timespec.s * 1000000 + timespec.ns / 1000;
polldesc_t desc = {0};
ret.errno = poll_initdesc(&desc, 1);
if (unlikely(ret.errno))
@@ -89,6 +97,12 @@ syscallret_t syscall_futex(context_t *, uint32_t *futexp, int op, uint32_t value
break;
}
// if timeout is zero, do not sleep
if (tm && us == 0) {
ret.errno = ETIMEDOUT;
break;
}
if (futex == NULL) {
futex = alloc(sizeof(futex_t));
if (futex == NULL) {
@@ -101,26 +115,21 @@ syscallret_t syscall_futex(context_t *, uint32_t *futexp, int op, uint32_t value
break;
}
uintmax_t us = timespec.s * 1000000 + timespec.ns / 1000;
++futex->waiting;
for (;;) {
poll_add(&futex->pollheader, &desc.data[0], POLLOUT);
MUTEX_RELEASE(&futexmutex);
ret.errno = poll_dowait(&desc, us);
int revents = 0;
if (desc.event)
revents = desc.event->revents;
MUTEX_ACQUIRE(&futexmutex);
if (revents == 0 || ret.errno == EINTR) {
if (ret.errno) {
// timed out or interrupted
--futex->waiting;
futex->waking = futex->waking > futex->waiting ? futex->waiting : futex->waking; // if it happened during a wakeup, just to make sure nothing bad happens
ret.errno = ret.errno == EINTR ? EINTR : 0;
} else if (futex->waking == 0) {
// should go back to sleep
poll_leave(&desc);
+5 -1
View File
@@ -75,11 +75,15 @@ syscallret_t syscall_poll(context_t *, pollfd_t *fds, size_t nfds, int timeoutms
if (eventcount == 0 && timeoutms != 0) {
ret.errno = poll_dowait(&desc, (timeoutms == -1 ? 0 : timeoutms) * 1000);
if (ret.errno == 0 && desc.event) {
if (ret.errno == 0) {
int fd = ((uintptr_t)desc.event - (uintptr_t)desc.data) / sizeof(polldata_t);
fdsbuff[fd].revents = desc.data[fd].revents;
eventcount = 1;
}
// timeout is not an error in poll()
if (ret.errno == ETIMEDOUT)
ret.errno = 0;
}
if (ret.errno == 0)