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
28 #include "util/u_string.h"
30 static void util_queue_killall_and_wait(struct util_queue
*queue
);
32 /****************************************************************************
33 * Wait for all queues to assert idle when exit() is called.
35 * Otherwise, C++ static variable destructors can be called while threads
36 * are using the static variables.
39 static once_flag atexit_once_flag
= ONCE_FLAG_INIT
;
40 static struct list_head queue_list
;
41 static mtx_t exit_mutex
= _MTX_INITIALIZER_NP
;
46 struct util_queue
*iter
;
48 mtx_lock(&exit_mutex
);
49 /* Wait for all queues to assert idle. */
50 LIST_FOR_EACH_ENTRY(iter
, &queue_list
, head
) {
51 util_queue_killall_and_wait(iter
);
53 mtx_unlock(&exit_mutex
);
59 LIST_INITHEAD(&queue_list
);
60 atexit(atexit_handler
);
64 add_to_atexit_list(struct util_queue
*queue
)
66 call_once(&atexit_once_flag
, global_init
);
68 mtx_lock(&exit_mutex
);
69 LIST_ADD(&queue
->head
, &queue_list
);
70 mtx_unlock(&exit_mutex
);
74 remove_from_atexit_list(struct util_queue
*queue
)
76 struct util_queue
*iter
, *tmp
;
78 mtx_lock(&exit_mutex
);
79 LIST_FOR_EACH_ENTRY_SAFE(iter
, tmp
, &queue_list
, head
) {
81 LIST_DEL(&iter
->head
);
85 mtx_unlock(&exit_mutex
);
88 /****************************************************************************
93 util_queue_fence_signal(struct util_queue_fence
*fence
)
95 mtx_lock(&fence
->mutex
);
96 fence
->signalled
= true;
97 cnd_broadcast(&fence
->cond
);
98 mtx_unlock(&fence
->mutex
);
102 util_queue_fence_wait(struct util_queue_fence
*fence
)
104 mtx_lock(&fence
->mutex
);
105 while (!fence
->signalled
)
106 cnd_wait(&fence
->cond
, &fence
->mutex
);
107 mtx_unlock(&fence
->mutex
);
111 util_queue_fence_init(struct util_queue_fence
*fence
)
113 memset(fence
, 0, sizeof(*fence
));
114 (void) mtx_init(&fence
->mutex
, mtx_plain
);
115 cnd_init(&fence
->cond
);
116 fence
->signalled
= true;
120 util_queue_fence_destroy(struct util_queue_fence
*fence
)
122 assert(fence
->signalled
);
123 cnd_destroy(&fence
->cond
);
124 mtx_destroy(&fence
->mutex
);
127 /****************************************************************************
128 * util_queue implementation
131 struct thread_input
{
132 struct util_queue
*queue
;
137 util_queue_thread_func(void *input
)
139 struct util_queue
*queue
= ((struct thread_input
*)input
)->queue
;
140 int thread_index
= ((struct thread_input
*)input
)->thread_index
;
146 util_snprintf(name
, sizeof(name
), "%s:%i", queue
->name
, thread_index
);
147 u_thread_setname(name
);
151 struct util_queue_job job
;
153 mtx_lock(&queue
->lock
);
154 assert(queue
->num_queued
>= 0 && queue
->num_queued
<= queue
->max_jobs
);
156 /* wait if the queue is empty */
157 while (!queue
->kill_threads
&& queue
->num_queued
== 0)
158 cnd_wait(&queue
->has_queued_cond
, &queue
->lock
);
160 if (queue
->kill_threads
) {
161 mtx_unlock(&queue
->lock
);
165 job
= queue
->jobs
[queue
->read_idx
];
166 memset(&queue
->jobs
[queue
->read_idx
], 0, sizeof(struct util_queue_job
));
167 queue
->read_idx
= (queue
->read_idx
+ 1) % queue
->max_jobs
;
170 cnd_signal(&queue
->has_space_cond
);
171 mtx_unlock(&queue
->lock
);
174 job
.execute(job
.job
, thread_index
);
175 util_queue_fence_signal(job
.fence
);
177 job
.cleanup(job
.job
, thread_index
);
181 /* signal remaining jobs before terminating */
182 mtx_lock(&queue
->lock
);
183 for (unsigned i
= queue
->read_idx
; i
!= queue
->write_idx
;
184 i
= (i
+ 1) % queue
->max_jobs
) {
185 if (queue
->jobs
[i
].job
) {
186 util_queue_fence_signal(queue
->jobs
[i
].fence
);
187 queue
->jobs
[i
].job
= NULL
;
190 queue
->read_idx
= queue
->write_idx
;
191 queue
->num_queued
= 0;
192 mtx_unlock(&queue
->lock
);
197 util_queue_init(struct util_queue
*queue
,
200 unsigned num_threads
,
205 memset(queue
, 0, sizeof(*queue
));
207 queue
->flags
= flags
;
208 queue
->num_threads
= num_threads
;
209 queue
->max_jobs
= max_jobs
;
211 queue
->jobs
= (struct util_queue_job
*)
212 calloc(max_jobs
, sizeof(struct util_queue_job
));
216 (void) mtx_init(&queue
->lock
, mtx_plain
);
218 queue
->num_queued
= 0;
219 cnd_init(&queue
->has_queued_cond
);
220 cnd_init(&queue
->has_space_cond
);
222 queue
->threads
= (thrd_t
*) calloc(num_threads
, sizeof(thrd_t
));
227 for (i
= 0; i
< num_threads
; i
++) {
228 struct thread_input
*input
=
229 (struct thread_input
*) malloc(sizeof(struct thread_input
));
230 input
->queue
= queue
;
231 input
->thread_index
= i
;
233 queue
->threads
[i
] = u_thread_create(util_queue_thread_func
, input
);
235 if (!queue
->threads
[i
]) {
239 /* no threads created, fail */
242 /* at least one thread created, so use it */
243 queue
->num_threads
= i
;
248 if (flags
& UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
) {
249 #if defined(__linux__) && defined(SCHED_IDLE)
250 struct sched_param sched_param
= {0};
252 /* The nice() function can only set a maximum of 19.
253 * SCHED_IDLE is the same as nice = 20.
255 * Note that Linux only allows decreasing the priority. The original
256 * priority can't be restored.
258 pthread_setschedparam(queue
->threads
[i
], SCHED_IDLE
, &sched_param
);
263 add_to_atexit_list(queue
);
267 free(queue
->threads
);
270 cnd_destroy(&queue
->has_space_cond
);
271 cnd_destroy(&queue
->has_queued_cond
);
272 mtx_destroy(&queue
->lock
);
275 /* also util_queue_is_initialized can be used to check for success */
276 memset(queue
, 0, sizeof(*queue
));
281 util_queue_killall_and_wait(struct util_queue
*queue
)
285 /* Signal all threads to terminate. */
286 mtx_lock(&queue
->lock
);
287 queue
->kill_threads
= 1;
288 cnd_broadcast(&queue
->has_queued_cond
);
289 mtx_unlock(&queue
->lock
);
291 for (i
= 0; i
< queue
->num_threads
; i
++)
292 thrd_join(queue
->threads
[i
], NULL
);
293 queue
->num_threads
= 0;
297 util_queue_destroy(struct util_queue
*queue
)
299 util_queue_killall_and_wait(queue
);
300 remove_from_atexit_list(queue
);
302 cnd_destroy(&queue
->has_space_cond
);
303 cnd_destroy(&queue
->has_queued_cond
);
304 mtx_destroy(&queue
->lock
);
306 free(queue
->threads
);
310 util_queue_add_job(struct util_queue
*queue
,
312 struct util_queue_fence
*fence
,
313 util_queue_execute_func execute
,
314 util_queue_execute_func cleanup
)
316 struct util_queue_job
*ptr
;
318 assert(fence
->signalled
);
320 mtx_lock(&queue
->lock
);
321 if (queue
->kill_threads
) {
322 mtx_unlock(&queue
->lock
);
323 /* well no good option here, but any leaks will be
324 * short-lived as things are shutting down..
329 fence
->signalled
= false;
331 assert(queue
->num_queued
>= 0 && queue
->num_queued
<= queue
->max_jobs
);
333 if (queue
->num_queued
== queue
->max_jobs
) {
334 if (queue
->flags
& UTIL_QUEUE_INIT_RESIZE_IF_FULL
) {
335 /* If the queue is full, make it larger to avoid waiting for a free
338 unsigned new_max_jobs
= queue
->max_jobs
+ 8;
339 struct util_queue_job
*jobs
=
340 (struct util_queue_job
*)calloc(new_max_jobs
,
341 sizeof(struct util_queue_job
));
344 /* Copy all queued jobs into the new list. */
345 unsigned num_jobs
= 0;
346 unsigned i
= queue
->read_idx
;
349 jobs
[num_jobs
++] = queue
->jobs
[i
];
350 i
= (i
+ 1) % queue
->max_jobs
;
351 } while (i
!= queue
->write_idx
);
353 assert(num_jobs
== queue
->num_queued
);
358 queue
->write_idx
= num_jobs
;
359 queue
->max_jobs
= new_max_jobs
;
361 /* Wait until there is a free slot. */
362 while (queue
->num_queued
== queue
->max_jobs
)
363 cnd_wait(&queue
->has_space_cond
, &queue
->lock
);
367 ptr
= &queue
->jobs
[queue
->write_idx
];
368 assert(ptr
->job
== NULL
);
371 ptr
->execute
= execute
;
372 ptr
->cleanup
= cleanup
;
373 queue
->write_idx
= (queue
->write_idx
+ 1) % queue
->max_jobs
;
376 cnd_signal(&queue
->has_queued_cond
);
377 mtx_unlock(&queue
->lock
);
381 * Remove a queued job. If the job hasn't started execution, it's removed from
382 * the queue. If the job has started execution, the function waits for it to
385 * In all cases, the fence is signalled when the function returns.
387 * The function can be used when destroying an object associated with the job
388 * when you don't care about the job completion state.
391 util_queue_drop_job(struct util_queue
*queue
, struct util_queue_fence
*fence
)
393 bool removed
= false;
395 if (util_queue_fence_is_signalled(fence
))
398 mtx_lock(&queue
->lock
);
399 for (unsigned i
= queue
->read_idx
; i
!= queue
->write_idx
;
400 i
= (i
+ 1) % queue
->max_jobs
) {
401 if (queue
->jobs
[i
].fence
== fence
) {
402 if (queue
->jobs
[i
].cleanup
)
403 queue
->jobs
[i
].cleanup(queue
->jobs
[i
].job
, -1);
405 /* Just clear it. The threads will treat as a no-op job. */
406 memset(&queue
->jobs
[i
], 0, sizeof(queue
->jobs
[i
]));
411 mtx_unlock(&queue
->lock
);
414 util_queue_fence_signal(fence
);
416 util_queue_fence_wait(fence
);
420 util_queue_get_thread_time_nano(struct util_queue
*queue
, unsigned thread_index
)
422 /* Allow some flexibility by not raising an error. */
423 if (thread_index
>= queue
->num_threads
)
426 return u_thread_get_time_nano(queue
->threads
[thread_index
]);