AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
sys_poll.c File Reference

System calls that wait for any of several descriptors: select() (and pselect()) and poll(). More...

#include "syscalls.h"

Go to the source code of this file.

Data Structures

struct  fd_set

Macros

#define FD_SETSIZE   64
#define NFDBITS   (sizeof (fd_mask) * 8)
#define _howmany(x, y)
#define FD_BIT(set, i)
#define FD_ADD(set, i)

Typedefs

typedef unsigned long fd_mask

Functions

static struct file * fd_peek (int fd)
 Returns the open file a descriptor refers to, without holding it (as fd_file() does): it is only used until the call sleeps.
static int fd_ready (struct file *file, int write)
 Returns whether file can be read (or written) without waiting.
static int fd_watch (struct file *file, int write)
 Registers the wait queue that file's readers (or writers) sleep on, or NULL (any wakeup) if the file can't tell.
static uint64_t poll_deadline (int64_t ns)
 Returns the deadline of a timeout of ns ns: 0 for none (ns < 0).
long sys_select (struct aquila_select_args *args)
 Implements select() and pselect(): waits until a descriptor of the sets can be read or written, or the timeout passes, and says which can.
long sys_poll (struct pollfd *fds, nfds_t nfds, int timeout_ms)
 Implements poll(): waits until one of the descriptors can be read or written, or the timeout passes, and says which can.

Detailed Description

System calls that wait for any of several descriptors: select() (and pselect()) and poll().

Both look at each descriptor, registering the wait queue its readers (or writers) sleep on. If none is ready, the thread sleeps on its own queue (thread->poll_wait), which a wakeup of any registered queue also wakes, and then looks at them all again. See sys/waitq.c.

Definition in file sys_poll.c.

Macro Definition Documentation

◆ _howmany

#define _howmany ( x,
y )
Value:
(((x)+((y)-1))/(y))

Definition at line 24 of file sys_poll.c.

◆ FD_ADD

#define FD_ADD ( set,
i )
Value:
((set).fds_bits[(i) / NFDBITS] |= (1UL << ((i) % NFDBITS)))
#define NFDBITS
Definition sys_poll.c:23

Referenced by sys_select().

◆ FD_BIT

#define FD_BIT ( set,
i )
Value:
((set).fds_bits[(i) / NFDBITS] & (1UL << ((i) % NFDBITS)))

Referenced by sys_select().

◆ FD_SETSIZE

#define FD_SETSIZE   64

Definition at line 22 of file sys_poll.c.

Referenced by sys_select().

◆ NFDBITS

#define NFDBITS   (sizeof (fd_mask) * 8)

Definition at line 23 of file sys_poll.c.

Typedef Documentation

◆ fd_mask

typedef unsigned long fd_mask

Definition at line 20 of file sys_poll.c.

Function Documentation

◆ fd_peek()

struct file * fd_peek ( int fd)
static

Returns the open file a descriptor refers to, without holding it (as fd_file() does): it is only used until the call sleeps.

Definition at line 15 of file sys_poll.c.

References curproc, fd_peek(), and proc_fd_file().

Referenced by fd_peek(), sys_poll(), and sys_select().

◆ fd_ready()

int fd_ready ( struct file * file,
int write )
static

Returns whether file can be read (or written) without waiting.

A file that can't tell counts as ready: the read or write reports the error.

Definition at line 33 of file sys_poll.c.

References fd_ready(), vfs_file_can_read(), and vfs_file_can_write().

Referenced by fd_ready(), sys_poll(), and sys_select().

◆ fd_watch()

int fd_watch ( struct file * file,
int write )
static

Registers the wait queue that file's readers (or writers) sleep on, or NULL (any wakeup) if the file can't tell.

Returns 0 or -ENOMEM.

Definition at line 41 of file sys_poll.c.

References fd_watch(), poll_register(), and vfs_file_wait_queue().

Referenced by fd_watch(), sys_poll(), and sys_select().

◆ poll_deadline()

uint64_t poll_deadline ( int64_t ns)
static

Returns the deadline of a timeout of ns ns: 0 for none (ns < 0).

A timeout of 0 means not to wait at all. A call restarted after a stop keeps the deadline it had.

Definition at line 49 of file sys_poll.c.

References arch_rtime_ns(), curthread, and poll_deadline().

Referenced by poll_deadline(), sys_poll(), and sys_select().

◆ sys_poll()

long sys_poll ( struct pollfd * fds,
nfds_t nfds,
int timeout_ms )

Implements poll(): waits until one of the descriptors can be read or written, or the timeout passes, and says which can.

Parameters
fdsthe descriptors and the events wanted, in user memory; their revents are set
nfdshow many there are
timeout_msthe timeout in ms, or -1 for none
Returns
how many descriptors have events (0 on timeout), or a negative errno: -EINVAL, -ENOMEM, -EFAULT, or -EINTR

Definition at line 198 of file sys_poll.c.

References curthread, ERESTARTNOHAND, fd_peek(), fd_ready(), fd_watch(), FDS_COUNT, kfree(), kmalloc(), LOG_DEBUG, NULL, poll_deadline(), poll_unregister(), sched_wait(), and sys_poll().

Referenced by sys_poll().

◆ sys_select()

long sys_select ( struct aquila_select_args * args)

Implements select() and pselect(): waits until a descriptor of the sets can be read or written, or the timeout passes, and says which can.

Exceptional conditions are never reported.

Parameters
argsthe arguments, in user memory: nfds, the read, write and except sets, the timeout, and pselect()'s signal mask
Returns
how many descriptors are ready (0 on timeout), or a negative errno: -EBADF, -EINVAL, -EFAULT, or -EINTR

Definition at line 68 of file sys_poll.c.

References curproc, curthread, ERESTARTNOHAND, FD_ADD, FD_BIT, fd_peek(), fd_ready(), FD_SETSIZE, fd_watch(), LOG_DEBUG, poll_deadline(), poll_unregister(), sched_wait(), SIG_UNMASKABLE, sys_select(), timeval::tv_sec, and timeval::tv_usec.

Referenced by sys_select().