Audacity  2.2.0
ODManager.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  ODManager.h
6 
7  Created by Michael Chinen (mchinen) on 6/8/08
8  Audacity(R) is copyright (c) 1999-2008 Audacity Team.
9  License: GPL v2. See License.txt.
10 
11 ******************************************************************//*******************************************************************/
18 
19 #ifndef __AUDACITY_ODMANAGER__
20 #define __AUDACITY_ODMANAGER__
21 
22 #include <vector>
23 #include "ODTask.h"
24 #include "ODTaskThread.h"
25 #include <wx/thread.h>
26 #include <wx/wx.h>
27 
28 #ifdef __WXMAC__
29 // On Mac OS X, it's better not to use the wxThread class.
30 // We use our own implementation based on pthreads instead.
31 #include <pthread.h>
32 #include <time.h>
33 #endif //__WXMAC__
34 
35 DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_ODTASK_UPDATE, -1)
36 
37 int CompareNoCaseFileName(const wxString& first, const wxString& second);
41 class WaveTrack;
43 class ODManager final
44 {
45  public:
49  static ODManager* (*Instance)();
51  static ODManager* InstanceFirstTime();
53  static ODManager* InstanceNormal();
54 
56  static void Quit();
57 
59  void DemandTrackUpdate(WaveTrack* track, double seconds);
60 
62  void DecrementCurrentThreads();
63 
65  void AddNewTask(movable_ptr<ODTask> &&mtask, bool lockMutex=true);
66 
68  void SignalTaskQueueLoop();
69 
71  void RemoveWaveTrack(WaveTrack* track);
72 
74  void MakeWaveTrackIndependent(WaveTrack* track);
75 
78  bool MakeWaveTrackDependent(WaveTrack* dependentTrack,WaveTrack* masterTrack);
79 
81  void ReplaceWaveTrack(WaveTrack* oldTrack,WaveTrack* newTrack);
82 
84  void AddTask(ODTask* task);
85 
86  void RemoveTaskIfInQueue(ODTask* task);
87 
89  static void MarkLoadedODFlag();
90 
92  static void UnmarkLoadedODFlag();
93 
95  static bool HasLoadedODFlag();
96 
98  static bool IsInstanceCreated();
99 
101  void FillTipForWaveTrack( const WaveTrack * t, wxString &tip );
102 
104  float GetOverallPercentComplete();
105 
107  int GetTotalNumTasks();
108 
109  // RAII object for pausing and resuming..
110  class Pauser
111  {
112  //Pause/unpause all OD Tasks. Does not occur immediately.
113  static void Pause(bool pause = true);
114  static void Resume();
115  public:
116  Pauser() { Pause(); }
117  ~Pauser() { Resume(); }
118  Pauser(const Pauser&) PROHIBITED;
119  Pauser &operator= (const Pauser&) PROHIBITED;
120  };
121 
122 
123 
124  protected:
125  //private constructor - Singleton.
126  ODManager();
127  //private constructor - DELETE with static method Quit()
128  friend std::default_delete < ODManager > ;
129  ~ODManager();
131  void Init();
132 
134  void Start();
135 
137  void UpdateQueues();
138 
139  //instance
140  static std::unique_ptr<ODManager> pMan;
141 
142  //List of tracks and their active and inactive tasks.
143  std::vector<movable_ptr<ODWaveTrackTaskQueue>> mQueues;
144  ODLock mQueuesMutex;
145 
146  //List of current Task to do.
147  std::vector<ODTask*> mTasks;
148  //mutex for above variable
149  ODLock mTasksMutex;
150 
151  //global pause switch for OD
152  volatile bool mPause;
153  ODLock mPauseLock;
154 
155  volatile int mNeedsDraw;
156 
158  volatile int mCurrentThreads;
159  //mutex for above variable
160  ODLock mCurrentThreadsMutex;
161 
163  int mMaxThreads;
164 
165  volatile bool mTerminate;
166  ODLock mTerminateMutex;
167 
168  volatile bool mTerminated;
169  ODLock mTerminatedMutex;
170 
171  //for the queue not empty comdition
172  ODLock mQueueNotEmptyCondLock;
173  std::unique_ptr<ODCondition> mQueueNotEmptyCond;
174 
175 #ifdef __WXMAC__
176 
177 // On Mac OS X, it's better not to use the wxThread class.
178 // We use our own implementation based on pthreads instead.
179 
180 class ODManagerHelperThread {
181  public:
182  typedef int ExitCode;
183  ODManagerHelperThread() { mDestroy = false; mThread = NULL; }
184  /* ExitCode*/ void Entry(){
186  }
187 
188  void Create() {}
189  void Delete() {
190  mDestroy = true;
191  pthread_join(mThread, NULL);
192  }
193  bool TestDestroy() { return mDestroy; }
194  void Sleep(int ms) {
195  struct timespec spec;
196  spec.tv_sec = 0;
197  spec.tv_nsec = ms * 1000 * 1000;
198  nanosleep(&spec, NULL);
199  }
200  static void *callback(void *p) {
202  /* return (void *) */th->Entry();
203  return NULL;
204  }
205 
208  void SetPriority(int priority)
209  {
210  mPriority=priority;
211  }
212 
213  void Run() {
214  pthread_create(&mThread, NULL, callback, this);
215  }
216  private:
217  bool mDestroy;
218  pthread_t mThread;
219  int mPriority;
220 };
221 #else
222  class ODManagerHelperThread final : public wxThread
223  {
224  public:
227  ODManagerHelperThread(): wxThread(){}
228 
229  protected:
231  void *Entry()
232  {
234  return NULL;
235  }
236 
237  };
238 #endif //__WXMAC__
239 };
240 
241 #endif
242 
int CompareNoCaseFileName(const wxString &first, const wxString &second)
wxstring compare function for sorting case, which is needed to load correctly.
Definition: ODManager.cpp:49
static ODManager *(* Instance)()
Definition: ODManager.h:49
A singleton that manages currently running Tasks on an arbitrary number of threads.
Definition: ODManager.h:43
~Pauser()
Definition: ODManager.h:117
#define PROHIBITED
Definition: Audacity.h:224
std::unique_ptr< T > movable_ptr
Definition: MemoryX.h:683
Definition: ODManager.h:222
ODManagerHelperThread()
Definition: ODManager.h:227
Definition: ODTaskThread.h:159
A class representing a modular task to be used with the On-Demand structures.
Definition: ODTask.h:39
void * Entry()
Executes a part of the task.
Definition: ODManager.h:231
Definition: ODManager.h:110
Pauser()
Definition: ODManager.h:116
A Track that contains audio waveform data.
Definition: WaveTrack.h:76
Definition: EditToolBar.cpp:210
Definition: ODTaskThread.h:150
void Start()
Start the main loop for the manager.
Definition: ODManager.cpp:242
A class representing a modular task to be used with the On-Demand structures.
Definition: ODWaveTrackTaskQueue.h:32
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, EVT_OPEN_AUDIO_FILE,-1)