2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-09-09 20:07:17 +02:00
|
|
|
|
|
|
|
#pragma once
|
2016-11-29 16:23:38 +01:00
|
|
|
#define HAVE_IRCD_CTX_CONTINUATION_H
|
|
|
|
|
2017-12-12 21:12:14 +01:00
|
|
|
// This file is not included with the IRCd standard include stack because
|
|
|
|
// it requires symbols we can't forward declare without boost headers. It
|
|
|
|
// is part of the <ircd/asio.h> stack which can be included in your
|
|
|
|
// definition file if necessary.
|
2016-09-09 20:07:17 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
2017-09-20 04:01:37 +02:00
|
|
|
using yield_context = boost::asio::yield_context;
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct continuation;
|
2017-09-21 04:31:54 +02:00
|
|
|
struct to_asio;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
2017-09-20 04:01:37 +02:00
|
|
|
using ctx::yield_context;
|
2017-08-28 23:51:22 +02:00
|
|
|
using ctx::continuation;
|
2017-09-21 04:31:54 +02:00
|
|
|
using ctx::to_asio;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-09-09 20:07:17 +02:00
|
|
|
|
2017-09-21 04:31:54 +02:00
|
|
|
/// This object must be placed on the stack when the context is yielding (INTERNAL)
|
2017-09-09 16:30:45 +02:00
|
|
|
///
|
2017-09-21 04:31:54 +02:00
|
|
|
/// The continuation constructor is the last thing executed before a context
|
|
|
|
/// yields. The continuation destructor is the first thing executed when a
|
|
|
|
/// context continues. This is not placed by a normal user wishing to context
|
|
|
|
/// switch, only a low-level library creator actually implementing the context
|
|
|
|
/// switch itself. The placement of this object must be correct. Generally,
|
|
|
|
/// we construct the `continuation` as an argument to `yield_context` as such
|
|
|
|
/// `yield_context{continuation{}}`. This ensures the continuation destructor
|
|
|
|
/// executes before control continues beyond the yield_context call itself and
|
|
|
|
/// ties this sequence together neatly.
|
2017-09-09 16:30:45 +02:00
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::ctx::continuation
|
2016-09-09 20:07:17 +02:00
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
ctx *self;
|
2016-09-09 20:07:17 +02:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
operator const boost::asio::yield_context &() const;
|
|
|
|
operator boost::asio::yield_context &();
|
2016-09-13 00:07:35 +02:00
|
|
|
|
2018-03-23 23:35:09 +01:00
|
|
|
virtual void interrupted(ctx *const &) noexcept;
|
|
|
|
|
2018-05-26 08:50:20 +02:00
|
|
|
continuation();
|
2018-03-23 23:35:09 +01:00
|
|
|
virtual ~continuation() noexcept;
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
2017-09-21 04:31:54 +02:00
|
|
|
|
|
|
|
/// This type of continuation should be used when yielding a context to a
|
|
|
|
/// boost::asio handler so we can have specific control over that type of
|
|
|
|
/// context switch in possible contrast or extension of the regular
|
|
|
|
/// continuation behavior.
|
|
|
|
///
|
|
|
|
/// The statement `yield_context{to_asio{}}` can be passed to any boost::asio
|
|
|
|
/// callback handler. Those handlers have overloads to accept this, many of
|
|
|
|
/// which are not documented.
|
|
|
|
///
|
|
|
|
struct ircd::ctx::to_asio
|
|
|
|
:ircd::ctx::continuation
|
|
|
|
{
|
2018-03-23 23:35:09 +01:00
|
|
|
using function = std::function<void (ctx *const &) noexcept>;
|
|
|
|
|
|
|
|
function handler;
|
|
|
|
|
|
|
|
void interrupted(ctx *const &) noexcept final override;
|
|
|
|
|
|
|
|
to_asio(const function &handler = {});
|
2017-09-21 04:31:54 +02:00
|
|
|
};
|
2018-03-23 23:35:09 +01:00
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::ctx::to_asio::to_asio(const function &handler)
|
2018-05-26 08:50:20 +02:00
|
|
|
:handler{handler}
|
2018-03-23 23:35:09 +01:00
|
|
|
{}
|