Audacity  2.2.2
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
ODManager Class Referencefinal

A singleton that manages currently running Tasks on an arbitrary number of threads. More...

#include <ODManager.h>

Classes

class  ODManagerHelperThread
 
class  Pauser
 

Public Member Functions

void DemandTrackUpdate (WaveTrack *track, double seconds)
 changes the tasks associated with this Waveform to process the task from a different point in the track More...
 
void DecrementCurrentThreads ()
 Reduces the count of current threads running. Meant to be called when ODTaskThreads end in their own threads. Thread-safe. More...
 
void AddNewTask (std::unique_ptr< ODTask > &&mtask, bool lockMutex=true)
 Adds a wavetrack, creates a queue member. More...
 
void SignalTaskQueueLoop ()
 Wakes the queue loop up by signalling its condition variable. More...
 
void RemoveWaveTrack (WaveTrack *track)
 removes a wavetrack and notifies its associated tasks to stop using its reference. More...
 
void MakeWaveTrackIndependent (WaveTrack *track)
 if it shares a queue/task, creates a NEW queue/task for the track, and removes it from any previously existing tasks. More...
 
bool MakeWaveTrackDependent (WaveTrack *dependentTrack, WaveTrack *masterTrack)
 
void ReplaceWaveTrack (WaveTrack *oldTrack, WaveTrack *newTrack)
 replace the wavetrack whose wavecache the gui watches for updates More...
 
void AddTask (ODTask *task)
 Adds a task to the running queue. Threas-safe. More...
 
void RemoveTaskIfInQueue (ODTask *task)
 removes a task from the active task queue More...
 
void FillTipForWaveTrack (const WaveTrack *t, wxString &tip)
 fills in the status bar message for a given track More...
 
float GetOverallPercentComplete ()
 Gets the total percent complete for all tasks combined. More...
 
int GetTotalNumTasks ()
 Get Total Number of Tasks. More...
 

Static Public Member Functions

static ODManagerInstanceFirstTime ()
 Gets the singleton instance. More...
 
static ODManagerInstanceNormal ()
 Gets the singleton instance. More...
 
static void Quit ()
 Kills the ODMananger Thread. More...
 
static void MarkLoadedODFlag ()
 sets a flag that is set if we have loaded some OD blockfiles from PCM. More...
 
static void UnmarkLoadedODFlag ()
 resets a flag that is set if we have loaded some OD blockfiles from PCM. More...
 
static bool HasLoadedODFlag ()
 returns a flag that is set if we have loaded some OD blockfiles from PCM. More...
 
static bool IsInstanceCreated ()
 returns whether or not the singleton instance was created yet More...
 

Static Public Attributes

static ODManager *(* Instance )() = &(ODManager::InstanceFirstTime)
 

Protected Member Functions

 ODManager ()
 
 ~ODManager ()
 
void Init ()
 Launches a thread for the manager and starts accepting Tasks. More...
 
void Start ()
 Start the main loop for the manager. More...
 
void UpdateQueues ()
 Remove references in our array to Tasks that have been completed/Schedule NEW ones. More...
 

Protected Attributes

std::vector< std::unique_ptr
< ODWaveTrackTaskQueue > > 
mQueues
 
ODLock mQueuesMutex
 
std::vector< ODTask * > mTasks
 
ODLock mTasksMutex
 
volatile bool mPause
 
ODLock mPauseLock
 
volatile int mNeedsDraw
 
volatile int mCurrentThreads
 Number of threads currently running. Accessed thru multiple threads. More...
 
ODLock mCurrentThreadsMutex
 
int mMaxThreads
 Maximum number of threads allowed out. More...
 
volatile bool mTerminate
 
ODLock mTerminateMutex
 
volatile bool mTerminated
 
ODLock mTerminatedMutex
 
ODLock mQueueNotEmptyCondLock
 
std::unique_ptr< ODConditionmQueueNotEmptyCond
 

Static Protected Attributes

static std::unique_ptr< ODManagerpMan {}
 

Detailed Description

A singleton that manages currently running Tasks on an arbitrary number of threads.

Definition at line 43 of file ODManager.h.

Constructor & Destructor Documentation

ODManager::ODManager ( )
protected

Definition at line 55 of file ODManager.cpp.

References gPause, mPause, mQueueNotEmptyCond, mQueueNotEmptyCondLock, mTerminate, and mTerminated.

Referenced by InstanceFirstTime().

