RetroArch/tasks/task_queue.c

415 lines
9.4 KiB
C
Raw Normal View History

2016-01-10 03:06:50 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2016 - Higor Euripedes
2016-01-10 03:06:50 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
2015-11-22 23:15:42 +00:00
#include <stdlib.h>
2015-11-30 22:24:11 +00:00
#include <stdarg.h>
2015-11-22 23:15:42 +00:00
2016-02-09 16:55:15 +00:00
#include "task_queue.h"
2015-11-22 23:15:42 +00:00
#ifdef HAVE_THREADS
2016-01-30 03:46:41 +00:00
#include <rthreads/rthreads.h>
2015-11-22 23:15:42 +00:00
#endif
2016-01-28 08:48:47 +00:00
typedef struct
{
2016-02-09 16:47:04 +00:00
retro_task_t *front;
retro_task_t *back;
} task_queue_t;
2015-11-22 23:15:42 +00:00
2016-02-09 16:47:04 +00:00
struct retro_task_impl
2016-01-28 08:48:47 +00:00
{
2016-02-09 16:47:04 +00:00
void (*push_running)(retro_task_t *);
void (*reset)(void);
void (*wait)(void);
2015-11-22 23:15:42 +00:00
void (*gather)(void);
2016-02-09 16:47:04 +00:00
bool (*find)(retro_task_finder_t, void*);
2015-11-22 23:15:42 +00:00
void (*init)(void);
void (*deinit)(void);
};
2016-01-28 08:48:47 +00:00
static task_queue_t tasks_running = {NULL, NULL};
static task_queue_t tasks_finished = {NULL, NULL};
2015-11-22 23:15:42 +00:00
2016-02-09 16:47:04 +00:00
static void task_queue_put(task_queue_t *queue, retro_task_t *task)
2015-11-22 23:15:42 +00:00
{
task->next = NULL;
2015-11-22 23:15:42 +00:00
2016-01-28 08:48:47 +00:00
if (queue->front)
queue->back->next = task;
2016-01-28 08:48:47 +00:00
else
queue->front = task;
2015-11-22 23:15:42 +00:00
queue->back = task;
2015-11-22 23:15:42 +00:00
}
2016-02-09 16:47:04 +00:00
static retro_task_t *task_queue_get(task_queue_t *queue)
2015-11-22 23:15:42 +00:00
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = queue->front;
2015-11-22 23:15:42 +00:00
if (task)
{
queue->front = task->next;
task->next = NULL;
}
2015-11-22 23:15:42 +00:00
return task;
}
2016-02-09 16:47:04 +00:00
static void retro_task_internal_gather(void)
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = NULL;
while ((task = task_queue_get(&tasks_finished)) != NULL)
{
2016-02-09 16:56:36 +00:00
task_queue_push_progress(task);
if (task->callback)
task->callback(task->task_data, task->user_data, task->error);
if (task->error)
free(task->error);
if (task->title)
free(task->title);
free(task);
}
}
2016-02-09 17:00:17 +00:00
static void retro_task_regular_push_running(retro_task_t *task)
2015-11-22 23:15:42 +00:00
{
task_queue_put(&tasks_running, task);
2015-11-22 23:15:42 +00:00
}
2016-02-09 17:00:17 +00:00
static void retro_task_regular_gather(void)
2015-11-22 23:15:42 +00:00
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = NULL;
retro_task_t *queue = NULL;
retro_task_t *next = NULL;
2015-11-22 23:15:42 +00:00
while ((task = task_queue_get(&tasks_running)) != NULL)
2015-11-22 23:15:42 +00:00
{
task->next = queue;
queue = task;
}
for (task = queue; task; task = next)
2015-11-22 23:15:42 +00:00
{
next = task->next;
2015-11-22 23:15:42 +00:00
task->handler(task);
2016-02-09 16:56:36 +00:00
task_queue_push_progress(task);
2015-11-22 23:15:42 +00:00
if (task->finished)
task_queue_put(&tasks_finished, task);
2015-11-22 23:15:42 +00:00
else
2016-02-09 17:00:17 +00:00
retro_task_regular_push_running(task);
2015-11-22 23:15:42 +00:00
}
2016-02-09 16:47:04 +00:00
retro_task_internal_gather();
2015-11-22 23:15:42 +00:00
}
2016-02-09 17:00:17 +00:00
static void retro_task_regular_wait(void)
{
while (tasks_running.front)
2016-02-09 17:00:17 +00:00
retro_task_regular_gather();
}
2016-02-09 17:00:17 +00:00
static void retro_task_regular_reset(void)
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = tasks_running.front;
2016-02-09 16:27:29 +00:00
for (; task; task = task->next)
task->cancelled = true;
}
2016-02-09 17:00:17 +00:00
static void retro_task_regular_init(void)
2015-11-22 23:15:42 +00:00
{
}
2016-02-09 17:00:17 +00:00
static void retro_task_regular_deinit(void)
2015-11-22 23:15:42 +00:00
{
}
2016-02-09 17:00:17 +00:00
static bool retro_task_regular_find(retro_task_finder_t func, void *user_data)
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = tasks_running.front;
2016-02-09 16:27:29 +00:00
for (; task; task = task->next)
{
if (func(task, user_data))
return true;
}
return false;
}
2016-02-09 16:47:04 +00:00
static struct retro_task_impl impl_regular = {
2016-02-09 17:00:17 +00:00
retro_task_regular_push_running,
retro_task_regular_reset,
retro_task_regular_wait,
retro_task_regular_gather,
retro_task_regular_find,
retro_task_regular_init,
retro_task_regular_deinit
2015-11-22 23:15:42 +00:00
};
#ifdef HAVE_THREADS
2016-02-09 16:01:07 +00:00
static slock_t *running_lock = NULL;
static slock_t *finished_lock = NULL;
static scond_t *worker_cond = NULL;
2015-11-22 23:15:42 +00:00
static sthread_t *worker_thread = NULL;
2016-02-09 16:01:07 +00:00
static bool worker_continue = true; /* use running_lock when touching it */
2015-11-22 23:15:42 +00:00
2016-02-09 17:00:17 +00:00
static void retro_task_threaded_push_running(retro_task_t *task)
2015-11-22 23:15:42 +00:00
{
slock_lock(running_lock);
task_queue_put(&tasks_running, task);
2015-11-22 23:15:42 +00:00
scond_signal(worker_cond);
slock_unlock(running_lock);
}
2016-02-09 17:00:17 +00:00
static void retro_task_threaded_gather(void)
2015-11-22 23:15:42 +00:00
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = NULL;
2016-01-24 00:30:19 +00:00
slock_lock(running_lock);
for (task = tasks_running.front; task; task = task->next)
2016-02-09 16:56:36 +00:00
task_queue_push_progress(task);
slock_unlock(running_lock);
2015-11-22 23:15:42 +00:00
slock_lock(finished_lock);
2016-02-09 16:47:04 +00:00
retro_task_internal_gather();
2015-11-22 23:15:42 +00:00
slock_unlock(finished_lock);
}
2016-02-09 17:00:17 +00:00
static void retro_task_threaded_wait(void)
{
2016-01-24 00:30:19 +00:00
bool wait = false;
do
{
2016-02-09 17:00:17 +00:00
retro_task_threaded_gather();
slock_lock(running_lock);
wait = (tasks_running.front != NULL);
slock_unlock(running_lock);
} while (wait);
}
2016-02-09 17:00:17 +00:00
static void retro_task_threaded_reset(void)
{
2016-02-09 16:47:04 +00:00
retro_task_t *task = NULL;
slock_lock(running_lock);
for (task = tasks_running.front; task; task = task->next)
task->cancelled = true;
slock_unlock(running_lock);
}
2016-02-09 17:00:17 +00:00
static bool retro_task_threaded_find(
retro_task_finder_t func, void *user_data)
{
retro_task_t *task = NULL;
slock_lock(running_lock);
for (task = tasks_running.front; task; task = task->next)
{
if (func(task, user_data))
return true;
}
slock_unlock(running_lock);
return false;
}
2015-11-22 23:15:42 +00:00
static void threaded_worker(void *userdata)
{
(void)userdata;
for (;;)
{
2016-02-09 16:47:04 +00:00
retro_task_t *queue = NULL;
retro_task_t *task = NULL;
retro_task_t *next = NULL;
2015-11-22 23:15:42 +00:00
2016-02-09 16:01:07 +00:00
/* pop all into a local queue,
2015-11-22 23:15:42 +00:00
* tasks are in the reverse order here. */
slock_lock(running_lock);
if (!worker_continue)
break; /* should we keep running until all tasks finished? */
while ((task = task_queue_get(&tasks_running)) != NULL)
2015-11-22 23:15:42 +00:00
{
task->next = queue;
queue = task;
}
if (queue == NULL) /* no tasks running, lets wait a bit */
{
scond_wait(worker_cond, running_lock);
slock_unlock(running_lock);
continue;
}
slock_unlock(running_lock);
2015-11-23 17:15:19 +00:00
for (task = queue; task; task = next)
2015-11-22 23:15:42 +00:00
{
2015-11-23 17:15:19 +00:00
next = task->next;
2015-11-22 23:15:42 +00:00
task->handler(task);
if (task->finished)
{
slock_lock(finished_lock);
task_queue_put(&tasks_finished, task);
slock_unlock(finished_lock);
}
2015-11-22 23:15:42 +00:00
else
2016-02-09 17:00:17 +00:00
retro_task_threaded_push_running(task);
2015-11-22 23:15:42 +00:00
}
}
slock_unlock(running_lock);
}
2016-02-09 17:00:17 +00:00
static void retro_task_threaded_init(void)
2015-11-22 23:15:42 +00:00
{
running_lock = slock_new();
finished_lock = slock_new();
worker_cond = scond_new();
slock_lock(running_lock);
worker_continue = true;
slock_unlock(running_lock);
worker_thread = sthread_create(threaded_worker, NULL);
}
2016-02-09 17:00:17 +00:00
static void retro_task_threaded_deinit(void)
2015-11-22 23:15:42 +00:00
{
slock_lock(running_lock);
worker_continue = false;
scond_signal(worker_cond);
slock_unlock(running_lock);
sthread_join(worker_thread);
scond_free(worker_cond);
slock_free(running_lock);
slock_free(finished_lock);
worker_thread = NULL;
worker_cond = NULL;
running_lock = NULL;
finished_lock = NULL;
}
2016-02-09 16:47:04 +00:00
static struct retro_task_impl impl_threaded = {
2016-02-09 17:00:17 +00:00
retro_task_threaded_push_running,
retro_task_threaded_reset,
retro_task_threaded_wait,
retro_task_threaded_gather,
retro_task_threaded_find,
retro_task_threaded_init,
retro_task_threaded_deinit
2015-11-22 23:15:42 +00:00
};
#endif
2016-02-09 16:51:51 +00:00
bool task_queue_ctl(enum task_queue_ctl_state state, void *data)
2016-01-28 08:35:25 +00:00
{
2016-02-09 16:47:04 +00:00
static struct retro_task_impl *impl_current = NULL;
static bool task_threaded_enable = false;
2016-01-28 08:52:28 +00:00
2016-01-28 08:35:25 +00:00
switch (state)
{
2016-01-28 08:48:47 +00:00
case TASK_CTL_DEINIT:
2016-02-09 16:01:07 +00:00
if (impl_current)
impl_current->deinit();
2016-01-28 08:48:47 +00:00
impl_current = NULL;
break;
case TASK_CTL_SET_THREADED:
task_threaded_enable = true;
break;
case TASK_CTL_UNSET_THREADED:
task_threaded_enable = false;
break;
case TASK_CTL_IS_THREADED:
return task_threaded_enable;
2016-01-28 08:52:28 +00:00
case TASK_CTL_INIT:
{
bool *boolean_val = (bool*)data;
impl_current = &impl_regular;
2016-01-28 08:52:28 +00:00
#ifdef HAVE_THREADS
if (*boolean_val)
{
2016-02-09 16:51:51 +00:00
task_queue_ctl(TASK_CTL_SET_THREADED, NULL);
impl_current = &impl_threaded;
}
2016-01-28 08:52:28 +00:00
#endif
impl_current->init();
}
2016-01-28 08:52:28 +00:00
break;
2016-01-28 08:57:55 +00:00
case TASK_CTL_FIND:
{
task_finder_data_t *find_data = (task_finder_data_t*)data;
if (!impl_current->find(find_data->func, find_data->userdata))
return false;
}
break;
2016-01-28 08:45:14 +00:00
case TASK_CTL_CHECK:
{
#ifdef HAVE_THREADS
bool current_threaded = (impl_current == &impl_threaded);
2016-02-09 16:51:51 +00:00
bool want_threaded = task_queue_ctl(TASK_CTL_IS_THREADED, NULL);
2016-01-28 08:45:14 +00:00
if (want_threaded != current_threaded)
2016-02-09 16:51:51 +00:00
task_queue_ctl(TASK_CTL_DEINIT, NULL);
2016-01-28 08:45:14 +00:00
if (!impl_current)
2016-02-09 16:51:51 +00:00
task_queue_ctl(TASK_CTL_INIT, NULL);
2016-01-28 08:45:14 +00:00
#endif
impl_current->gather();
}
break;
2016-01-28 08:42:55 +00:00
case TASK_CTL_PUSH:
{
/* The lack of NULL checks in the following functions
* is proposital to ensure correct control flow by the users. */
2016-02-09 16:47:04 +00:00
retro_task_t *task = (retro_task_t*)data;
2016-01-28 08:42:55 +00:00
impl_current->push_running(task);
break;
}
2016-01-28 08:38:21 +00:00
case TASK_CTL_RESET:
impl_current->reset();
break;
case TASK_CTL_WAIT:
impl_current->wait();
break;
2016-01-28 08:35:25 +00:00
case TASK_CTL_NONE:
default:
break;
}
return true;
}