2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-02-19 02:12:24 +00:00

boost.asio related stuff (fiber-aware io_servic etc.)

This commit is contained in:
Oliver Kowalke
2013-06-14 20:37:47 +02:00
parent 1db7951449
commit de3bc64e4f
15 changed files with 621 additions and 23 deletions

View File

@@ -8,12 +8,10 @@
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/move/move.hpp>
#include <boost/thread/locks.hpp>
#include <boost/utility.hpp>
#include <boost/fiber/detail/config.hpp>
#include <boost/fiber/fiber.hpp>
#include <boost/fiber/detail/fiber_base.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX

View File

@@ -8,7 +8,9 @@
#define BOOST_FIBERS_H
#include <boost/fiber/algorithm.hpp>
#include <boost/fiber/asio/io_service.hpp>
#include <boost/fiber/asio/use_future.hpp>
#include <boost/fiber/asio/yield.hpp>
#include <boost/fiber/attributes.hpp>
#include <boost/fiber/barrier.hpp>
#include <boost/fiber/bounded_queue.hpp>

View File

@@ -0,0 +1,182 @@
#ifndef BOOST_FIBERS_ASIO_DETAIL_YIELD_HPP
#define BOOST_FIBERS_ASIO_DETAIL_YIELD_HPP
#include <boost/asio/async_result.hpp>
#include <boost/asio/detail/config.hpp>
#include <boost/asio/handler_type.hpp>
#include <boost/system/error_code.hpp>
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include <boost/fiber/all.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
namespace boost {
namespace fibers {
namespace asio {
namespace detail {
template< typename T >
class yield_handler
{
public:
yield_handler( yield_t const& y) :
fiber_( boost::fibers::detail::scheduler::instance().active() ),
ec_( y.ec_), value_( 0)
{}
void operator()( T t)
{
* ec_ = boost::system::error_code();
* value_ = t;
fiber_->set_ready();
boost::fibers::detail::scheduler::instance().spawn( fiber_);
}
void operator()( boost::system::error_code const& ec, T t)
{
* ec_ = ec;
* value_ = t;
fiber_->set_ready();
boost::fibers::detail::scheduler::instance().spawn( fiber_);
}
//private:
boost::fibers::detail::fiber_base::ptr_t fiber_;
boost::system::error_code * ec_;
T * value_;
};
// Completion handler to adapt a void promise as a completion handler.
template<>
class yield_handler< void >
{
public:
yield_handler( yield_t const& y) :
fiber_( boost::fibers::detail::scheduler::instance().active() ),
ec_( y.ec_)
{}
void operator()()
{
* ec_ = boost::system::error_code();
fiber_->set_ready();
boost::fibers::detail::scheduler::instance().spawn( fiber_);
}
void operator()( boost::system::error_code const& ec)
{
* ec_ = ec;
fiber_->set_ready();
boost::fibers::detail::scheduler::instance().spawn( fiber_);
}
//private:
boost::fibers::detail::fiber_base::ptr_t fiber_;
boost::system::error_code * ec_;
};
} // namespace detail
} // namespace asio
} // namespace fibers
} // namespace boost
namespace boost {
namespace asio {
template< typename T >
class async_result< boost::fibers::asio::detail::yield_handler< T > >
{
public:
typedef T type;
explicit async_result( boost::fibers::asio::detail::yield_handler< T > & h)
{
out_ec_ = h.ec_;
if ( ! out_ec_) h.ec_ = & ec_;
h.value_ = & value_;
}
type get()
{
boost::fibers::detail::scheduler::instance().active()->set_waiting();
boost::fibers::detail::scheduler::instance().active()->suspend();
if ( ! out_ec_ && ec_)
throw_exception( boost::system::system_error( ec_) );
return value_;
}
private:
boost::system::error_code * out_ec_;
boost::system::error_code ec_;
type value_;
};
template<>
class async_result< boost::fibers::asio::detail::yield_handler< void > >
{
public:
typedef void type;
explicit async_result( boost::fibers::asio::detail::yield_handler< void > & h)
{
out_ec_ = h.ec_;
if ( ! out_ec_) h.ec_ = & ec_;
}
void get()
{
boost::fibers::detail::scheduler::instance().active()->set_waiting();
boost::fibers::detail::scheduler::instance().active()->suspend();
if ( ! out_ec_ && ec_)
throw_exception( boost::system::system_error( ec_) );
}
private:
boost::system::error_code * out_ec_;
boost::system::error_code ec_;
};
// Handler type specialisation for use_future.
template< typename ReturnType >
struct handler_type<
boost::fibers::asio::yield_t,
ReturnType()
>
{ typedef boost::fibers::asio::detail::yield_handler< void > type; };
// Handler type specialisation for use_future.
template< typename ReturnType, typename Arg1 >
struct handler_type<
boost::fibers::asio::yield_t,
ReturnType( Arg1)
>
{ typedef boost::fibers::asio::detail::yield_handler< Arg1 > type; };
// Handler type specialisation for use_future.
template< typename ReturnType >
struct handler_type<
boost::fibers::asio::yield_t,
ReturnType( boost::system::error_code)
>
{ typedef boost::fibers::asio::detail::yield_handler< void > type; };
// Handler type specialisation for use_future.
template< typename ReturnType, typename Arg2 >
struct handler_type<
boost::fibers::asio::yield_t,
ReturnType( boost::system::error_code, Arg2)
>
{ typedef boost::fibers::asio::detail::yield_handler< Arg2 > type; };
} // namespace asio
} // namespace boost
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#endif // BOOST_FIBERS_ASIO_DETAIL_YIELD_HPP

