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 17:51:22 +02:00
|
|
|
#include "os/os_time.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
util_queue_fence_signal(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
pipe_mutex_lock(fence->mutex);
|
|
|
|
fence->signalled = true;
|
|
|
|
pipe_condvar_broadcast(fence->cond);
|
|
|
|
pipe_mutex_unlock(fence->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_job_wait(struct util_queue_fence *fence)
|
|
|
|
{
|
|
|
|
if (fence->signalled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pipe_mutex_lock(fence->mutex);
|
|
|
|
while (!fence->signalled)
|
|
|
|
pipe_condvar_wait(fence->cond, fence->mutex);
|
|
|
|
pipe_mutex_unlock(fence->mutex);
|
|
|
|
}
|
2016-06-11 13:10:49 +02:00
|
|
|
|
|
|
|
static PIPE_THREAD_ROUTINE(util_queue_thread_func, param)
|
|
|
|
{
|
|
|
|
struct util_queue *queue = (struct util_queue*)param;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct util_queue_job job;
|
|
|
|
|
|
|
|
pipe_semaphore_wait(&queue->queued);
|
|
|
|
if (queue->kill_thread)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pipe_mutex_lock(queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
job = queue->jobs[queue->read_idx];
|
|
|
|
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
|
|
|
pipe_mutex_unlock(queue->lock);
|
|
|
|
|
|
|
|
pipe_semaphore_signal(&queue->has_space);
|
|
|
|
|
|
|
|
if (job.job) {
|
|
|
|
queue->execute_job(job.job);
|
2016-06-11 17:51:22 +02:00
|
|
|
util_queue_fence_signal(job.fence);
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* signal remaining jobs before terminating */
|
|
|
|
pipe_mutex_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
|
|
|
}
|
|
|
|
pipe_mutex_unlock(queue->lock);
|
|
|
|
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 12:54:42 +02:00
|
|
|
unsigned max_jobs,
|
2016-06-11 13:10:49 +02:00
|
|
|
void (*execute_job)(void *))
|
|
|
|
{
|
|
|
|
memset(queue, 0, sizeof(*queue));
|
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;
|
|
|
|
|
2016-06-11 13:10:49 +02:00
|
|
|
queue->execute_job = execute_job;
|
|
|
|
pipe_mutex_init(queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
pipe_semaphore_init(&queue->has_space, max_jobs);
|
2016-06-11 13:10:49 +02:00
|
|
|
pipe_semaphore_init(&queue->queued, 0);
|
2016-06-12 12:54:42 +02:00
|
|
|
|
2016-06-11 13:10:49 +02:00
|
|
|
queue->thread = pipe_thread_create(util_queue_thread_func, queue);
|
2016-06-12 12:54:42 +02:00
|
|
|
if (!queue->thread)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (queue->jobs) {
|
|
|
|
pipe_semaphore_destroy(&queue->has_space);
|
|
|
|
pipe_semaphore_destroy(&queue->queued);
|
|
|
|
pipe_mutex_destroy(queue->lock);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_destroy(struct util_queue *queue)
|
|
|
|
{
|
|
|
|
queue->kill_thread = 1;
|
|
|
|
pipe_semaphore_signal(&queue->queued);
|
|
|
|
pipe_thread_wait(queue->thread);
|
|
|
|
pipe_semaphore_destroy(&queue->has_space);
|
|
|
|
pipe_semaphore_destroy(&queue->queued);
|
|
|
|
pipe_mutex_destroy(queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
FREE(queue->jobs);
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_fence_init(struct util_queue_fence *fence)
|
|
|
|
{
|
2016-06-11 17:51:22 +02:00
|
|
|
memset(fence, 0, sizeof(*fence));
|
|
|
|
pipe_mutex_init(fence->mutex);
|
|
|
|
pipe_condvar_init(fence->cond);
|
|
|
|
fence->signalled = true;
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_fence_destroy(struct util_queue_fence *fence)
|
|
|
|
{
|
2016-06-11 17:51:22 +02:00
|
|
|
pipe_condvar_destroy(fence->cond);
|
|
|
|
pipe_mutex_destroy(fence->mutex);
|
2016-06-11 13:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
util_queue_add_job(struct util_queue *queue,
|
|
|
|
void *job,
|
|
|
|
struct util_queue_fence *fence)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
/* if the queue is full, wait until there is space */
|
|
|
|
pipe_semaphore_wait(&queue->has_space);
|
|
|
|
|
|
|
|
pipe_mutex_lock(queue->lock);
|
2016-06-12 12:54:42 +02:00
|
|
|
ptr = &queue->jobs[queue->write_idx];
|
|
|
|
assert(ptr->job == NULL);
|
|
|
|
ptr->job = job;
|
|
|
|
ptr->fence = fence;
|
|
|
|
queue->write_idx = (queue->write_idx + 1) % queue->max_jobs;
|
2016-06-11 13:10:49 +02:00
|
|
|
pipe_mutex_unlock(queue->lock);
|
|
|
|
pipe_semaphore_signal(&queue->queued);
|
|
|
|
}
|