input/{cdio,ffmpeg,file,smbclient}: unlock the mutex during blocking I/O

InputStream::Read() and InputStream::Seek() are called with the mutex
locked.  That means the implementation must not block, or unlock the
mutex before calling into blocking code.

Previously, a slow CD drive could stall the whole MPD process,
including the main thread, due to this problem.

Closes #149
This commit is contained in:
Max Kellermann 2017-11-13 17:08:00 +01:00
parent f82e1453e4
commit 31ab78ae8e
5 changed files with 35 additions and 5 deletions

2
NEWS
View File

@ -1,5 +1,7 @@
ver 0.20.12 (not yet released)
* input
- cdio_paranoia, ffmpeg, file, smbclient: reduce lock contention,
fixing lots of xrun problems
- curl: fix seeking
* decoder
- ffmpeg: fix GCC 8 warning

View File

@ -270,7 +270,10 @@ CdioParanoiaInputStream::Seek(offset_type new_offset)
lsn_relofs = new_offset / CDIO_CD_FRAMESIZE_RAW;
offset = new_offset;
cdio_paranoia_seek(para, lsn_from + lsn_relofs, SEEK_SET);
{
const ScopeUnlock unlock(mutex);
cdio_paranoia_seek(para, lsn_from + lsn_relofs, SEEK_SET);
}
}
size_t
@ -292,6 +295,8 @@ CdioParanoiaInputStream::Read(void *ptr, size_t length)
//current sector was changed ?
if (lsn_relofs != buffer_lsn) {
const ScopeUnlock unlock(mutex);
rbuf = cdio_paranoia_read(para, nullptr);
s_err = cdda_errors(drv);

View File

@ -104,7 +104,13 @@ input_ffmpeg_open(const char *uri,
size_t
FfmpegInputStream::Read(void *ptr, size_t read_size)
{
auto result = avio_read(h, (unsigned char *)ptr, read_size);
int result;
{
const ScopeUnlock unlock(mutex);
result = avio_read(h, (unsigned char *)ptr, read_size);
}
if (result <= 0) {
if (result < 0)
throw MakeFfmpegError(result, "avio_read() failed");
@ -126,7 +132,12 @@ FfmpegInputStream::IsEOF() noexcept
void
FfmpegInputStream::Seek(offset_type new_offset)
{
auto result = avio_seek(h, new_offset, SEEK_SET);
int64_t result;
{
const ScopeUnlock unlock(mutex);
result = avio_seek(h, new_offset, SEEK_SET);
}
if (result < 0)
throw MakeFfmpegError(result, "avio_seek() failed");

View File

@ -87,14 +87,24 @@ input_file_open(gcc_unused const char *filename,
void
FileInputStream::Seek(offset_type new_offset)
{
reader.Seek((off_t)new_offset);
{
const ScopeUnlock unlock(mutex);
reader.Seek((off_t)new_offset);
}
offset = new_offset;
}
size_t
FileInputStream::Read(void *ptr, size_t read_size)
{
size_t nbytes = reader.Read(ptr, read_size);
size_t nbytes;
{
const ScopeUnlock unlock(mutex);
nbytes = reader.Read(ptr, read_size);
}
offset += nbytes;
return nbytes;
}

View File

@ -128,6 +128,7 @@ SmbclientInputStream::Read(void *ptr, size_t read_size)
ssize_t nbytes;
{
const ScopeUnlock unlock(mutex);
const std::lock_guard<Mutex> lock(smbclient_mutex);
nbytes = smbc_read(fd, ptr, read_size);
}
@ -145,6 +146,7 @@ SmbclientInputStream::Seek(offset_type new_offset)
off_t result;
{
const ScopeUnlock unlock(mutex);
const std::lock_guard<Mutex> lock(smbclient_mutex);
result = smbc_lseek(fd, new_offset, SEEK_SET);
}