Source

pexl / src / windows / multiplexer.cpp

#include "multiplexer.hpp"

#include "multiplexer_impl.hpp"
#include "process_impl.hpp"
#include "process.hpp"

namespace pexl
{
    namespace detail
    {
        multiplexer::multiplexer() :
            impl_(new multiplexer_impl())
        {
        }

        multiplexer::~multiplexer()
        {
            impl_->remove_unreferenced_processes();
        }

        process multiplexer::add_process(char *argv[], const std::string &cwd,
                                         const shared_source &in, const shared_sink &out, const shared_sink &err)
        {
            // TODO: increase this limit.
            if (((num_processes() + 1) * 4) > MAXIMUM_WAIT_OBJECTS)
                throw resource_allocation_failure(resource_allocation_failure::pipe, "pexl::multiplexer::add_process");

            proc_impl_ptr p = impl_->add_process(argv, cwd, in, out, err);
            return process(p, impl_);
        }

        std::size_t multiplexer::num_processes() const
        {
            return impl_->num_processes();
        }

        bool multiplexer::empty() const
        {
            return num_processes() == 0;
        }

        bool multiplexer::run(unsigned long milliseconds)
        {
            return impl_->run(milliseconds);
        }

        process multiplexer::wait_next()
        {
            proc_impl_ptr pi = impl_->wait_next();
            assert(pi);
            return process(pi, impl_);
        }

        void multiplexer::wait_all()
        {
            while (!empty()) wait_next();
        }


    } // close namespace detail

} // close namespace pexl