View File

@@ -0,0 +1,57 @@
// Copyright Oliver Kowalke, Christopher M. Kohlhoff 2009.
// 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_FIBERS_ASIO_IO_SERVICE_HPP
#define BOOST_FIBERS_ASIO_IO_SERVICE_HPP
#include <deque>
#include <boost/asio/io_service.hpp>
#include <boost/config.hpp>
#include <boost/fiber/algorithm.hpp>
#include <boost/fiber/detail/config.hpp>
#include <boost/fiber/detail/fiber_base.hpp>
namespace boost {
namespace fibers {
namespace asio {
class BOOST_FIBERS_DECL io_service : public algorithm
{
private:
typedef std::deque< detail::fiber_base::ptr_t > wqueue_t;
typedef std::deque< boost::asio::io_service::work > wqueue_work_t;
boost::asio::io_service& io_service_;
detail::fiber_base::ptr_t active_fiber_;
wqueue_t wqueue_;
wqueue_work_t wqueue_work_;
public:
io_service( boost::asio::io_service & svc) BOOST_NOEXCEPT;
~io_service() BOOST_NOEXCEPT;
void spawn( detail::fiber_base::ptr_t const&);
void priority( detail::fiber_base::ptr_t const&, int);
void join( detail::fiber_base::ptr_t const&);
detail::fiber_base::ptr_t active() BOOST_NOEXCEPT
{ return active_fiber_; }
bool run();
void wait();
void yield();
};
}}}
#endif // BOOST_FIBERS_ASIO_IO_SERVICE_HPP

View File

@@ -13,6 +13,8 @@
#ifndef BOOST_FIBERS_ASIO_USE_FUTURE_HPP
#define BOOST_FIBERS_ASIO_USE_FUTURE_HPP
#include <memory>
#include <boost/asio/detail/config.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
@@ -45,7 +47,7 @@ public:
typedef Allocator allocator_type;
/// Construct using default-constructed allocator.
use_future_t()
BOOST_CONSTEXPR use_future_t()
{}
/// Construct using specified allocator.
@@ -70,7 +72,7 @@ private:
/**
* See the documentation for boost::asio::use_future_t for a usage example.
*/
BOOST_CONSTEXPR use_future_t<> use_future;
BOOST_CONSTEXPR_OR_CONST use_future_t<> use_future;
} // namespace asio
} // namespace fibers

View File

@@ -0,0 +1,56 @@
//
// yield.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// 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)
//
// modified by Oliver Kowalke
//
#ifndef BOOST_FIBERS_ASIO_YIELD_HPP
#define BOOST_FIBERS_ASIO_YIELD_HPP
#include <memory>
#include <boost/asio/detail/config.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
namespace boost {
namespace fibers {
namespace asio {
class yield_t
{
public:
BOOST_CONSTEXPR yield_t() :
ec_( 0)
{}
yield_t operator[]( boost::system::error_code & ec)
{
yield_t tmp;
tmp.ec_ = & ec;
return tmp;
}
//private:
boost::system::error_code * ec_;
};
BOOST_CONSTEXPR_OR_CONST yield_t yield;
}}}
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#include <boost/fiber/asio/detail/yield.hpp>
#endif // BOOST_FIBERS_ASIO_YIELD_HPP

View File

@@ -32,6 +32,8 @@ namespace boost {
namespace fibers {
namespace detail {
struct forced_unwind {};
class BOOST_FIBERS_DECL fiber_base : public notify
{
public:

View File

@@ -30,8 +30,6 @@ namespace boost {
namespace fibers {
namespace detail {
struct forced_unwind {};
template< typename Fiber >
void trampoline( intptr_t vp)
{

View File

@@ -6,19 +6,13 @@
#ifndef BOOST_FIBERS_DEFAULT_SCHEDULER_H
#define BOOST_FIBERS_DEFAULT_SCHEDULER_H
#include <cstddef>
#include <deque>
#include <set>
#include <vector>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/move/move.hpp>
#include <boost/thread/locks.hpp>
#include <boost/fiber/algorithm.hpp>
#include <boost/fiber/detail/config.hpp>
#include <boost/fiber/detail/fiber_base.hpp>
#include <boost/fiber/algorithm.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX