Kernel/LibC: Implement super basic select
This does not really even block but it works... :D
This commit is contained in:
@@ -21,6 +21,7 @@ set(LIBC_SOURCES
|
||||
stropts.cpp
|
||||
sys/banan-os.cpp
|
||||
sys/mman.cpp
|
||||
sys/select.cpp
|
||||
sys/socket.cpp
|
||||
sys/stat.cpp
|
||||
sys/wait.cpp
|
||||
|
||||
20
libc/include/bits/types/timeval.h
Normal file
20
libc/include/bits/types/timeval.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef _BITS_TIMEVAL_H
|
||||
#define _BITS_TIMEVAL_H 1
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define __need_time_t
|
||||
#define __need_suseconds_t
|
||||
#include <sys/types.h>
|
||||
|
||||
struct timeval
|
||||
{
|
||||
time_t tv_sec; /* Seconds. */
|
||||
suseconds_t tc_usec; /* Microseconds. */
|
||||
};
|
||||
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define __need_time_t
|
||||
#define __need_suseconds_t
|
||||
#include <sys/types.h>
|
||||
#include <bits/types/timeval.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
@@ -50,6 +48,16 @@ typedef struct {
|
||||
(setp)->__bits[i] = (__fd_mask)0; \
|
||||
} while (0)
|
||||
|
||||
struct sys_pselect_t
|
||||
{
|
||||
int nfds;
|
||||
fd_set* readfds;
|
||||
fd_set* writefds;
|
||||
fd_set* errorfds;
|
||||
const struct timespec* timeout;
|
||||
const sigset_t* sigmask;
|
||||
};
|
||||
|
||||
int pselect(int nfds, fd_set* __restrict readfds, fd_set* __restrict writefds, fd_set* __restrict errorfds, const struct timespec* __restrict timeout, const sigset_t* __restrict sigmask);
|
||||
int select(int nfds, fd_set* __restrict readfds, fd_set* __restrict writefds, fd_set* __restrict errorfds, struct timeval* __restrict timeout);
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ __BEGIN_DECLS
|
||||
#define SYS_ACCEPT 70
|
||||
#define SYS_CONNECT 71
|
||||
#define SYS_LISTEN 72
|
||||
#define SYS_PSELECT 73
|
||||
|
||||
__END_DECLS
|
||||
|
||||
|
||||
@@ -7,18 +7,10 @@
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define __need_time_t
|
||||
#define __need_suseconds_t
|
||||
#include <sys/types.h>
|
||||
|
||||
// NOTE: select is declared from here
|
||||
#include <sys/select.h>
|
||||
|
||||
struct timeval
|
||||
{
|
||||
time_t tv_sec; /* Seconds. */
|
||||
suseconds_t tc_usec; /* Microseconds. */
|
||||
};
|
||||
#include <bits/types/timeval.h>
|
||||
|
||||
struct itimerval
|
||||
{
|
||||
|
||||
31
libc/sys/select.cpp
Normal file
31
libc/sys/select.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <sys/select.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int pselect(int nfds, fd_set* __restrict readfds, fd_set* __restrict writefds, fd_set* __restrict errorfds, const struct timespec* __restrict timeout, const sigset_t* __restrict sigmask)
|
||||
{
|
||||
sys_pselect_t arguments {
|
||||
.nfds = nfds,
|
||||
.readfds = readfds,
|
||||
.writefds = writefds,
|
||||
.errorfds = errorfds,
|
||||
.timeout = timeout,
|
||||
.sigmask = sigmask
|
||||
};
|
||||
return syscall(SYS_PSELECT, &arguments);
|
||||
}
|
||||
|
||||
int select(int nfds, fd_set* __restrict readfds, fd_set* __restrict writefds, fd_set* __restrict errorfds, struct timeval* __restrict timeout)
|
||||
{
|
||||
timespec* pts = nullptr;
|
||||
timespec ts;
|
||||
if (timeout)
|
||||
{
|
||||
ts.tv_sec = timeout->tv_sec;
|
||||
ts.tv_nsec = timeout->tc_usec * 1000;
|
||||
pts = &ts;
|
||||
}
|
||||
|
||||
// TODO: "select may update timeout", should we?
|
||||
return pselect(nfds, readfds, writefds, errorfds, pts, nullptr);
|
||||
}
|
||||
Reference in New Issue
Block a user