2 * Copyright © 2016 Advanced Micro Devices, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 /* Job queue with execution in a separate thread.
29 * Jobs can be added from any thread. After that, the wait call can be used
30 * to wait for completion of the job.
38 #include "util/futex.h"
39 #include "util/list.h"
40 #include "util/macros.h"
41 #include "util/u_atomic.h"
42 #include "util/u_thread.h"
48 #define UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY (1 << 0)
49 #define UTIL_QUEUE_INIT_RESIZE_IF_FULL (1 << 1)
51 #if defined(__GNUC__) && defined(HAVE_FUTEX)
52 #define UTIL_QUEUE_FENCE_FUTEX
54 #define UTIL_QUEUE_FENCE_STANDARD
57 #ifdef UTIL_QUEUE_FENCE_FUTEX
58 /* Job completion fence.
59 * Put this into your job structure.
61 struct util_queue_fence
{
62 /* The fence can be in one of three states:
65 * 2 - unsignaled, may have waiters
71 util_queue_fence_init(struct util_queue_fence
*fence
)
77 util_queue_fence_destroy(struct util_queue_fence
*fence
)
79 assert(fence
->val
== 0);
84 util_queue_fence_wait(struct util_queue_fence
*fence
)
86 uint32_t v
= fence
->val
;
93 v
= p_atomic_cmpxchg(&fence
->val
, 1, 2);
98 futex_wait(&fence
->val
, 2);
104 util_queue_fence_signal(struct util_queue_fence
*fence
)
106 uint32_t val
= p_atomic_xchg(&fence
->val
, 0);
111 futex_wake(&fence
->val
, INT_MAX
);
115 * Move \p fence back into unsignalled state.
117 * \warning The caller must ensure that no other thread may currently be
118 * waiting (or about to wait) on the fence.
121 util_queue_fence_reset(struct util_queue_fence
*fence
)
126 uint32_t v
= p_atomic_xchg(&fence
->val
, 1);
132 util_queue_fence_is_signalled(struct util_queue_fence
*fence
)
134 return fence
->val
== 0;
138 #ifdef UTIL_QUEUE_FENCE_STANDARD
139 /* Job completion fence.
140 * Put this into your job structure.
142 struct util_queue_fence
{
148 void util_queue_fence_init(struct util_queue_fence
*fence
);
149 void util_queue_fence_destroy(struct util_queue_fence
*fence
);
150 void util_queue_fence_wait(struct util_queue_fence
*fence
);
151 void util_queue_fence_signal(struct util_queue_fence
*fence
);
154 * Move \p fence back into unsignalled state.
156 * \warning The caller must ensure that no other thread may currently be
157 * waiting (or about to wait) on the fence.
160 util_queue_fence_reset(struct util_queue_fence
*fence
)
162 assert(fence
->signalled
);
163 fence
->signalled
= 0;
167 util_queue_fence_is_signalled(struct util_queue_fence
*fence
)
169 return fence
->signalled
!= 0;
173 typedef void (*util_queue_execute_func
)(void *job
, int thread_index
);
175 struct util_queue_job
{
177 struct util_queue_fence
*fence
;
178 util_queue_execute_func execute
;
179 util_queue_execute_func cleanup
;
182 /* Put this into your context. */
186 cnd_t has_queued_cond
;
187 cnd_t has_space_cond
;
191 unsigned num_threads
;
194 int write_idx
, read_idx
; /* ring buffer pointers */
195 struct util_queue_job
*jobs
;
197 /* for cleanup at exit(), protected by exit_mutex */
198 struct list_head head
;
201 bool util_queue_init(struct util_queue
*queue
,
204 unsigned num_threads
,
206 void util_queue_destroy(struct util_queue
*queue
);
208 /* optional cleanup callback is called after fence is signaled: */
209 void util_queue_add_job(struct util_queue
*queue
,
211 struct util_queue_fence
*fence
,
212 util_queue_execute_func execute
,
213 util_queue_execute_func cleanup
);
214 void util_queue_drop_job(struct util_queue
*queue
,
215 struct util_queue_fence
*fence
);
217 void util_queue_finish(struct util_queue
*queue
);
219 int64_t util_queue_get_thread_time_nano(struct util_queue
*queue
,
220 unsigned thread_index
);
222 /* util_queue needs to be cleared to zeroes for this to work */
224 util_queue_is_initialized(struct util_queue
*queue
)
226 return queue
->threads
!= NULL
;
229 /* Convenient structure for monitoring the queue externally and passing
230 * the structure between Mesa components. The queue doesn't use it directly.
232 struct util_queue_monitoring
234 /* For querying the thread busyness. */
235 struct util_queue
*queue
;
237 /* Counters updated by the user of the queue. */
238 unsigned num_offloaded_items
;
239 unsigned num_direct_items
;