1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-23 15:50:05 +02:00

ODLock has a non-blocking TryLock (not yet used); remove unnecessary mallocs.

This commit is contained in:
Paul Licameli 2016-04-16 12:12:39 -04:00
parent 3ed5defb69
commit 09b7fed2f9
2 changed files with 17 additions and 15 deletions

View File

@ -59,28 +59,26 @@ void *ODTaskThread::Entry()
#ifdef __WXMAC__ #ifdef __WXMAC__
ODCondition::ODCondition(ODLock *lock) ODCondition::ODCondition(ODLock *lock)
{ {
condition = (pthread_cond_t *) malloc (sizeof (pthread_cond_t)); pthread_cond_init(&condition, NULL);
pthread_cond_init(condition, NULL);
m_lock=lock; m_lock=lock;
} }
ODCondition::~ODCondition() ODCondition::~ODCondition()
{ {
pthread_cond_destroy (condition); pthread_cond_destroy (&condition);
free(condition);
} }
void ODCondition::Signal() void ODCondition::Signal()
{ {
pthread_cond_signal(condition); pthread_cond_signal(&condition);
} }
void ODCondition::Broadcast() void ODCondition::Broadcast()
{ {
pthread_cond_broadcast(condition); pthread_cond_broadcast(&condition);
} }
void ODCondition::Wait() void ODCondition::Wait()
{ {
pthread_cond_wait(condition,m_lock->mutex); pthread_cond_wait(&condition, &m_lock->mutex);
} }
#endif #endif

View File

@ -83,29 +83,33 @@ class ODTaskThread {
class ODLock { class ODLock {
public: public:
ODLock(){ ODLock(){
mutex = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t)); pthread_mutex_init (&mutex, NULL);
pthread_mutex_init (mutex, NULL);
} }
void Lock() void Lock()
{ {
pthread_mutex_lock (mutex); pthread_mutex_lock (&mutex);
}
// Returns 0 iff the lock was acquired.
bool TryLock()
{
return pthread_mutex_trylock (&mutex);
} }
void Unlock() void Unlock()
{ {
pthread_mutex_unlock (mutex); pthread_mutex_unlock (&mutex);
} }
virtual ~ODLock() virtual ~ODLock()
{ {
pthread_mutex_destroy (mutex); pthread_mutex_destroy (&mutex);
free(mutex);
} }
private: private:
friend class ODCondition; //needs friendship for wait() friend class ODCondition; //needs friendship for wait()
pthread_mutex_t *mutex ; pthread_mutex_t mutex ;
}; };
class ODCondition class ODCondition
@ -118,7 +122,7 @@ public:
void Wait(); void Wait();
protected: protected:
pthread_cond_t *condition; pthread_cond_t condition;
ODLock* m_lock; ODLock* m_lock;
}; };