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

Wait queues (see sys/waitq.h). More...

#include <core/system.h>
#include <core/panic.h>
#include <core/arch.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/signal.h>
#include <sys/waitq.h>

Go to the source code of this file.

Data Structures

struct  poll_reg
 One wait queue that one polling thread waits for. More...

Macros

#define POLL_HASH   64

Functions

static void sleep_on (struct waitq *waitq, state_t state)
 Puts the current thread to sleep on waitq in state (ISLEEP or USLEEP), and returns once it has been woken up and has run again.
int waitq_sleep (struct waitq *waitq)
 Puts the current thread to sleep on a wait queue until it is woken up.
void waitq_sleep_unint (struct waitq *waitq)
 Puts the current thread to sleep on a wait queue until it is woken up, whatever signals come meanwhile.
static struct poll_reg ** poll_chain (struct waitq *queue)
 Returns the hash chain that registrations of queue are on.
int poll_register (struct waitq *queue)
 Registers a wait queue that the current thread, in select() or poll(), waits for: a wakeup of that queue also wakes the thread.
void poll_unregister (struct thread *thread)
 Removes every registration of a thread, when it stops waiting (it was woken up, or it is going away).
static void poll_wake (struct waitq *waitq)
 Wakes up the threads polling waitq, and those polling any queue.
void waitq_wakeup (struct waitq *waitq)
 Wakes up every thread sleeping on a wait queue, and every thread polling it.
void waitq_wakeup_nopoll (struct waitq *waitq)
 Wakes up every thread sleeping on a wait queue, but not the threads polling it.
void thread_wakeup (struct thread *thread)
 Wakes up one thread, if it is sleeping on a wait queue.

Variables

static struct poll_reg * poll_hash [POLL_HASH]
static int poll_regs

Detailed Description

Wait queues (see sys/waitq.h).

A thread that has to wait for something sleeps on the wait queue that belongs to it, and whatever makes it happen wakes the queue's threads up. select() and poll() wait for any of several queues at once.

Sleeping and waking up allocate nothing: a sleeping thread is linked into its wait queue through its own run_link, the same link that puts it on the ready list when it can run.

Definition in file waitq.c.

Macro Definition Documentation

◆ POLL_HASH

#define POLL_HASH   64

Definition at line 93 of file waitq.c.

Referenced by poll_chain().

Function Documentation

◆ poll_chain()

struct poll_reg ** poll_chain ( struct waitq * queue)
static

Returns the hash chain that registrations of queue are on.

Definition at line 99 of file waitq.c.

References poll_chain(), POLL_HASH, and poll_hash.

Referenced by poll_chain(), poll_register(), poll_unregister(), and poll_wake().

◆ poll_register()

int poll_register ( struct waitq * queue)

Registers a wait queue that the current thread, in select() or poll(), waits for: a wakeup of that queue also wakes the thread.

Registering the same queue twice does nothing.

Parameters
queuethe wait queue, or NULL to be woken by any wakeup
Returns
0, or -ENOMEM if there was no memory for the registration

Definition at line 112 of file waitq.c.

References curthread, kmalloc(), poll_reg::next, poll_chain(), poll_register(), poll_regs, thread::poll_regs, poll_reg::queue, poll_reg::thread, and poll_reg::thread_next.

Referenced by fd_watch(), and poll_register().

◆ poll_unregister()

void poll_unregister ( struct thread * thread)

Removes every registration of a thread, when it stops waiting (it was woken up, or it is going away).

Parameters
threadthe thread whose registrations go

Definition at line 144 of file waitq.c.

References kfree(), poll_reg::next, poll_chain(), poll_regs, thread::poll_regs, poll_unregister(), poll_reg::queue, and poll_reg::thread_next.

Referenced by poll_unregister(), sys_poll(), sys_select(), and thread_kill().

◆ poll_wake()

void poll_wake ( struct waitq * waitq)
static

