The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
completion.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file completion.h
5 * @brief wait for a device: sleep until its interrupt, or poll.
6 *
7 * a driver starts a command, then calls completion_wait_until with a
8 * function that reads the device status. its interrupt handler calls
9 * completion_signal. the waiting process sleeps between interrupts, so the
10 * CPU runs other processes while the device works.
11 *
12 * the wait polls instead of sleeping when:
13 * - the driver has no interrupt (use_irq is false),
14 * - the caller cannot sleep (before the scheduler runs, or with
15 * interrupts off),
16 * - the interrupt did not come: if the device is ready after a short
17 * sleep without an interrupt, the interrupt does not work, and the
18 * completion polls from then on (and says so in the kernel log).
19 */
20
21#include "common/stdint.h"
22#include "kernel/time/timer.h"
23
24typedef struct
25{
26 volatile bool done; // set by completion_signal, cleared by the waiter
27 spinlock_t lock; // guards done
28 bool use_irq; // the device has a working interrupt
29 const char *name; // for the kernel log
30 timed_queue_t queue; // the sleeping waiter
32
33/**
34 * a function that reads the device status.
35 * @return true when the command is done (or has failed).
36 */
37typedef bool (*completion_ready_t)(void *context);
38
39/**
40 * initialize a completion. call once (it registers a timed queue).
41 * @param use_irq true if the driver has an interrupt handler.
42 */
43void completion_init(completion_t *completion, const char *name, bool use_irq);
44
45/**
46 * the device has something to report. safe in an interrupt handler.
47 */
48void completion_signal(completion_t *completion);
49
50/**
51 * wait until ready(context) returns true.
52 * @param timeout_ms how long the device may take.
53 * @return false if the device was not ready in time.
54 */
55bool completion_wait_until(completion_t *completion, completion_ready_t ready, void *context,
56 uint32_t timeout_ms);
bool completion_wait_until(completion_t *completion, completion_ready_t ready, void *context, uint32_t timeout_ms)
wait until ready(context) returns true.
Definition completion.c:53
void completion_init(completion_t *completion, const char *name, bool use_irq)
initialize a completion.
Definition completion.c:18
void completion_signal(completion_t *completion)
the device has something to report.
Definition completion.c:27
bool(*) completion_ready_t(void *context)
a function that reads the device status.
Definition completion.h:37
unsigned int uint32_t
Definition fb.h:17
#define bool
Definition stdbool.h:8
Definition completion.h:25
volatile bool done
Definition completion.h:26
timed_queue_t queue
Definition completion.h:30
bool use_irq
Definition completion.h:28
spinlock_t lock
Definition completion.h:27
const char * name
Definition completion.h:29
kernel locking rules:
Definition spinlock.h:67
struct timed_queue timed_queue_t
a wait queue with a deadline.