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
31 #include "util/os_time.h"
32 #include "util/u_string.h"
33 #include "util/u_thread.h"
34 #include "u_process.h"
37 util_queue_kill_threads(struct util_queue
*queue
, unsigned keep_num_threads
,
40 /****************************************************************************
41 * Wait for all queues to assert idle when exit() is called.
43 * Otherwise, C++ static variable destructors can be called while threads
44 * are using the static variables.
47 static once_flag atexit_once_flag
= ONCE_FLAG_INIT
;
48 static struct list_head queue_list
;
49 static mtx_t exit_mutex
= _MTX_INITIALIZER_NP
;
54 struct util_queue
*iter
;
56 mtx_lock(&exit_mutex
);
57 /* Wait for all queues to assert idle. */
58 LIST_FOR_EACH_ENTRY(iter
, &queue_list
, head
) {
59 util_queue_kill_threads(iter
, 0, false);
61 mtx_unlock(&exit_mutex
);
67 LIST_INITHEAD(&queue_list
);
68 atexit(atexit_handler
);
72 add_to_atexit_list(struct util_queue
*queue
)
74 call_once(&atexit_once_flag
, global_init
);
76 mtx_lock(&exit_mutex
);
77 LIST_ADD(&queue
->head
, &queue_list
);
78 mtx_unlock(&exit_mutex
);
82 remove_from_atexit_list(struct util_queue
*queue
)
84 struct util_queue
*iter
, *tmp
;
86 mtx_lock(&exit_mutex
);
87 LIST_FOR_EACH_ENTRY_SAFE(iter
, tmp
, &queue_list
, head
) {
89 LIST_DEL(&iter
->head
);
93 mtx_unlock(&exit_mutex
);
96 /****************************************************************************
100 #ifdef UTIL_QUEUE_FENCE_FUTEX
102 do_futex_fence_wait(struct util_queue_fence
*fence
,
103 bool timeout
, int64_t abs_timeout
)
105 uint32_t v
= fence
->val
;
107 ts
.tv_sec
= abs_timeout
/ (1000*1000*1000);
108 ts
.tv_nsec
= abs_timeout
% (1000*1000*1000);
112 v
= p_atomic_cmpxchg(&fence
->val
, 1, 2);
117 int r
= futex_wait(&fence
->val
, 2, timeout
? &ts
: NULL
);
118 if (timeout
&& r
< 0) {
119 if (errno
== ETIMEDOUT
)
130 _util_queue_fence_wait(struct util_queue_fence
*fence
)
132 do_futex_fence_wait(fence
, false, 0);
136 _util_queue_fence_wait_timeout(struct util_queue_fence
*fence
,
139 return do_futex_fence_wait(fence
, true, abs_timeout
);
144 #ifdef UTIL_QUEUE_FENCE_STANDARD
146 util_queue_fence_signal(struct util_queue_fence
*fence
)
148 mtx_lock(&fence
->mutex
);
149 fence
->signalled
= true;
150 cnd_broadcast(&fence
->cond
);
151 mtx_unlock(&fence
->mutex
);
155 _util_queue_fence_wait(struct util_queue_fence
*fence
)
157 mtx_lock(&fence
->mutex
);
158 while (!fence
->signalled
)
159 cnd_wait(&fence
->cond
, &fence
->mutex
);
160 mtx_unlock(&fence
->mutex
);
164 _util_queue_fence_wait_timeout(struct util_queue_fence
*fence
,
167 /* This terrible hack is made necessary by the fact that we really want an
168 * internal interface consistent with os_time_*, but cnd_timedwait is spec'd
169 * to be relative to the TIME_UTC clock.
171 int64_t rel
= abs_timeout
- os_time_get_nano();
176 timespec_get(&ts
, TIME_UTC
);
178 ts
.tv_sec
+= abs_timeout
/ (1000*1000*1000);
179 ts
.tv_nsec
+= abs_timeout
% (1000*1000*1000);
180 if (ts
.tv_nsec
>= (1000*1000*1000)) {
182 ts
.tv_nsec
-= (1000*1000*1000);
185 mtx_lock(&fence
->mutex
);
186 while (!fence
->signalled
) {
187 if (cnd_timedwait(&fence
->cond
, &fence
->mutex
, &ts
) != thrd_success
)
190 mtx_unlock(&fence
->mutex
);
193 return fence
->signalled
;
197 util_queue_fence_init(struct util_queue_fence
*fence
)
199 memset(fence
, 0, sizeof(*fence
));
200 (void) mtx_init(&fence
->mutex
, mtx_plain
);
201 cnd_init(&fence
->cond
);
202 fence
->signalled
= true;
206 util_queue_fence_destroy(struct util_queue_fence
*fence
)
208 assert(fence
->signalled
);
210 /* Ensure that another thread is not in the middle of
211 * util_queue_fence_signal (having set the fence to signalled but still
212 * holding the fence mutex).
214 * A common contract between threads is that as soon as a fence is signalled
215 * by thread A, thread B is allowed to destroy it. Since
216 * util_queue_fence_is_signalled does not lock the fence mutex (for
217 * performance reasons), we must do so here.
219 mtx_lock(&fence
->mutex
);
220 mtx_unlock(&fence
->mutex
);
222 cnd_destroy(&fence
->cond
);
223 mtx_destroy(&fence
->mutex
);
227 /****************************************************************************
228 * util_queue implementation
231 struct thread_input
{
232 struct util_queue
*queue
;
237 util_queue_thread_func(void *input
)
239 struct util_queue
*queue
= ((struct thread_input
*)input
)->queue
;
240 int thread_index
= ((struct thread_input
*)input
)->thread_index
;
244 #ifdef HAVE_PTHREAD_SETAFFINITY
245 if (queue
->flags
& UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY
) {
246 /* Don't inherit the thread affinity from the parent thread.
251 for (unsigned i
= 0; i
< CPU_SETSIZE
; i
++)
254 pthread_setaffinity_np(pthread_self(), sizeof(cpuset
), &cpuset
);
258 if (strlen(queue
->name
) > 0) {
260 util_snprintf(name
, sizeof(name
), "%s%i", queue
->name
, thread_index
);
261 u_thread_setname(name
);
265 struct util_queue_job job
;
267 mtx_lock(&queue
->lock
);
268 assert(queue
->num_queued
>= 0 && queue
->num_queued
<= queue
->max_jobs
);
270 /* wait if the queue is empty */
271 while (thread_index
< queue
->num_threads
&& queue
->num_queued
== 0)
272 cnd_wait(&queue
->has_queued_cond
, &queue
->lock
);
274 /* only kill threads that are above "num_threads" */
275 if (thread_index
>= queue
->num_threads
) {
276 mtx_unlock(&queue
->lock
);
280 job
= queue
->jobs
[queue
->read_idx
];
281 memset(&queue
->jobs
[queue
->read_idx
], 0, sizeof(struct util_queue_job
));
282 queue
->read_idx
= (queue
->read_idx
+ 1) % queue
->max_jobs
;
285 cnd_signal(&queue
->has_space_cond
);
286 mtx_unlock(&queue
->lock
);
289 job
.execute(job
.job
, thread_index
);
290 util_queue_fence_signal(job
.fence
);
292 job
.cleanup(job
.job
, thread_index
);
296 /* signal remaining jobs if all threads are being terminated */
297 mtx_lock(&queue
->lock
);
298 if (queue
->num_threads
== 0) {
299 for (unsigned i
= queue
->read_idx
; i
!= queue
->write_idx
;
300 i
= (i
+ 1) % queue
->max_jobs
) {
301 if (queue
->jobs
[i
].job
) {
302 util_queue_fence_signal(queue
->jobs
[i
].fence
);
303 queue
->jobs
[i
].job
= NULL
;
306 queue
->read_idx
= queue
->write_idx
;
307 queue
->num_queued
= 0;
309 mtx_unlock(&queue
->lock
);
314 util_queue_create_thread(struct util_queue
*queue
, unsigned index
)
316 struct thread_input
*input
=
317 (struct thread_input
*) malloc(sizeof(struct thread_input
));
318 input
->queue
= queue
;
319 input
->thread_index
= index
;
321 queue
->threads
[index
] = u_thread_create(util_queue_thread_func
, input
);
323 if (!queue
->threads
[index
]) {
328 if (queue
->flags
& UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
) {
329 #if defined(__linux__) && defined(SCHED_IDLE)
330 struct sched_param sched_param
= {0};
332 /* The nice() function can only set a maximum of 19.
333 * SCHED_IDLE is the same as nice = 20.
335 * Note that Linux only allows decreasing the priority. The original
336 * priority can't be restored.
338 pthread_setschedparam(queue
->threads
[index
], SCHED_IDLE
, &sched_param
);
345 util_queue_adjust_num_threads(struct util_queue
*queue
, unsigned num_threads
)
347 num_threads
= MIN2(num_threads
, queue
->max_threads
);
348 num_threads
= MAX2(num_threads
, 1);
350 mtx_lock(&queue
->finish_lock
);
351 unsigned old_num_threads
= queue
->num_threads
;
353 if (num_threads
== old_num_threads
) {
354 mtx_unlock(&queue
->finish_lock
);
358 if (num_threads
< old_num_threads
) {
359 util_queue_kill_threads(queue
, num_threads
, true);
360 mtx_unlock(&queue
->finish_lock
);
366 * We need to update num_threads first, because threads terminate
367 * when thread_index < num_threads.
369 queue
->num_threads
= num_threads
;
370 for (unsigned i
= old_num_threads
; i
< num_threads
; i
++) {
371 if (!util_queue_create_thread(queue
, i
))
374 mtx_unlock(&queue
->finish_lock
);
378 util_queue_init(struct util_queue
*queue
,
381 unsigned num_threads
,
386 /* Form the thread name from process_name and name, limited to 13
387 * characters. Characters 14-15 are reserved for the thread number.
388 * Character 16 should be 0. Final form: "process:name12"
390 * If name is too long, it's truncated. If any space is left, the process
393 const char *process_name
= util_get_process_name();
394 int process_len
= process_name
? strlen(process_name
) : 0;
395 int name_len
= strlen(name
);
396 const int max_chars
= sizeof(queue
->name
) - 1;
398 name_len
= MIN2(name_len
, max_chars
);
400 /* See if there is any space left for the process name, reserve 1 for
402 process_len
= MIN2(process_len
, max_chars
- name_len
- 1);
403 process_len
= MAX2(process_len
, 0);
405 memset(queue
, 0, sizeof(*queue
));
408 util_snprintf(queue
->name
, sizeof(queue
->name
), "%.*s:%s",
409 process_len
, process_name
, name
);
411 util_snprintf(queue
->name
, sizeof(queue
->name
), "%s", name
);
414 queue
->flags
= flags
;
415 queue
->max_threads
= num_threads
;
416 queue
->num_threads
= num_threads
;
417 queue
->max_jobs
= max_jobs
;
419 queue
->jobs
= (struct util_queue_job
*)
420 calloc(max_jobs
, sizeof(struct util_queue_job
));
424 (void) mtx_init(&queue
->lock
, mtx_plain
);
425 (void) mtx_init(&queue
->finish_lock
, mtx_plain
);
427 queue
->num_queued
= 0;
428 cnd_init(&queue
->has_queued_cond
);
429 cnd_init(&queue
->has_space_cond
);
431 queue
->threads
= (thrd_t
*) calloc(num_threads
, sizeof(thrd_t
));
436 for (i
= 0; i
< num_threads
; i
++) {
437 if (!util_queue_create_thread(queue
, i
)) {
439 /* no threads created, fail */
442 /* at least one thread created, so use it */
443 queue
->num_threads
= i
;
449 add_to_atexit_list(queue
);
453 free(queue
->threads
);
456 cnd_destroy(&queue
->has_space_cond
);
457 cnd_destroy(&queue
->has_queued_cond
);
458 mtx_destroy(&queue
->lock
);
461 /* also util_queue_is_initialized can be used to check for success */
462 memset(queue
, 0, sizeof(*queue
));
467 util_queue_kill_threads(struct util_queue
*queue
, unsigned keep_num_threads
,
472 /* Signal all threads to terminate. */
474 mtx_lock(&queue
->finish_lock
);
476 if (keep_num_threads
>= queue
->num_threads
) {
477 mtx_unlock(&queue
->finish_lock
);
481 mtx_lock(&queue
->lock
);
482 unsigned old_num_threads
= queue
->num_threads
;
483 /* Setting num_threads is what causes the threads to terminate.
484 * Then cnd_broadcast wakes them up and they will exit their function.
486 queue
->num_threads
= keep_num_threads
;
487 cnd_broadcast(&queue
->has_queued_cond
);
488 mtx_unlock(&queue
->lock
);
490 for (i
= keep_num_threads
; i
< old_num_threads
; i
++)
491 thrd_join(queue
->threads
[i
], NULL
);
494 mtx_unlock(&queue
->finish_lock
);
498 util_queue_destroy(struct util_queue
*queue
)
500 util_queue_kill_threads(queue
, 0, false);
501 remove_from_atexit_list(queue
);
503 cnd_destroy(&queue
->has_space_cond
);
504 cnd_destroy(&queue
->has_queued_cond
);
505 mtx_destroy(&queue
->finish_lock
);
506 mtx_destroy(&queue
->lock
);
508 free(queue
->threads
);
512 util_queue_add_job(struct util_queue
*queue
,
514 struct util_queue_fence
*fence
,
515 util_queue_execute_func execute
,
516 util_queue_execute_func cleanup
)
518 struct util_queue_job
*ptr
;
520 mtx_lock(&queue
->lock
);
521 if (queue
->num_threads
== 0) {
522 mtx_unlock(&queue
->lock
);
523 /* well no good option here, but any leaks will be
524 * short-lived as things are shutting down..
529 util_queue_fence_reset(fence
);
531 assert(queue
->num_queued
>= 0 && queue
->num_queued
<= queue
->max_jobs
);
533 if (queue
->num_queued
== queue
->max_jobs
) {
534 if (queue
->flags
& UTIL_QUEUE_INIT_RESIZE_IF_FULL
) {
535 /* If the queue is full, make it larger to avoid waiting for a free
538 unsigned new_max_jobs
= queue
->max_jobs
+ 8;
539 struct util_queue_job
*jobs
=
540 (struct util_queue_job
*)calloc(new_max_jobs
,
541 sizeof(struct util_queue_job
));
544 /* Copy all queued jobs into the new list. */
545 unsigned num_jobs
= 0;
546 unsigned i
= queue
->read_idx
;
549 jobs
[num_jobs
++] = queue
->jobs
[i
];
550 i
= (i
+ 1) % queue
->max_jobs
;
551 } while (i
!= queue
->write_idx
);
553 assert(num_jobs
== queue
->num_queued
);
558 queue
->write_idx
= num_jobs
;
559 queue
->max_jobs
= new_max_jobs
;
561 /* Wait until there is a free slot. */
562 while (queue
->num_queued
== queue
->max_jobs
)
563 cnd_wait(&queue
->has_space_cond
, &queue
->lock
);
567 ptr
= &queue
->jobs
[queue
->write_idx
];
568 assert(ptr
->job
== NULL
);
571 ptr
->execute
= execute
;
572 ptr
->cleanup
= cleanup
;
573 queue
->write_idx
= (queue
->write_idx
+ 1) % queue
->max_jobs
;
576 cnd_signal(&queue
->has_queued_cond
);
577 mtx_unlock(&queue
->lock
);
581 * Remove a queued job. If the job hasn't started execution, it's removed from
582 * the queue. If the job has started execution, the function waits for it to
585 * In all cases, the fence is signalled when the function returns.
587 * The function can be used when destroying an object associated with the job
588 * when you don't care about the job completion state.
591 util_queue_drop_job(struct util_queue
*queue
, struct util_queue_fence
*fence
)
593 bool removed
= false;
595 if (util_queue_fence_is_signalled(fence
))
598 mtx_lock(&queue
->lock
);
599 for (unsigned i
= queue
->read_idx
; i
!= queue
->write_idx
;
600 i
= (i
+ 1) % queue
->max_jobs
) {
601 if (queue
->jobs
[i
].fence
== fence
) {
602 if (queue
->jobs
[i
].cleanup
)
603 queue
->jobs
[i
].cleanup(queue
->jobs
[i
].job
, -1);
605 /* Just clear it. The threads will treat as a no-op job. */
606 memset(&queue
->jobs
[i
], 0, sizeof(queue
->jobs
[i
]));
611 mtx_unlock(&queue
->lock
);
614 util_queue_fence_signal(fence
);
616 util_queue_fence_wait(fence
);
620 util_queue_finish_execute(void *data
, int num_thread
)
622 util_barrier
*barrier
= data
;
623 util_barrier_wait(barrier
);
627 * Wait until all previously added jobs have completed.
630 util_queue_finish(struct util_queue
*queue
)
632 util_barrier barrier
;
633 struct util_queue_fence
*fences
;
635 /* If 2 threads were adding jobs for 2 different barries at the same time,
636 * a deadlock would happen, because 1 barrier requires that all threads
637 * wait for it exclusively.
639 mtx_lock(&queue
->finish_lock
);
640 fences
= malloc(queue
->num_threads
* sizeof(*fences
));
641 util_barrier_init(&barrier
, queue
->num_threads
);
643 for (unsigned i
= 0; i
< queue
->num_threads
; ++i
) {
644 util_queue_fence_init(&fences
[i
]);
645 util_queue_add_job(queue
, &barrier
, &fences
[i
], util_queue_finish_execute
, NULL
);
648 for (unsigned i
= 0; i
< queue
->num_threads
; ++i
) {
649 util_queue_fence_wait(&fences
[i
]);
650 util_queue_fence_destroy(&fences
[i
]);
652 mtx_unlock(&queue
->finish_lock
);
654 util_barrier_destroy(&barrier
);
660 util_queue_get_thread_time_nano(struct util_queue
*queue
, unsigned thread_index
)
662 /* Allow some flexibility by not raising an error. */
663 if (thread_index
>= queue
->num_threads
)
666 return u_thread_get_time_nano(queue
->threads
[thread_index
]);