Monado OpenXR Runtime
u_worker.h
Go to the documentation of this file.
1// Copyright 2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Worker and threading pool.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 *
8 * @ingroup aux_util
9 */
10
11#pragma once
12
13#include "xrt/xrt_defines.h"
14
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20
21/*
22 *
23 * Worker thread pool.
24 *
25 */
26
27/*!
28 * A worker pool, can shared between multiple groups worker pool.
29 *
30 * @ingroup aux_util
31 */
33{
34 struct xrt_reference reference;
35};
36
37/*!
38 * Creates a new thread pool to be used by a worker group.
39 *
40 * @param starting_worker_count How many worker threads can be active at the
41 * same time without any "donated" threads.
42 * @param thread_count The number of threads to be created in total,
43 * this is the maximum threads that can be in
44 * flight at the same time.
45 * @param prefix Prefix to used when naming threads, used for
46 * tracing and debugging.
47 *
48 * @ingroup aux_util
49 */
51u_worker_thread_pool_create(uint32_t starting_worker_count, uint32_t thread_count, const char *prefix);
52
53/*!
54 * Internal function, only called by reference.
55 *
56 * @ingroup aux_util
57 */
58void
60
61/*!
62 * Standard Monado reference function.
63 *
64 * @ingroup aux_util
65 */
66static inline void
68{
69 struct u_worker_thread_pool *old_dst = *dst;
70
71 if (old_dst == src) {
72 return;
73 }
74
75 if (src) {
76 xrt_reference_inc(&src->reference);
77 }
78
79 *dst = src;
80
81 if (old_dst) {
82 if (xrt_reference_dec_and_is_zero(&old_dst->reference)) {
84 }
85 }
86}
87
88
89/*
90 *
91 * Worker group.
92 *
93 */
94
95/*!
96 * A worker group where you submit tasks to. Can share a thread pool with
97 * multiple groups. Also can "donate" a thread to the thread pool by waiting.
98 *
99 * @ingroup aux_util
100 */
102{
103 struct xrt_reference reference;
104};
105
106/*!
107 * Function typedef for tasks.
108 *
109 * @ingroup aux_util
110 */
111typedef void (*u_worker_group_func_t)(void *);
112
113/*!
114 * Create a new worker group.
115 *
116 * @ingroup aux_util
117 */
118struct u_worker_group *
120
121/*!
122 * Push a new task to worker group.
123 *
124 * @ingroup aux_util
125 */
126void
128
129/*!
130 * Wait for all pushed tasks to be completed, "donates" this thread to the
131 * shared thread pool.
132 *
133 * @ingroup aux_util
134 */
135void
137
138/*!
139 * Destroy a worker pool.
140 *
141 * @ingroup aux_util
142 */
143void
145
146/*!
147 * Standard Monado reference function.
148 *
149 * @ingroup aux_util
150 */
151static inline void
153{
154 struct u_worker_group *old_dst = *dst;
155
156 if (old_dst == src) {
157 return;
158 }
159
160 if (src) {
161 xrt_reference_inc(&src->reference);
162 }
163
164 *dst = src;
165
166 if (old_dst) {
167 if (xrt_reference_dec_and_is_zero(&old_dst->reference)) {
168 u_worker_group_destroy(old_dst);
169 }
170 }
171}
172
173
174#ifdef __cplusplus
175}
176#endif
void(* u_worker_group_func_t)(void *)
Function typedef for tasks.
Definition: u_worker.h:111
void u_worker_group_destroy(struct u_worker_group *uwg)
Destroy a worker pool.
Definition: u_worker.c:550
void u_worker_group_wait_all(struct u_worker_group *uwg)
Wait for all pushed tasks to be completed, "donates" this thread to the shared thread pool.
Definition: u_worker.c:525
struct u_worker_group * u_worker_group_create(struct u_worker_thread_pool *uwtp)
Create a new worker group.
Definition: u_worker.c:483
static void u_worker_thread_pool_reference(struct u_worker_thread_pool **dst, struct u_worker_thread_pool *src)
Standard Monado reference function.
Definition: u_worker.h:67
void u_worker_thread_pool_destroy(struct u_worker_thread_pool *uwtp)
Internal function, only called by reference.
Definition: u_worker.c:451
static void u_worker_group_reference(struct u_worker_group **dst, struct u_worker_group *src)
Standard Monado reference function.
Definition: u_worker.h:152
struct u_worker_thread_pool * u_worker_thread_pool_create(uint32_t starting_worker_count, uint32_t thread_count, const char *prefix)
Creates a new thread pool to be used by a worker group.
Definition: u_worker.c:399
void u_worker_group_push(struct u_worker_group *uwg, u_worker_group_func_t f, void *data)
Push a new task to worker group.
Definition: u_worker.c:497
static XRT_CHECK_RESULT bool xrt_reference_dec_and_is_zero(struct xrt_reference *xref)
Decrement the reference and return true if the value is now zero.
Definition: xrt_defines.h:1598
static void xrt_reference_inc(struct xrt_reference *xref)
Increment the reference, probably want xrt_reference_inc_and_was_zero.
Definition: xrt_defines.h:1561
A worker group where you submit tasks to.
Definition: u_worker.h:102
A worker pool, can shared between multiple groups worker pool.
Definition: u_worker.h:33
A base class for reference counted objects.
Definition: xrt_defines.h:96
Common defines and enums for XRT.