KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
workqueue.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/workqueue.h
4 Copyright (C) 2026 Paul Cercueil
5*/
6
7/** \file kos/workqueue.h
8 \brief Threaded work queue support.
9 \ingroup kthreads
10
11 This file contains the API to create and manage work queues.
12
13 A work queue is a thread that will execute tasks (aka. jobs) that are
14 enqueued by client code, at a predeterminated moment in time. Multiple
15 jobs can be enqueued. Once a job is executed, it is removed from the
16 execution queue.
17
18 \author Paul Cercueil
19
20 \see kos/thread.h
21 \see kos/worker_thread.h
22*/
23
24#ifndef __KOS_WORKQUEUE_H
25#define __KOS_WORKQUEUE_H
26
27#include <kos/cdefs.h>
28
29__BEGIN_DECLS
30
31#include <kos/thread.h>
32#include <stdint.h>
33#include <sys/queue.h>
34
35struct workqueue;
36
37/** \struct workqueue_t
38 \brief Opaque structure describing one work queue.
39*/
40typedef struct workqueue workqueue_t;
41
42/** \struct workqueue_job_t
43 \brief Structure describing a job for the work queue.
44*/
45typedef struct workqueue_job {
46 /** \brief Routine to call. */
47 void (*cb)(workqueue_t *queue, struct workqueue_job *job);
48
49 /** \brief Time at which the job will be processed.
50 If set to 0, the job will be set to execute immediately. */
51 uint64_t time_ms;
52
53 /** \brief List handle. No need to set manually. */
54 STAILQ_ENTRY(workqueue_job) entry;
56
57/** \brief Create a new work queue.
58 \relatesalso workqueue_t
59
60 This function will create a new work queue.
61
62 \return The new work queue on success, NULL on failure.
63
64 \sa workqueue_destroy
65*/
67
68/** \brief Destroy a work queue.
69 \relatesalso workqueue_t
70
71 This function will destroy a work queue and free up any allocated memory.
72
73 \param wq A pointer to the work queue
74
75 \sa workqueue_create
76*/
78
79/** \brief Stop a work queue from running.
80 \relatesalso workqueue_t
81
82 This function can optionally be called before destroying a work queue.
83 The work queue then stops processing previously or newly enqueued jobs.
84
85 \param wq A pointer to the work queue
86
87 \sa workqueue_destroy
88*/
90
91/** \brief Enqueue a job to a work queue.
92 \relatesalso workqueue_t
93
94 This function will enqueue a job to the given work queue. The job's struct
95 must have been initialized properly.
96
97 \param wq A pointer to the work queue
98 \param job A pointer to the job to enqueue
99
100 \sa workqueue_create
101*/
103
104/** \brief Cancel a job and remove it from the work queue.
105 \relatesalso workqueue_t
106
107 This function can be used when a job should be removed from a work queue
108 before the job is set to be executed (note that jobs are automatically
109 removed from the work queue right before their execution).
110
111 \param wq A pointer to the work queue
112 \param job A pointer to the job to cancel
113
114 \sa workqueue_create
115*/
117
118/** \brief Get a handle to the underlying thread.
119 \relatesalso workqueue_t
120
121 \param wq The workqueue whose thread should be returned.
122
123 \return A handle to the underlying thread.
124*/
126
127__END_DECLS
128
129#endif /* __KOS_WORKQUEUE_H */
Various common macros used throughout the codebase.
Structure describing one running thread.
Definition thread.h:168
Structure describing a job for the work queue.
Definition workqueue.h:45
STAILQ_ENTRY(workqueue_job) entry
List handle.
uint64_t time_ms
Time at which the job will be processed.
Definition workqueue.h:51
Opaque structure describing one work queue.
kthread_t * workqueue_get_thread(workqueue_t *wq)
Get a handle to the underlying thread.
void workqueue_cancel(workqueue_t *wq, workqueue_job_t *job)
Cancel a job and remove it from the work queue.
void workqueue_destroy(workqueue_t *wq)
Destroy a work queue.
void workqueue_kill(workqueue_t *wq)
Stop a work queue from running.
void workqueue_enqueue(workqueue_t *wq, workqueue_job_t *job)
Enqueue a job to a work queue.
workqueue_t * workqueue_create(void)
Create a new work queue.
Threading support.