1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-07-08 13:54:52 +02:00

Fixes for thread sanitizer

This commit is contained in:
Filip Gawin 2019-04-21 23:00:53 +02:00
parent 1b588da1d1
commit c234eb00f6
3 changed files with 32 additions and 10 deletions

View File

@ -20,8 +20,8 @@ struct SoundBuffer {
bool isStopped() const;
virtual void play();
void pause();
void stop();
virtual void pause();
virtual void stop();
void setPosition(const glm::vec3& position);
void setLooping(bool looping);

View File

@ -18,16 +18,18 @@ SoundBufferStreamed::SoundBufferStreamed() {
}
SoundBufferStreamed::~SoundBufferStreamed() {
alCheck(alSourceUnqueueBuffers(source, kNrBuffersStreaming, buffers.data()));
alCheck(
alSourceUnqueueBuffers(source, kNrBuffersStreaming, buffers.data()));
alCheck(alDeleteBuffers(kNrBuffersStreaming, buffers.data()));
}
bool SoundBufferStreamed::bufferData(SoundSource &soundSource) {
std::lock_guard<std::mutex> lock(soundSource.mutex);
/* Rewind the source position and clear the buffer queue */
alCheck(alSourceRewind(source));
alCheck(alSourcei(source, AL_BUFFER, 0));
std::lock_guard<std::mutex> lock(soundSource.mutex);
/* Fill the buffer queue */
for (auto i = 0u; i < buffers.size() &&
streamedData * kSizeOfChunk < soundSource.data.size();
@ -52,24 +54,31 @@ bool SoundBufferStreamed::bufferData(SoundSource &soundSource) {
}
void SoundBufferStreamed::play() {
alSourcePlay(source);
running = true;
{
std::lock_guard<std::mutex> lock(soundSource->mutex);
alSourcePlay(source);
running = true;
}
loadingThread = std::async(std::launch::async,
&SoundBufferStreamed::updateBuffers, this);
}
void SoundBufferStreamed::updateBuffers() {
while (running) {
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::lock_guard<std::mutex> lock(soundSource->mutex);
if (!running) {
return;
}
ALint processed, state;
/* Get relevant source info */
alCheck(alGetSourcei(source, AL_SOURCE_STATE, &state));
alCheck(alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed));
std::lock_guard<std::mutex> lock(soundSource->mutex);
bool bufferedData = false;
/* Unqueue and handle each processed buffer */
@ -110,3 +119,14 @@ void SoundBufferStreamed::updateBuffers() {
}
}
}
void SoundBufferStreamed::pause() {
std::lock_guard<std::mutex> lock(soundSource->mutex);
running = false;
alCheck(alSourcePause(source));
}
void SoundBufferStreamed::stop() {
std::lock_guard<std::mutex> lock(soundSource->mutex);
running = false;
alCheck(alSourceStop(source));
}

View File

@ -14,7 +14,9 @@ struct SoundBufferStreamed : public SoundBuffer {
~SoundBufferStreamed() override;
bool bufferData(SoundSource& soundSource) final;
virtual void play() final;
void play() final;
void pause() final;
void stop() final;
private:
SoundSource* soundSource = nullptr;