Audacity  3.0.3
ODTaskThread.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  ODTaskThread.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 ******************************************************************//*******************************************************************/
17 
18 
19 
20 
21 
22 #ifndef __AUDACITY_ODTASKTHREAD__
23 #define __AUDACITY_ODTASKTHREAD__
24 
25 #include "../Audacity.h" // contains the set-up of AUDACITY_DLL_API
26 
27 #include <wx/thread.h> // to inherit
28 
29 #include "../MemoryX.h"
30 
31 class ODTask;
32 
33 #ifdef __WXMAC__
34 
35 #include <pthread.h>
36 #include <time.h>
37 
38 class ODLock {
39  public:
40  ODLock(){
41  pthread_mutex_init (&mutex, NULL);
42  }
43 
44  void Lock()
45  {
46  pthread_mutex_lock (&mutex);
47  }
48 
49  // Returns 0 iff the lock was acquired.
50  bool TryLock()
51  {
52  return pthread_mutex_trylock (&mutex);
53  }
54 
55  void Unlock()
56  {
57  pthread_mutex_unlock (&mutex);
58  }
59 
60  virtual ~ODLock()
61  {
62  pthread_mutex_destroy (&mutex);
63  }
64 
65 private:
66  friend class ODCondition; //needs friendship for wait()
67  pthread_mutex_t mutex;
68 };
69 
70 class ODCondition
71 {
72 public:
73  ODCondition(ODLock *lock);
74  virtual ~ODCondition();
75  void Signal();
76  void Broadcast();
77  void Wait();
78 
79 protected:
80  pthread_cond_t condition;
81  ODLock* m_lock;
82 };
83 
84 #else
85 
86 
87 //a wrapper for wxMutex.
88 class AUDACITY_DLL_API ODLock final : public wxMutex
89 {
90 public:
93  ODLock(){}
94  virtual ~ODLock(){}
95 };
96 
97 class ODCondition final : public wxCondition
98 {
99 public:
100  ODCondition(ODLock *lock):wxCondition(*lock){}
101  virtual ~ODCondition(){}
102  //these calls are implemented in wxCondition:
103  //void Signal();
104  //void Broadcast();
105  //void Wait();
106 
107 protected:
108 };
109 
110 #endif // __WXMAC__
111 
112 // class ODLocker
113 // So you can use the RAII idiom with ODLock, on whatever platform
114 // Construct with pointer to the lock, or default-construct and later
115 // reset()
116 // If constructed with only a try-lock, and the lock was not acquired,
117 // then it returns false when cast to bool
118 struct ODUnlocker { void operator () (ODLock *p) const { if(p) p->Unlock(); } };
119 using ODLockerBase = std::unique_ptr<ODLock, ODUnlocker>;
120 class ODLocker : public ODLockerBase {
121 public:
122  // Lock any bare pointer to ODLock at construction time or when resetting.
123  explicit ODLocker(ODLock *p = nullptr, bool tryOnly = false)
124  {
125  reset(p, tryOnly);
126  }
127 
128  void reset(ODLock *p = nullptr, bool tryOnly = false)
129  {
130  ODLockerBase::reset(p);
131  if(p) {
132  if (tryOnly) {
133  if (p->TryLock() != 0)
134  ODLockerBase::reset(nullptr);
135  }
136  else
137  p->Lock();
138  }
139  }
140 
141  // Assume already locked when moving ODLocker. Don't lock again.
142  ODLocker(ODLocker&& that) : ODLockerBase { std::move(that) } {}
144  ODLockerBase::operator= ( std::move(that) );
145  return *this;
146  }
147 
148  ODLocker(const ODLocker &that) PROHIBITED;
149  ODLocker &operator= (const ODLocker &that) PROHIBITED;
150 };
151 
152 #endif //__AUDACITY_ODTASKTHREAD__
153 
ODUnlocker
Definition: ODTaskThread.h:118
ODLocker::ODLocker
ODLocker(const ODLocker &that) PROHIBITED
ODLocker::ODLocker
ODLocker(ODLocker &&that)
Definition: ODTaskThread.h:142
ODUnlocker::operator()
void operator()(ODLock *p) const
Definition: ODTaskThread.h:118
ODLocker::ODLocker
ODLocker(ODLock *p=nullptr, bool tryOnly=false)
Definition: ODTaskThread.h:123
ODCondition::~ODCondition
virtual ~ODCondition()
Definition: ODTaskThread.h:101
ODCondition
Definition: ODTaskThread.h:98
ODLocker::reset
void reset(ODLock *p=nullptr, bool tryOnly=false)
Definition: ODTaskThread.h:128
ODCondition::ODCondition
ODCondition(ODLock *lock)
Definition: ODTaskThread.h:100
ODLocker
Definition: ODTaskThread.h:120
ODTask
A class representing a modular task to be used with the On-Demand structures.
Definition: ODTask.h:40
ODLocker::operator=
ODLocker & operator=(ODLocker &&that)
Definition: ODTaskThread.h:143
ODLock
Definition: ODTaskThread.h:89
ODLock::ODLock
ODLock()
Definition: ODTaskThread.h:93
ODLockerBase
std::unique_ptr< ODLock, ODUnlocker > ODLockerBase
Definition: ODTaskThread.h:119
ODLock::~ODLock
virtual ~ODLock()
Definition: ODTaskThread.h:94