/* -*-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>

#include <OpenThreads/Mutex>
#include <OpenThreads/Condition>

namespace Producer {

class PR_EXPORT Block: public Referenced {
    public:
        Block():_released(false) {}

        void block()
        {
            _mut.lock();
            if( !_released )
                _cond.wait(&_mut);
            _mut.unlock();
        }

        void release()
        {
            _mut.lock();
            _released = true;
            _cond.broadcast();
            _mut.unlock();
        }

        void reset()
        {
            _mut.lock();
            _released = false;
            _mut.unlock();
        }

    protected:

        ~Block()
        {
            release();
        }

    private:
        OpenThreads::Mutex _mut;
        OpenThreads::Condition _cond;
        bool _released;
};

}

#endif
