KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
worker_thread.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/worker_thread.h
4 Copyright (C) 2024 Paul Cercueil
5*/
6
7/** \file kos/worker_thread.h
8 \brief Threaded worker support.
9 \ingroup kthreads
10
11 This file contains the threaded worker API. Threaded workers are threads
12 that are idle most of the time, until they are notified that there is work
13 pending; in which case they will call their associated work function.
14
15 The work function can then process any number of tasks, until it clears out
16 all of its tasks or decides that it worked enough; in which case the
17 function can return, and will re-start the next time it is notified, or if
18 it was notified while it was running.
19
20 An optional API is also present, which provides a FIFO for jobs to be
21 processed by the threaded worker. This is useful when jobs have to be
22 processed in sequence.
23
24 \author Paul Cercueil
25
26 \see kos/thread.h
27*/
28
29#ifndef __KOS_WORKER_THREAD_H
30#define __KOS_WORKER_THREAD_H
31
32#include <sys/cdefs.h>
33__BEGIN_DECLS
34
35#include <kos/thread.h>
36#include <sys/queue.h>
37
38struct kthread_worker;
39
40/** \struct kthread_worker_t
41 \brief Opaque structure describing one worker thread.
42*/
43typedef struct kthread_worker kthread_worker_t;
44
45/** \brief Structure describing one job for the worker. */
46typedef struct kthread_job {
47 /** \brief List handle. */
48 STAILQ_ENTRY(kthread_job) entry;
49
50 /** \brief User pointer to the work data. */
51 void *data;
53
54/** \brief Create a new worker thread with the specific set of attributes.
55 \relatesalso kthread_worker_t
56
57 This function will create a thread with the specified attributes that will
58 call the given routine with the given param pointer when notified.
59 The thread will only stop when thd_worker_destroy() is called.
60
61 \param attr A set of thread attributes for the created thread.
62 Passing NULL will initialize all attributes to their
63 default values.
64 \param routine The function to call in the worker thread.
65 \param data A parameter to pass to the function called.
66
67 \return The new worker thread on success, NULL on failure.
68
69 \sa thd_worker_destroy, thd_worker_wakeup
70*/
72 void (*routine)(void *), void *data);
73
74/** \brief Create a new worker thread.
75 \relatesalso kthread_worker_t
76
77 This function will create a thread with the default attributes that will
78 call the given routine with the given param pointer when notified.
79 The thread will only stop when thd_worker_destroy() is called.
80
81 \param routine The function to call in the worker thread.
82 \param data A parameter to pass to the function called.
83
84 \return The new worker thread on success, NULL on failure.
85
86 \sa thd_worker_destroy, thd_worker_wakeup
87*/
88static inline kthread_worker_t *
89thd_worker_create(void (*routine)(void *), void *data) {
90 return thd_worker_create_ex(NULL, routine, data);
91}
92
93/** \brief Stop and destroy a worker thread.
94 \relatesalso kthread_worker_t
95
96 This function will stop the worker thread and free its memory.
97
98 \param thd The worker thread to destroy.
99
100 \sa thd_worker_create, thd_worker_wakeup
101*/
103
104/** \brief Wake up a worker thread.
105 \relatesalso kthread_worker_t
106
107 This function will wake up the worker thread, causing it to call its
108 corresponding work function. Usually, this should be called after a new
109 job has been added with thd_worker_add_job().
110
111 \param thd The worker thread to wake up.
112
113 \sa thd_worker_create, thd_worker_destroy, thd_worker_add_job
114*/
116
117/** \brief Get a handle to the underlying thread.
118 \relatesalso kthread_worker_t
119
120 \param thd The worker thread whose handle should be returned.
121
122 \return A handle to the underlying thread.
123*/
125
126/** \brief Add a new job to the worker thread.
127 \relatesalso kthread_worker_t
128
129 This function will append the job to the worker thread's to-do queue.
130 Note that it is the responsability of the work function (the one passed to
131 thd_worker_create()) to dequeue and process the jobs with
132 thd_worker_dequeue_job(). Also, this function won't automatically notify the
133 worker thread - you still need to call thd_worker_wakeup().
134
135 \param thd The worker thread to add a job to.
136 \param job The new job to give to the worker thread.
137*/
139
140/** \brief Dequeue one job from the worker thread's to-do queue.
141 \relatesalso kthread_worker_t
142
143 Use this function to dequeue one job from the worker thread, that has been
144 previously queued using thd_worker_add_job(). This function is typically
145 used inside the work function registered with thd_worker_create().
146
147 \param worker The worker thread to add a job to.
148
149 \return A new job to process, or NULL if there is none.
150*/
152
153__END_DECLS
154
155#endif /* __KOS_WORKER_THREAD_H */
Thread creation attributes.
Definition thread.h:284
Structure describing one job for the worker.
Definition worker_thread.h:46
void * data
User pointer to the work data.
Definition worker_thread.h:51
STAILQ_ENTRY(kthread_job) entry
List handle.
Structure describing one running thread.
Definition thread.h:169
Opaque structure describing one worker thread.
void thd_worker_wakeup(kthread_worker_t *thd)
Wake up a worker thread.
kthread_t * thd_worker_get_thread(kthread_worker_t *thd)
Get a handle to the underlying thread.
void thd_worker_destroy(kthread_worker_t *thd)
Stop and destroy a worker thread.
void thd_worker_add_job(kthread_worker_t *thd, kthread_job_t *job)
Add a new job to the worker thread.
static kthread_worker_t * thd_worker_create(void(*routine)(void *), void *data)
Create a new worker thread.
Definition worker_thread.h:89
kthread_worker_t * thd_worker_create_ex(const kthread_attr_t *attr, void(*routine)(void *), void *data)
Create a new worker thread with the specific set of attributes.
kthread_job_t * thd_worker_dequeue_job(kthread_worker_t *worker)
Dequeue one job from the worker thread's to-do queue.
Threading support.
kthread_worker_t * thd_worker_create_ex(const kthread_attr_t *attr, void(*routine)(void *), void *data)
Create a new worker thread with the specific set of attributes.