Wakes up the threads polling waitq, and those polling any queue.

Definition at line 161 of file waitq.c.

References poll_reg::next, NULL, poll_chain(), poll_wake(), and waitq_wakeup_nopoll().

Referenced by poll_wake(), and waitq_wakeup().

◆ sleep_on()

void sleep_on ( struct waitq * waitq,
state_t state )
static

Puts the current thread to sleep on waitq in state (ISLEEP or USLEEP), and returns once it has been woken up and has run again.

Definition at line 24 of file waitq.c.

References arch_sleep(), curthread, panic, RUNNABLE, sleep_on(), and waitq::threads.

Referenced by sleep_on(), waitq_sleep(), and waitq_sleep_unint().

◆ thread_wakeup()

void thread_wakeup ( struct thread * thread)

Wakes up one thread, if it is sleeping on a wait queue.

The other threads on that queue keep sleeping.

Parameters
threadthe thread to wake up

Definition at line 216 of file waitq.c.

References NULL, thread::run_link, sched_thread_wake(), thread_wakeup(), and thread::waitq.

Referenced by net_callout_set(), rx_queue(), sched_timer_tick(), signal_to(), and thread_wakeup().

◆ waitq_sleep()

int waitq_sleep ( struct waitq * waitq)

Puts the current thread to sleep on a wait queue until it is woken up.

A signal that the process doesn't block also ends the sleep (signal_to() wakes the thread). The system call should then return, so that the signal can be delivered. If a signal is already pending, the thread doesn't sleep at all.

A wakeup only means that something changed, so callers sleep in a loop and look again at what they wait for each time.

Parameters
waitqthe wait queue to sleep on
Returns
0 once woken up, or -ERESTARTSYS if a signal ended the sleep

Definition at line 50 of file waitq.c.

References curthread, ERESTARTSYS, ISLEEP, signal_interrupts(), sleep_on(), and waitq_sleep().

Referenced by pipefs_fifo_open(), posix_file_write(), pty_master_write(), pty_slave_write(), sched_wait(), socket_unix_accept(), socket_unix_connect(), socket_unix_recv(), socket_unix_send(), sys_sigsuspend(), sys_thread_join(), sys_waitpid(), tty_wait_start(), vfs_file_flock(), vt_kd_ioctl(), and waitq_sleep().

◆ waitq_sleep_unint()

void waitq_sleep_unint ( struct waitq * waitq)

Puts the current thread to sleep on a wait queue until it is woken up, whatever signals come meanwhile.

This is for waits that can't be abandoned halfway, such as a disk transfer or a page being read in.

Parameters
waitqthe wait queue to sleep on

Definition at line 67 of file waitq.c.

References sleep_on(), USLEEP, and waitq_sleep_unint().

Referenced by channel_acquire(), dma_transfer(), proc_threads_exec(), signal_stop(), and waitq_sleep_unint().

◆ waitq_wakeup()

◆ waitq_wakeup_nopoll()

void waitq_wakeup_nopoll ( struct waitq * waitq)

Wakes up every thread sleeping on a wait queue, but not the threads polling it.

This is for queues that no file descriptor waits through, such as a disk request's, and for the pollers' own queues.

Parameters
waitqthe wait queue (NULL does nothing)

Definition at line 196 of file waitq.c.

References list_entry, NULL, thread::run_link, sched_thread_wake(), waitq::threads, thread::waitq, and waitq_wakeup_nopoll().

Referenced by channel_release(), dma_irq(), poll_wake(), waitq_wakeup(), and waitq_wakeup_nopoll().

Variable Documentation

◆ poll_hash

struct poll_reg* poll_hash[POLL_HASH]
static

Definition at line 95 of file waitq.c.

Referenced by poll_chain().

◆ poll_regs

int poll_regs
static

Definition at line 96 of file waitq.c.

Referenced by poll_register(), poll_unregister(), and waitq_wakeup().