UpdateGlue: pass UpdateQueueItem around

Fixes a few kludges and avoids GLib allocation.
This commit is contained in:
Max Kellermann 2013-10-17 21:30:49 +02:00
parent b93523c0b1
commit 0c63632cc2

View File

@ -36,8 +36,6 @@
#include "thread/Id.hxx" #include "thread/Id.hxx"
#include "thread/Thread.hxx" #include "thread/Thread.hxx"
#include <glib.h>
#include <assert.h> #include <assert.h>
static enum update_progress { static enum update_progress {
@ -54,8 +52,7 @@ static const unsigned update_task_id_max = 1 << 15;
static unsigned update_task_id; static unsigned update_task_id;
/* XXX this flag is passed to update_task() */ static UpdateQueueItem next;
static bool discard;
unsigned unsigned
isUpdatingDB(void) isUpdatingDB(void)
@ -64,16 +61,15 @@ isUpdatingDB(void)
} }
static void static void
update_task(void *_path) update_task(gcc_unused void *ctx)
{ {
const char *path = (const char *)_path; if (!next.path_utf8.empty())
FormatDebug(update_domain, "starting: %s",
if (path != NULL && *path != 0) next.path_utf8.c_str());
FormatDebug(update_domain, "starting: %s", path);
else else
LogDebug(update_domain, "starting"); LogDebug(update_domain, "starting");
modified = update_walk(path, discard); modified = update_walk(next.path_utf8.c_str(), next.discard);
if (modified || !db_exists()) { if (modified || !db_exists()) {
Error error; Error error;
@ -81,26 +77,28 @@ update_task(void *_path)
LogError(error, "Failed to save database"); LogError(error, "Failed to save database");
} }
if (path != NULL && *path != 0) if (!next.path_utf8.empty())
FormatDebug(update_domain, "finished: %s", path); FormatDebug(update_domain, "finished: %s",
next.path_utf8.c_str());
else else
LogDebug(update_domain, "finished"); LogDebug(update_domain, "finished");
g_free(_path);
progress = UPDATE_PROGRESS_DONE; progress = UPDATE_PROGRESS_DONE;
GlobalEvents::Emit(GlobalEvents::UPDATE); GlobalEvents::Emit(GlobalEvents::UPDATE);
} }
static void static void
spawn_update_task(const char *path) spawn_update_task(UpdateQueueItem &&i)
{ {
assert(main_thread.IsInside()); assert(main_thread.IsInside());
progress = UPDATE_PROGRESS_RUNNING; progress = UPDATE_PROGRESS_RUNNING;
modified = false; modified = false;
next = std::move(i);
Error error; Error error;
if (!update_thread.Start(update_task, g_strdup(path), error)) if (!update_thread.Start(update_task, nullptr, error))
FatalError(error); FatalError(error);
if (++update_task_id > update_task_id_max) if (++update_task_id > update_task_id_max)
@ -110,7 +108,7 @@ spawn_update_task(const char *path)
} }
unsigned unsigned
update_enqueue(const char *path, bool _discard) update_enqueue(const char *path, bool discard)
{ {
assert(main_thread.IsInside()); assert(main_thread.IsInside());
@ -126,8 +124,7 @@ update_enqueue(const char *path, bool _discard)
return next_task_id > update_task_id_max ? 1 : next_task_id; return next_task_id > update_task_id_max ? 1 : next_task_id;
} }
discard = _discard; spawn_update_task(UpdateQueueItem(path, discard));
spawn_update_task(path);
idle_add(IDLE_UPDATE); idle_add(IDLE_UPDATE);
@ -152,8 +149,7 @@ static void update_finished_event(void)
auto i = update_queue_shift(); auto i = update_queue_shift();
if (i.IsDefined()) { if (i.IsDefined()) {
/* schedule the next path */ /* schedule the next path */
discard = i.discard; spawn_update_task(std::move(i));
spawn_update_task(i.path_utf8.c_str());
} else { } else {
progress = UPDATE_PROGRESS_IDLE; progress = UPDATE_PROGRESS_IDLE;