Remove.cxx 1.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h" /* must be first for large file support */
21
#include "Remove.hxx"
22
#include "UpdateDomain.hxx"
23
#include "db/DatabaseListener.hxx"
24
#include "Log.hxx"
25 26

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

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

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

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

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

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

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

64 65 66 67
	/* 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)
68
		defer.Schedule();
69
}