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 \author Paul Cercueil
21
22 \see kos/thread.h
23*/
24
25#ifndef __KOS_WORKER_THREAD_H
26#define __KOS_WORKER_THREAD_H
27
28#include <kos/cdefs.h>
29__BEGIN_DECLS
30
31#include <kos/thread.h>
32#include <sys/queue.h>
33
34struct kthread_worker;
35
36/** \struct kthread_worker_t
37 \brief Opaque structure describing one worker thread.
38*/
39typedef struct kthread_worker kthread_worker_t;
40
41/** \brief Create a new worker thread with the specific set of attributes.
42 \relatesalso kthread_worker_t
43
44 This function will create a thread with the specified attributes that will
45 call the given routine with the given param pointer when notified.
46 The thread will only stop when thd_worker_destroy() is called.
47
48 \param attr A set of thread attributes for the created thread.
49 Passing NULL will initialize all attributes to their
50 default values.
51 \param routine The function to call in the worker thread.
52 \param data A parameter to pass to the function called.
53
54 \return The new worker thread on success, NULL on failure.
55
56 \sa thd_worker_destroy, thd_worker_wakeup
57*/
59 void (*routine)(void *), void *data);
60
61/** \brief Create a new worker thread.
62 \relatesalso kthread_worker_t
63
64 This function will create a thread with the default attributes that will
65 call the given routine with the given param pointer when notified.
66 The thread will only stop when thd_worker_destroy() is called.
67
68 \param routine The function to call in the worker thread.
69 \param data A parameter to pass to the function called.
70
71 \return The new worker thread on success, NULL on failure.
72
73 \sa thd_worker_destroy, thd_worker_wakeup
74*/
75static inline kthread_worker_t *
76thd_worker_create(void (*routine)(void *), void *data) {
77 return thd_worker_create_ex(NULL, routine, data);
78}
79
80/** \brief Stop and destroy a worker thread.
81 \relatesalso kthread_worker_t
82
83 This function will stop the worker thread and free its memory.
84
85 \param thd The worker thread to destroy.
86
87 \sa thd_worker_create, thd_worker_wakeup
88*/
90
91/** \brief Wake up a worker thread.
92 \relatesalso kthread_worker_t
93
94 This function will wake up the worker thread, causing it to call its
95 corresponding work function.
96
97 \param thd The worker thread to wake up.
98
99 \sa thd_worker_create, thd_worker_destroy
100*/
102
103/** \brief Get a handle to the underlying thread.
104 \relatesalso kthread_worker_t
105
106 \param thd The worker thread whose handle should be returned.
107
108 \return A handle to the underlying thread.
109*/
111
112__END_DECLS
113
114#endif /* __KOS_WORKER_THREAD_H */
static struct @68 data[BARRIER_COUNT]
Various common macros used throughout the codebase.
void * thd(void *v)
Definition compiler_tls.c:63
#define NULL
Definition memtest.h:26
Thread creation attributes.
Definition thread.h:280
Structure describing one running thread.
Definition thread.h:168
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.
static kthread_worker_t * thd_worker_create(void(*routine)(void *), void *data)
Create a new worker thread.
Definition worker_thread.h:76
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.
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.