1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-26 07:13:49 +01:00

Exception safety in: locking of BlockFile for read

This commit is contained in:
Paul Licameli
2016-11-28 14:28:44 -05:00
parent f5fe9281e4
commit ed6f2ea80f
5 changed files with 66 additions and 61 deletions

View File

@@ -164,7 +164,7 @@ BlockFilePtr ODDecodeBlockFile::Copy(wxFileNameWrapper &&newFileName)
BlockFilePtr newBlockFile;
//mAliasedFile can change so we lock readdatamutex, which is responsible for it.
LockRead();
auto locker = LockForRead();
if(IsSummaryAvailable())
{
//create a simpleblockfile, because once it has the summary it is a simpleblockfile for all intents an purposes
@@ -181,8 +181,6 @@ BlockFilePtr ODDecodeBlockFile::Copy(wxFileNameWrapper &&newFileName)
//It can do this by checking for IsDataAvailable()==false.
}
UnlockRead();
return newBlockFile;
}
@@ -194,7 +192,7 @@ BlockFilePtr ODDecodeBlockFile::Copy(wxFileNameWrapper &&newFileName)
void ODDecodeBlockFile::SaveXML(XMLWriter &xmlFile)
// may throw
{
LockRead();
auto locker = LockForRead();
if(IsSummaryAvailable())
{
SimpleBlockFile::SaveXML(xmlFile);
@@ -202,12 +200,12 @@ void ODDecodeBlockFile::SaveXML(XMLWriter &xmlFile)
else
{
xmlFile.StartTag(wxT("oddecodeblockfile"));
//unlock to prevent deadlock and resume lock after.
UnlockRead();
mFileNameMutex.Lock();
xmlFile.WriteAttr(wxT("summaryfile"), mFileName.GetFullName());
mFileNameMutex.Unlock();
LockRead();
{
//unlock to prevent deadlock and resume lock after.
auto suspension = locker.Suspend();
ODLocker locker2{ &mFileNameMutex };
xmlFile.WriteAttr(wxT("summaryfile"), mFileName.GetFullName());
}
xmlFile.WriteAttr(wxT("audiofile"), mAudioFileName.GetFullPath());
xmlFile.WriteAttr(wxT("aliasstart"),
mAliasStart.as_long_long());
@@ -217,7 +215,6 @@ void ODDecodeBlockFile::SaveXML(XMLWriter &xmlFile)
xmlFile.EndTag(wxT("oddecodeblockfile"));
}
UnlockRead();
}
/// Constructs a ODDecodeBlockFile from the xml output of WriteXML.
@@ -435,17 +432,16 @@ size_t ODDecodeBlockFile::ReadData(samplePtr data, sampleFormat format,
size_t start, size_t len) const
{
size_t ret;
LockRead();
auto locker = LockForRead();
if(IsSummaryAvailable())
ret = SimpleBlockFile::ReadData(data,format,start,len);
else
{
//we should do an ODRequest to start processing the data here, and wait till it finishes. and just do a SimpleBlockFIle
//we should do an ODRequest to start processing the data here, and wait till it finishes. and just do a SimpleBlockFile
//ReadData.
ClearSamples(data, format, 0, len);
ret = len;
}
UnlockRead();
return ret;
}

View File

@@ -196,7 +196,7 @@ BlockFilePtr ODPCMAliasBlockFile::Copy(wxFileNameWrapper &&newFileName)
BlockFilePtr newBlockFile;
//mAliasedFile can change so we lock readdatamutex, which is responsible for it.
LockRead();
auto locker = LockForRead();
//If the file has been written AND it has been saved, we create a PCM alias blockfile because for
//all intents and purposes, it is the same.
//However, if it hasn't been saved yet, we shouldn't create one because the default behavior of the
@@ -218,8 +218,6 @@ BlockFilePtr ODPCMAliasBlockFile::Copy(wxFileNameWrapper &&newFileName)
//The client code will need to schedule this blockfile for OD summarizing if it is going to a NEW track.
}
UnlockRead();
return newBlockFile;
}
@@ -232,7 +230,7 @@ void ODPCMAliasBlockFile::SaveXML(XMLWriter &xmlFile)
// may throw
{
//we lock this so that mAliasedFileName doesn't change.
LockRead();
auto locker = LockForRead();
if(IsSummaryAvailable())
{
PCMAliasBlockFile::SaveXML(xmlFile);
@@ -243,11 +241,11 @@ void ODPCMAliasBlockFile::SaveXML(XMLWriter &xmlFile)
xmlFile.StartTag(wxT("odpcmaliasblockfile"));
//unlock to prevent deadlock and resume lock after.
UnlockRead();
mFileNameMutex.Lock();
xmlFile.WriteAttr(wxT("summaryfile"), mFileName.GetFullName());
mFileNameMutex.Unlock();
LockRead();
{
auto suspension = locker.Suspend();
ODLocker locker2 { &mFileNameMutex };
xmlFile.WriteAttr(wxT("summaryfile"), mFileName.GetFullName());
}
xmlFile.WriteAttr(wxT("aliasfile"), mAliasedFileName.GetFullPath());
xmlFile.WriteAttr(wxT("aliasstart"),
@@ -257,8 +255,6 @@ void ODPCMAliasBlockFile::SaveXML(XMLWriter &xmlFile)
xmlFile.EndTag(wxT("odpcmaliasblockfile"));
}
UnlockRead();
}
/// Constructs a ODPCMAliasBlockFile from the xml output of WriteXML.
@@ -488,20 +484,16 @@ size_t ODPCMAliasBlockFile::ReadData(samplePtr data, sampleFormat format,
size_t start, size_t len) const
{
LockRead();
auto locker = LockForRead();
if(!mAliasedFileName.IsOk()){ // intentionally silenced
memset(data,0,SAMPLE_SIZE(format)*len);
UnlockRead();
return len;
}
auto result = CommonReadData(
return CommonReadData(
mAliasedFileName, mSilentAliasLog, this, mAliasStart, mAliasChannel,
data, format, start, len);
UnlockRead();
return result;
}
/// Read the summary of this alias block from disk. Since the audio data