2015-11-22 23:15:42 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "../general.h"
|
2015-11-23 14:28:20 +00:00
|
|
|
#include "../verbosity.h"
|
2015-11-30 13:02:36 +00:00
|
|
|
#include "../msg_hash.h"
|
2015-11-22 23:15:42 +00:00
|
|
|
#include "tasks.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
#include "rthreads/rthreads.h"
|
|
|
|
#endif
|
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
typedef struct {
|
|
|
|
rarch_task_t *front;
|
|
|
|
rarch_task_t *back;
|
|
|
|
} task_queue_t;
|
2015-11-22 23:15:42 +00:00
|
|
|
|
|
|
|
struct rarch_task_impl {
|
|
|
|
void (*push_running)(rarch_task_t *);
|
2015-11-30 14:36:53 +00:00
|
|
|
void (*reset)(void);
|
2015-11-25 00:50:29 +00:00
|
|
|
void (*wait)(void);
|
2015-11-22 23:15:42 +00:00
|
|
|
void (*gather)(void);
|
2015-11-30 14:36:53 +00:00
|
|
|
bool (*find)(rarch_task_finder_t, void*);
|
2015-11-22 23:15:42 +00:00
|
|
|
void (*init)(void);
|
|
|
|
void (*deinit)(void);
|
|
|
|
};
|
2015-11-24 19:56:01 +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
|
|
|
|
|
|
|
static struct rarch_task_impl *impl_current = NULL;
|
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
static void task_queue_put(task_queue_t *queue, rarch_task_t *task)
|
2015-11-22 23:15:42 +00:00
|
|
|
{
|
2015-11-24 19:56:01 +00:00
|
|
|
task->next = NULL;
|
2015-11-22 23:15:42 +00:00
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
if (queue->front == NULL)
|
|
|
|
queue->front = task;
|
|
|
|
else
|
|
|
|
queue->back->next = task;
|
2015-11-22 23:15:42 +00:00
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
queue->back = task;
|
2015-11-22 23:15:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
static rarch_task_t *task_queue_get(task_queue_t *queue)
|
2015-11-22 23:15:42 +00:00
|
|
|
{
|
2015-11-24 19:56:01 +00:00
|
|
|
rarch_task_t *task = queue->front;
|
2015-11-22 23:15:42 +00:00
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
if (task)
|
|
|
|
{
|
|
|
|
queue->front = task->next;
|
|
|
|
task->next = NULL;
|
|
|
|
}
|
2015-11-22 23:15:42 +00:00
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rarch_task_internal_gather(void)
|
|
|
|
{
|
2015-11-25 00:50:29 +00:00
|
|
|
rarch_task_t *task;
|
|
|
|
while ((task = task_queue_get(&tasks_finished)) != NULL)
|
2015-11-22 23:15:42 +00:00
|
|
|
{
|
|
|
|
if (task->callback)
|
|
|
|
task->callback(task->task_data, task->user_data, task->error);
|
|
|
|
|
|
|
|
if (task->error)
|
|
|
|
free(task->error);
|
|
|
|
|
2015-11-30 12:17:46 +00:00
|
|
|
if (task->title)
|
|
|
|
free(task->title);
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
free(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:17:46 +00:00
|
|
|
static void push_task_progress(rarch_task_t *task)
|
|
|
|
{
|
|
|
|
if (task->title)
|
|
|
|
{
|
|
|
|
if (task->finished)
|
|
|
|
{
|
|
|
|
if (task->error)
|
2015-11-30 13:02:36 +00:00
|
|
|
rarch_main_msg_queue_pushf(1, 60, true, "%s: %s",
|
|
|
|
msg_hash_to_str(MSG_TASK_FAILED), task->title);
|
2015-11-30 12:17:46 +00:00
|
|
|
else
|
|
|
|
rarch_main_msg_queue_pushf(1, 60, true, "100%%: %s", task->title);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (task->progress >= 0 && task->progress <= 100)
|
2015-11-30 15:26:29 +00:00
|
|
|
rarch_main_msg_queue_pushf(1, 60, true, "%i%%: %s", task->progress, task->title);
|
2015-11-30 12:17:46 +00:00
|
|
|
else
|
2015-11-30 15:26:29 +00:00
|
|
|
rarch_main_msg_queue_pushf(1, 60, true, "%s...", task->title);
|
2015-11-30 12:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
static void regular_push_running(rarch_task_t *task)
|
|
|
|
{
|
2015-11-24 19:56:01 +00:00
|
|
|
task_queue_put(&tasks_running, task);
|
2015-11-22 23:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void regular_gather(void)
|
|
|
|
{
|
|
|
|
rarch_task_t *task = NULL;
|
|
|
|
rarch_task_t *queue = NULL;
|
2015-11-23 20:45:15 +00:00
|
|
|
rarch_task_t *next = NULL;
|
2015-11-22 23:15:42 +00:00
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
while ((task = task_queue_get(&tasks_running)) != NULL)
|
2015-11-22 23:15:42 +00:00
|
|
|
{
|
|
|
|
task->next = queue;
|
|
|
|
queue = task;
|
|
|
|
}
|
|
|
|
|
2015-11-23 20:45:15 +00:00
|
|
|
for (task = queue; task; task = next)
|
2015-11-22 23:15:42 +00:00
|
|
|
{
|
2015-11-23 20:45:15 +00:00
|
|
|
next = task->next;
|
2015-11-22 23:15:42 +00:00
|
|
|
task->handler(task);
|
|
|
|
|
2015-11-30 12:17:46 +00:00
|
|
|
push_task_progress(task);
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
if (task->finished)
|
2015-11-25 00:50:29 +00:00
|
|
|
task_queue_put(&tasks_finished, task);
|
2015-11-22 23:15:42 +00:00
|
|
|
else
|
|
|
|
regular_push_running(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
rarch_task_internal_gather();
|
|
|
|
}
|
|
|
|
|
2015-11-25 00:50:29 +00:00
|
|
|
static void regular_wait(void)
|
|
|
|
{
|
|
|
|
while (tasks_running.front)
|
|
|
|
regular_gather();
|
|
|
|
}
|
|
|
|
|
2015-11-30 14:36:53 +00:00
|
|
|
static void regular_reset()
|
2015-11-25 00:50:29 +00:00
|
|
|
{
|
|
|
|
rarch_task_t *task;
|
|
|
|
|
|
|
|
for (task = tasks_running.front; task; task = task->next)
|
|
|
|
task->cancelled = true;
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
static void regular_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void regular_deinit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-30 14:36:53 +00:00
|
|
|
static bool regular_find(rarch_task_finder_t func, void *user_data)
|
|
|
|
{
|
|
|
|
rarch_task_t *task;
|
|
|
|
|
|
|
|
for (task = tasks_running.front; task; task = task->next)
|
|
|
|
{
|
|
|
|
if (func(task, user_data))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
static struct rarch_task_impl impl_regular = {
|
|
|
|
regular_push_running,
|
2015-11-30 14:36:53 +00:00
|
|
|
regular_reset,
|
2015-11-25 00:50:29 +00:00
|
|
|
regular_wait,
|
2015-11-22 23:15:42 +00:00
|
|
|
regular_gather,
|
2015-11-30 14:36:53 +00:00
|
|
|
regular_find,
|
2015-11-22 23:15:42 +00:00
|
|
|
regular_init,
|
|
|
|
regular_deinit
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
static slock_t *running_lock = NULL;
|
|
|
|
static slock_t *finished_lock = NULL;
|
|
|
|
static scond_t *worker_cond = NULL;
|
|
|
|
static sthread_t *worker_thread = NULL;
|
|
|
|
static bool worker_continue = true; /* use running_lock when touching it */
|
|
|
|
|
|
|
|
static void threaded_push_running(rarch_task_t *task)
|
|
|
|
{
|
|
|
|
slock_lock(running_lock);
|
2015-11-24 19:56:01 +00:00
|
|
|
task_queue_put(&tasks_running, task);
|
2015-11-22 23:15:42 +00:00
|
|
|
scond_signal(worker_cond);
|
|
|
|
slock_unlock(running_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void threaded_gather(void)
|
|
|
|
{
|
2015-11-30 12:17:46 +00:00
|
|
|
rarch_task_t *task;
|
|
|
|
slock_lock(running_lock);
|
|
|
|
for (task = tasks_running.front; task; task = task->next)
|
|
|
|
push_task_progress(task);
|
|
|
|
|
|
|
|
slock_unlock(running_lock);
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
slock_lock(finished_lock);
|
|
|
|
rarch_task_internal_gather();
|
|
|
|
slock_unlock(finished_lock);
|
|
|
|
}
|
|
|
|
|
2015-11-25 00:50:29 +00:00
|
|
|
static void threaded_wait(void)
|
|
|
|
{
|
|
|
|
bool wait;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
threaded_gather();
|
|
|
|
|
|
|
|
slock_lock(running_lock);
|
|
|
|
wait = (tasks_running.front != NULL);
|
|
|
|
slock_unlock(running_lock);
|
|
|
|
} while (wait);
|
|
|
|
}
|
|
|
|
|
2015-11-30 14:36:53 +00:00
|
|
|
static void threaded_reset(void)
|
2015-11-25 00:50:29 +00:00
|
|
|
{
|
|
|
|
rarch_task_t *task;
|
|
|
|
|
|
|
|
slock_lock(running_lock);
|
|
|
|
for (task = tasks_running.front; task; task = task->next)
|
|
|
|
task->cancelled = true;
|
|
|
|
slock_unlock(running_lock);
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:15:42 +00:00
|
|
|
static void threaded_worker(void *userdata)
|
|
|
|
{
|
|
|
|
(void)userdata;
|
|
|
|
|
|
|
|
RARCH_LOG("Threaded rarch_task started\n");
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
rarch_task_t *queue = NULL;
|
|
|
|
rarch_task_t *task = NULL;
|
2015-11-23 17:15:19 +00:00
|
|
|
rarch_task_t *next = NULL;
|
2015-11-22 23:15:42 +00:00
|
|
|
|
|
|
|
/* pop all into a local queue to avoid trouble with rarch_task_push(),
|
|
|
|
* tasks are in the reverse order here. */
|
|
|
|
slock_lock(running_lock);
|
|
|
|
|
|
|
|
if (!worker_continue)
|
|
|
|
break; /* should we keep running until all tasks finished? */
|
|
|
|
|
2015-11-24 19:56:01 +00:00
|
|
|
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)
|
2015-11-25 00:50:29 +00:00
|
|
|
{
|
|
|
|
slock_lock(finished_lock);
|
|
|
|
task_queue_put(&tasks_finished, task);
|
|
|
|
slock_unlock(finished_lock);
|
|
|
|
}
|
2015-11-22 23:15:42 +00:00
|
|
|
else
|
|
|
|
threaded_push_running(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
slock_unlock(running_lock);
|
|
|
|
}
|
|
|
|
|
2015-11-30 14:36:53 +00:00
|
|
|
static bool threaded_find(rarch_task_finder_t func, void *user_data)
|
|
|
|
{
|
|
|
|
rarch_task_t *task;
|
|
|
|
|
|
|
|
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_init(void)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void threaded_deinit(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rarch_task_impl impl_threaded = {
|
|
|
|
threaded_push_running,
|
2015-11-30 14:36:53 +00:00
|
|
|
threaded_reset,
|
2015-11-25 00:50:29 +00:00
|
|
|
threaded_wait,
|
2015-11-22 23:15:42 +00:00
|
|
|
threaded_gather,
|
2015-11-30 14:36:53 +00:00
|
|
|
threaded_find,
|
2015-11-22 23:15:42 +00:00
|
|
|
threaded_init,
|
|
|
|
threaded_deinit
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void rarch_task_init(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_THREADS
|
2015-11-24 19:56:01 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
if (settings->threaded_data_runloop_enable)
|
2015-11-22 23:15:42 +00:00
|
|
|
impl_current = &impl_threaded;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
impl_current = &impl_regular;
|
|
|
|
|
|
|
|
impl_current->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rarch_task_deinit(void)
|
|
|
|
{
|
|
|
|
if (!impl_current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
impl_current->deinit();
|
|
|
|
impl_current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rarch_task_check(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
bool current_threaded = impl_current == &impl_threaded;
|
|
|
|
bool want_threaded = config_get_ptr()->threaded_data_runloop_enable;
|
|
|
|
|
|
|
|
if (want_threaded != current_threaded) {
|
|
|
|
RARCH_LOG("Switching rarch_task implementation.\n");
|
|
|
|
rarch_task_deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (impl_current == NULL)
|
|
|
|
rarch_task_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
impl_current->gather();
|
|
|
|
}
|
|
|
|
|
2015-11-25 00:50:29 +00:00
|
|
|
/* The lack of NULL checks in the following functions is proposital
|
|
|
|
* to ensure correct control flow by the users. */
|
2015-11-22 23:15:42 +00:00
|
|
|
void rarch_task_push(rarch_task_t *task)
|
|
|
|
{
|
|
|
|
impl_current->push_running(task);
|
|
|
|
}
|
2015-11-25 00:50:29 +00:00
|
|
|
|
|
|
|
void rarch_task_wait(void)
|
|
|
|
{
|
|
|
|
impl_current->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rarch_task_reset(void)
|
|
|
|
{
|
2015-11-30 14:36:53 +00:00
|
|
|
impl_current->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rarch_task_find(rarch_task_finder_t func, void *user_data)
|
|
|
|
{
|
|
|
|
return impl_current->find(func, user_data);
|
2015-11-25 00:50:29 +00:00
|
|
|
}
|