mpd/src/MusicBuffer.cxx

39 lines
817 B
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2013-01-04 10:16:16 +01:00
#include "MusicBuffer.hxx"
#include "MusicChunk.hxx"
#include <cassert>
MusicBuffer::MusicBuffer(unsigned num_chunks)
:buffer(num_chunks)
{
buffer.SetName("MusicBuffer");
}
MusicChunkPtr
MusicBuffer::Allocate() noexcept
{
const std::scoped_lock<Mutex> protect(mutex);
return {buffer.Allocate(), MusicChunkDeleter(*this)};
}
void
MusicBuffer::Return(MusicChunk *chunk) noexcept
{
2013-09-26 22:09:42 +02:00
assert(chunk != nullptr);
/* these attributes need to be cleared before locking the
mutex, because they might recursively call this method,
causing a deadlock */
chunk->next.reset();
chunk->other.reset();
const std::scoped_lock<Mutex> protect(mutex);
assert(!chunk->other || !chunk->other->other);
2013-09-26 22:09:42 +02:00
buffer.Free(chunk);
}