/*////////////////////////////////////////////////////////////////////////////// Copyright (c) 2013 Jamboree Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) //////////////////////////////////////////////////////////////////////////////*/ #ifndef BOOST_ASIO_CONDITION_VARIABLE_HPP_INCLUDED #define BOOST_ASIO_CONDITION_VARIABLE_HPP_INCLUDED #include #include #include namespace boost { namespace asio { class condition_variable { struct handler_t { explicit handler_t(yield_context const& ctx) : coro_(ctx.coro_.lock()) {} void operator()() { (*coro_)(); } detail::shared_ptr coro_; }; public: explicit condition_variable(io_service& io) : _io(io) {} void wait(yield_context const& ctx) { _suspended.push_back(handler_t(ctx)); ctx.ca_(); } void notify_one() { if (_suspended.empty()) return; _io.post(_suspended.front()); _suspended.pop_front(); } void notify_all() { for (handler_t& h : _suspended) _io.post(h); _suspended.clear(); } private: io_service& _io; std::deque _suspended; }; struct condition_flag { explicit condition_flag(io_service& io) : _b(), _cond(io) {} void wait(yield_context const& ctx) { while (!_b) _cond.wait(ctx); } condition_flag& operator=(bool b) { if (_b = b) _cond.notify_all(); } private: bool _b; condition_variable _cond; }; }} #endif