Reviewed the ODLocks removed at cbf1bb5, restored just two of them...

... as std::mutex instead.

One is the mutex for the set of all files.

Another is in Profiler.h, which is now back into CMakeLists because the header
is included in one place.  Maybe this utility will find more use again.

Restored one other but commented out, as not necessary now, but need might be
found to put it back again.

The class ODLock was a misnamed thing not specific to the on-demand manager but
used more widely as a thread synchronization utility, functioning as a mutex
object.  It was a mistake to remove all uses of it along with the rest of the
on-demand system.
This commit is contained in:
Paul Licameli 2020-07-07 16:22:31 -04:00
parent 163dd719cc
commit ba7ebbb032
7 changed files with 31 additions and 4 deletions

View File

@ -191,6 +191,8 @@ list( APPEND SOURCES
Prefs.h
Printing.cpp
Printing.h
Profiler.cpp
Profiler.h
Project.cpp
Project.h
ProjectAudioIO.cpp

View File

@ -320,6 +320,8 @@ static OSType sf_header_mactype(int format)
#endif // __WXMAC__
//std::mutex libSndFileMutex;
int SFFileCloser::operator() (SNDFILE *sf) const
{
auto err = SFCall<int>(sf_close, sf);

View File

@ -15,6 +15,7 @@
#include "audacity/Types.h"
//#include <mutex>
#include "MemoryX.h"
#include "sndfile.h"
@ -105,9 +106,16 @@ extern FileExtensions sf_get_all_extensions();
wxString sf_normalize_name(const char *name);
// This function wrapper uses a mutex to serialize calls to the SndFile library.
// PRL: Keeping this in a comment, but with Unitary, the only remaining uses
// of libsndfile should be in import/export and so are on the main thread only
//extern std::mutex libSndFileMutex;
template<typename R, typename F, typename... Args>
inline R SFCall(F fun, Args&&... args)
{
//std::lock_guard<std::mutex> guard{ libSndFileMutex };
return fun(std::forward<Args>(args)...);
}

View File

@ -63,20 +63,18 @@ Profiler::~Profiler()
///start the task timer.
void Profiler::Begin(const char* fileName, int lineNum, const char* taskDescription)
{
mTasksMutex.Lock();
std::lock_guard<std::mutex> guard{ mTasksMutex };
GetOrCreateTaskProfile(fileName,lineNum)->Begin(fileName,lineNum,taskDescription);
mTasksMutex.Unlock();
}
///end the task timer.
void Profiler::End(const char* fileName, int lineNum, const char* taskDescription)
{
mTasksMutex.Lock();
std::lock_guard<std::mutex> guard{ mTasksMutex };
TaskProfile* tp;
tp=GetTaskProfileByDescription(taskDescription);
if(tp)
tp->End(fileName,lineNum,taskDescription);
mTasksMutex.Unlock();
}
///Gets the singleton instance

View File

@ -26,8 +26,10 @@ but it will probably work fine if you use it on a high level.
#ifndef __AUDACITY_PROFILER__
#define __AUDACITY_PROFILER__
#include <mutex>
#include <vector>
#include <time.h>
#include "MemoryX.h"
#define BEGIN_TASK_PROFILING(TASK_DESCRIPTION) Profiler::Instance()->Begin(__FILE__,__LINE__,TASK_DESCRIPTION)
@ -59,6 +61,8 @@ class Profiler
//List of current Task to do.
std::vector<std::unique_ptr<TaskProfile>> mTasks;
//mutex for above variable
std::mutex mTasksMutex;
};

View File

@ -47,6 +47,7 @@ auto AllProjects::rend() const -> const_reverse_iterator
auto AllProjects::Remove( AudacityProject &project ) -> value_type
{
std::lock_guard<std::mutex> guard{ Mutex() };
auto start = begin(), finish = end(), iter = std::find_if(
start, finish,
[&]( const value_type &ptr ){ return ptr.get() == &project; }
@ -60,6 +61,7 @@ auto AllProjects::Remove( AudacityProject &project ) -> value_type
void AllProjects::Add( const value_type &pProject )
{
std::lock_guard<std::mutex> guard{ Mutex() };
gAudacityProjects.push_back( pProject );
}
@ -85,6 +87,12 @@ bool AllProjects::Close( bool force )
return true;
}
std::mutex &AllProjects::Mutex()
{
static std::mutex theMutex;
return theMutex;
}
int AudacityProject::mProjectCounter=0;// global counter.
/* Define Global Variables */

View File

@ -16,6 +16,7 @@
#include "ClientData.h" // to inherit
#include <memory>
#include <mutex>
#include <wx/weakref.h> // member variable
#include <wx/window.h> // MSVC wants this
@ -63,6 +64,10 @@ public:
// This invalidates iterators
void Add( const value_type &pProject );
/// In case you must iterate in a non-main thread, use this to prevent
/// changes in the set of open projects
static std::mutex &Mutex();
// Return true if all projects do close (always so if force == true)
// But if return is false, that means the user cancelled close of at least
// one un-saved project.