COMMON: Uplift SafeMutexedSeekableSubReadStream to common

This commit is contained in:
Vladimir Serbinenko 2022-12-20 20:14:04 +01:00 committed by Eugene Sandulenko
parent fdccec381d
commit 0adf22278b
3 changed files with 25 additions and 24 deletions

View File

@ -581,4 +581,9 @@ WriteStream *wrapBufferedWriteStream(WriteStream *parentStream, uint32 bufSize)
return nullptr;
}
uint32 SafeMutexedSeekableSubReadStream::read(void *dataPtr, uint32 dataSize) {
Common::StackLock lock(_mutex);
return Common::SafeSeekableSubReadStream::read(dataPtr, dataSize);
}
} // End of namespace Common

View File

@ -22,6 +22,7 @@
#ifndef COMMON_SUBSTREAM_H
#define COMMON_SUBSTREAM_H
#include "common/mutex.h"
#include "common/ptr.h"
#include "common/stream.h"
#include "common/types.h"
@ -133,6 +134,24 @@ public:
virtual uint32 read(void *dataPtr, uint32 dataSize);
};
/**
* A special variant of SafeSeekableSubReadStream which locks a mutex during each read.
* This is necessary if the music is streamed from disk and it could happen
* that a sound effect or another music track is played from the same read stream
* while the first music track is updated/read.
*/
class SafeMutexedSeekableSubReadStream : public Common::SafeSeekableSubReadStream {
public:
SafeMutexedSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream,
Common::Mutex &mutex)
: SafeSeekableSubReadStream(parentStream, begin, end, disposeParentStream), _mutex(mutex) {
}
uint32 read(void *dataPtr, uint32 dataSize) override;
protected:
Common::Mutex &_mutex;
};
/** @} */
} // End of namespace Common

View File

@ -24,29 +24,6 @@
namespace Neverhood {
/**
* A special variant of SafeSeekableSubReadStream which locks a mutex during each read.
* This is necessary because the music is streamed from disk and it could happen
* that a sound effect or another music track is played from the same read stream
* while the first music track is updated/read.
*/
class SafeMutexedSeekableSubReadStream : public Common::SafeSeekableSubReadStream {
public:
SafeMutexedSeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, DisposeAfterUse::Flag disposeParentStream,
Common::Mutex &mutex)
: SafeSeekableSubReadStream(parentStream, begin, end, disposeParentStream), _mutex(mutex) {
}
uint32 read(void *dataPtr, uint32 dataSize) override;
protected:
Common::Mutex &_mutex;
};
uint32 SafeMutexedSeekableSubReadStream::read(void *dataPtr, uint32 dataSize) {
Common::StackLock lock(_mutex);
return Common::SafeSeekableSubReadStream::read(dataPtr, dataSize);
}
BlbArchive::BlbArchive() : _extData(nullptr) {
}
@ -158,7 +135,7 @@ Common::SeekableReadStream *BlbArchive::createStream(uint index) {
}
Common::SeekableReadStream *BlbArchive::createStream(BlbArchiveEntry *entry) {
return new SafeMutexedSeekableSubReadStream(&_fd, entry->offset, entry->offset + entry->diskSize,
return new Common::SafeMutexedSeekableSubReadStream(&_fd, entry->offset, entry->offset + entry->diskSize,
DisposeAfterUse::NO, _mutex);
}