1 /*
2 * Copyright © 2016 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26
27 /* Job queue with execution in a separate thread.
28 *
29 * Jobs can be added from any thread. After that, the wait call can be used
30 * to wait for completion of the job.
31 */
32
33 #ifndef U_QUEUE_H
34 #define U_QUEUE_H
35
36 #include <string.h>
37
38 #include "cnd_monotonic.h"
39 #include "simple_mtx.h"
40 #include "util/futex.h"
41 #include "util/list.h"
42 #include "util/macros.h"
43 #include "util/os_time.h"
44 #include "util/u_atomic.h"
45 #include "util/u_thread.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #define UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY (1 << 0)
52 #define UTIL_QUEUE_INIT_RESIZE_IF_FULL (1 << 1)
53 #define UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY (1 << 2)
54
55 #if UTIL_FUTEX_SUPPORTED
56 #define UTIL_QUEUE_FENCE_FUTEX
57 #else
58 #define UTIL_QUEUE_FENCE_STANDARD
59 #endif
60
61 #ifdef UTIL_QUEUE_FENCE_FUTEX
62 /* Job completion fence.
63 * Put this into your job structure.
64 */
65 struct util_queue_fence {
66 /* The fence can be in one of three states:
67 * 0 - signaled
68 * 1 - unsignaled
69 * 2 - unsignaled, may have waiters
70 */
71 uint32_t val;
72 };
73
74 static inline void
util_queue_fence_init(struct util_queue_fence * fence)75 util_queue_fence_init(struct util_queue_fence *fence)
76 {
77 fence->val = 0;
78 }
79
80 static inline void
util_queue_fence_destroy(struct util_queue_fence * fence)81 util_queue_fence_destroy(struct util_queue_fence *fence)
82 {
83 assert(p_atomic_read_relaxed(&fence->val) == 0);
84 /* no-op */
85 }
86
87 static inline void
util_queue_fence_signal(struct util_queue_fence * fence)88 util_queue_fence_signal(struct util_queue_fence *fence)
89 {
90 uint32_t val = p_atomic_xchg(&fence->val, 0);
91
92 assert(val != 0);
93
94 if (val == 2)
95 futex_wake(&fence->val, INT32_MAX);
96 }
97
98 /**
99 * Move \p fence back into unsignalled state.
100 *
101 * \warning The caller must ensure that no other thread may currently be
102 * waiting (or about to wait) on the fence.
103 */
104 static inline void
util_queue_fence_reset(struct util_queue_fence * fence)105 util_queue_fence_reset(struct util_queue_fence *fence)
106 {
107 #ifdef NDEBUG
108 fence->val = 1;
109 #else
110 uint32_t v = p_atomic_xchg(&fence->val, 1);
111 assert(v == 0);
112 #endif
113 }
114
115 static inline bool
util_queue_fence_is_signalled(struct util_queue_fence * fence)116 util_queue_fence_is_signalled(struct util_queue_fence *fence)
117 {
118 return p_atomic_read_relaxed(&fence->val) == 0;
119 }
120 #endif
121
122 #ifdef UTIL_QUEUE_FENCE_STANDARD
123 /* Job completion fence.
124 * Put this into your job structure.
125 */
126 struct util_queue_fence {
127 mtx_t mutex;
128 struct u_cnd_monotonic cond;
129 int signalled;
130 };
131
132 void util_queue_fence_init(struct util_queue_fence *fence);
133 void util_queue_fence_destroy(struct util_queue_fence *fence);
134 void util_queue_fence_signal(struct util_queue_fence *fence);
135
136 /**
137 * Move \p fence back into unsignalled state.
138 *
139 * \warning The caller must ensure that no other thread may currently be
140 * waiting (or about to wait) on the fence.
141 */
142 #if !THREAD_SANITIZER
143 static inline void
util_queue_fence_reset(struct util_queue_fence * fence)144 util_queue_fence_reset(struct util_queue_fence *fence)
145 {
146 assert(fence->signalled);
147 fence->signalled = 0;
148 }
149
150 static inline bool
util_queue_fence_is_signalled(struct util_queue_fence * fence)151 util_queue_fence_is_signalled(struct util_queue_fence *fence)
152 {
153 return fence->signalled != 0;
154 }
155 #else
156 static inline void
util_queue_fence_reset(struct util_queue_fence * fence)157 util_queue_fence_reset(struct util_queue_fence *fence)
158 {
159 assert(fence->signalled);
160 fence->signalled = 0;
161 }
162
163 static inline bool
util_queue_fence_is_signalled(struct util_queue_fence * fence)164 util_queue_fence_is_signalled(struct util_queue_fence *fence)
165 {
166 mtx_lock(&fence->mutex);
167 bool signalled = fence->signalled != 0;
168 mtx_unlock(&fence->mutex);
169 return signalled;
170 }
171 #endif
172 #endif
173
174 void
175 _util_queue_fence_wait(struct util_queue_fence *fence);
176
177 static inline void
util_queue_fence_wait(struct util_queue_fence * fence)178 util_queue_fence_wait(struct util_queue_fence *fence)
179 {
180 if (unlikely(!util_queue_fence_is_signalled(fence)))
181 _util_queue_fence_wait(fence);
182 }
183
184 bool
185 _util_queue_fence_wait_timeout(struct util_queue_fence *fence,
186 int64_t abs_timeout);
187
188 /**
189 * Wait for the fence to be signaled with a timeout.
190 *
191 * \param fence the fence
192 * \param abs_timeout the absolute timeout in nanoseconds, relative to the
193 * clock provided by os_time_get_nano.
194 *
195 * \return true if the fence was signaled, false if the timeout occurred.
196 */
197 static inline bool
util_queue_fence_wait_timeout(struct util_queue_fence * fence,int64_t abs_timeout)198 util_queue_fence_wait_timeout(struct util_queue_fence *fence,
199 int64_t abs_timeout)
200 {
201 if (util_queue_fence_is_signalled(fence))
202 return true;
203
204 if (abs_timeout == (int64_t)OS_TIMEOUT_INFINITE) {
205 _util_queue_fence_wait(fence);
206 return true;
207 }
208
209 return _util_queue_fence_wait_timeout(fence, abs_timeout);
210 }
211
212 typedef void (*util_queue_execute_func)(void *job, void *gdata, int thread_index);
213
214 struct util_queue_job {
215 void *job;
216 void *global_data;
217 size_t job_size;
218 struct util_queue_fence *fence;
219 util_queue_execute_func execute;
220 util_queue_execute_func cleanup;
221 };
222
223 /* Put this into your context. */
224 struct util_queue {
225 char name[14]; /* 13 characters = the thread name without the index */
226 mtx_t lock;
227 bool create_threads_on_demand;
228 cnd_t has_queued_cond;
229 cnd_t has_space_cond;
230 thrd_t *threads;
231 unsigned flags;
232 int num_queued;
233 unsigned max_threads;
234 unsigned num_threads; /* decreasing this number will terminate threads */
235 int max_jobs;
236 int write_idx, read_idx; /* ring buffer pointers */
237 size_t total_jobs_size; /* memory use of all jobs in the queue */
238 struct util_queue_job *jobs;
239 void *global_data;
240
241 /* for cleanup at exit(), protected by exit_mutex */
242 struct list_head head;
243 };
244
245 bool util_queue_init(struct util_queue *queue,
246 const char *name,
247 unsigned max_jobs,
248 unsigned num_threads,
249 unsigned flags,
250 void *global_data);
251 void util_queue_destroy(struct util_queue *queue);
252
253 /* optional cleanup callback is called after fence is signaled: */
254 void util_queue_add_job(struct util_queue *queue,
255 void *job,
256 struct util_queue_fence *fence,
257 util_queue_execute_func execute,
258 util_queue_execute_func cleanup,
259 const size_t job_size);
260 void util_queue_drop_job(struct util_queue *queue,
261 struct util_queue_fence *fence);
262
263 void util_queue_finish(struct util_queue *queue);
264
265 /* Adjust the number of active threads. The new number of threads can't be
266 * greater than the initial number of threads at the creation of the queue,
267 * and it can't be less than 1.
268 */
269 void
270 util_queue_adjust_num_threads(struct util_queue *queue, unsigned num_threads,
271 bool locked);
272
273 int64_t util_queue_get_thread_time_nano(struct util_queue *queue,
274 unsigned thread_index);
275
276 /* util_queue needs to be cleared to zeroes for this to work */
277 static inline bool
util_queue_is_initialized(struct util_queue * queue)278 util_queue_is_initialized(struct util_queue *queue)
279 {
280 return queue->threads != NULL;
281 }
282
283 /* Convenient structure for monitoring the queue externally and passing
284 * the structure between Mesa components. The queue doesn't use it directly.
285 */
286 struct util_queue_monitoring
287 {
288 /* For querying the thread busyness. */
289 struct util_queue *queue;
290
291 /* Counters updated by the user of the queue. */
292 unsigned num_offloaded_items;
293 unsigned num_direct_items;
294 unsigned num_syncs;
295 unsigned num_batches;
296 };
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302 #endif
303