Monado OpenXR Runtime
u_worker.hpp
Go to the documentation of this file.
1// Copyright 2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief C++ wrappers for workers.
6 * @author Jakob Bornecrantz <jakob@collabora.com>
7 *
8 * @ingroup aux_util
9 */
10
11#pragma once
12
13#include "util/u_worker.h"
14
15#include <vector>
16#include <cassert>
17#include <functional>
18
19
20namespace xrt::auxiliary::util {
21
22class TaskCollection;
23class SharedThreadGroup;
24
25/*!
26 * Wrapper around @ref u_worker_thread_pool.
27 *
28 * @ingroup aux_util
29 */
31{
32private:
33 u_worker_thread_pool *mPool = nullptr;
34
35
36public:
38 {
39 u_worker_thread_pool_reference(&mPool, copy.mPool);
40 }
41
42 /*!
43 * Take a C thread pool as argument in case the pool is shared between
44 * different C++ components over C interfaces, or created externally.
45 */
47 {
49 }
50
51 /*!
52 * @copydoc u_worker_thread_pool_create
53 */
54 SharedThreadPool(uint32_t starting_worker_count, uint32_t thread_count, const char *prefix)
55 {
56 mPool = u_worker_thread_pool_create(starting_worker_count, thread_count, prefix);
57 }
58
60 {
61 u_worker_thread_pool_reference(&mPool, nullptr);
62 }
63
64 SharedThreadPool &
65 operator=(const SharedThreadPool &other)
66 {
67 if (this == &other) {
68 return *this;
69 }
70
71 u_worker_thread_pool_reference(&mPool, other.mPool);
72 return *this;
73 }
74
75 friend SharedThreadGroup;
76
77 // No default constructor.
78 SharedThreadPool() = delete;
79 // No move.
80 SharedThreadPool(SharedThreadPool &&) = delete;
81 // No move assign.
82 SharedThreadPool &
83 operator=(SharedThreadPool &&) = delete;
84};
85
86/*!
87 * Wrapper around @ref u_worker_group, use @ref TaskCollection to dispatch work.
88 *
89 * @ingroup aux_util
90 */
92{
93private:
94 u_worker_group *mGroup = nullptr;
95
96
97public:
99 {
100 mGroup = u_worker_group_create(stp.mPool);
101 }
102
104 {
105 u_worker_group_reference(&mGroup, nullptr);
106 }
107
108 friend TaskCollection;
109
110 // No default constructor.
111 SharedThreadGroup() = delete;
112 // Do not move or copy the shared thread group.
113 SharedThreadGroup(SharedThreadGroup const &) = delete;
116 operator=(SharedThreadGroup const &) = delete;
118 operator=(SharedThreadGroup &&) = delete;
119};
120
121/*!
122 * Class to let users fall into a pit of success by
123 * being designed as a one shot dispatcher instance.
124 *
125 * @ingroup aux_util
126 */
128{
129public:
130 typedef std::function<void()> Functor;
131
132
133private:
134 static constexpr size_t kSize = 16;
135
136 Functor mFunctors[kSize] = {};
137 u_worker_group *mGroup = nullptr;
138
139
140public:
141 /*!
142 * Give all Functors when constructed, some what partially
143 * avoids use after leaving scope issues of function delegates.
144 */
145 TaskCollection(SharedThreadGroup const &stc, std::vector<Functor> const &funcs)
146 {
147 assert(funcs.size() <= kSize);
148
149 u_worker_group_reference(&mGroup, stc.mGroup);
150
151 for (size_t i = 0; i < kSize && i < funcs.size(); i++) {
152 mFunctors[i] = funcs[i];
153 u_worker_group_push(mGroup, &cCallback, &mFunctors[i]);
154 }
155 }
156
158 {
159 // Also unreferences the group.
160 waitAll();
161 }
162
163 /*!
164 * Waits for all given tasks to complete, also frees the group.
165 */
166 void
168 {
169 if (mGroup == nullptr) {
170 return;
171 }
173 u_worker_group_reference(&mGroup, nullptr);
174 }
175
176
177 // Do not move or copy the task collection.
178 TaskCollection(TaskCollection const &) = delete;
179 TaskCollection(TaskCollection &&) = delete;
181 operator=(TaskCollection const &) = delete;
183 operator=(TaskCollection &&) = delete;
184
185
186private:
187 static void
188 cCallback(void *data_ptr);
189};
190
191} // namespace xrt::auxiliary::util
Wrapper around u_worker_group, use TaskCollection to dispatch work.
Definition: u_worker.hpp:92
Wrapper around u_worker_thread_pool.
Definition: u_worker.hpp:31
SharedThreadPool(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.hpp:54
SharedThreadPool(u_worker_thread_pool *uwtp)
Take a C thread pool as argument in case the pool is shared between different C++ components over C i...
Definition: u_worker.hpp:46
Class to let users fall into a pit of success by being designed as a one shot dispatcher instance.
Definition: u_worker.hpp:128
void waitAll()
Waits for all given tasks to complete, also frees the group.
Definition: u_worker.hpp:167
TaskCollection(SharedThreadGroup const &stc, std::vector< Functor > const &funcs)
Give all Functors when constructed, some what partially avoids use after leaving scope issues of func...
Definition: u_worker.hpp:145
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
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
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
Worker and threading pool.