util/queue: Don't crash in util_queue_destroy when init failed

This simplifies the error exit paths for drivers that use these queues.

v2: Move allocation of queue->jobs after initializing the mutxes and
condition variables.  Noticed by Ken.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11229>
This commit is contained in:
Ian Romanick 2021-06-07 12:04:03 -07:00 committed by Marge Bot
parent d2b4a59474
commit d78e980523

View file

@ -445,11 +445,6 @@ util_queue_init(struct util_queue *queue,
queue->max_jobs = max_jobs;
queue->global_data = global_data;
queue->jobs = (struct util_queue_job*)
calloc(max_jobs, sizeof(struct util_queue_job));
if (!queue->jobs)
goto fail;
(void) mtx_init(&queue->lock, mtx_plain);
(void) mtx_init(&queue->finish_lock, mtx_plain);
@ -457,6 +452,11 @@ util_queue_init(struct util_queue *queue,
cnd_init(&queue->has_queued_cond);
cnd_init(&queue->has_space_cond);
queue->jobs = (struct util_queue_job*)
calloc(max_jobs, sizeof(struct util_queue_job));
if (!queue->jobs)
goto fail;
queue->threads = (thrd_t*) calloc(queue->max_threads, sizeof(thrd_t));
if (!queue->threads)
goto fail;
@ -534,7 +534,10 @@ void
util_queue_destroy(struct util_queue *queue)
{
util_queue_kill_threads(queue, 0, false);
remove_from_atexit_list(queue);
/* This makes it safe to call on a queue that failedutil_queue_init. */
if (queue->head.next != NULL)
remove_from_atexit_list(queue);
cnd_destroy(&queue->has_space_cond);
cnd_destroy(&queue->has_queued_cond);