- Moved all music related synchronization to the top level

Most of the synchronization was too deep in the implementation so that it did not guard everything it needed.

Now each song has precisely one mutex which must be locked for all access to its internals - this is done in the public ZMusic interface
This commit is contained in:
Christoph Oelckers 2019-10-15 00:49:40 +02:00
parent dc32c2148a
commit 6854a509e9
10 changed files with 20 additions and 21 deletions

View file

@ -66,7 +66,6 @@ OPLmusicBlock::~OPLmusicBlock()
void OPLmusicBlock::ResetChips (int numchips)
{
std::lock_guard<std::mutex> lock(ChipAccess);
io->Reset ();
NumChips = io->Init(currentCore, std::min(numchips, 2), FullPan, false);
}
@ -255,7 +254,6 @@ bool OPLmusicBlock::ServiceStream (void *buff, int numbytes)
memset(buff, 0, numbytes);
std::lock_guard<std::mutex> lock(ChipAccess);
while (numsamples > 0)
{
int tick_in = int(NextTickIn);

View file

@ -29,8 +29,6 @@ protected:
int currentCore;
bool Looping;
bool FullPan;
std::mutex ChipAccess;
};
class OPLmusicFile : public OPLmusicBlock

View file

@ -84,7 +84,6 @@ public:
SoundStreamInfo GetStreamInfo() const override;
protected:
std::mutex CritSec;
double Tempo;
double Division;
double SamplesPerTick;

View file

@ -479,7 +479,6 @@ std::string FluidSynthMIDIDevice::GetStats()
return "FluidSynth is invalid";
}
std::lock_guard<std::mutex> lock(CritSec);
int polyphony = fluid_synth_get_polyphony(FluidSynth);
int voices = fluid_synth_get_active_voice_count(FluidSynth);
double load = fluid_synth_get_cpu_load(FluidSynth);

View file

@ -223,7 +223,6 @@ void SoftSynthMIDIDevice::Stop()
int SoftSynthMIDIDevice::StreamOutSync(MidiHeader *header)
{
std::lock_guard<std::mutex> lock(CritSec);
StreamOut(header);
return 0;
}
@ -374,7 +373,6 @@ bool SoftSynthMIDIDevice::ServiceStream (void *buff, int numbytes)
samples1 = samples;
memset(buff, 0, numbytes);
std::lock_guard<std::mutex> lock(CritSec);
while (Events != NULL && numsamples > 0)
{
double ticky = NextTickIn;

View file

@ -86,7 +86,6 @@ protected:
size_t written;
DUH *duh;
DUH_SIGRENDERER *sr;
std::mutex crit_sec;
bool open2(long pos);
long render(double volume, double delta, long samples, sample_t **buffer);
@ -950,7 +949,6 @@ bool DumbSong::GetData(void *buffer, size_t sizebytes)
memset(buffer, 0, sizebytes);
return false;
}
std::lock_guard<std::mutex> lock_(crit_sec);
while (sizebytes > 0)
{
@ -1070,7 +1068,6 @@ bool DumbSong::SetSubsong(int order)
start_order = order;
return true;
}
std::lock_guard<std::mutex> lock(crit_sec);
DUH_SIGRENDERER *oldsr = sr;
sr = NULL;
start_order = order;

View file

@ -60,7 +60,6 @@ public:
SoundStreamInfo GetFormat() override;
protected:
std::mutex CritSec;
Music_Emu *Emu;
gme_info_t *TrackInfo;
int SampleRate;
@ -245,7 +244,6 @@ bool GMESong::StartTrack(int track, bool getcritsec)
if (getcritsec)
{
std::lock_guard<std::mutex> lock(CritSec);
err = gme_start_track(Emu, track);
}
else
@ -351,7 +349,6 @@ bool GMESong::GetData(void *buffer, size_t len)
{
gme_err_t err;
std::lock_guard<std::mutex> lock(CritSec);
if (gme_track_ended(Emu))
{
if (m_Looping)

View file

@ -53,7 +53,6 @@ public:
bool GetData(void *buffer, size_t len) override;
protected:
std::mutex CritSec;
SoundDecoder *Decoder;
int Channels;
int SampleRate;
@ -440,7 +439,6 @@ std::string SndFileSong::GetStats()
bool SndFileSong::GetData(void *vbuff, size_t len)
{
char *buff = (char*)vbuff;
std::lock_guard<std::mutex> lock(CritSec);
size_t currentpos = Decoder->getSampleOffset();
size_t framestoread = len / (Channels*2);

View file

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <mutex>
#include "mididefs.h"
// The base music class. Everything is derived from this --------------------
@ -37,4 +38,5 @@ public:
STATE_Paused
} m_Status = STATE_Stopped;
bool m_Looping = false;
std::mutex CritSec;
};

View file

@ -312,10 +312,11 @@ MusInfo *ZMusic_OpenCDSong (int track, int id)
//
//==========================================================================
bool ZMusic_FillStream(MusInfo* stream, void* buff, int len)
bool ZMusic_FillStream(MusInfo* song, void* buff, int len)
{
if (stream == nullptr) return false;
return stream->ServiceStream(buff, len);
if (song == nullptr) return false;
std::lock_guard<std::mutex> lock(song->CritSec);
return song->ServiceStream(buff, len);
}
//==========================================================================
@ -339,36 +340,42 @@ void ZMusic_Start(MusInfo *song, int subsong, bool loop)
void ZMusic_Pause(MusInfo *song)
{
if (!song) return;
std::lock_guard<std::mutex> lock(song->CritSec);
song->Pause();
}
void ZMusic_Resume(MusInfo *song)
{
if (!song) return;
std::lock_guard<std::mutex> lock(song->CritSec);
song->Resume();
}
void ZMusic_Update(MusInfo *song)
{
if (!song) return;
std::lock_guard<std::mutex> lock(song->CritSec);
song->Update();
}
bool ZMusic_IsPlaying(MusInfo *song)
{
if (!song) return false;
std::lock_guard<std::mutex> lock(song->CritSec);
return song->IsPlaying();
}
void ZMusic_Stop(MusInfo *song)
{
if (!song) return;
std::lock_guard<std::mutex> lock(song->CritSec);
song->Stop();
}
bool ZMusic_SetSubsong(MusInfo *song, int subsong)
{
if (!song) return false;
std::lock_guard<std::mutex> lock(song->CritSec);
return song->SetSubsong(subsong);
}
@ -387,21 +394,27 @@ bool ZMusic_IsMIDI(MusInfo *song)
SoundStreamInfo ZMusic_GetStreamInfo(MusInfo *song)
{
if (!song) return {};
std::lock_guard<std::mutex> lock(song->CritSec);
return song->GetStreamInfo();
}
void ZMusic_Close(MusInfo *song)
{
if (song) delete song;
if (!song) return;
std::lock_guard<std::mutex> lock(song->CritSec);
delete song;
}
void ZMusic_VolumeChanged(MusInfo *song)
{
if (song) song->MusicVolumeChanged();
if (!song) return;
std::lock_guard<std::mutex> lock(song->CritSec);
song->MusicVolumeChanged();
}
std::string ZMusic_GetStats(MusInfo *song)
{
if (!song) return "";
std::lock_guard<std::mutex> lock(song->CritSec);
return song->GetStats();
}