56 {
57  mTerminate = false;
58  mTerminated = false;
59  mPause = gPause;
60 
61  //must set up the queue condition
62  mQueueNotEmptyCond = std::make_unique<ODCondition>(&mQueueNotEmptyCondLock);
63 }
ODLock mQueueNotEmptyCondLock
Definition: ODManager.h:172
std::unique_ptr< ODCondition > mQueueNotEmptyCond
Definition: ODManager.h:173
volatile bool mTerminate
Definition: ODManager.h:165
volatile bool mTerminated
Definition: ODManager.h:168
static bool gPause
Definition: ODManager.cpp:31
volatile bool mPause
Definition: ODManager.h:152
ODManager::~ODManager ( )
protected

Definition at line 66 of file ODManager.cpp.

References mQueueNotEmptyCond, mQueueNotEmptyCondLock, mQueues, mTerminate, mTerminated, mTerminatedMutex, and mTerminateMutex.

67 {
68  mTerminateMutex.Lock();
69  mTerminate = true;
70  mTerminateMutex.Unlock();
71 
72  //This while loop waits for ODTasks to finish and the DELETE removes all tasks from the Queue.
73  //This function is called from the main audacity event thread, so there should not be more requests for pMan
74  mTerminatedMutex.Lock();
75  while (!mTerminated)
76  {
77  mTerminatedMutex.Unlock();
78  wxThread::Sleep(200);
79 
80  //signal the queue not empty condition since the ODMan thread will wait on the queue condition
82  mQueueNotEmptyCond->Signal();
83  mQueueNotEmptyCondLock.Unlock();
84 
85  mTerminatedMutex.Lock();
86  }
87  mTerminatedMutex.Unlock();
88 
89  //get rid of all the queues. The queues get rid of the tasks, so we don't worry abut them.
90  //nothing else should be running on OD related threads at this point, so we don't lock.
91  mQueues.clear();
92 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mTerminateMutex
Definition: ODManager.h:166
ODLock mQueueNotEmptyCondLock
Definition: ODManager.h:172
std::unique_ptr< ODCondition > mQueueNotEmptyCond
Definition: ODManager.h:173
volatile bool mTerminate
Definition: ODManager.h:165
ODLock mTerminatedMutex
Definition: ODManager.h:169
volatile bool mTerminated
Definition: ODManager.h:168

Member Function Documentation

void ODManager::AddNewTask ( std::unique_ptr< ODTask > &&  mtask,
bool  lockMutex = true 
)

Adds a wavetrack, creates a queue member.

Adds a NEW task to the queue. Creates a queue if the tracks associated with the task is not in the list

Parameters
taskthe task to add
lockMutexlocks the mutexes if true (default). This function is used within other ODManager calls, which many need to set this to false.

Definition at line 145 of file ODManager.cpp.

References ODWaveTrackTaskQueue::AddTask(), AddTask(), mQueues, and mQueuesMutex.

Referenced by AudacityProject::EnqueueODTasks(), PCMImportFileHandle::Import(), ODWaveTrackTaskQueue::MakeWaveTrackIndependent(), and AudacityProject::PopState().

146 {
147  auto task = mtask.get();
148  ODWaveTrackTaskQueue* queue = NULL;
149 
150  if(lockMutex)
151  mQueuesMutex.Lock();
152  for(unsigned int i=0;i<mQueues.size();i++)
153  {
154  //search for a task containing the lead track. wavetrack removal is threadsafe and bound to the mQueuesMutex
155  //note that GetWaveTrack is not threadsafe, but we are assuming task is not running on a different thread yet.
156  if(mQueues[i]->ContainsWaveTrack(task->GetWaveTrack(0)))
157  queue = mQueues[i].get();
158  }
159 
160  if(queue)
161  {
162  //Add it to the existing queue but keep the lock since this reference can be deleted.
163  queue->AddTask(std::move(mtask));
164  if(lockMutex)
165  mQueuesMutex.Unlock();
166  }
167  else
168  {
169  //Make a NEW one, add it to the local track queue, and to the immediate running task list,
170  //since this task is definitely at the head
171  auto newqueue = std::make_unique<ODWaveTrackTaskQueue>();
172  newqueue->AddTask(std::move(mtask));
173  mQueues.push_back(std::move(newqueue));
174  if(lockMutex)
175  mQueuesMutex.Unlock();
176 
177  AddTask(task);
178  }
179 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
void AddTask(ODTask *task)
Adds a task to the running queue. Threas-safe.
Definition: ODManager.cpp:95
ODLock mQueuesMutex
Definition: ODManager.h:144
A class representing a modular task to be used with the On-Demand structures.
void AddTask(std::unique_ptr< ODTask > &&mtask)
Add a task to the queue.
void ODManager::AddTask ( ODTask task)

Adds a task to the running queue. Threas-safe.

Adds a task to running queue. Thread-safe.

Definition at line 95 of file ODManager.cpp.

References mPause, mPauseLock, mQueueNotEmptyCond, mTasks, and mTasksMutex.

Referenced by AddNewTask(), ODTask::DoSome(), and UpdateQueues().

96 {
97  mTasksMutex.Lock();
98  mTasks.push_back(task);
99  mTasksMutex.Unlock();
100  //signal the queue not empty condition.
101  bool paused;
102 
103  mPauseLock.Lock();
104  paused=mPause;
105  mPauseLock.Unlock();
106 
107  //don't signal if we are paused since if we wake up the loop it will start processing other tasks while paused
108  if(!paused)
109  mQueueNotEmptyCond->Signal();
110 }
ODLock mTasksMutex
Definition: ODManager.h:149
ODLock mPauseLock
Definition: ODManager.h:153
std::vector< ODTask * > mTasks
Definition: ODManager.h:147
std::unique_ptr< ODCondition > mQueueNotEmptyCond
Definition: ODManager.h:173
volatile bool mPause
Definition: ODManager.h:152
void ODManager::DecrementCurrentThreads ( )

Reduces the count of current threads running. Meant to be called when ODTaskThreads end in their own threads. Thread-safe.

Definition at line 234 of file ODManager.cpp.

References mCurrentThreads, and mCurrentThreadsMutex.

Referenced by ODTaskThread::Entry().

235 {
236  mCurrentThreadsMutex.Lock();
237  mCurrentThreads--;
238  mCurrentThreadsMutex.Unlock();
239 }
volatile int mCurrentThreads
Number of threads currently running. Accessed thru multiple threads.
Definition: ODManager.h:158
ODLock mCurrentThreadsMutex
Definition: ODManager.h:160
void ODManager::DemandTrackUpdate ( WaveTrack track,
double  seconds 
)

changes the tasks associated with this Waveform to process the task from a different point in the track

changes the tasks associated with this Waveform to process the task from a different point in the track

Parameters
trackthe track to update
secondsthe point in the track from which the tasks associated with track should begin processing from.

Definition at line 464 of file ODManager.cpp.

References mQueues, and mQueuesMutex.

Referenced by SelectHandle::AssignSelection(), and SelectHandle::Click().

465 {
466  mQueuesMutex.Lock();
467  for(unsigned int i=0;i<mQueues.size();i++)
468  {
469  mQueues[i]->DemandTrackUpdate(track,seconds);
470  }
471  mQueuesMutex.Unlock();
472 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
void ODManager::FillTipForWaveTrack ( const WaveTrack t,
wxString &  tip 
)

fills in the status bar message for a given track

Definition at line 528 of file ODManager.cpp.

References mQueues, and mQueuesMutex.

529 {
530  mQueuesMutex.Lock();
531  for(unsigned int i=0;i<mQueues.size();i++)
532  {
533  mQueues[i]->FillTipForWaveTrack(t, tip);
534  }
535  mQueuesMutex.Unlock();
536 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
float ODManager::GetOverallPercentComplete ( )

Gets the total percent complete for all tasks combined.

Definition at line 539 of file ODManager.cpp.

References GetTotalNumTasks(), mQueues, and mQueuesMutex.

Referenced by AudacityProject::OnTimer().

540 {
541  float total=0.0;
542  mQueuesMutex.Lock();
543  for(unsigned int i=0;i<mQueues.size();i++)
544  {
545  total+=mQueues[i]->GetFrontTask()->PercentComplete();
546  }
547  mQueuesMutex.Unlock();
548 
549  //avoid div by zero and be thread smart.
550  int totalTasks = GetTotalNumTasks();
551  return (float) total/(totalTasks>0?totalTasks:1);
552 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
int GetTotalNumTasks()
Get Total Number of Tasks.
Definition: ODManager.cpp:555
ODLock mQueuesMutex
Definition: ODManager.h:144
int ODManager::GetTotalNumTasks ( )

Get Total Number of Tasks.

Definition at line 555 of file ODManager.cpp.

References mQueues, and mQueuesMutex.

Referenced by GetOverallPercentComplete(), and AudacityProject::OnTimer().

556 {
557  int ret=0;
558  mQueuesMutex.Lock();
559  for(unsigned int i=0;i<mQueues.size();i++)
560  {
561  ret+=mQueues[i]->GetNumTasks();
562  }
563  mQueuesMutex.Unlock();
564  return ret;
565 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
bool ODManager::HasLoadedODFlag ( )
static

returns a flag that is set if we have loaded some OD blockfiles from PCM.

Definition at line 522 of file ODManager.cpp.

References sHasLoadedOD.

Referenced by AudacityProject::EnqueueODTasks(), and DirManager::ProjectFSCK().

523 {
524  return sHasLoadedOD;
525 }
static bool sHasLoadedOD
a flag that is set if we have loaded some OD blockfiles from PCM.
Definition: ODManager.cpp:33
void ODManager::Init ( )
protected

Launches a thread for the manager and starts accepting Tasks.

Definition at line 215 of file ODManager.cpp.

References mCurrentThreads, mMaxThreads, and safenew.

216 {
217  mCurrentThreads = 0;
218  mMaxThreads = 5;
219 
220  // wxLogDebug(wxT("Initializing ODManager...Creating manager thread"));
221  // This is a detached thread, so it deletes itself when it finishes
222  // ... except on Mac where we we don't use wxThread for reasons unexplained
223  ODManagerHelperThread* startThread = safenew ODManagerHelperThread;
224 
225 // startThread->SetPriority(0);//default of 50.
226  startThread->Create();
227 // wxPrintf("starting thread from init\n");
228  startThread->Run();
229 
230 // wxPrintf("started thread from init\n");
231  //destruction of thread is taken care of by thread library
232 }
volatile int mCurrentThreads
Number of threads currently running. Accessed thru multiple threads.
Definition: ODManager.h:158
#define safenew
Definition: Audacity.h:230
int mMaxThreads
Maximum number of threads allowed out.
Definition: ODManager.h:163
ODManager * ODManager::InstanceFirstTime ( )
static

Gets the singleton instance.

Definition at line 182 of file ODManager.cpp.

References gManagerCreated, Instance, InstanceNormal(), ODManager(), pMan, and safenew.

183 {
184  gODInitedMutex.Lock();
185  if(!pMan)
186  {
187  pMan.reset(safenew ODManager());
188  pMan->Init();
189  gManagerCreated = true;
190  }
191  gODInitedMutex.Unlock();
192 
193  //change the accessor function to use the quicker method.
195 
196  return pMan.get();
197 }
static ODManager *(* Instance)()
Definition: ODManager.h:49
#define safenew
Definition: Audacity.h:230
static ODManager * InstanceNormal()
Gets the singleton instance.
Definition: ODManager.cpp:200
static ODLock gODInitedMutex
Definition: ODManager.cpp:29
static bool gManagerCreated
Definition: ODManager.cpp:30
static std::unique_ptr< ODManager > pMan
Definition: ODManager.h:140
ODManager * ODManager::InstanceNormal ( )
static

Gets the singleton instance.

Definition at line 200 of file ODManager.cpp.

References pMan.

Referenced by InstanceFirstTime().

201 {
202  return pMan.get();
203 }
static std::unique_ptr< ODManager > pMan
Definition: ODManager.h:140
bool ODManager::IsInstanceCreated ( )
static

returns whether or not the singleton instance was created yet

Definition at line 205 of file ODManager.cpp.

References gManagerCreated.

Referenced by SelectHandle::AssignSelection(), SelectHandle::Click(), WaveTrackMenuTable::OnMergeStereo(), AudacityProject::OnTimer(), ODManager::Pauser::Pause(), AudacityProject::PopState(), Quit(), Effect::ReplaceProcessedTracks(), WaveTrackMenuTable::SplitStereo(), and WaveTrack::~WaveTrack().

206 {
207  bool ret;
208  gODInitedMutex.Lock();
209  ret= gManagerCreated;
210  gODInitedMutex.Unlock();
211  return ret;
212 }
static ODLock gODInitedMutex
Definition: ODManager.cpp:29
static bool gManagerCreated
Definition: ODManager.cpp:30
bool ODManager::MakeWaveTrackDependent ( WaveTrack dependentTrack,
WaveTrack masterTrack 
)

attach the track in question to another, already existing track's queues and tasks. Remove the task/tracks. Returns success if it was possible.. Some ODTask conditions make it impossible until the Tasks finish.

attach the track in question to another, already existing track's queues and tasks. Remove the task/tracks. only works if both tracks exist. Sets needODUpdate flag for the task. This is complicated and will probably need better design in the future.

Returns
returns success. Some ODTask conditions require that the tasks finish before merging. e.g. they have different effects being processed at the same time.

Definition at line 418 of file ODManager.cpp.

References ODWaveTrackTaskQueue::CanMergeWith(), ODWaveTrackTaskQueue::MergeWaveTrack(), mQueues, and mQueuesMutex.

419 {
420  //First, check to see if the task lists are mergeable. If so, we can simply add this track to the other task and queue,
421  //then DELETE this one.
422  ODWaveTrackTaskQueue* masterQueue=NULL;
423  ODWaveTrackTaskQueue* dependentQueue=NULL;
424  unsigned int dependentIndex = 0;
425  bool canMerge = false;
426 
427  mQueuesMutex.Lock();
428  for(unsigned int i=0;i<mQueues.size();i++)
429  {
430  if(mQueues[i]->ContainsWaveTrack(masterTrack))
431  {
432  masterQueue = mQueues[i].get();
433  }
434  else if(mQueues[i]->ContainsWaveTrack(dependentTrack))
435  {
436  dependentQueue = mQueues[i].get();
437  dependentIndex = i;
438  }
439 
440  }
441  if(masterQueue&&dependentQueue)
442  canMerge=masterQueue->CanMergeWith(dependentQueue);
443 
444  //otherwise we need to let dependentTrack's queue live on. We'll have to wait till the conflicting tasks are done.
445  if(!canMerge)
446  {
447  mQueuesMutex.Unlock();
448  return false;
449  }
450  //then we add dependentTrack to the masterTrack's queue - this will allow future ODScheduling to affect them together.
451  //this sets the NeedODUpdateFlag since we don't want the head task to finish without haven't dealt with the depednent
452  masterQueue->MergeWaveTrack(dependentTrack);
453 
454  //finally remove the dependent track
455  mQueues.erase(mQueues.begin()+dependentIndex);
456  mQueuesMutex.Unlock();
457  return true;
458 }
void MergeWaveTrack(WaveTrack *track)
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
A class representing a modular task to be used with the On-Demand structures.
bool CanMergeWith(ODWaveTrackTaskQueue *otherQueue)
returns whether or not this queue's task list and another's can merge together, as when we make two m...
void ODManager::MakeWaveTrackIndependent ( WaveTrack track)

if it shares a queue/task, creates a NEW queue/task for the track, and removes it from any previously existing tasks.

Definition at line 395 of file ODManager.cpp.

References ODWaveTrackTaskQueue::MakeWaveTrackIndependent(), mQueues, and mQueuesMutex.

Referenced by WaveTrackMenuTable::SplitStereo().

396 {
397  ODWaveTrackTaskQueue* owner=NULL;
398  mQueuesMutex.Lock();
399  for(unsigned int i=0;i<mQueues.size();i++)
400  {
401  if(mQueues[i]->ContainsWaveTrack(track))
402  {
403  owner = mQueues[i].get();
404  break;
405  }
406  }
407  if(owner)
408  owner->MakeWaveTrackIndependent(track);
409 
410  mQueuesMutex.Unlock();
411 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
void MakeWaveTrackIndependent(WaveTrack *track)
A class representing a modular task to be used with the On-Demand structures.
void ODManager::MarkLoadedODFlag ( )
static

sets a flag that is set if we have loaded some OD blockfiles from PCM.

Definition at line 508 of file ODManager.cpp.

References sHasLoadedOD.

Referenced by DirManager::HandleXMLTag().

509 {
510  sHasLoadedOD = true;
511 }
static bool sHasLoadedOD
a flag that is set if we have loaded some OD blockfiles from PCM.
Definition: ODManager.cpp:33
void ODManager::Quit ( )
static

Kills the ODMananger Thread.

Definition at line 363 of file ODManager.cpp.

References IsInstanceCreated(), and pMan.

Referenced by QuitAudacity().

364 {
365  if(IsInstanceCreated())
366  {
367  pMan.reset();
368  }
369 }
static bool IsInstanceCreated()
returns whether or not the singleton instance was created yet
Definition: ODManager.cpp:205
static std::unique_ptr< ODManager > pMan
Definition: ODManager.h:140
void ODManager::RemoveTaskIfInQueue ( ODTask task)

removes a task from the active task queue

Definition at line 125 of file ODManager.cpp.

References mTasks, and mTasksMutex.

Referenced by ODWaveTrackTaskQueue::~ODWaveTrackTaskQueue().

126 {
127  mTasksMutex.Lock();
128  //linear search okay for now, (probably only 1-5 tasks exist at a time.)
129  for(unsigned int i=0;i<mTasks.size();i++)
130  {
131  if(mTasks[i]==task)
132  {
133  mTasks.erase(mTasks.begin()+i);
134  break;
135  }
136  }
137  mTasksMutex.Unlock();
138 
139 }
ODLock mTasksMutex
Definition: ODManager.h:149
std::vector< ODTask * > mTasks
Definition: ODManager.h:147
void ODManager::RemoveWaveTrack ( WaveTrack track)

removes a wavetrack and notifies its associated tasks to stop using its reference.

Definition at line 372 of file ODManager.cpp.

References mQueues, and mQueuesMutex.

Referenced by WaveTrack::~WaveTrack().

373 {
374  mQueuesMutex.Lock();
375  for(unsigned int i=0;i<mQueues.size();i++)
376  {
377  if(mQueues[i]->ContainsWaveTrack(track))
378  mQueues[i]->RemoveWaveTrack(track);
379  }
380  mQueuesMutex.Unlock();
381 }
void RemoveWaveTrack(WaveTrack *track)
removes a wavetrack and notifies its associated tasks to stop using its reference.
Definition: ODManager.cpp:372
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
void ODManager::ReplaceWaveTrack ( WaveTrack oldTrack,
WaveTrack newTrack 
)

replace the wavetrack whose wavecache the gui watches for updates

Definition at line 384 of file ODManager.cpp.

References mQueues, and mQueuesMutex.

Referenced by Effect::ReplaceProcessedTracks().

385 {
386  mQueuesMutex.Lock();
387  for(unsigned int i=0;i<mQueues.size();i++)
388  {
389  mQueues[i]->ReplaceWaveTrack(oldTrack,newTrack);
390  }
391  mQueuesMutex.Unlock();
392 }
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mQueuesMutex
Definition: ODManager.h:144
void ODManager::SignalTaskQueueLoop ( )

Wakes the queue loop up by signalling its condition variable.

Definition at line 112 of file ODManager.cpp.

References mPause, mPauseLock, and mQueueNotEmptyCond.

Referenced by AudacityProject::OnTimer().

113 {
114  bool paused;
115 
116  mPauseLock.Lock();
117  paused=mPause;
118  mPauseLock.Unlock();
119  //don't signal if we are paused
120  if(!paused)
121  mQueueNotEmptyCond->Signal();
122 }
ODLock mPauseLock
Definition: ODManager.h:153
std::unique_ptr< ODCondition > mQueueNotEmptyCond
Definition: ODManager.h:173
volatile bool mPause
Definition: ODManager.h:152
void ODManager::Start ( )
protected

Start the main loop for the manager.

Main loop for managing threads and tasks.

Definition at line 242 of file ODManager.cpp.

References AudacityProject::AllProjectDeleteMutex(), GetActiveProject(), mCurrentThreads, mCurrentThreadsMutex, mMaxThreads, mNeedsDraw, mPause, mPauseLock, mQueueNotEmptyCond, mQueueNotEmptyCondLock, mQueues, mQueuesMutex, mTasks, mTasksMutex, mTerminate, mTerminated, mTerminatedMutex, mTerminateMutex, safenew, and UpdateQueues().

Referenced by ODManager::ODManagerHelperThread::Entry().

243 {
244  bool tasksInArray;
245  bool paused;
246  int numQueues=0;
247 
248  mNeedsDraw=0;
249 
250  //wxLog calls not threadsafe. are printfs? thread-messy for sure, but safe?
251 // wxPrintf("ODManager thread strating \n");
252  //TODO: Figure out why this has no effect at all.
253  //wxThread::This()->SetPriority(30);
254  mTerminateMutex.Lock();
255  while(!mTerminate)
256  {
257  mTerminateMutex.Unlock();
258 // wxPrintf("ODManager thread running \n");
259 
260  //we should look at our WaveTrack queues to see if we can process a NEW task to the running queue.
261  UpdateQueues();
262 
263  //start some threads if necessary
264 
265  mTasksMutex.Lock();
266  tasksInArray = mTasks.size()>0;
267  mTasksMutex.Unlock();
268 
269  mPauseLock.Lock();
270  paused=mPause;
271  mPauseLock.Unlock();
272 
273  mCurrentThreadsMutex.Lock();
274  // keep adding tasks if there is work to do, up to the limit.
275  while(!paused && tasksInArray && (mCurrentThreads < mMaxThreads))
276  {
277  mCurrentThreads++;
278  mCurrentThreadsMutex.Unlock();
279 
280  mTasksMutex.Lock();
281  //detach a NEW thread.
282  // This is a detached thread, so it deletes itself when it finishes
283  // ... except on Mac where we we don't use wxThread for reasons unexplained
284  auto thread = safenew ODTaskThread(mTasks[0]);//task);
285  //thread->SetPriority(10);//default is 50.
286  thread->Create();
287  thread->Run();
288 
289  mTasks.erase(mTasks.begin());
290  tasksInArray = mTasks.size()>0;
291  mTasksMutex.Unlock();
292 
293  mCurrentThreadsMutex.Lock();
294  }
295 
296  mCurrentThreadsMutex.Unlock();
297  //use a conditon variable to block here instead of a sleep.
298 
299  // JKC: If there are no tasks ready to run, or we're paused then
300  // we wait for there to be tasks in the queue.
301  {
302  ODLocker locker{ &mQueueNotEmptyCondLock };
303  if( (!tasksInArray) || paused)
304  mQueueNotEmptyCond->Wait();
305  }
306 
307  //if there is some ODTask running, then there will be something in the queue. If so then redraw to show progress
308  mQueuesMutex.Lock();
309  mNeedsDraw += mQueues.size()>0?1:0;
310  numQueues=mQueues.size();
311  mQueuesMutex.Unlock();
312 
313  //redraw the current project only (ODTasks will send a redraw on complete even if the projects are in the background)
314  //we don't want to redraw at a faster rate when we have more queues because
315  //this means the CPU is already taxed. This if statement normalizes the rate
316  if((mNeedsDraw>numQueues) && numQueues)
317  {
318  mNeedsDraw=0;
319  wxCommandEvent event( EVT_ODTASK_UPDATE );
322  if(proj)
323  proj->GetEventHandler()->AddPendingEvent(event);
324  }
325  mTerminateMutex.Lock();
326  }
327  mTerminateMutex.Unlock();
328 
329  mTerminatedMutex.Lock();
330  mTerminated=true;
331  mTerminatedMutex.Unlock();
332 
333  //wxLogDebug Not thread safe.
334  //wxPrintf("ODManager thread terminating\n");
335 }
volatile int mCurrentThreads
Number of threads currently running. Accessed thru multiple threads.
Definition: ODManager.h:158
ODLock mCurrentThreadsMutex
Definition: ODManager.h:160
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
ODLock mTerminateMutex
Definition: ODManager.h:166
ODLock mTasksMutex
Definition: ODManager.h:149
#define safenew
Definition: Audacity.h:230
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:176
A thread that executes a part of the task specfied by an ODTask.
Definition: ODTaskThread.h:133
static ODLock & AllProjectDeleteMutex()
Prevents DELETE from external thread - for e.g. use of GetActiveProject.
Definition: Project.cpp:183
ODLock mQueueNotEmptyCondLock
Definition: ODManager.h:172
ODLock mPauseLock
Definition: ODManager.h:153
ODLock mQueuesMutex
Definition: ODManager.h:144
std::vector< ODTask * > mTasks
Definition: ODManager.h:147
std::unique_ptr< ODCondition > mQueueNotEmptyCond
Definition: ODManager.h:173
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:308
void UpdateQueues()
Remove references in our array to Tasks that have been completed/Schedule NEW ones.
Definition: ODManager.cpp:476
int mMaxThreads
Maximum number of threads allowed out.
Definition: ODManager.h:163
volatile bool mTerminate
Definition: ODManager.h:165
ODLock mTerminatedMutex
Definition: ODManager.h:169
volatile int mNeedsDraw
Definition: ODManager.h:155
volatile bool mTerminated
Definition: ODManager.h:168
volatile bool mPause
Definition: ODManager.h:152
void ODManager::UnmarkLoadedODFlag ( )
static

resets a flag that is set if we have loaded some OD blockfiles from PCM.

Definition at line 515 of file ODManager.cpp.

References sHasLoadedOD.

Referenced by AudacityProject::OpenFile().

516 {
517  sHasLoadedOD = false;
518 }
static bool sHasLoadedOD
a flag that is set if we have loaded some OD blockfiles from PCM.
Definition: ODManager.cpp:33
void ODManager::UpdateQueues ( )
protected

Remove references in our array to Tasks that have been completed/Schedule NEW ones.

remove tasks from ODWaveTrackTaskQueues that have been done. Schedules NEW ones if they exist Also remove queues that have become empty.

Definition at line 476 of file ODManager.cpp.

References AddTask(), ODWaveTrackTaskQueue::GetFrontTask(), mQueues, and mQueuesMutex.

Referenced by Start().

477 {
478  mQueuesMutex.Lock();
479  for(unsigned int i=0;i<mQueues.size();i++)
480  {
481  if(mQueues[i]->IsFrontTaskComplete())
482  {
483  //this should DELETE and remove the front task instance.
484  mQueues[i]->RemoveFrontTask();
485  //schedule next.
486  if(!mQueues[i]->IsEmpty())
487  {
488  //we need to release the lock on the queue vector before using the task vector's lock or we deadlock
489  //so get a temp.
490  ODWaveTrackTaskQueue* queue = mQueues[i].get();
491 
492  AddTask(queue->GetFrontTask());
493  }
494  }
495 
496  //if the queue is empty DELETE it.
497  if(mQueues[i]->IsEmpty())
498  {
499  mQueues.erase(mQueues.begin()+i);
500  i--;
501  }
502  }
503  mQueuesMutex.Unlock();
504 }
ODTask * GetFrontTask()
Schedules the front task for immediate execution.
std::vector< std::unique_ptr< ODWaveTrackTaskQueue > > mQueues
Definition: ODManager.h:143
void AddTask(ODTask *task)
Adds a task to the running queue. Threas-safe.
Definition: ODManager.cpp:95
ODLock mQueuesMutex
Definition: ODManager.h:144
A class representing a modular task to be used with the On-Demand structures.

Member Data Documentation

pfodman ODManager::Instance = &(ODManager::InstanceFirstTime)
static

Gets the singleton instance - this is a function pointer that points to one of the below two instance calls. Note that it is not a member function pointer since it is a static function. the function pointer swapping is valid as long as the initial calls only happen from the main thread.

Definition at line 49 of file ODManager.h.

Referenced by SelectHandle::AssignSelection(), SelectHandle::Click(), ODTask::DoSome(), AudacityProject::EnqueueODTasks(), ODTaskThread::Entry(), ODManager::ODManagerHelperThread::Entry(), PCMImportFileHandle::Import(), InstanceFirstTime(), ODWaveTrackTaskQueue::MakeWaveTrackIndependent(), WaveTrackMenuTable::OnMergeStereo(), AudacityProject::OnTimer(), AudacityProject::PopState(), Effect::ReplaceProcessedTracks(), WaveTrackMenuTable::SplitStereo(), ODWaveTrackTaskQueue::~ODWaveTrackTaskQueue(), and WaveTrack::~WaveTrack().

volatile int ODManager::mCurrentThreads
protected

Number of threads currently running. Accessed thru multiple threads.

Definition at line 158 of file ODManager.h.

Referenced by DecrementCurrentThreads(), Init(), and Start().

ODLock ODManager::mCurrentThreadsMutex
protected

Definition at line 160 of file ODManager.h.

Referenced by DecrementCurrentThreads(), and Start().

int ODManager::mMaxThreads
protected

Maximum number of threads allowed out.

Definition at line 163 of file ODManager.h.

Referenced by Init(), and Start().

volatile int ODManager::mNeedsDraw
protected

Definition at line 155 of file ODManager.h.

Referenced by Start().

volatile bool ODManager::mPause
protected

Definition at line 152 of file ODManager.h.

Referenced by AddTask(), ODManager(), SignalTaskQueueLoop(), and Start().

ODLock ODManager::mPauseLock
protected

Definition at line 153 of file ODManager.h.

Referenced by AddTask(), SignalTaskQueueLoop(), and Start().

std::unique_ptr<ODCondition> ODManager::mQueueNotEmptyCond
protected

Definition at line 173 of file ODManager.h.

Referenced by AddTask(), ODManager(), SignalTaskQueueLoop(), Start(), and ~ODManager().

ODLock ODManager::mQueueNotEmptyCondLock
protected

Definition at line 172 of file ODManager.h.

Referenced by ODManager(), Start(), and ~ODManager().

std::vector<std::unique_ptr<ODWaveTrackTaskQueue> > ODManager::mQueues
protected
ODLock ODManager::mQueuesMutex
protected
std::vector<ODTask*> ODManager::mTasks
protected

Definition at line 147 of file ODManager.h.

Referenced by AddTask(), RemoveTaskIfInQueue(), and Start().

ODLock ODManager::mTasksMutex
protected

Definition at line 149 of file ODManager.h.

Referenced by AddTask(), RemoveTaskIfInQueue(), and Start().

volatile bool ODManager::mTerminate
protected

Definition at line 165 of file ODManager.h.

Referenced by ODManager(), Start(), and ~ODManager().

volatile bool ODManager::mTerminated
protected

Definition at line 168 of file ODManager.h.

Referenced by ODManager(), Start(), and ~ODManager().

ODLock ODManager::mTerminatedMutex
protected

Definition at line 169 of file ODManager.h.

Referenced by Start(), and ~ODManager().

ODLock ODManager::mTerminateMutex
protected

Definition at line 166 of file ODManager.h.

Referenced by Start(), and ~ODManager().

std::unique_ptr< ODManager > ODManager::pMan {}
staticprotected

Definition at line 140 of file ODManager.h.

Referenced by InstanceFirstTime(), InstanceNormal(), ODManager::Pauser::Pause(), and Quit().


The documentation for this class was generated from the following files: