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"
36 static void util_queue_killall_and_wait(struct util_queue
*queue
);
38 /****************************************************************************
39 * Wait for all queues to assert idle when exit() is called.
41 * Otherwise, C++ static variable destructors can be called while threads
42 * are using the static variables.
45 static once_flag atexit_once_flag
= ONCE_FLAG_INIT
;
46 static struct list_head queue_list
;
47 static mtx_t exit_mutex
= _MTX_INITIALIZER_NP
;
52 struct util_queue
*iter
;
54 mtx_lock(&exit_mutex
);
55 /* Wait for all queues to assert idle. */
56 LIST_FOR_EACH_ENTRY(iter
, &queue_list
, head
) {
57 util_queue_killall_and_wait(iter
);
59 mtx_unlock(&exit_mutex
);
65 LIST_INITHEAD(&queue_list
);
66 atexit(atexit_handler
);
70 add_to_atexit_list(struct util_queue
*queue
)
72 call_once(&atexit_once_flag
, global_init
);
74 mtx_lock(&exit_mutex
);
75 LIST_ADD(&queue
->head
, &queue_list
);
76 mtx_unlock(&exit_mutex
);
80 remove_from_atexit_list(struct util_queue
*queue
)
82 struct util_queue
*iter
, *tmp
;
84 mtx_lock(&exit_mutex
);
85 LIST_FOR_EACH_ENTRY_SAFE(iter
, tmp
, &queue_list
, head
) {
87 LIST_DEL(&iter
->head
);
91 mtx_unlock(&exit_mutex
);
94 /****************************************************************************
98 #ifdef UTIL_QUEUE_FENCE_FUTEX
100 do_futex_fence_wait(struct util_queue_fence
*fence
,
101 bool timeout
, int64_t abs_timeout
)
103 uint32_t v
= fence
->val
;
105 ts
.tv_sec
= abs_timeout
/ (1000*1000*1000);
106 ts
.tv_nsec
= abs_timeout
% (1000*1000*1000);
110 v
= p_atomic_cmpxchg(&fence
->val
, 1, 2);
115 int r
= futex_wait(&fence
->val
, 2, timeout
? &ts
: NULL
);
116 if (timeout
&& r
< 0) {
117 if (errno
== ETIMEDOUT
)
128 _util_queue_fence_wait(struct util_queue_fence
*fence
)
130 do_futex_fence_wait(fence
, false, 0);
134 _util_queue_fence_wait_timeout(struct util_queue_fence
*fence
,
137 return do_futex_fence_wait(fence
, true, abs_timeout
);
142 #ifdef UTIL_QUEUE_FENCE_STANDARD
144 util_queue_fence_signal(struct util_queue_fence
*fence
)
146 mtx_lock(&fence
->mutex
);
147 fence
->signalled
= true;
148 cnd_broadcast(&fence
->cond
);
149 mtx_unlock(&fence
->mutex
);
153 _util_queue_fence_wait(struct util_queue_fence
*fence
)
155 mtx_lock(&fence
->mutex
);
156 while (!fence
->signalled
)
157 cnd_wait(&fence
->cond
, &fence
->mutex
);
158 mtx_unlock(&fence
->mutex
);
162 _util_queue_fence_wait_timeout(struct util_queue_fence
*fence
,
165 /* This terrible hack is made necessary by the fact that we really want an
166 * internal interface consistent with os_time_*, but cnd_timedwait is spec'd
167 * to be relative to the TIME_UTC clock.
169 int64_t rel
= abs_timeout
- os_time_get_nano();
174 timespec_get(&ts
, TIME_UTC
);
176 ts
.tv_sec
+= abs_timeout
/ (1000*1000*1000);
177 ts
.tv_nsec
+= abs_timeout
% (1000*1000*1000);
178 if (ts
.tv_nsec
>= (1000*1000*1000)) {
180 ts
.tv_nsec
-= (1000*1000*1000);
183 mtx_lock(&fence
->mutex
);
184 while (!fence
->signalled
) {
185 if (cnd_timedwait(&fence
->cond
, &fence
->mutex
, &ts
) != thrd_success
)
188 mtx_unlock(&fence
->mutex
);
191 return fence
->signalled
;
195 util_queue_fence_init(struct util_queue_fence
*fence
)
197 memset(fence
, 0, sizeof(*fence
));
198 (void) mtx_init(&fence
->mutex
, mtx_plain
);
199 cnd_init(&fence
->cond
);
200 fence
->signalled
= true;
204 util_queue_fence_destroy(struct util_queue_fence
*fence
)
206 assert(fence
->signalled
);
208 /* Ensure that another thread is not in the middle of
209 * util_queue_fence_signal (having set the fence to signalled but still
210 * holding the fence mutex).
212 * A common contract between threads is that as soon as a fence is signalled
213 * by thread A, thread B is allowed to destroy it. Since
214 * util_queue_fence_is_signalled does not lock the fence mutex (for
215 * performance reasons), we must do so here.
217 mtx_lock(&fence
->mutex
);
218 mtx_unlock(&fence
->mutex
);
220 cnd_destroy(&fence
->cond
);
221 mtx_destroy(&fence
->mutex
);
225 /****************************************************************************
226 * util_queue implementation
229 struct thread_input
{
230 struct util_queue
*queue
;
235 util_queue_thread_func(void *input
)
237 struct util_queue
*queue
= ((struct thread_input
*)input
)->queue
;
238 int thread_index
= ((struct thread_input
*)input
)->thread_index
;
242 #ifdef HAVE_PTHREAD_SETAFFINITY
243 if (queue
->flags
& UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY
) {
244 /* Don't inherit the thread affinity from the parent thread.
249 for (unsigned i
= 0; i
< CPU_SETSIZE
; i
++)
252 pthread_setaffinity_np(pthread_self(), sizeof(cpuset
), &cpuset
);
256 if (strlen(queue
->name
) > 0) {
258 util_snprintf(name
, sizeof(name
), "%s%i", queue
->name
, thread_index
);
259 u_thread_setname(name
);
263 struct util_queue_job job
;
265 mtx_lock(&queue
->lock
);
266 assert(queue
->num_queued
>= 0 && queue
->num_queued
<= queue
->max_jobs
);
268 /* wait if the queue is empty */
269 while (!queue
->kill_threads
&& queue
->num_queued
== 0)
270 cnd_wait(&queue
->has_queued_cond
, &queue
->lock
);
272 if (queue
->kill_threads
) {
273 mtx_unlock(&queue
->lock
);
277 job
= queue
->jobs
[queue
->read_idx
];
278 memset(&queue
->jobs
[queue
->read_idx
], 0, sizeof(struct util_queue_job
));
279 queue
->read_idx
= (queue
->read_idx
+ 1) % queue
->max_jobs
;
282 cnd_signal(&queue
->has_space_cond
);
283 mtx_unlock(&queue
->lock
);
286 job
.execute(job
.job
, thread_index
);
287 util_queue_fence_signal(job
.fence
);
289 job
.cleanup(job
.job
, thread_index
);
293 /* signal remaining jobs before terminating */
294 mtx_lock(&queue
->lock
);
295 for (unsigned i
= queue
->read_idx
; i
!= queue
->write_idx
;
296 i
= (i
+ 1) % queue
->max_jobs
) {
297 if (queue
->jobs
[i
].job
) {
298 util_queue_fence_signal(queue
->jobs
[i
].fence
);
299 queue
->jobs
[i
].job
= NULL
;
302 queue
->read_idx
= queue
->write_idx
;
303 queue
->num_queued
= 0;
304 mtx_unlock(&queue
->lock
);
309 util_queue_init(struct util_queue
*queue
,
312 unsigned num_threads
,
317 /* Form the thread name from process_name and name, limited to 13
318 * characters. Characters 14-15 are reserved for the thread number.
319 * Character 16 should be 0. Final form: "process:name12"
321 * If name is too long, it's truncated. If any space is left, the process
324 const char *process_name
= util_get_process_name();
325 int process_len
= process_name
? strlen(process_name
) : 0;
326 int name_len
= strlen(name
);
327 const int max_chars
= sizeof(queue
->name
) - 1;
329 name_len
= MIN2(name_len
, max_chars
);
331 /* See if there is any space left for the process name, reserve 1 for
333 process_len
= MIN2(process_len
, max_chars
- name_len
- 1);
334 process_len
= MAX2(process_len
, 0);
336 memset(queue
, 0, sizeof(*queue
));
339 util_snprintf(queue
->name
, sizeof(queue
->name
), "%.*s:%s",
340 process_len
, process_name
, name
);
342 util_snprintf(queue
->name
, sizeof(queue
->name
), "%s", name
);
345 queue
->flags
= flags
;
346 queue
->num_threads
= num_threads
;
347 queue
->max_jobs
= max_jobs
;
349 queue
->jobs
= (struct util_queue_job
*)
350 calloc(max_jobs
, sizeof(struct util_queue_job
));
354 (void) mtx_init(&queue
->lock
, mtx_plain
);
355 (void) mtx_init(&queue
->finish_lock
, mtx_plain
);
357 queue
->num_queued
= 0;
358 cnd_init(&queue
->has_queued_cond
);
359 cnd_init(&queue
->has_space_cond
);
361 queue
->threads
= (thrd_t
*) calloc(num_threads
, sizeof(thrd_t
));
366 for (i
= 0; i
< num_threads
; i
++) {
367 struct thread_input
*input
=
368 (struct thread_input
*) malloc(sizeof(struct thread_input
));
369 input
->queue
= queue
;
370 input
->thread_index
= i
;
372 queue
->threads
[i
] = u_thread_create(util_queue_thread_func
, input
);
374 if (!queue
->threads
[i
]) {
378 /* no threads created, fail */
381 /* at least one thread created, so use it */
382 queue
->num_threads
= i
;
387 if (flags
& UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
) {
388 #if defined(__linux__) && defined(SCHED_IDLE)
389 struct sched_param sched_param
= {0};
391 /* The nice() function can only set a maximum of 19.
392 * SCHED_IDLE is the same as nice = 20.
394 * Note that Linux only allows decreasing the priority. The original
395 * priority can't be restored.
397 pthread_setschedparam(queue
->threads
[i
], SCHED_IDLE
, &sched_param
);
402 add_to_atexit_list(queue
);
406 free(queue
->threads
);
409 cnd_destroy(&queue
->has_space_cond
);
410 cnd_destroy(&queue
->has_queued_cond
);
411 mtx_destroy(&queue
->lock
);
414 /* also util_queue_is_initialized can be used to check for success */
415 memset(queue
, 0, sizeof(*queue
));
420 util_queue_killall_and_wait(struct util_queue
*queue
)
424 /* Signal all threads to terminate. */
425 mtx_lock(&queue
->lock
);
426 queue
->kill_threads
= 1;
427 cnd_broadcast(&queue
->has_queued_cond
);
428 mtx_unlock(&queue
->lock
);
430 for (i
= 0; i
< queue
->num_threads
; i
++)
431 thrd_join(queue
->threads
[i
], NULL
);
432 queue
->num_threads
= 0;
436 util_queue_destroy(struct util_queue
*queue
)
438 util_queue_killall_and_wait(queue
);
439 remove_from_atexit_list(queue
);
441 cnd_destroy(&queue
->has_space_cond
);
442 cnd_destroy(&queue
->has_queued_cond
);
443 mtx_destroy(&queue
->finish_lock
);
444 mtx_destroy(&queue
->lock
);
446 free(queue
->threads
);
450 util_queue_add_job(struct util_queue
*queue
,
452 struct util_queue_fence
*fence
,
453 util_queue_execute_func execute
,
454 util_queue_execute_func cleanup
)
456 struct util_queue_job
*ptr
;
458 mtx_lock(&queue
->lock
);
459 if (queue
->kill_threads
) {
460 mtx_unlock(&queue
->lock
);
461 /* well no good option here, but any leaks will be
462 * short-lived as things are shutting down..
467 util_queue_fence_reset(fence
);
469 assert(queue
->num_queued
>= 0 && queue
->num_queued
<= queue
->max_jobs
);
471 if (queue
->num_queued
== queue
->max_jobs
) {
472 if (queue
->flags
& UTIL_QUEUE_INIT_RESIZE_IF_FULL
) {
473 /* If the queue is full, make it larger to avoid waiting for a free
476 unsigned new_max_jobs
= queue
->max_jobs
+ 8;
477 struct util_queue_job
*jobs
=
478 (struct util_queue_job
*)calloc(new_max_jobs
,
479 sizeof(struct util_queue_job
));
482 /* Copy all queued jobs into the new list. */
483 unsigned num_jobs
= 0;
484 unsigned i
= queue
->read_idx
;
487 jobs
[num_jobs
++] = queue
->jobs
[i
];
488 i
= (i
+ 1) % queue
->max_jobs
;
489 } while (i
!= queue
->write_idx
);
491 assert(num_jobs
== queue
->num_queued
);
496 queue
->write_idx
= num_jobs
;
497 queue
->max_jobs
= new_max_jobs
;
499 /* Wait until there is a free slot. */
500 while (queue
->num_queued
== queue
->max_jobs
)
501 cnd_wait(&queue
->has_space_cond
, &queue
->lock
);
505 ptr
= &queue
->jobs
[queue
->write_idx
];
506 assert(ptr
->job
== NULL
);
509 ptr
->execute
= execute
;
510 ptr
->cleanup
= cleanup
;
511 queue
->write_idx
= (queue
->write_idx
+ 1) % queue
->max_jobs
;
514 cnd_signal(&queue
->has_queued_cond
);
515 mtx_unlock(&queue
->lock
);
519 * Remove a queued job. If the job hasn't started execution, it's removed from
520 * the queue. If the job has started execution, the function waits for it to
523 * In all cases, the fence is signalled when the function returns.
525 * The function can be used when destroying an object associated with the job
526 * when you don't care about the job completion state.
529 util_queue_drop_job(struct util_queue
*queue
, struct util_queue_fence
*fence
)
531 bool removed
= false;
533 if (util_queue_fence_is_signalled(fence
))
536 mtx_lock(&queue
->lock
);
537 for (unsigned i
= queue
->read_idx
; i
!= queue
->write_idx
;
538 i
= (i
+ 1) % queue
->max_jobs
) {
539 if (queue
->jobs
[i
].fence
== fence
) {
540 if (queue
->jobs
[i
].cleanup
)
541 queue
->jobs
[i
].cleanup(queue
->jobs
[i
].job
, -1);
543 /* Just clear it. The threads will treat as a no-op job. */
544 memset(&queue
->jobs
[i
], 0, sizeof(queue
->jobs
[i
]));
549 mtx_unlock(&queue
->lock
);
552 util_queue_fence_signal(fence
);
554 util_queue_fence_wait(fence
);
558 util_queue_finish_execute(void *data
, int num_thread
)
560 util_barrier
*barrier
= data
;
561 util_barrier_wait(barrier
);
565 * Wait until all previously added jobs have completed.
568 util_queue_finish(struct util_queue
*queue
)
570 util_barrier barrier
;
571 struct util_queue_fence
*fences
= malloc(queue
->num_threads
* sizeof(*fences
));
573 util_barrier_init(&barrier
, queue
->num_threads
);
575 /* If 2 threads were adding jobs for 2 different barries at the same time,
576 * a deadlock would happen, because 1 barrier requires that all threads
577 * wait for it exclusively.
579 mtx_lock(&queue
->finish_lock
);
581 for (unsigned i
= 0; i
< queue
->num_threads
; ++i
) {
582 util_queue_fence_init(&fences
[i
]);
583 util_queue_add_job(queue
, &barrier
, &fences
[i
], util_queue_finish_execute
, NULL
);
586 for (unsigned i
= 0; i
< queue
->num_threads
; ++i
) {
587 util_queue_fence_wait(&fences
[i
]);
588 util_queue_fence_destroy(&fences
[i
]);
590 mtx_unlock(&queue
->finish_lock
);
592 util_barrier_destroy(&barrier
);
598 util_queue_get_thread_time_nano(struct util_queue
*queue
, unsigned thread_index
)
600 /* Allow some flexibility by not raising an error. */
601 if (thread_index
>= queue
->num_threads
)
604 return u_thread_get_time_nano(queue
->threads
[thread_index
]);