blob: 5943df4fcbbf00976af01f9bf1ab7851fada32b4 [file] [log] [blame]
Marek Olšák562cb032016-06-11 13:10:49 +02001/*
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
Timothy Arceri04ec4db2017-03-08 15:20:33 +110036#include <string.h>
37
Nicolai Hähnled1ff0822017-10-22 17:38:31 +020038#include "util/futex.h"
Marek Olšák4aea8fe2017-02-20 15:27:07 +010039#include "util/list.h"
Nicolai Hähnled1ff0822017-10-22 17:38:31 +020040#include "util/macros.h"
Nicolai Hähnlef53570a2017-11-10 10:40:41 +010041#include "util/os_time.h"
Nicolai Hähnled1ff0822017-10-22 17:38:31 +020042#include "util/u_atomic.h"
Timothy Arceri04ec4db2017-03-08 15:20:33 +110043#include "util/u_thread.h"
Marek Olšák562cb032016-06-11 13:10:49 +020044
Timothy Arceri13d69a82017-03-10 13:28:53 +110045#ifdef __cplusplus
46extern "C" {
47#endif
48
Marek Olšák89b6c932017-05-31 22:04:29 +020049#define UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY (1 << 0)
Marek Olšák59ad7692017-07-10 21:17:04 +020050#define UTIL_QUEUE_INIT_RESIZE_IF_FULL (1 << 1)
Marek Olšákd8774512018-10-01 15:51:06 -040051#define UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY (1 << 2)
Marek Olšák89b6c932017-05-31 22:04:29 +020052
Jan Beich46c36892020-06-02 00:28:09 +000053#if UTIL_FUTEX_SUPPORTED
Nicolai Hähnled1ff0822017-10-22 17:38:31 +020054#define UTIL_QUEUE_FENCE_FUTEX
55#else
56#define UTIL_QUEUE_FENCE_STANDARD
57#endif
58
59#ifdef UTIL_QUEUE_FENCE_FUTEX
60/* Job completion fence.
61 * Put this into your job structure.
62 */
63struct util_queue_fence {
64 /* The fence can be in one of three states:
65 * 0 - signaled
66 * 1 - unsignaled
67 * 2 - unsignaled, may have waiters
68 */
69 uint32_t val;
70};
71
72static inline void
73util_queue_fence_init(struct util_queue_fence *fence)
74{
75 fence->val = 0;
76}
77
78static inline void
79util_queue_fence_destroy(struct util_queue_fence *fence)
80{
81 assert(fence->val == 0);
82 /* no-op */
83}
84
85static inline void
Nicolai Hähnled1ff0822017-10-22 17:38:31 +020086util_queue_fence_signal(struct util_queue_fence *fence)
87{
88 uint32_t val = p_atomic_xchg(&fence->val, 0);
89
90 assert(val != 0);
91
92 if (val == 2)
93 futex_wake(&fence->val, INT_MAX);
94}
95
96/**
97 * Move \p fence back into unsignalled state.
98 *
99 * \warning The caller must ensure that no other thread may currently be
100 * waiting (or about to wait) on the fence.
101 */
102static inline void
103util_queue_fence_reset(struct util_queue_fence *fence)
104{
105#ifdef NDEBUG
106 fence->val = 1;
107#else
108 uint32_t v = p_atomic_xchg(&fence->val, 1);
109 assert(v == 0);
110#endif
111}
112
113static inline bool
114util_queue_fence_is_signalled(struct util_queue_fence *fence)
115{
116 return fence->val == 0;
117}
118#endif
119
120#ifdef UTIL_QUEUE_FENCE_STANDARD
Marek Olšák562cb032016-06-11 13:10:49 +0200121/* Job completion fence.
122 * Put this into your job structure.
123 */
124struct util_queue_fence {
Timothy Arceri2efddc62017-03-05 12:32:01 +1100125 mtx_t mutex;
Timothy Arcerie92293a2017-03-06 10:41:39 +1100126 cnd_t cond;
Marek Olšák4358f6d2016-06-11 17:51:22 +0200127 int signalled;
Marek Olšák562cb032016-06-11 13:10:49 +0200128};
129
Nicolai Hähnleb20f9552017-10-22 17:38:29 +0200130void util_queue_fence_init(struct util_queue_fence *fence);
131void util_queue_fence_destroy(struct util_queue_fence *fence);
Nicolai Hähnle1b9d5ec2017-10-22 17:38:29 +0200132void util_queue_fence_signal(struct util_queue_fence *fence);
Nicolai Hähnleb20f9552017-10-22 17:38:29 +0200133
Nicolai Hähnle574c59d2017-10-22 17:38:30 +0200134/**
135 * Move \p fence back into unsignalled state.
136 *
137 * \warning The caller must ensure that no other thread may currently be
138 * waiting (or about to wait) on the fence.
139 */
140static inline void
141util_queue_fence_reset(struct util_queue_fence *fence)
142{
143 assert(fence->signalled);
144 fence->signalled = 0;
145}
146
Nicolai Hähnleb20f9552017-10-22 17:38:29 +0200147static inline bool
148util_queue_fence_is_signalled(struct util_queue_fence *fence)
149{
150 return fence->signalled != 0;
151}
Nicolai Hähnled1ff0822017-10-22 17:38:31 +0200152#endif
Nicolai Hähnleb20f9552017-10-22 17:38:29 +0200153
Nicolai Hähnlee3a80132017-10-22 17:38:46 +0200154void
155_util_queue_fence_wait(struct util_queue_fence *fence);
156
157static inline void
158util_queue_fence_wait(struct util_queue_fence *fence)
159{
160 if (unlikely(!util_queue_fence_is_signalled(fence)))
161 _util_queue_fence_wait(fence);
162}
163
164bool
165_util_queue_fence_wait_timeout(struct util_queue_fence *fence,
166 int64_t abs_timeout);
167
168/**
169 * Wait for the fence to be signaled with a timeout.
170 *
171 * \param fence the fence
172 * \param abs_timeout the absolute timeout in nanoseconds, relative to the
173 * clock provided by os_time_get_nano.
174 *
175 * \return true if the fence was signaled, false if the timeout occurred.
176 */
177static inline bool
178util_queue_fence_wait_timeout(struct util_queue_fence *fence,
179 int64_t abs_timeout)
180{
181 if (util_queue_fence_is_signalled(fence))
182 return true;
183
Nicolai Hähnlef53570a2017-11-10 10:40:41 +0100184 if (abs_timeout == (int64_t)OS_TIMEOUT_INFINITE) {
185 _util_queue_fence_wait(fence);
186 return true;
187 }
188
Nicolai Hähnlee3a80132017-10-22 17:38:46 +0200189 return _util_queue_fence_wait_timeout(fence, abs_timeout);
190}
191
Marek Olšákcbb5adb2016-06-11 17:28:52 +0200192typedef void (*util_queue_execute_func)(void *job, int thread_index);
193
Marek Olšák562cb032016-06-11 13:10:49 +0200194struct util_queue_job {
195 void *job;
Timothy Arceri89688502019-09-03 13:05:08 +1000196 size_t job_size;
Marek Olšák562cb032016-06-11 13:10:49 +0200197 struct util_queue_fence *fence;
Marek Olšákcbb5adb2016-06-11 17:28:52 +0200198 util_queue_execute_func execute;
Rob Clark44bbfed2016-07-13 12:17:05 -0400199 util_queue_execute_func cleanup;
Marek Olšák562cb032016-06-11 13:10:49 +0200200};
201
202/* Put this into your context. */
203struct util_queue {
Marek Olšákb238e332018-07-03 14:48:16 -0400204 char name[14]; /* 13 characters = the thread name without the index */
Marek Olšákbb111552018-08-06 20:32:31 -0400205 mtx_t finish_lock; /* for util_queue_finish and protects threads/num_threads */
Timothy Arceri2efddc62017-03-05 12:32:01 +1100206 mtx_t lock;
Timothy Arcerie92293a2017-03-06 10:41:39 +1100207 cnd_t has_queued_cond;
208 cnd_t has_space_cond;
Timothy Arcerie5375ba2017-03-05 12:39:49 +1100209 thrd_t *threads;
Marek Olšák59ad7692017-07-10 21:17:04 +0200210 unsigned flags;
Marek Olšák4a067862016-06-17 01:33:12 +0200211 int num_queued;
Marek Olšák050fae32018-08-06 20:40:05 -0400212 unsigned max_threads;
Marek Olšákbb111552018-08-06 20:32:31 -0400213 unsigned num_threads; /* decreasing this number will terminate threads */
Marek Olšákd8367e92016-06-12 12:54:42 +0200214 int max_jobs;
215 int write_idx, read_idx; /* ring buffer pointers */
Timothy Arceri89688502019-09-03 13:05:08 +1000216 size_t total_jobs_size; /* memory use of all jobs in the queue */
Marek Olšákd8367e92016-06-12 12:54:42 +0200217 struct util_queue_job *jobs;
Marek Olšák4aea8fe2017-02-20 15:27:07 +0100218
219 /* for cleanup at exit(), protected by exit_mutex */
220 struct list_head head;
Marek Olšák562cb032016-06-11 13:10:49 +0200221};
222
Marek Olšákd8367e92016-06-12 12:54:42 +0200223bool util_queue_init(struct util_queue *queue,
Marek Olšák2fba0aa2016-06-12 13:36:39 +0200224 const char *name,
Marek Olšákd8367e92016-06-12 12:54:42 +0200225 unsigned max_jobs,
Marek Olšák89b6c932017-05-31 22:04:29 +0200226 unsigned num_threads,
227 unsigned flags);
Marek Olšák562cb032016-06-11 13:10:49 +0200228void util_queue_destroy(struct util_queue *queue);
Marek Olšák562cb032016-06-11 13:10:49 +0200229
Rob Clark44bbfed2016-07-13 12:17:05 -0400230/* optional cleanup callback is called after fence is signaled: */
Marek Olšák562cb032016-06-11 13:10:49 +0200231void util_queue_add_job(struct util_queue *queue,
232 void *job,
Marek Olšákcbb5adb2016-06-11 17:28:52 +0200233 struct util_queue_fence *fence,
Rob Clark44bbfed2016-07-13 12:17:05 -0400234 util_queue_execute_func execute,
Timothy Arceri89688502019-09-03 13:05:08 +1000235 util_queue_execute_func cleanup,
236 const size_t job_size);
Marek Olšák33e507e2017-05-31 16:44:12 +0200237void util_queue_drop_job(struct util_queue *queue,
238 struct util_queue_fence *fence);
Rob Clark44bbfed2016-07-13 12:17:05 -0400239
Nicolai Hähnle185061a2017-10-22 17:38:41 +0200240void util_queue_finish(struct util_queue *queue);
241
Marek Olšák050fae32018-08-06 20:40:05 -0400242/* Adjust the number of active threads. The new number of threads can't be
243 * greater than the initial number of threads at the creation of the queue,
244 * and it can't be less than 1.
245 */
246void
247util_queue_adjust_num_threads(struct util_queue *queue, unsigned num_threads);
248
Marek Olšák626e4ef2017-02-11 20:51:41 +0100249int64_t util_queue_get_thread_time_nano(struct util_queue *queue,
250 unsigned thread_index);
Marek Olšák562cb032016-06-11 13:10:49 +0200251
252/* util_queue needs to be cleared to zeroes for this to work */
253static inline bool
254util_queue_is_initialized(struct util_queue *queue)
255{
Marek Olšák404d0d52016-06-11 15:40:28 +0200256 return queue->threads != NULL;
Marek Olšák562cb032016-06-11 13:10:49 +0200257}
258
Marek Olšák5fa69be2017-06-21 20:45:38 +0200259/* Convenient structure for monitoring the queue externally and passing
260 * the structure between Mesa components. The queue doesn't use it directly.
261 */
262struct util_queue_monitoring
263{
264 /* For querying the thread busyness. */
265 struct util_queue *queue;
266
267 /* Counters updated by the user of the queue. */
268 unsigned num_offloaded_items;
269 unsigned num_direct_items;
270 unsigned num_syncs;
271};
272
Timothy Arceri13d69a82017-03-10 13:28:53 +1100273#ifdef __cplusplus
274}
275#endif
276
Marek Olšák562cb032016-06-11 13:10:49 +0200277#endif