mpd/src/UpdateGlue.cxx

172 lines
3.8 KiB
C++
Raw Normal View History

/*
2013-01-04 20:50:00 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program 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 Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
2013-01-02 19:22:15 +01:00
#include "UpdateGlue.hxx"
#include "UpdateQueue.hxx"
#include "UpdateWalk.hxx"
#include "UpdateRemove.hxx"
#include "UpdateDomain.hxx"
2013-01-02 22:43:56 +01:00
#include "Mapper.hxx"
#include "DatabaseSimple.hxx"
2013-01-09 08:36:52 +01:00
#include "Idle.hxx"
2013-01-09 23:12:53 +01:00
#include "GlobalEvents.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
2013-10-02 12:14:07 +02:00
#include "Stats.hxx"
2012-09-28 00:40:00 +02:00
#include "Main.hxx"
#include "Instance.hxx"
#include "system/FatalError.hxx"
2013-10-17 19:27:02 +02:00
#include "thread/Id.hxx"
#include "thread/Thread.hxx"
#include <assert.h>
2008-12-29 17:29:49 +01:00
static enum update_progress {
UPDATE_PROGRESS_IDLE = 0,
UPDATE_PROGRESS_RUNNING = 1,
UPDATE_PROGRESS_DONE = 2
} progress;
static bool modified;
static Thread update_thread;
static const unsigned update_task_id_max = 1 << 15;
static unsigned update_task_id;
static UpdateQueueItem next;
unsigned
isUpdatingDB(void)
{
return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0;
}
static void
update_task(gcc_unused void *ctx)
{
if (!next.path_utf8.empty())
FormatDebug(update_domain, "starting: %s",
next.path_utf8.c_str());
else
LogDebug(update_domain, "starting");
modified = update_walk(next.path_utf8.c_str(), next.discard);
2011-09-09 23:28:27 +02:00
if (modified || !db_exists()) {
Error error;
if (!db_save(error))
LogError(error, "Failed to save database");
2011-09-09 23:28:27 +02:00
}
if (!next.path_utf8.empty())
FormatDebug(update_domain, "finished: %s",
next.path_utf8.c_str());
else
LogDebug(update_domain, "finished");
progress = UPDATE_PROGRESS_DONE;
2013-01-09 23:12:53 +01:00
GlobalEvents::Emit(GlobalEvents::UPDATE);
}
static void
spawn_update_task(UpdateQueueItem &&i)
{
2013-10-17 19:27:02 +02:00
assert(main_thread.IsInside());
progress = UPDATE_PROGRESS_RUNNING;
modified = false;
next = std::move(i);
Error error;
if (!update_thread.Start(update_task, nullptr, error))
FatalError(error);
if (++update_task_id > update_task_id_max)
update_task_id = 1;
FormatDebug(update_domain,
"spawned thread for update job id %i", update_task_id);
}
unsigned
update_enqueue(const char *path, bool discard)
{
2013-10-17 19:27:02 +02:00
assert(main_thread.IsInside());
if (!db_is_simple() || !mapper_has_music_directory())
return 0;
if (progress != UPDATE_PROGRESS_IDLE) {
unsigned next_task_id =
update_queue_push(path, discard, update_task_id);
if (next_task_id == 0)
return 0;
return next_task_id > update_task_id_max ? 1 : next_task_id;
}
spawn_update_task(UpdateQueueItem(path, discard));
idle_add(IDLE_UPDATE);
return update_task_id;
}
/**
* Called in the main thread after the database update is finished.
*/
static void update_finished_event(void)
{
assert(progress == UPDATE_PROGRESS_DONE);
update_thread.Join();
idle_add(IDLE_UPDATE);
if (modified)
/* send "idle" events */
instance->DatabaseModified();
auto i = update_queue_shift();
if (i.IsDefined()) {
/* schedule the next path */
spawn_update_task(std::move(i));
} else {
progress = UPDATE_PROGRESS_IDLE;
2009-01-04 20:57:06 +01:00
stats_update();
}
}
void update_global_init(void)
{
2013-01-09 23:12:53 +01:00
GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event);
update_remove_global_init();
update_walk_global_init();
}
void update_global_finish(void)
{
update_walk_global_finish();
}