2016-06-11 13:10:49 +02: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"
|
2016-06-11 15:40:28 +02:00
|
|
|
#include "u_memory.h"
|
2017-03-08 14:36:17 +11:00
|
|
|
#include "util/u_string.h"
|
2016-06-11 17:51:22 +02:00
|
|
|
|
2017-02-20 15:27:07 +01:00
|
|
|
static void util_queue_killall_and_wait(struct util_queue *queue);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 12:32:02 +11:00
|
|
|
static mtx_t exit_mutex = _MTX_INITIALIZER_NP;
|
2017-02-20 15:27:07 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
atexit_handler(void)
|
|
|
|
{
|
|
|
|
struct util_queue *iter;
|
|
|
|
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&exit_mutex);
|
2017-02-20 15:27:07 +01:00
|
|
|
/* Wait for all queues to assert idle. */
|
|
|
|
LIST_FOR_EACH_ENTRY(iter, &queue_list, head) {
|
|
|
|
util_queue_killall_and_wait(iter);
|
|
|
|
}
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&exit_mutex);
|
2017-02-20 15:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
global_init(void)
|
|
|
|
{
|
|
|
|
LIST_INITHEAD(&queue_list);
|
|
|
|
atexit(atexit_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_to_atexit_list(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
call_once(&atexit_once_flag, global_init);
|
|
|
|
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&exit_mutex);
|
2017-02-20 15:27:07 +01:00
|
|
|
LIST_ADD(&queue->head, &queue_list);
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&exit_mutex);
|
2017-02-20 15:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_from_atexit_list(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
struct util_queue *iter, *tmp;
|
|
|
|
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&exit_mutex);
|
2017-02-20 15:27:07 +01:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(iter, tmp, &queue_list, head) {
|
|
|
|
if (iter == queue) {
|
|
|
|
LIST_DEL(&iter->head);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&exit_mutex);
|
2017-02-20 15:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-20 18:42:41 +01:00
|
|
|
* util_queue_fence
|
2017-02-20 15:27:07 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-11 17:51:22 +02:00
|
|
|
static void
|
|
|
|
util_queue_fence_signal(struct util_queue_fence *fence)
|
|
|
|
{
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&fence->mutex);
|
2016-06-11 17:51:22 +02:00
|
|
|
fence->signalled = true;
|
2017-03-05 10:41:33 +11:00
|
|
|
cnd_broadcast(&fence->cond);
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&fence->mutex);
|
2016-06-11 17:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-02-20 18:42:41 +01:00
|
|
|
util_queue_fence_wait(struct util_queue_fence *fence)
|
2016-06-11 17:51:22 +02:00
|
|
|
{
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&fence->mutex);
|
2016-06-11 17:51:22 +02:00
|
|
|
while (!fence->signalled)
|
2017-03-05 10:41:31 +11:00
|
|
|
cnd_wait(&fence->cond, &fence->mutex);
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&fence->mutex);
|
2016-06-11 17:51:22 +02:00
|
|
|
}
|
2016-06-11 13:10:49 +02:00
|
|
|
|
2017-02-20 18:42:41 +01:00
|
|
|
void
|
|
|
|
util_queue_fence_init(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
memset(fence, 0, sizeof(*fence));
|
2017-03-05 12:00:15 +11:00
|
|
|
(void) mtx_init(&fence->mutex, mtx_plain);
|
2017-03-05 10:41:29 +11:00
|
|
|
cnd_init(&fence->cond);
|
2017-02-20 18:42:41 +01:00
|
|
|
fence->signalled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_fence_destroy(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
assert(fence->signalled);
|
2017-03-05 10:41:30 +11:00
|
|
|
cnd_destroy(&fence->cond);
|
2017-03-05 12:32:04 +11:00
|
|
|
mtx_destroy(&fence->mutex);
|
2017-02-20 18:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* util_queue implementation
|
|
|
|
*/
|
|
|
|
|
2016-06-11 15:40:28 +02:00
|
|
|
struct thread_input {
|
|
|
|
struct util_queue *queue;
|
|
|
|
int thread_index;
|
|
|
|
};
|
|
|
|
|
2017-03-06 11:58:26 +11:00
|
|
|
static int
|
|
|
|
util_queue_thread_func(void *input)
|
2016-06-11 13:10:49 +02:00
|
|
|
{
|
2016-06-11 15:40:28 +02:00
|
|
|
struct util_queue *queue = ((struct thread_input*)input)->queue;
|
|
|
|
int thread_index = ((struct thread_input*)input)->thread_index;
|
|
|
|
|
|
|
|
FREE(input);
|
2016-06-11 13:10:49 +02:00
|
|
|
|
2016-06-12 13:36:39 +02:00
|
|
|
if (queue->name) {
|
|
|
|
char name[16];
|
|
|
|
util_snprintf(name, sizeof(name), "%s:%i", queue->name, thread_index);
|
|
|
|
pipe_thread_setname(name);
|
|
|
|
}
|
|
|
|
|
2016-06-11 13:10:49 +02:00
|
|
|
while (1) {
|
|
|
|
struct util_queue_job job;
|
|
|
|
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&queue->lock);
|
2016-06-17 01:33:12 +02:00
|
|
|
assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs);
|
|
|
|
|
|
|
|
/* wait if the queue is empty */
|
|
|
|
while (!queue->kill_threads && queue->num_queued == 0)
|
2017-03-05 10:41:31 +11:00
|
|
|
cnd_wait(&queue->has_queued_cond, &queue->lock);
|
2016-06-17 01:33:12 +02:00
|
|
|
|
|
|
|
if (queue->kill_threads) {
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 13:10:49 +02:00
|
|
|
break;
|
2016-06-17 01:33:12 +02:00
|
|
|
}
|
2016-06-11 13:10:49 +02:00
|
|
|
|
2016-06-12 12:54:42 +02:00
|
|
|
job = queue->jobs[queue->read_idx];
|
2016-06-11 17:28:52 +02:00
|
|
|
memset(&queue->jobs[queue->read_idx], 0, sizeof(struct util_queue_job));
|
2016-06-12 12:54:42 +02:00
|
|
|
queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
|
2016-06-11 13:10:49 +02:00
|
|
|
|
2016-06-17 01:33:12 +02:00
|
|
|
queue->num_queued--;
|
2017-03-05 10:41:32 +11:00
|
|
|
cnd_signal(&queue->has_space_cond);
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 13:10:49 +02:00
|
|
|
|
|
|
|
if (job.job) {
|
2016-06-11 17:28:52 +02:00
|
|
|
job.execute(job.job, thread_index);
|
2016-06-11 17:51:22 +02:00
|
|
|
util_queue_fence_signal(job.fence);
|
2016-07-13 12:17:05 -04:00
|
|
|
if (job.cleanup)
|
|
|
|
job.cleanup(job.job, thread_index);
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* signal remaining jobs before terminating */
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
while (queue->jobs[queue->read_idx].job) {
|
2016-06-11 17:51:22 +02:00
|
|
|
util_queue_fence_signal(queue->jobs[queue->read_idx].fence);
|
|
|
|
|
2016-06-12 12:54:42 +02:00
|
|
|
queue->jobs[queue->read_idx].job = NULL;
|
|
|
|
queue->read_idx = (queue->read_idx + 1) % queue->max_jobs;
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
2017-02-20 15:27:07 +01:00
|
|
|
queue->num_queued = 0; /* reset this when exiting the thread */
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 13:10:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-12 12:54:42 +02:00
|
|
|
bool
|
2016-06-11 13:10:49 +02:00
|
|
|
util_queue_init(struct util_queue *queue,
|
2016-06-12 13:36:39 +02:00
|
|
|
const char *name,
|
2016-06-12 12:54:42 +02:00
|
|
|
unsigned max_jobs,
|
2016-06-11 17:28:52 +02:00
|
|
|
unsigned num_threads)
|
2016-06-11 13:10:49 +02:00
|
|
|
{
|
2016-06-11 15:40:28 +02:00
|
|
|
unsigned i;
|
|
|
|
|
2016-06-11 13:10:49 +02:00
|
|
|
memset(queue, 0, sizeof(*queue));
|
2016-06-12 13:36:39 +02:00
|
|
|
queue->name = name;
|
2016-06-11 15:40:28 +02:00
|
|
|
queue->num_threads = num_threads;
|
2016-06-12 12:54:42 +02:00
|
|
|
queue->max_jobs = max_jobs;
|
|
|
|
|
|
|
|
queue->jobs = (struct util_queue_job*)
|
|
|
|
CALLOC(max_jobs, sizeof(struct util_queue_job));
|
|
|
|
if (!queue->jobs)
|
|
|
|
goto fail;
|
|
|
|
|
2017-03-05 12:00:15 +11:00
|
|
|
(void) mtx_init(&queue->lock, mtx_plain);
|
2016-06-17 01:33:12 +02:00
|
|
|
|
|
|
|
queue->num_queued = 0;
|
2017-03-05 10:41:29 +11:00
|
|
|
cnd_init(&queue->has_queued_cond);
|
|
|
|
cnd_init(&queue->has_space_cond);
|
2016-06-12 12:54:42 +02:00
|
|
|
|
2017-03-05 12:39:49 +11:00
|
|
|
queue->threads = (thrd_t*)CALLOC(num_threads, sizeof(thrd_t));
|
2016-06-11 15:40:28 +02:00
|
|
|
if (!queue->threads)
|
2016-06-12 12:54:42 +02:00
|
|
|
goto fail;
|
|
|
|
|
2016-06-11 15:40:28 +02:00
|
|
|
/* start threads */
|
|
|
|
for (i = 0; i < num_threads; i++) {
|
|
|
|
struct thread_input *input = MALLOC_STRUCT(thread_input);
|
|
|
|
input->queue = queue;
|
|
|
|
input->thread_index = i;
|
|
|
|
|
|
|
|
queue->threads[i] = pipe_thread_create(util_queue_thread_func, input);
|
|
|
|
|
|
|
|
if (!queue->threads[i]) {
|
|
|
|
FREE(input);
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
/* no threads created, fail */
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
/* at least one thread created, so use it */
|
2017-02-26 02:44:07 +02:00
|
|
|
queue->num_threads = i;
|
2016-06-11 15:40:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 15:27:07 +01:00
|
|
|
|
|
|
|
add_to_atexit_list(queue);
|
2016-06-12 12:54:42 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
2016-06-11 15:40:28 +02:00
|
|
|
FREE(queue->threads);
|
|
|
|
|
2016-06-12 12:54:42 +02:00
|
|
|
if (queue->jobs) {
|
2017-03-05 10:41:30 +11:00
|
|
|
cnd_destroy(&queue->has_space_cond);
|
|
|
|
cnd_destroy(&queue->has_queued_cond);
|
2017-03-05 12:32:04 +11:00
|
|
|
mtx_destroy(&queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
FREE(queue->jobs);
|
|
|
|
}
|
|
|
|
/* also util_queue_is_initialized can be used to check for success */
|
|
|
|
memset(queue, 0, sizeof(*queue));
|
|
|
|
return false;
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
|
2017-02-20 15:27:07 +01:00
|
|
|
static void
|
|
|
|
util_queue_killall_and_wait(struct util_queue *queue)
|
2016-06-11 13:10:49 +02:00
|
|
|
{
|
2016-06-11 15:40:28 +02:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Signal all threads to terminate. */
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&queue->lock);
|
2016-06-11 15:40:28 +02:00
|
|
|
queue->kill_threads = 1;
|
2017-03-05 10:41:33 +11:00
|
|
|
cnd_broadcast(&queue->has_queued_cond);
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 15:40:28 +02:00
|
|
|
|
|
|
|
for (i = 0; i < queue->num_threads; i++)
|
2017-03-06 11:58:27 +11:00
|
|
|
thrd_join(queue->threads[i], NULL);
|
2017-02-26 02:44:06 +02:00
|
|
|
queue->num_threads = 0;
|
2017-02-20 15:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_destroy(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
util_queue_killall_and_wait(queue);
|
|
|
|
remove_from_atexit_list(queue);
|
2016-06-11 15:40:28 +02:00
|
|
|
|
2017-03-05 10:41:30 +11:00
|
|
|
cnd_destroy(&queue->has_space_cond);
|
|
|
|
cnd_destroy(&queue->has_queued_cond);
|
2017-03-05 12:32:04 +11:00
|
|
|
mtx_destroy(&queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
FREE(queue->jobs);
|
2016-06-11 15:40:28 +02:00
|
|
|
FREE(queue->threads);
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_add_job(struct util_queue *queue,
|
|
|
|
void *job,
|
2016-06-11 17:28:52 +02:00
|
|
|
struct util_queue_fence *fence,
|
2016-07-13 12:17:05 -04:00
|
|
|
util_queue_execute_func execute,
|
|
|
|
util_queue_execute_func cleanup)
|
2016-06-11 13:10:49 +02:00
|
|
|
{
|
2016-06-12 12:54:42 +02:00
|
|
|
struct util_queue_job *ptr;
|
2016-06-11 17:51:22 +02:00
|
|
|
|
|
|
|
assert(fence->signalled);
|
|
|
|
fence->signalled = false;
|
2016-06-11 13:10:49 +02:00
|
|
|
|
2017-03-05 12:12:30 +11:00
|
|
|
mtx_lock(&queue->lock);
|
2016-06-17 01:33:12 +02:00
|
|
|
assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs);
|
|
|
|
|
2016-06-11 13:10:49 +02:00
|
|
|
/* if the queue is full, wait until there is space */
|
2016-06-17 01:33:12 +02:00
|
|
|
while (queue->num_queued == queue->max_jobs)
|
2017-03-05 10:41:31 +11:00
|
|
|
cnd_wait(&queue->has_space_cond, &queue->lock);
|
2016-06-11 13:10:49 +02:00
|
|
|
|
2016-06-12 12:54:42 +02:00
|
|
|
ptr = &queue->jobs[queue->write_idx];
|
|
|
|
assert(ptr->job == NULL);
|
|
|
|
ptr->job = job;
|
|
|
|
ptr->fence = fence;
|
2016-06-11 17:28:52 +02:00
|
|
|
ptr->execute = execute;
|
2016-07-13 12:17:05 -04:00
|
|
|
ptr->cleanup = cleanup;
|
2016-06-12 12:54:42 +02:00
|
|
|
queue->write_idx = (queue->write_idx + 1) % queue->max_jobs;
|
2016-06-17 01:33:12 +02:00
|
|
|
|
|
|
|
queue->num_queued++;
|
2017-03-05 10:41:32 +11:00
|
|
|
cnd_signal(&queue->has_queued_cond);
|
2017-03-05 12:32:06 +11:00
|
|
|
mtx_unlock(&queue->lock);
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
2017-02-11 20:51:41 +01: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;
|
|
|
|
|
|
|
|
return pipe_thread_get_time_nano(queue->threads[thread_index]);
|
|
|
|
}
|