MusicPipe.cxx 2.16 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21 22
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
23 24

#ifndef NDEBUG
25 26

bool
27
MusicPipe::Contains(const MusicChunk *chunk) const noexcept
28
{
29
	const std::lock_guard<Mutex> protect(mutex);
30

31
	for (const MusicChunk *i = head.get(); i != nullptr; i = i->next.get())
32
		if (i == chunk)
33 34 35 36 37
			return true;

	return false;
}

38 39
#endif

40
MusicChunkPtr
41
MusicPipe::Shift() noexcept
42
{
43
	const std::lock_guard<Mutex> protect(mutex);
44

45
	auto chunk = std::move(head);
46
	if (chunk != nullptr) {
47
		assert(!chunk->IsEmpty());
48

49
		head = std::move(chunk->next);
50
		--size;
51

52 53 54
		if (head == nullptr) {
			assert(size == 0);
			assert(tail_r == &chunk->next);
55

56
			tail_r = &head;
57
		} else {
58 59
			assert(size > 0);
			assert(tail_r != &chunk->next);
60
		}
61 62

#ifndef NDEBUG
63 64
		if (size == 0)
			audio_format.Clear();
65
#endif
66
	}
67

68
	return chunk;
69 70
}

71
void
72
MusicPipe::Clear() noexcept
73
{
74
	while (Shift()) {}
75
}
76

77
void
78
MusicPipe::Push(MusicChunkPtr chunk) noexcept
79
{
80
	assert(!chunk->IsEmpty());
81
	assert(chunk->length == 0 || chunk->audio_format.IsValid());
82

83
	const std::lock_guard<Mutex> protect(mutex);
84

85 86 87
	assert(size > 0 || !audio_format.IsDefined());
	assert(!audio_format.IsDefined() ||
	       chunk->CheckFormat(audio_format));
88 89

#ifndef NDEBUG
90 91
	if (!audio_format.IsDefined() && chunk->length > 0)
		audio_format = chunk->audio_format;
92 93
#endif

94 95 96
	chunk->next.reset();
	*tail_r = std::move(chunk);
	tail_r = &(*tail_r)->next;
97

98
	++size;
99
}