[ previous ] [ next ] [ threads ]
 To :  Yate mailing list <yate@v...>
 From :  Maciek Kaminski <maciejka@t...>
 Subject :  Condition class proposal
 Date :  Wed, 09 Aug 2006 18:17:44 +0200
There is a reapearing need in yate for synchronization construct called 
conditional variables. There is a need for that in extmodule, wavefile 
like modules (for ThreadedSource cancelation) and others. Here is 
initial impementation for unix. I am asking windows people to verify 
whether it is possible to implement the same under windows:


class YATE_API Condition
{
  public:
   Condition(Mutex *mutex);
   Condition();
   ~Condition();
   void acquire();
   void release();
   void wait(int timeout = 0);
   void notify();
   void notifyAll();
  private:
   Mutex *m_mutex;
   pthread_cond_t m_cond;
   int m_waiting;
   bool cleanup;
};


Condition::Condition(Mutex *mutex)
   :m_mutex(mutex), m_cond(),
    m_waiting(0), cleanup(false)
{
   ::pthread_cond_init(&m_cond, NULL);
}

Condition::Condition()
   :m_mutex(0), m_waiting(0), cleanup(true)
{
   m_mutex = new Mutex();
   ::pthread_cond_init(&m_cond, NULL);
}

Condition::~Condition() {
   if(cleanup)
     delete m_mutex;
   pthread_cond_destroy(&m_cond);
}

void Condition::acquire() {
   m_mutex->lock();
}

void Condition::release() {
   m_mutex->unlock();
}

void Condition::wait(int timeout) {
   m_waiting++;
   Debug(DebugAll,"Condition::wait() on condition [%p]",this);
   ::pthread_cond_wait(&m_cond, &m_mutex->m_private->m_mutex);
   Debug(DebugAll,"Condition::wait() succedded on condition [%p]",this);
   m_waiting--;
}

void Condition::notify() {
   Debug(DebugAll,"Condition::notify() on condition [%p]",this);
   ::pthread_cond_signal(&m_cond);
}

void Condition::notifyAll() {
   for(int i = m_waiting; i-- > 0; notify());
}

mk