/* -*-c++-*- header - Open Producer - Copyright (C) 2002 Don Burns
 * Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL)
 * as published by the Free Software Foundation.
 */
#ifndef PRODUCER_BLOCK
#define PRODUCER_BLOCK

#include <Producer/Export>
#include <Producer/Referenced>

#ifdef _USE_OPEN_THREAD
#include <OpenThread/Mutex.h>
#include <OpenThread/Condition.h>
#else
#include <pthread.h>
#endif

namespace Producer {

class PR_EXPORT Block: public Referenced {
    public:
        Block()
        {
#ifndef _USE_OPEN_THREAD
            pthread_mutex_init( &_mut, 0L );
            pthread_cond_init( &_cond, 0L );
#endif
        }


        void block()
        {
#ifdef _USE_OPEN_THREAD
            _cond.wait(&_mut);
#else
            pthread_mutex_lock( &_mut );
            pthread_cond_wait( &_cond, &_mut );
            pthread_mutex_unlock( &_mut );
#endif
        }

        void release()
        {
#ifdef _USE_OPEN_THREAD
            _cond.broadcast();
#else
            pthread_mutex_lock( &_mut );
            pthread_cond_broadcast( &_cond);
            pthread_mutex_unlock( &_mut );
#endif
        }

    protected:

        ~Block()
        {
            release();
        }

    private:
#ifdef _USE_OPEN_THREAD
        OpenThread::Mutex _mut;
        OpenThread::Condition _cond;
#else
        pthread_mutex_t _mut;
        pthread_cond_t _cond;
#endif
};


}

#endif
