2016-06-11 12:10:49 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2016 Advanced Micro Devices, Inc.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
|
|
|
|
* AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "u_queue.h"
|
2017-10-22 16:38:41 +01:00
|
|
|
|
2019-08-23 09:35:13 +01:00
|
|
|
#include "c11/threads.h"
|
2020-11-28 09:18:32 +00:00
|
|
|
#include "util/u_cpu_detect.h"
|
2017-10-22 16:38:46 +01:00
|
|
|
#include "util/os_time.h"
|
2017-03-08 03:36:17 +00:00
|
|
|
#include "util/u_string.h"
|
2017-10-22 16:38:41 +01:00
|
|
|
#include "util/u_thread.h"
|
2018-07-10 23:00:13 +01:00
|
|
|
#include "u_process.h"
|
2016-06-11 16:51:22 +01:00
|
|
|
|
2020-05-06 09:03:37 +01:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-09-03 04:05:08 +01:00
|
|
|
/* Define 256MB */
|
|
|
|
#define S_256MB (256 * 1024 * 1024)
|
|
|
|
|
2018-08-07 01:32:31 +01:00
|
|
|
static void
|
2018-08-07 01:40:05 +01:00
|
|
|
util_queue_kill_threads(struct util_queue *queue, unsigned keep_num_threads,
|
|
|
|
bool finish_locked);
|
2017-02-20 14:27:07 +00:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Wait for all queues to assert idle when exit() is called.
|
|
|
|
*
|
|
|
|
* Otherwise, C++ static variable destructors can be called while threads
|
|
|
|
* are using the static variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static once_flag atexit_once_flag = ONCE_FLAG_INIT;
|
|
|
|
static struct list_head queue_list;
|
2017-03-05 01:32:02 +00:00
|
|
|
static mtx_t exit_mutex = _MTX_INITIALIZER_NP;
|
2017-02-20 14:27:07 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
atexit_handler(void)
|
|
|
|
{
|
|
|
|
struct util_queue *iter;
|
|
|
|
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&exit_mutex);
|
2017-02-20 14:27:07 +00:00
|
|
|
/* Wait for all queues to assert idle. */
|
|
|
|
LIST_FOR_EACH_ENTRY(iter, &queue_list, head) {
|
2018-08-07 01:40:05 +01:00
|
|
|
util_queue_kill_threads(iter, 0, false);
|
2017-02-20 14:27:07 +00:00
|
|
|
}
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&exit_mutex);
|
2017-02-20 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
global_init(void)
|
|
|
|
{
|
2019-10-27 22:49:39 +00:00
|
|
|
list_inithead(&queue_list);
|
2017-02-20 14:27:07 +00:00
|
|
|
atexit(atexit_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_to_atexit_list(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
call_once(&atexit_once_flag, global_init);
|
|
|
|
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&exit_mutex);
|
2019-10-27 23:03:21 +00:00
|
|
|
list_add(&queue->head, &queue_list);
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&exit_mutex);
|
2017-02-20 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_from_atexit_list(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
struct util_queue *iter, *tmp;
|
|
|
|
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&exit_mutex);
|
2017-02-20 14:27:07 +00:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(iter, tmp, &queue_list, head) {
|
|
|
|
if (iter == queue) {
|
2019-10-27 23:11:53 +00:00
|
|
|
list_del(&iter->head);
|
2017-02-20 14:27:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&exit_mutex);
|
2017-02-20 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-20 17:42:41 +00:00
|
|
|
* util_queue_fence
|
2017-02-20 14:27:07 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-22 16:38:46 +01:00
|
|
|
#ifdef UTIL_QUEUE_FENCE_FUTEX
|
|
|
|
static bool
|
|
|
|
do_futex_fence_wait(struct util_queue_fence *fence,
|
|
|
|
bool timeout, int64_t abs_timeout)
|
|
|
|
{
|
2021-01-28 17:55:29 +00:00
|
|
|
uint32_t v = p_atomic_read_relaxed(&fence->val);
|
2017-10-22 16:38:46 +01:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = abs_timeout / (1000*1000*1000);
|
|
|
|
ts.tv_nsec = abs_timeout % (1000*1000*1000);
|
|
|
|
|
|
|
|
while (v != 0) {
|
|
|
|
if (v != 2) {
|
|
|
|
v = p_atomic_cmpxchg(&fence->val, 1, 2);
|
|
|
|
if (v == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = futex_wait(&fence->val, 2, timeout ? &ts : NULL);
|
|
|
|
if (timeout && r < 0) {
|
2017-11-13 13:35:50 +00:00
|
|
|
if (errno == ETIMEDOUT)
|
2017-10-22 16:38:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:55:29 +00:00
|
|
|
v = p_atomic_read_relaxed(&fence->val);
|
2017-10-22 16:38:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_util_queue_fence_wait(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
do_futex_fence_wait(fence, false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
_util_queue_fence_wait_timeout(struct util_queue_fence *fence,
|
|
|
|
int64_t abs_timeout)
|
|
|
|
{
|
|
|
|
return do_futex_fence_wait(fence, true, abs_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-10-22 16:38:31 +01:00
|
|
|
#ifdef UTIL_QUEUE_FENCE_STANDARD
|
2017-10-22 16:38:29 +01:00
|
|
|
void
|
2016-06-11 16:51:22 +01:00
|
|
|
util_queue_fence_signal(struct util_queue_fence *fence)
|
|
|
|
{
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&fence->mutex);
|
2016-06-11 16:51:22 +01:00
|
|
|
fence->signalled = true;
|
2017-03-04 23:41:33 +00:00
|
|
|
cnd_broadcast(&fence->cond);
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&fence->mutex);
|
2016-06-11 16:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-22 16:38:46 +01:00
|
|
|
_util_queue_fence_wait(struct util_queue_fence *fence)
|
2016-06-11 16:51:22 +01:00
|
|
|
{
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&fence->mutex);
|
2016-06-11 16:51:22 +01:00
|
|
|
while (!fence->signalled)
|
2017-03-04 23:41:31 +00:00
|
|
|
cnd_wait(&fence->cond, &fence->mutex);
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&fence->mutex);
|
2016-06-11 16:51:22 +01:00
|
|
|
}
|
2016-06-11 12:10:49 +01:00
|
|
|
|
2017-10-22 16:38:46 +01:00
|
|
|
bool
|
|
|
|
_util_queue_fence_wait_timeout(struct util_queue_fence *fence,
|
|
|
|
int64_t abs_timeout)
|
|
|
|
{
|
|
|
|
/* This terrible hack is made necessary by the fact that we really want an
|
|
|
|
* internal interface consistent with os_time_*, but cnd_timedwait is spec'd
|
|
|
|
* to be relative to the TIME_UTC clock.
|
|
|
|
*/
|
|
|
|
int64_t rel = abs_timeout - os_time_get_nano();
|
|
|
|
|
|
|
|
if (rel > 0) {
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
timespec_get(&ts, TIME_UTC);
|
|
|
|
|
|
|
|
ts.tv_sec += abs_timeout / (1000*1000*1000);
|
|
|
|
ts.tv_nsec += abs_timeout % (1000*1000*1000);
|
|
|
|
if (ts.tv_nsec >= (1000*1000*1000)) {
|
|
|
|
ts.tv_sec++;
|
|
|
|
ts.tv_nsec -= (1000*1000*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx_lock(&fence->mutex);
|
|
|
|
while (!fence->signalled) {
|
|
|
|
if (cnd_timedwait(&fence->cond, &fence->mutex, &ts) != thrd_success)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mtx_unlock(&fence->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fence->signalled;
|
|
|
|
}
|
|
|
|
|
2017-02-20 17:42:41 +00:00
|
|
|
void
|
|
|
|
util_queue_fence_init(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
memset(fence, 0, sizeof(*fence));
|
2017-03-05 01:00:15 +00:00
|
|
|
(void) mtx_init(&fence->mutex, mtx_plain);
|
2017-03-04 23:41:29 +00:00
|
|
|
cnd_init(&fence->cond);
|
2017-02-20 17:42:41 +00:00
|
|
|
fence->signalled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_fence_destroy(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
assert(fence->signalled);
|
2017-09-28 16:52:42 +01:00
|
|
|
|
|
|
|
/* Ensure that another thread is not in the middle of
|
|
|
|
* util_queue_fence_signal (having set the fence to signalled but still
|
|
|
|
* holding the fence mutex).
|
|
|
|
*
|
|
|
|
* A common contract between threads is that as soon as a fence is signalled
|
|
|
|
* by thread A, thread B is allowed to destroy it. Since
|
|
|
|
* util_queue_fence_is_signalled does not lock the fence mutex (for
|
|
|
|
* performance reasons), we must do so here.
|
|
|
|
*/
|
|
|
|
mtx_lock(&fence->mutex);
|
|
|
|
mtx_unlock(&fence->mutex);
|
|
|
|
|
2017-03-04 23:41:30 +00:00
|
|
|
cnd_destroy(&fence->cond);
|
2017-03-05 01:32:04 +00:00
|
|
|
mtx_destroy(&fence->mutex);
|
2017-02-20 17:42:41 +00:00
|
|
|
}
|
2017-10-22 16:38:31 +01:00
|
|
|
#endif
|
2017-02-20 17:42:41 +00:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* util_queue implementation
|
|
|
|
*/
|
|
|
|
|
2016-06-11 14:40:28 +01:00
|
|
|
struct thread_input {
|
|
|
|
struct util_queue *queue;
|
|
|
|
int thread_index;
|
|
|
|
};
|
|
|
|
|
2017-03-06 00:58:26 +00:00
|
|
|
static int
|
|
|
|
util_queue_thread_func(void *input)
|
2016-06-11 12:10:49 +01:00
|
|
|
{
|
2016-06-11 14:40:28 +01:00
|
|
|
struct util_queue *queue = ((struct thread_input*)input)->queue;
|
|
|
|
int thread_index = ((struct thread_input*)input)->thread_index;
|
|
|
|
|
2017-03-08 03:58:00 +00:00
|
|
|
free(input);
|
2016-06-11 12:10:49 +01:00
|
|
|
|
2018-10-01 20:51:06 +01:00
|
|
|
if (queue->flags & UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY) {
|
|
|
|
/* Don't inherit the thread affinity from the parent thread.
|
|
|
|
* Set the full mask.
|
|
|
|
*/
|
2020-10-06 23:44:08 +01:00
|
|
|
uint32_t mask[UTIL_MAX_CPUS / 32];
|
2018-10-01 20:51:06 +01:00
|
|
|
|
2020-10-06 23:44:08 +01:00
|
|
|
memset(mask, 0xff, sizeof(mask));
|
2021-02-24 21:59:15 +00:00
|
|
|
|
2020-11-28 09:18:32 +00:00
|
|
|
util_set_current_thread_affinity(mask, NULL,
|
2021-02-25 18:04:50 +00:00
|
|
|
util_get_cpu_caps()->num_cpu_mask_bits);
|
2018-10-01 20:51:06 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 09:03:37 +01:00
|
|
|
#if defined(__linux__)
|
|
|
|
if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) {
|
|
|
|
/* The nice() function can only set a maximum of 19. */
|
|
|
|
setpriority(PRIO_PROCESS, syscall(SYS_gettid), 19);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-05 11:55:43 +01:00
|
|
|
if (strlen(queue->name) > 0) {
|
2016-06-12 12:36:39 +01:00
|
|
|
char name[16];
|
2018-11-20 11:59:28 +00:00
|
|
|
snprintf(name, sizeof(name), "%s%i", queue->name, thread_index);
|
2017-03-08 04:20:33 +00:00
|
|
|
u_thread_setname(name);
|
2016-06-12 12:36:39 +01:00
|
|
|
}
|
|
|
|
|
2016-06-11 12:10:49 +01:00
|
|
|
while (1) {
|
|
|
|
struct util_queue_job job;
|
|
|
|
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&queue->lock);
|
2016-06-17 00:33:12 +01:00
|
|
|
assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs);
|
|
|
|
|
|
|
|
/* wait if the queue is empty */
|
2018-08-07 01:32:31 +01:00
|
|
|
while (thread_index < queue->num_threads && queue->num_queued == 0)
|
2017-03-04 23:41:31 +00:00
|
|
|
cnd_wait(&queue->has_queued_cond, &queue->lock);
|
2016-06-17 00:33:12 +01:00
|
|
|
|
2018-08-07 01:32:31 +01:00
|
|
|
/* only kill threads that are above "num_threads" */
|
|
|
|
if (thread_index >= queue->num_threads) {
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 12:10:49 +01:00
|
|
|
break;
|
2016-06-17 00:33:12 +01:00
|
|
|
}
|
2016-06-11 12:10:49 +01:00
|
|
|
|
2016-06-12 11:54:42 +01:00
|
|
|
job = queue->jobs[queue->read_idx];
|
2016-06-11 16:28:52 +01:00
|
|
|
memset(&queue->jobs[queue->read_idx], 0, sizeof(struct util_queue_job));
|
2016-06-12 11:54:42 +01:00
|
|
|
queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
|
2016-06-11 12:10:49 +01:00
|
|
|
|
2016-06-17 00:33:12 +01:00
|
|
|
queue->num_queued--;
|
2017-03-04 23:41:32 +00:00
|
|
|
cnd_signal(&queue->has_space_cond);
|
2020-03-26 15:50:31 +00:00
|
|
|
if (job.job)
|
|
|
|
queue->total_jobs_size -= job.job_size;
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 12:10:49 +01:00
|
|
|
|
|
|
|
if (job.job) {
|
2021-06-09 23:14:29 +01:00
|
|
|
job.execute(job.job, job.global_data, thread_index);
|
2021-05-09 15:25:00 +01:00
|
|
|
if (job.fence)
|
|
|
|
util_queue_fence_signal(job.fence);
|
2016-07-13 17:17:05 +01:00
|
|
|
if (job.cleanup)
|
2021-06-09 23:14:29 +01:00
|
|
|
job.cleanup(job.job, job.global_data, thread_index);
|
2016-06-11 12:10:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:32:31 +01:00
|
|
|
/* signal remaining jobs if all threads are being terminated */
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&queue->lock);
|
2018-08-07 01:32:31 +01:00
|
|
|
if (queue->num_threads == 0) {
|
|
|
|
for (unsigned i = queue->read_idx; i != queue->write_idx;
|
|
|
|
i = (i + 1) % queue->max_jobs) {
|
|
|
|
if (queue->jobs[i].job) {
|
2021-05-09 15:25:00 +01:00
|
|
|
if (queue->jobs[i].fence)
|
|
|
|
util_queue_fence_signal(queue->jobs[i].fence);
|
2018-08-07 01:32:31 +01:00
|
|
|
queue->jobs[i].job = NULL;
|
|
|
|
}
|
2017-05-31 15:44:12 +01:00
|
|
|
}
|
2018-08-07 01:32:31 +01:00
|
|
|
queue->read_idx = queue->write_idx;
|
|
|
|
queue->num_queued = 0;
|
2016-06-11 12:10:49 +01:00
|
|
|
}
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 12:10:49 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:15:43 +01:00
|
|
|
static bool
|
|
|
|
util_queue_create_thread(struct util_queue *queue, unsigned index)
|
|
|
|
{
|
|
|
|
struct thread_input *input =
|
|
|
|
(struct thread_input *) malloc(sizeof(struct thread_input));
|
|
|
|
input->queue = queue;
|
|
|
|
input->thread_index = index;
|
|
|
|
|
2022-06-14 07:00:57 +01:00
|
|
|
if (thrd_success != u_thread_create(queue->threads + index, util_queue_thread_func, input)) {
|
2018-08-07 01:15:43 +01:00
|
|
|
free(input);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) {
|
2020-05-06 09:03:37 +01:00
|
|
|
#if defined(__linux__) && defined(SCHED_BATCH)
|
2018-08-07 01:15:43 +01:00
|
|
|
struct sched_param sched_param = {0};
|
|
|
|
|
|
|
|
/* The nice() function can only set a maximum of 19.
|
2020-05-06 09:03:37 +01:00
|
|
|
* SCHED_BATCH gives the scheduler a hint that this is a latency
|
|
|
|
* insensitive thread.
|
2018-08-07 01:15:43 +01:00
|
|
|
*
|
|
|
|
* Note that Linux only allows decreasing the priority. The original
|
|
|
|
* priority can't be restored.
|
|
|
|
*/
|
2020-05-06 09:03:37 +01:00
|
|
|
pthread_setschedparam(queue->threads[index], SCHED_BATCH, &sched_param);
|
2018-08-07 01:15:43 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:40:05 +01:00
|
|
|
void
|
|
|
|
util_queue_adjust_num_threads(struct util_queue *queue, unsigned num_threads)
|
|
|
|
{
|
|
|
|
num_threads = MIN2(num_threads, queue->max_threads);
|
|
|
|
num_threads = MAX2(num_threads, 1);
|
|
|
|
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_lock(&queue->finish_lock);
|
2018-08-07 01:40:05 +01:00
|
|
|
unsigned old_num_threads = queue->num_threads;
|
|
|
|
|
|
|
|
if (num_threads == old_num_threads) {
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2018-08-07 01:40:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_threads < old_num_threads) {
|
|
|
|
util_queue_kill_threads(queue, num_threads, true);
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2018-08-07 01:40:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create threads.
|
|
|
|
*
|
|
|
|
* We need to update num_threads first, because threads terminate
|
|
|
|
* when thread_index < num_threads.
|
|
|
|
*/
|
|
|
|
queue->num_threads = num_threads;
|
|
|
|
for (unsigned i = old_num_threads; i < num_threads; i++) {
|
2022-02-18 00:11:17 +00:00
|
|
|
if (!util_queue_create_thread(queue, i)) {
|
|
|
|
queue->num_threads = i;
|
2018-08-07 01:40:05 +01:00
|
|
|
break;
|
2022-02-18 00:11:17 +00:00
|
|
|
}
|
2018-08-07 01:40:05 +01:00
|
|
|
}
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2018-08-07 01:40:05 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 11:54:42 +01:00
|
|
|
bool
|
2016-06-11 12:10:49 +01:00
|
|
|
util_queue_init(struct util_queue *queue,
|
2016-06-12 12:36:39 +01:00
|
|
|
const char *name,
|
2016-06-12 11:54:42 +01:00
|
|
|
unsigned max_jobs,
|
2017-05-31 21:04:29 +01:00
|
|
|
unsigned num_threads,
|
2021-06-09 23:14:29 +01:00
|
|
|
unsigned flags,
|
|
|
|
void *global_data)
|
2016-06-11 12:10:49 +01:00
|
|
|
{
|
2016-06-11 14:40:28 +01:00
|
|
|
unsigned i;
|
|
|
|
|
2018-07-03 19:48:16 +01:00
|
|
|
/* Form the thread name from process_name and name, limited to 13
|
|
|
|
* characters. Characters 14-15 are reserved for the thread number.
|
|
|
|
* Character 16 should be 0. Final form: "process:name12"
|
|
|
|
*
|
|
|
|
* If name is too long, it's truncated. If any space is left, the process
|
|
|
|
* name fills it.
|
|
|
|
*/
|
|
|
|
const char *process_name = util_get_process_name();
|
|
|
|
int process_len = process_name ? strlen(process_name) : 0;
|
|
|
|
int name_len = strlen(name);
|
|
|
|
const int max_chars = sizeof(queue->name) - 1;
|
|
|
|
|
|
|
|
name_len = MIN2(name_len, max_chars);
|
|
|
|
|
|
|
|
/* See if there is any space left for the process name, reserve 1 for
|
|
|
|
* the colon. */
|
|
|
|
process_len = MIN2(process_len, max_chars - name_len - 1);
|
|
|
|
process_len = MAX2(process_len, 0);
|
|
|
|
|
2016-06-11 12:10:49 +01:00
|
|
|
memset(queue, 0, sizeof(*queue));
|
2018-07-03 19:48:16 +01:00
|
|
|
|
|
|
|
if (process_len) {
|
2018-11-20 11:59:28 +00:00
|
|
|
snprintf(queue->name, sizeof(queue->name), "%.*s:%s",
|
|
|
|
process_len, process_name, name);
|
2018-07-03 19:48:16 +01:00
|
|
|
} else {
|
2018-11-20 11:59:28 +00:00
|
|
|
snprintf(queue->name, sizeof(queue->name), "%s", name);
|
2018-07-03 19:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:17:04 +01:00
|
|
|
queue->flags = flags;
|
2018-08-07 01:40:05 +01:00
|
|
|
queue->max_threads = num_threads;
|
2021-06-09 12:49:23 +01:00
|
|
|
queue->num_threads = (flags & UTIL_QUEUE_INIT_SCALE_THREADS) ? 1 : num_threads;
|
2016-06-12 11:54:42 +01:00
|
|
|
queue->max_jobs = max_jobs;
|
2021-06-09 23:14:29 +01:00
|
|
|
queue->global_data = global_data;
|
2016-06-12 11:54:42 +01:00
|
|
|
|
2017-03-05 01:00:15 +00:00
|
|
|
(void) mtx_init(&queue->lock, mtx_plain);
|
2021-10-01 20:40:48 +01:00
|
|
|
(void) simple_mtx_init(&queue->finish_lock, mtx_plain);
|
2016-06-17 00:33:12 +01:00
|
|
|
|
|
|
|
queue->num_queued = 0;
|
2017-03-04 23:41:29 +00:00
|
|
|
cnd_init(&queue->has_queued_cond);
|
|
|
|
cnd_init(&queue->has_space_cond);
|
2016-06-12 11:54:42 +01:00
|
|
|
|
2021-06-07 20:04:03 +01:00
|
|
|
queue->jobs = (struct util_queue_job*)
|
|
|
|
calloc(max_jobs, sizeof(struct util_queue_job));
|
|
|
|
if (!queue->jobs)
|
|
|
|
goto fail;
|
|
|
|
|
2021-06-09 12:49:23 +01:00
|
|
|
queue->threads = (thrd_t*) calloc(queue->max_threads, sizeof(thrd_t));
|
2016-06-11 14:40:28 +01:00
|
|
|
if (!queue->threads)
|
2016-06-12 11:54:42 +01:00
|
|
|
goto fail;
|
|
|
|
|
2016-06-11 14:40:28 +01:00
|
|
|
/* start threads */
|
2021-06-09 12:49:23 +01:00
|
|
|
for (i = 0; i < queue->num_threads; i++) {
|
2018-08-07 01:15:43 +01:00
|
|
|
if (!util_queue_create_thread(queue, i)) {
|
2016-06-11 14:40:28 +01:00
|
|
|
if (i == 0) {
|
|
|
|
/* no threads created, fail */
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
/* at least one thread created, so use it */
|
2017-02-26 00:44:07 +00:00
|
|
|
queue->num_threads = i;
|
2016-06-11 14:40:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 14:27:07 +00:00
|
|
|
|
|
|
|
add_to_atexit_list(queue);
|
2016-06-12 11:54:42 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
2017-03-08 03:58:00 +00:00
|
|
|
free(queue->threads);
|
2016-06-11 14:40:28 +01:00
|
|
|
|
2016-06-12 11:54:42 +01:00
|
|
|
if (queue->jobs) {
|
2017-03-04 23:41:30 +00:00
|
|
|
cnd_destroy(&queue->has_space_cond);
|
|
|
|
cnd_destroy(&queue->has_queued_cond);
|
2017-03-05 01:32:04 +00:00
|
|
|
mtx_destroy(&queue->lock);
|
2017-03-08 03:58:00 +00:00
|
|
|
free(queue->jobs);
|
2016-06-12 11:54:42 +01:00
|
|
|
}
|
|
|
|
/* also util_queue_is_initialized can be used to check for success */
|
|
|
|
memset(queue, 0, sizeof(*queue));
|
|
|
|
return false;
|
2016-06-11 12:10:49 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 14:27:07 +00:00
|
|
|
static void
|
2018-08-07 01:40:05 +01:00
|
|
|
util_queue_kill_threads(struct util_queue *queue, unsigned keep_num_threads,
|
|
|
|
bool finish_locked)
|
2016-06-11 12:10:49 +01:00
|
|
|
{
|
2016-06-11 14:40:28 +01:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Signal all threads to terminate. */
|
2018-08-07 01:40:05 +01:00
|
|
|
if (!finish_locked)
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_lock(&queue->finish_lock);
|
2018-08-07 01:32:31 +01:00
|
|
|
|
|
|
|
if (keep_num_threads >= queue->num_threads) {
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2018-08-07 01:32:31 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&queue->lock);
|
2018-08-07 01:32:31 +01:00
|
|
|
unsigned old_num_threads = queue->num_threads;
|
|
|
|
/* Setting num_threads is what causes the threads to terminate.
|
|
|
|
* Then cnd_broadcast wakes them up and they will exit their function.
|
|
|
|
*/
|
|
|
|
queue->num_threads = keep_num_threads;
|
2017-03-04 23:41:33 +00:00
|
|
|
cnd_broadcast(&queue->has_queued_cond);
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 14:40:28 +01:00
|
|
|
|
2018-08-07 01:32:31 +01:00
|
|
|
for (i = keep_num_threads; i < old_num_threads; i++)
|
2017-03-06 00:58:27 +00:00
|
|
|
thrd_join(queue->threads[i], NULL);
|
2018-08-07 01:32:31 +01:00
|
|
|
|
2018-08-07 01:40:05 +01:00
|
|
|
if (!finish_locked)
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2017-02-20 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 12:48:25 +01:00
|
|
|
static void
|
|
|
|
util_queue_finish_execute(void *data, void *gdata, int num_thread)
|
|
|
|
{
|
|
|
|
util_barrier *barrier = data;
|
2021-11-18 15:58:42 +00:00
|
|
|
if (util_barrier_wait(barrier))
|
|
|
|
util_barrier_destroy(barrier);
|
2021-06-09 12:48:25 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 14:27:07 +00:00
|
|
|
void
|
|
|
|
util_queue_destroy(struct util_queue *queue)
|
|
|
|
{
|
2018-08-07 01:40:05 +01:00
|
|
|
util_queue_kill_threads(queue, 0, false);
|
2021-06-07 20:04:03 +01:00
|
|
|
|
2022-02-18 00:04:06 +00:00
|
|
|
/* This makes it safe to call on a queue that failed util_queue_init. */
|
2021-06-07 20:04:03 +01:00
|
|
|
if (queue->head.next != NULL)
|
|
|
|
remove_from_atexit_list(queue);
|
2016-06-11 14:40:28 +01:00
|
|
|
|
2017-03-04 23:41:30 +00:00
|
|
|
cnd_destroy(&queue->has_space_cond);
|
|
|
|
cnd_destroy(&queue->has_queued_cond);
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_destroy(&queue->finish_lock);
|
2017-03-05 01:32:04 +00:00
|
|
|
mtx_destroy(&queue->lock);
|
2017-03-08 03:58:00 +00:00
|
|
|
free(queue->jobs);
|
|
|
|
free(queue->threads);
|
2016-06-11 12:10:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_add_job(struct util_queue *queue,
|
|
|
|
void *job,
|
2016-06-11 16:28:52 +01:00
|
|
|
struct util_queue_fence *fence,
|
2016-07-13 17:17:05 +01:00
|
|
|
util_queue_execute_func execute,
|
2019-09-03 04:05:08 +01:00
|
|
|
util_queue_execute_func cleanup,
|
|
|
|
const size_t job_size)
|
2016-06-11 12:10:49 +01:00
|
|
|
{
|
2016-06-12 11:54:42 +01:00
|
|
|
struct util_queue_job *ptr;
|
2016-06-11 16:51:22 +01:00
|
|
|
|
2017-03-05 01:12:30 +00:00
|
|
|
mtx_lock(&queue->lock);
|
2018-08-07 01:32:31 +01:00
|
|
|
if (queue->num_threads == 0) {
|
util/queue: don't hang at exit
So atexit() is horrible and 4aea8fe7 is probably not a good idea. But
add an extra layer of duct-tape to the problem. Otherwise we hit a
situation where app using an atexit() handler that runs later than ours
doesn't hang when trying to tear down a context.
(gdb) bt
#0 util_queue_killall_and_wait (queue=queue@entry=0x52bc80) at ../../../src/util/u_queue.c:264
#1 0x0000007fb6c380c0 in atexit_handler () at ../../../src/util/u_queue.c:51
#2 0x0000007fb7730e2c in __run_exit_handlers () from /lib64/libc.so.6
#3 0x0000007fb7730e5c in exit () from /lib64/libc.so.6
#4 0x0000007fb7ce17dc in piglit_report_result (result=PIGLIT_PASS) at /home/robclark/src/piglit/tests/util/piglit-util.c:267
#5 0x0000007fb7ef99f8 in process_next_event (x11_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:139
#6 0x0000007fb7ef9a90 in enter_event_loop (winsys_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:153
#7 0x0000007fb7ef8e50 in run_test (gl_fw=0x432c20, argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_winsys_framework.c:88
#8 0x0000007fb7edb890 in piglit_gl_test_run (argc=1, argv=0x7ffffff588, config=0x7ffffff400) at /home/robclark/src/piglit/tests/util/piglit-framework-gl.c:203
#9 0x0000000000401224 in main (argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/bugs/drawbuffer-modes.c:46
(gdb) c
Continuing.
[Thread 0x7fb67580c0 (LWP 3471) exited]
^C
Thread 1 "drawbuffer-mode" received signal SIGINT, Interrupt.
0x0000007fb72dda34 in pthread_cond_wait@@GLIBC_2.17 () from /lib64/libpthread.so.0
(gdb) bt
#0 0x0000007fb72dda34 in pthread_cond_wait@@GLIBC_2.17 () from /lib64/libpthread.so.0
#1 0x0000007fb6c38304 in cnd_wait (mtx=0x5bdc90, cond=0x5bdcc0) at ../../../include/c11/threads_posix.h:159
#2 util_queue_fence_wait (fence=0x5bdc90) at ../../../src/util/u_queue.c:106
#3 0x0000007fb6daac70 in fd_batch_sync (batch=0x5bdc70) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:233
#4 batch_reset (batch=batch@entry=0x5bdc70) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:183
#5 0x0000007fb6daa5e0 in batch_flush (batch=0x5bdc70) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:290
#6 fd_batch_flush (batch=0x5bdc70, sync=<optimized out>) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:308
#7 0x0000007fb6daba2c in fd_bc_flush (cache=0x461220, ctx=0x52b920) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch_cache.c:141
#8 0x0000007fb6dac954 in fd_context_flush (pctx=0x52b920, fence=0x0, flags=<optimized out>) at ../../../../../src/gallium/drivers/freedreno/freedreno_context.c:54
#9 0x0000007fb6b43294 in st_glFlush (ctx=<optimized out>) at ../../../src/mesa/state_tracker/st_cb_flush.c:121
#10 0x0000007fb69a84e8 in _mesa_make_current (newCtx=newCtx@entry=0x0, drawBuffer=drawBuffer@entry=0x0, readBuffer=readBuffer@entry=0x0) at ../../../src/mesa/main/context.c:1654
#11 0x0000007fb6b7ca58 in st_api_make_current (stapi=<optimized out>, stctxi=0x0, stdrawi=0x0, streadi=0x0) at ../../../src/mesa/state_tracker/st_manager.c:827
#12 0x0000007fb6cc87e8 in dri_unbind_context (cPriv=<optimized out>) at ../../../../../src/gallium/state_trackers/dri/dri_context.c:217
#13 0x0000007fb6cc80b0 in driUnbindContext (pcp=0x5271e0) at ../../../../../../src/mesa/drivers/dri/common/dri_util.c:591
#14 0x0000007fb7d1da08 in MakeContextCurrent (dpy=0x433380, draw=0, read=0, gc_user=0x0) at ../../../src/glx/glxcurrent.c:214
#15 0x0000007fb7a8d5e0 in glx_platform_make_current () from /lib64/libwaffle-1.so.0
#16 0x0000007fb7a894e4 in waffle_make_current () from /lib64/libwaffle-1.so.0
#17 0x0000007fb7ef8c60 in piglit_wfl_framework_teardown (wfl_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_wfl_framework.c:628
#18 0x0000007fb7ef939c in piglit_winsys_framework_teardown (winsys_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_winsys_framework.c:238
#19 0x0000007fb7ef9c30 in destroy (gl_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:212
#20 0x0000007fb7edb7c4 in destroy () at /home/robclark/src/piglit/tests/util/piglit-framework-gl.c:184
#21 0x0000007fb7730e2c in __run_exit_handlers () from /lib64/libc.so.6
#22 0x0000007fb7730e5c in exit () from /lib64/libc.so.6
#23 0x0000007fb7ce17dc in piglit_report_result (result=PIGLIT_PASS) at /home/robclark/src/piglit/tests/util/piglit-util.c:267
#24 0x0000007fb7ef99f8 in process_next_event (x11_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:139
#25 0x0000007fb7ef9a90 in enter_event_loop (winsys_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:153
#26 0x0000007fb7ef8e50 in run_test (gl_fw=0x432c20, argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_winsys_framework.c:88
#27 0x0000007fb7edb890 in piglit_gl_test_run (argc=1, argv=0x7ffffff588, config=0x7ffffff400) at /home/robclark/src/piglit/tests/util/piglit-framework-gl.c:203
#28 0x0000000000401224 in main (argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/bugs/drawbuffer-modes.c:46
(gdb) r
Fixes: 4aea8fe7 ("gallium/u_queue: fix random crashes when the app calls exit()")
Signed-off-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
2017-04-18 02:20:37 +01:00
|
|
|
mtx_unlock(&queue->lock);
|
|
|
|
/* well no good option here, but any leaks will be
|
|
|
|
* short-lived as things are shutting down..
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:25:00 +01:00
|
|
|
if (fence)
|
|
|
|
util_queue_fence_reset(fence);
|
util/queue: don't hang at exit
So atexit() is horrible and 4aea8fe7 is probably not a good idea. But
add an extra layer of duct-tape to the problem. Otherwise we hit a
situation where app using an atexit() handler that runs later than ours
doesn't hang when trying to tear down a context.
(gdb) bt
#0 util_queue_killall_and_wait (queue=queue@entry=0x52bc80) at ../../../src/util/u_queue.c:264
#1 0x0000007fb6c380c0 in atexit_handler () at ../../../src/util/u_queue.c:51
#2 0x0000007fb7730e2c in __run_exit_handlers () from /lib64/libc.so.6
#3 0x0000007fb7730e5c in exit () from /lib64/libc.so.6
#4 0x0000007fb7ce17dc in piglit_report_result (result=PIGLIT_PASS) at /home/robclark/src/piglit/tests/util/piglit-util.c:267
#5 0x0000007fb7ef99f8 in process_next_event (x11_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:139
#6 0x0000007fb7ef9a90 in enter_event_loop (winsys_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:153
#7 0x0000007fb7ef8e50 in run_test (gl_fw=0x432c20, argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_winsys_framework.c:88
#8 0x0000007fb7edb890 in piglit_gl_test_run (argc=1, argv=0x7ffffff588, config=0x7ffffff400) at /home/robclark/src/piglit/tests/util/piglit-framework-gl.c:203
#9 0x0000000000401224 in main (argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/bugs/drawbuffer-modes.c:46
(gdb) c
Continuing.
[Thread 0x7fb67580c0 (LWP 3471) exited]
^C
Thread 1 "drawbuffer-mode" received signal SIGINT, Interrupt.
0x0000007fb72dda34 in pthread_cond_wait@@GLIBC_2.17 () from /lib64/libpthread.so.0
(gdb) bt
#0 0x0000007fb72dda34 in pthread_cond_wait@@GLIBC_2.17 () from /lib64/libpthread.so.0
#1 0x0000007fb6c38304 in cnd_wait (mtx=0x5bdc90, cond=0x5bdcc0) at ../../../include/c11/threads_posix.h:159
#2 util_queue_fence_wait (fence=0x5bdc90) at ../../../src/util/u_queue.c:106
#3 0x0000007fb6daac70 in fd_batch_sync (batch=0x5bdc70) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:233
#4 batch_reset (batch=batch@entry=0x5bdc70) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:183
#5 0x0000007fb6daa5e0 in batch_flush (batch=0x5bdc70) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:290
#6 fd_batch_flush (batch=0x5bdc70, sync=<optimized out>) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch.c:308
#7 0x0000007fb6daba2c in fd_bc_flush (cache=0x461220, ctx=0x52b920) at ../../../../../src/gallium/drivers/freedreno/freedreno_batch_cache.c:141
#8 0x0000007fb6dac954 in fd_context_flush (pctx=0x52b920, fence=0x0, flags=<optimized out>) at ../../../../../src/gallium/drivers/freedreno/freedreno_context.c:54
#9 0x0000007fb6b43294 in st_glFlush (ctx=<optimized out>) at ../../../src/mesa/state_tracker/st_cb_flush.c:121
#10 0x0000007fb69a84e8 in _mesa_make_current (newCtx=newCtx@entry=0x0, drawBuffer=drawBuffer@entry=0x0, readBuffer=readBuffer@entry=0x0) at ../../../src/mesa/main/context.c:1654
#11 0x0000007fb6b7ca58 in st_api_make_current (stapi=<optimized out>, stctxi=0x0, stdrawi=0x0, streadi=0x0) at ../../../src/mesa/state_tracker/st_manager.c:827
#12 0x0000007fb6cc87e8 in dri_unbind_context (cPriv=<optimized out>) at ../../../../../src/gallium/state_trackers/dri/dri_context.c:217
#13 0x0000007fb6cc80b0 in driUnbindContext (pcp=0x5271e0) at ../../../../../../src/mesa/drivers/dri/common/dri_util.c:591
#14 0x0000007fb7d1da08 in MakeContextCurrent (dpy=0x433380, draw=0, read=0, gc_user=0x0) at ../../../src/glx/glxcurrent.c:214
#15 0x0000007fb7a8d5e0 in glx_platform_make_current () from /lib64/libwaffle-1.so.0
#16 0x0000007fb7a894e4 in waffle_make_current () from /lib64/libwaffle-1.so.0
#17 0x0000007fb7ef8c60 in piglit_wfl_framework_teardown (wfl_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_wfl_framework.c:628
#18 0x0000007fb7ef939c in piglit_winsys_framework_teardown (winsys_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_winsys_framework.c:238
#19 0x0000007fb7ef9c30 in destroy (gl_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:212
#20 0x0000007fb7edb7c4 in destroy () at /home/robclark/src/piglit/tests/util/piglit-framework-gl.c:184
#21 0x0000007fb7730e2c in __run_exit_handlers () from /lib64/libc.so.6
#22 0x0000007fb7730e5c in exit () from /lib64/libc.so.6
#23 0x0000007fb7ce17dc in piglit_report_result (result=PIGLIT_PASS) at /home/robclark/src/piglit/tests/util/piglit-util.c:267
#24 0x0000007fb7ef99f8 in process_next_event (x11_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:139
#25 0x0000007fb7ef9a90 in enter_event_loop (winsys_fw=0x432c20) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_x11_framework.c:153
#26 0x0000007fb7ef8e50 in run_test (gl_fw=0x432c20, argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/util/piglit-framework-gl/piglit_winsys_framework.c:88
#27 0x0000007fb7edb890 in piglit_gl_test_run (argc=1, argv=0x7ffffff588, config=0x7ffffff400) at /home/robclark/src/piglit/tests/util/piglit-framework-gl.c:203
#28 0x0000000000401224 in main (argc=1, argv=0x7ffffff588) at /home/robclark/src/piglit/tests/bugs/drawbuffer-modes.c:46
(gdb) r
Fixes: 4aea8fe7 ("gallium/u_queue: fix random crashes when the app calls exit()")
Signed-off-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
2017-04-18 02:20:37 +01:00
|
|
|
|
2016-06-17 00:33:12 +01:00
|
|
|
assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs);
|
|
|
|
|
2022-05-02 12:25:03 +01:00
|
|
|
/* Scale the number of threads up if there's already one job waiting. */
|
|
|
|
if (queue->num_queued > 0 &&
|
|
|
|
queue->flags & UTIL_QUEUE_INIT_SCALE_THREADS &&
|
|
|
|
execute != util_queue_finish_execute &&
|
|
|
|
queue->num_threads < queue->max_threads) {
|
|
|
|
util_queue_adjust_num_threads(queue, queue->num_threads + 1);
|
|
|
|
}
|
2021-06-09 12:49:23 +01:00
|
|
|
|
2017-07-10 20:17:04 +01:00
|
|
|
if (queue->num_queued == queue->max_jobs) {
|
2019-09-03 04:05:08 +01:00
|
|
|
if (queue->flags & UTIL_QUEUE_INIT_RESIZE_IF_FULL &&
|
|
|
|
queue->total_jobs_size + job_size < S_256MB) {
|
2017-07-10 20:17:04 +01:00
|
|
|
/* If the queue is full, make it larger to avoid waiting for a free
|
|
|
|
* slot.
|
|
|
|
*/
|
|
|
|
unsigned new_max_jobs = queue->max_jobs + 8;
|
|
|
|
struct util_queue_job *jobs =
|
|
|
|
(struct util_queue_job*)calloc(new_max_jobs,
|
|
|
|
sizeof(struct util_queue_job));
|
|
|
|
assert(jobs);
|
|
|
|
|
|
|
|
/* Copy all queued jobs into the new list. */
|
|
|
|
unsigned num_jobs = 0;
|
|
|
|
unsigned i = queue->read_idx;
|
|
|
|
|
|
|
|
do {
|
|
|
|
jobs[num_jobs++] = queue->jobs[i];
|
|
|
|
i = (i + 1) % queue->max_jobs;
|
|
|
|
} while (i != queue->write_idx);
|
|
|
|
|
|
|
|
assert(num_jobs == queue->num_queued);
|
|
|
|
|
|
|
|
free(queue->jobs);
|
|
|
|
queue->jobs = jobs;
|
|
|
|
queue->read_idx = 0;
|
|
|
|
queue->write_idx = num_jobs;
|
|
|
|
queue->max_jobs = new_max_jobs;
|
|
|
|
} else {
|
|
|
|
/* Wait until there is a free slot. */
|
|
|
|
while (queue->num_queued == queue->max_jobs)
|
|
|
|
cnd_wait(&queue->has_space_cond, &queue->lock);
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 12:10:49 +01:00
|
|
|
|
2016-06-12 11:54:42 +01:00
|
|
|
ptr = &queue->jobs[queue->write_idx];
|
|
|
|
assert(ptr->job == NULL);
|
|
|
|
ptr->job = job;
|
2021-06-09 23:14:29 +01:00
|
|
|
ptr->global_data = queue->global_data;
|
2016-06-12 11:54:42 +01:00
|
|
|
ptr->fence = fence;
|
2016-06-11 16:28:52 +01:00
|
|
|
ptr->execute = execute;
|
2016-07-13 17:17:05 +01:00
|
|
|
ptr->cleanup = cleanup;
|
2019-09-03 04:05:08 +01:00
|
|
|
ptr->job_size = job_size;
|
|
|
|
|
2016-06-12 11:54:42 +01:00
|
|
|
queue->write_idx = (queue->write_idx + 1) % queue->max_jobs;
|
2019-09-03 04:05:08 +01:00
|
|
|
queue->total_jobs_size += ptr->job_size;
|
2016-06-17 00:33:12 +01:00
|
|
|
|
|
|
|
queue->num_queued++;
|
2017-03-04 23:41:32 +00:00
|
|
|
cnd_signal(&queue->has_queued_cond);
|
2017-03-05 01:32:06 +00:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 12:10:49 +01:00
|
|
|
}
|
2017-02-11 19:51:41 +00:00
|
|
|
|
2017-05-31 15:44:12 +01:00
|
|
|
/**
|
|
|
|
* Remove a queued job. If the job hasn't started execution, it's removed from
|
|
|
|
* the queue. If the job has started execution, the function waits for it to
|
|
|
|
* complete.
|
|
|
|
*
|
|
|
|
* In all cases, the fence is signalled when the function returns.
|
|
|
|
*
|
|
|
|
* The function can be used when destroying an object associated with the job
|
|
|
|
* when you don't care about the job completion state.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
util_queue_drop_job(struct util_queue *queue, struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
bool removed = false;
|
|
|
|
|
|
|
|
if (util_queue_fence_is_signalled(fence))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mtx_lock(&queue->lock);
|
|
|
|
for (unsigned i = queue->read_idx; i != queue->write_idx;
|
|
|
|
i = (i + 1) % queue->max_jobs) {
|
|
|
|
if (queue->jobs[i].fence == fence) {
|
|
|
|
if (queue->jobs[i].cleanup)
|
2021-06-09 23:14:29 +01:00
|
|
|
queue->jobs[i].cleanup(queue->jobs[i].job, queue->global_data, -1);
|
2017-05-31 15:44:12 +01:00
|
|
|
|
|
|
|
/* Just clear it. The threads will treat as a no-op job. */
|
|
|
|
memset(&queue->jobs[i], 0, sizeof(queue->jobs[i]));
|
|
|
|
removed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtx_unlock(&queue->lock);
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
util_queue_fence_signal(fence);
|
|
|
|
else
|
|
|
|
util_queue_fence_wait(fence);
|
|
|
|
}
|
|
|
|
|
2017-10-22 16:38:41 +01:00
|
|
|
/**
|
|
|
|
* Wait until all previously added jobs have completed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
util_queue_finish(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
util_barrier barrier;
|
2018-08-07 01:34:24 +01:00
|
|
|
struct util_queue_fence *fences;
|
2017-10-22 16:38:41 +01:00
|
|
|
|
2018-04-24 22:01:35 +01:00
|
|
|
/* If 2 threads were adding jobs for 2 different barries at the same time,
|
|
|
|
* a deadlock would happen, because 1 barrier requires that all threads
|
|
|
|
* wait for it exclusively.
|
|
|
|
*/
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_lock(&queue->finish_lock);
|
2019-10-24 02:01:38 +01:00
|
|
|
|
|
|
|
/* The number of threads can be changed to 0, e.g. by the atexit handler. */
|
|
|
|
if (!queue->num_threads) {
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2019-10-24 02:01:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:34:24 +01:00
|
|
|
fences = malloc(queue->num_threads * sizeof(*fences));
|
|
|
|
util_barrier_init(&barrier, queue->num_threads);
|
2018-04-24 22:01:35 +01:00
|
|
|
|
2017-10-22 16:38:41 +01:00
|
|
|
for (unsigned i = 0; i < queue->num_threads; ++i) {
|
|
|
|
util_queue_fence_init(&fences[i]);
|
2019-09-03 04:05:08 +01:00
|
|
|
util_queue_add_job(queue, &barrier, &fences[i],
|
|
|
|
util_queue_finish_execute, NULL, 0);
|
2017-10-22 16:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < queue->num_threads; ++i) {
|
|
|
|
util_queue_fence_wait(&fences[i]);
|
|
|
|
util_queue_fence_destroy(&fences[i]);
|
|
|
|
}
|
2021-10-01 20:40:48 +01:00
|
|
|
simple_mtx_unlock(&queue->finish_lock);
|
2017-10-22 16:38:41 +01:00
|
|
|
|
|
|
|
free(fences);
|
|
|
|
}
|
|
|
|
|
2017-02-11 19:51:41 +00:00
|
|
|
int64_t
|
|
|
|
util_queue_get_thread_time_nano(struct util_queue *queue, unsigned thread_index)
|
|
|
|
{
|
|
|
|
/* Allow some flexibility by not raising an error. */
|
|
|
|
if (thread_index >= queue->num_threads)
|
|
|
|
return 0;
|
|
|
|
|
2020-10-06 23:59:31 +01:00
|
|
|
return util_thread_get_time_nano(queue->threads[thread_index]);
|
2017-02-11 19:51:41 +00:00
|
|
|
}
|