Remove.cxx 1.89 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "Remove.hxx"
21
#include "UpdateDomain.hxx"
22
#include "db/DatabaseListener.hxx"
23
#include "Log.hxx"
24 25

/**
26
 * Safely remove songs from the database.  This must be done in the
27 28
 * main task, because some (thread-unsafe) data structures are
 * available only there.
29
 */
30
void
31
UpdateRemoveService::RunDeferred() noexcept
32
{
33 34 35 36
	/* copy the list and unlock the mutex before invoking
	   callbacks */

	std::forward_list<std::string> copy;
37

38
	{
39
		const std::lock_guard<Mutex> protect(mutex);
40 41 42 43
		std::swap(uris, copy);
	}

	for (const auto &uri : copy) {
44
		FormatDefault(update_domain, "removing %s", uri.c_str());
45
		listener.OnDatabaseSongRemoved(uri.c_str());
46
	}
47

48 49
	/* note: if Remove() was called in the meantime, it saw an
	   empty list, and scheduled another event */
50 51 52
}

void
53
UpdateRemoveService::Remove(std::string &&uri)
54
{
55
	bool was_empty;
56

57
	{
58
		const std::lock_guard<Mutex> protect(mutex);
59
		was_empty = uris.empty();
60
		uris.emplace_front(std::move(uri));
61
	}
62

63 64 65 66
	/* inject an event into the main thread, but only if the list
	   was empty; if it was not, then that even was already
	   pending */
	if (was_empty)
67
		defer.Schedule();
68
}