From c9204af7fe0633a384f913f7ba5ba4121e39c035 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Mon, 15 Aug 2016 10:51:16 -0400 Subject: [PATCH] Rewrite functions managing the missing block file... ... removing one ref and deref of block files. This unblocks a future development that will manage all block files with std::shared_ptr. --- src/AudacityApp.cpp | 59 +++++++++++++++++++++------------------------ src/AudacityApp.h | 7 ++++-- src/MemoryX.h | 1 + 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 3ba24552f..5aaa1e4ce 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -889,24 +889,15 @@ void AudacityApp::OnTimer(wxTimerEvent& WXUNUSED(event)) // find which project owns the blockfile // note: there may be more than 1, but just go with the first one. size_t numProjects = gAudacityProjects.size(); + AudacityProject *offendingProject {}; wxString missingFileName; - AudacityProject *offendingProject = NULL; - m_LastMissingBlockFileLock.Lock(); - if (numProjects == 1) { - // if there is only one project open, no need to search - offendingProject = gAudacityProjects[0].get(); - } else if (numProjects > 1) { - for (size_t i = 0; i < numProjects; i++) { - // search each project for the blockfile - if (gAudacityProjects[i]->GetDirManager()->ContainsBlockFile(m_LastMissingBlockFile)) { - offendingProject = gAudacityProjects[i].get(); - break; - } - } + { + ODLocker locker { &m_LastMissingBlockFileLock }; + offendingProject = + AProjectHolder{ m_LastMissingBlockFileProject }.get(); + missingFileName = m_LastMissingBlockFilePath; } - missingFileName = ((AliasBlockFile*)m_LastMissingBlockFile)->GetAliasedFileName().GetFullPath(); - m_LastMissingBlockFileLock.Unlock(); // if there are no projects open, don't show the warning (user has closed it) if (offendingProject) { @@ -937,19 +928,26 @@ locations of the missing files."), missingFileName.c_str()); } } -void AudacityApp::MarkAliasedFilesMissingWarning(const BlockFile *b) +void AudacityApp::MarkAliasedFilesMissingWarning(const AliasBlockFile *b) { - // the reference counting provides thread safety. + ODLocker locker { &m_LastMissingBlockFileLock }; + if (b) { + size_t numProjects = gAudacityProjects.size(); + for (size_t ii = 0; ii < numProjects; ++ii) { + // search each project for the blockfile + if (gAudacityProjects[ii]->GetDirManager()->ContainsBlockFile(b)) { + m_LastMissingBlockFileProject = gAudacityProjects[ii]; + break; + } + } + } + else + m_LastMissingBlockFileProject = {}; + if (b) - b->Ref(); - - m_LastMissingBlockFileLock.Lock(); - if (m_LastMissingBlockFile) - m_LastMissingBlockFile->Deref(); - - m_LastMissingBlockFile = b; - - m_LastMissingBlockFileLock.Unlock(); + m_LastMissingBlockFilePath = b->GetAliasedFileName().GetFullPath(); + else + m_LastMissingBlockFilePath = wxString{}; } void AudacityApp::SetMissingAliasedFileWarningShouldShow(bool b) @@ -961,15 +959,15 @@ void AudacityApp::SetMissingAliasedFileWarningShouldShow(bool b) m_aliasMissingWarningShouldShow = b; // reset the warnings as they were probably marked by a previous run if (m_aliasMissingWarningShouldShow) { - MarkAliasedFilesMissingWarning(NULL); + MarkAliasedFilesMissingWarning( nullptr ); } } bool AudacityApp::ShouldShowMissingAliasedFileWarning() { - bool ret = m_LastMissingBlockFile && m_aliasMissingWarningShouldShow; - - return ret; + ODLocker locker { &m_LastMissingBlockFileLock }; + auto ptr = m_LastMissingBlockFileProject.lock(); + return ptr && m_aliasMissingWarningShouldShow; } AudacityLogger *AudacityApp::GetLogger() @@ -1185,7 +1183,6 @@ bool AudacityApp::OnInit() mLocale = NULL; m_aliasMissingWarningShouldShow = true; - m_LastMissingBlockFile = NULL; #if defined(__WXMAC__) // Disable window animation diff --git a/src/AudacityApp.h b/src/AudacityApp.h index d0be6882d..33767f8a7 100644 --- a/src/AudacityApp.h +++ b/src/AudacityApp.h @@ -41,6 +41,7 @@ class Importer; class CommandHandler; class AppCommandEvent; class AudacityLogger; +class AudacityProject; void SaveWindowSize(); @@ -179,6 +180,7 @@ inline CommandFlag & operator |= (CommandFlag &lhs, CommandFlag rhs) using CommandMask = CommandFlag; class BlockFile; +class AliasBlockFile; class AudacityApp final : public wxApp { public: @@ -226,7 +228,7 @@ class AudacityApp final : public wxApp { * ShouldShowMissingAliasedFileWarning can be called to determine * if the user should be notified */ - void MarkAliasedFilesMissingWarning(const BlockFile *b); + void MarkAliasedFilesMissingWarning(const AliasBlockFile *b); /** \brief Changes the behavior of missing aliased blockfiles warnings */ @@ -293,7 +295,8 @@ class AudacityApp final : public wxApp { wxTimer mTimer; bool m_aliasMissingWarningShouldShow; - const BlockFile *m_LastMissingBlockFile; + std::weak_ptr< AudacityProject > m_LastMissingBlockFileProject; + wxString m_LastMissingBlockFilePath; ODLock m_LastMissingBlockFileLock; diff --git a/src/MemoryX.h b/src/MemoryX.h index 5058a6e6c..6cb6f89d4 100644 --- a/src/MemoryX.h +++ b/src/MemoryX.h @@ -33,6 +33,7 @@ using std::isinf; namespace std { using std::tr1::shared_ptr; + using std::tr1::weak_ptr; using std::tr1::remove_reference; template struct default_delete