2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-08 10:48:48 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-08 10:48:48 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2014-01-29 20:16:43 +01:00
|
|
|
#include "Service.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "UpdateDomain.hxx"
|
2014-02-04 08:37:05 +01:00
|
|
|
#include "db/plugins/SimpleDatabasePlugin.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2012-09-28 00:40:00 +02:00
|
|
|
#include "Main.hxx"
|
2013-04-17 22:58:33 +02:00
|
|
|
#include "Instance.hxx"
|
2013-08-07 10:31:31 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2013-10-17 19:27:02 +02:00
|
|
|
#include "thread/Id.hxx"
|
2013-10-17 18:42:14 +02:00
|
|
|
#include "thread/Thread.hxx"
|
2014-01-14 09:09:52 +01:00
|
|
|
#include "thread/Util.hxx"
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2008-12-29 17:29:49 +01:00
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
inline void
|
|
|
|
UpdateService::Task()
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 21:30:49 +02:00
|
|
|
if (!next.path_utf8.empty())
|
|
|
|
FormatDebug(update_domain, "starting: %s",
|
|
|
|
next.path_utf8.c_str());
|
2010-02-27 16:41:54 +01:00
|
|
|
else
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(update_domain, "starting");
|
2010-02-27 16:41:54 +01:00
|
|
|
|
2014-01-14 09:09:52 +01:00
|
|
|
SetThreadIdlePriority();
|
|
|
|
|
2014-02-04 08:37:05 +01:00
|
|
|
modified = walk.Walk(*db.GetRoot(), next.path_utf8.c_str(),
|
2014-02-04 08:57:40 +01:00
|
|
|
next.discard);
|
2009-08-14 11:51:35 +02:00
|
|
|
|
2014-02-04 08:37:05 +01:00
|
|
|
if (modified || !db.FileExists()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2014-02-04 08:37:05 +01:00
|
|
|
if (!db.Save(error))
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error, "Failed to save database");
|
2011-09-09 23:28:27 +02:00
|
|
|
}
|
2009-01-08 07:36:40 +01:00
|
|
|
|
2013-10-17 21:30:49 +02:00
|
|
|
if (!next.path_utf8.empty())
|
|
|
|
FormatDebug(update_domain, "finished: %s",
|
|
|
|
next.path_utf8.c_str());
|
2010-02-27 16:41:54 +01:00
|
|
|
else
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(update_domain, "finished");
|
2010-02-27 16:41:54 +01:00
|
|
|
|
2008-10-08 10:48:48 +02:00
|
|
|
progress = UPDATE_PROGRESS_DONE;
|
2014-01-29 20:16:43 +01:00
|
|
|
DeferredMonitor::Schedule();
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
void
|
|
|
|
UpdateService::Task(void *ctx)
|
|
|
|
{
|
|
|
|
UpdateService &service = *(UpdateService *)ctx;
|
|
|
|
return service.Task();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateService::StartThread(UpdateQueueItem &&i)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 19:27:02 +02:00
|
|
|
assert(main_thread.IsInside());
|
2008-10-08 10:48:48 +02:00
|
|
|
|
|
|
|
progress = UPDATE_PROGRESS_RUNNING;
|
2008-10-09 19:41:58 +02:00
|
|
|
modified = false;
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2013-10-17 21:30:49 +02:00
|
|
|
next = std::move(i);
|
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
Error error;
|
2014-01-29 20:16:43 +01:00
|
|
|
if (!update_thread.Start(Task, this, error))
|
2013-10-17 18:42:14 +02:00
|
|
|
FatalError(error);
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(update_domain,
|
2013-10-17 21:34:36 +02:00
|
|
|
"spawned thread for update job id %i", next.id);
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
unsigned
|
|
|
|
UpdateService::GenerateId()
|
2013-10-17 21:34:36 +02:00
|
|
|
{
|
|
|
|
unsigned id = update_task_id + 1;
|
|
|
|
if (id > update_task_id_max)
|
|
|
|
id = 1;
|
|
|
|
return id;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2008-10-09 19:20:05 +02:00
|
|
|
unsigned
|
2014-01-29 20:16:43 +01:00
|
|
|
UpdateService::Enqueue(const char *path, bool discard)
|
2008-10-08 10:48:48 +02:00
|
|
|
{
|
2013-10-17 19:27:02 +02:00
|
|
|
assert(main_thread.IsInside());
|
2008-10-08 10:48:48 +02:00
|
|
|
|
|
|
|
if (progress != UPDATE_PROGRESS_IDLE) {
|
2014-01-29 20:16:43 +01:00
|
|
|
const unsigned id = GenerateId();
|
|
|
|
if (!queue.Push(path, discard, id))
|
2008-10-09 19:17:44 +02:00
|
|
|
return 0;
|
2008-10-09 19:20:54 +02:00
|
|
|
|
2013-10-17 21:34:36 +02:00
|
|
|
update_task_id = id;
|
|
|
|
return id;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
2009-09-24 21:39:43 +02:00
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
const unsigned id = update_task_id = GenerateId();
|
|
|
|
StartThread(UpdateQueueItem(path, discard, id));
|
2009-07-05 08:46:53 +02:00
|
|
|
|
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
2013-10-17 21:34:36 +02:00
|
|
|
return id;
|
2008-10-08 10:48:48 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 20:30:23 +01:00
|
|
|
/**
|
|
|
|
* Called in the main thread after the database update is finished.
|
|
|
|
*/
|
2014-01-29 20:16:43 +01:00
|
|
|
void
|
|
|
|
UpdateService::RunDeferred()
|
2009-01-04 20:30:23 +01:00
|
|
|
{
|
|
|
|
assert(progress == UPDATE_PROGRESS_DONE);
|
2013-10-17 21:34:36 +02:00
|
|
|
assert(next.IsDefined());
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2013-10-17 18:42:14 +02:00
|
|
|
update_thread.Join();
|
2013-10-17 21:34:36 +02:00
|
|
|
next = UpdateQueueItem();
|
2008-10-09 19:41:58 +02:00
|
|
|
|
2009-07-05 08:46:53 +02:00
|
|
|
idle_add(IDLE_UPDATE);
|
|
|
|
|
2013-04-17 22:58:33 +02:00
|
|
|
if (modified)
|
2009-01-04 20:30:23 +01:00
|
|
|
/* send "idle" events */
|
2013-04-17 22:58:33 +02:00
|
|
|
instance->DatabaseModified();
|
2008-10-08 10:48:48 +02:00
|
|
|
|
2014-01-29 20:16:43 +01:00
|
|
|
auto i = queue.Pop();
|
2013-10-17 21:13:40 +02:00
|
|
|
if (i.IsDefined()) {
|
2009-01-04 20:30:23 +01:00
|
|
|
/* schedule the next path */
|
2014-01-29 20:16:43 +01:00
|
|
|
StartThread(std::move(i));
|
2008-10-08 10:48:48 +02:00
|
|
|
} else {
|
|
|
|
progress = UPDATE_PROGRESS_IDLE;
|
|
|
|
}
|
|
|
|
}
|
2008-11-27 19:34:54 +01:00
|
|
|
|
2014-02-04 08:37:05 +01:00
|
|
|
UpdateService::UpdateService(EventLoop &_loop, SimpleDatabase &_db)
|
|
|
|
:DeferredMonitor(_loop), db(_db), walk(_loop)
|
2008-11-27 19:34:54 +01:00
|
|
|
{
|
|
|
|
}
|