2016-09-05 17:53:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-09-07 02:16:44 +02:00
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_CTX_H
|
|
|
|
|
2016-09-05 17:53:36 +02:00
|
|
|
#include <boost/asio/io_service.hpp>
|
|
|
|
#include <boost/asio/steady_timer.hpp>
|
|
|
|
#include <boost/asio/spawn.hpp>
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace ctx {
|
|
|
|
|
|
|
|
struct ctx
|
|
|
|
{
|
2016-09-07 23:39:41 +02:00
|
|
|
boost::asio::steady_timer alarm; // 64B
|
2016-09-05 17:53:36 +02:00
|
|
|
boost::asio::yield_context *yc;
|
2016-09-07 23:39:41 +02:00
|
|
|
uintptr_t stack_base;
|
|
|
|
size_t stack_max;
|
2016-09-10 07:23:07 +02:00
|
|
|
int64_t notes; // norm: 0 = asleep; 1 = awake; inc by others; dec by self
|
2016-09-07 23:39:41 +02:00
|
|
|
ctx *adjoindre;
|
2016-09-10 07:23:07 +02:00
|
|
|
enum flags flags;
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2016-09-10 07:23:07 +02:00
|
|
|
bool finished() const { return yc == nullptr; }
|
|
|
|
bool started() const { return bool(yc); }
|
2016-09-07 23:39:41 +02:00
|
|
|
bool wait();
|
|
|
|
void wake();
|
|
|
|
bool note();
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
void operator()(boost::asio::yield_context, const std::function<void ()>) noexcept;
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2016-09-10 07:23:07 +02:00
|
|
|
ctx(const size_t &stack_max = DEFAULT_STACK_SIZE,
|
|
|
|
const enum flags &flags = (enum flags)0,
|
|
|
|
boost::asio::io_service *const &ios = ircd::ios);
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
ctx(ctx &&) noexcept = delete;
|
2016-09-05 17:53:36 +02:00
|
|
|
ctx(const ctx &) = delete;
|
|
|
|
};
|
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
struct continuation
|
|
|
|
{
|
|
|
|
ctx *self;
|
|
|
|
|
|
|
|
operator const boost::asio::yield_context &() const;
|
|
|
|
operator boost::asio::yield_context &();
|
|
|
|
|
|
|
|
continuation(ctx *const &self = ircd::ctx::current);
|
|
|
|
~continuation() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
continuation::continuation(ctx *const &self)
|
|
|
|
:self{self}
|
|
|
|
{
|
2016-09-26 00:46:54 +02:00
|
|
|
mark(prof::event::CUR_YIELD);
|
2016-09-07 23:39:41 +02:00
|
|
|
assert(self != nullptr);
|
|
|
|
assert(self->notes <= 1);
|
|
|
|
ircd::ctx::current = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:53:36 +02:00
|
|
|
inline
|
2016-09-07 23:39:41 +02:00
|
|
|
continuation::~continuation()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
ircd::ctx::current = self;
|
2016-09-26 00:46:54 +02:00
|
|
|
mark(prof::event::CUR_CONTINUE);
|
2016-09-07 23:39:41 +02:00
|
|
|
self->notes = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
continuation::operator boost::asio::yield_context &()
|
|
|
|
{
|
|
|
|
return *self->yc;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
continuation::operator const boost::asio::yield_context &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return *self->yc;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ctx::note()
|
|
|
|
{
|
|
|
|
if(notes++ > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wake();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ctx::wake()
|
|
|
|
try
|
|
|
|
{
|
2016-09-10 07:23:07 +02:00
|
|
|
alarm.cancel();
|
2016-09-07 23:39:41 +02:00
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
2016-09-05 17:53:36 +02:00
|
|
|
{
|
2016-09-07 23:39:41 +02:00
|
|
|
ircd::log::error("ctx::wake(%p): %s", this, e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ctx::wait()
|
|
|
|
{
|
|
|
|
if(--notes > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
boost::system::error_code ec;
|
|
|
|
alarm.async_wait(boost::asio::yield_context(continuation(this))[ec]);
|
|
|
|
|
|
|
|
assert(ec == boost::system::errc::operation_canceled ||
|
|
|
|
ec == boost::system::errc::success);
|
|
|
|
|
2016-09-10 07:23:07 +02:00
|
|
|
// Interruption shouldn't be used for normal operation,
|
|
|
|
// so please eat this branch misprediction.
|
|
|
|
if(unlikely(flags & INTERRUPTED))
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
mark(prof::event::CUR_INTERRUPT);
|
2016-11-11 22:15:09 +01:00
|
|
|
flags &= ~INTERRUPTED;
|
2016-09-10 07:23:07 +02:00
|
|
|
throw interrupted("ctx(%p)::wait()", (const void *)this);
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
2016-09-10 07:23:07 +02:00
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
// notes = 1; set by continuation dtor on wakeup
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:53:36 +02:00
|
|
|
} // namespace ctx
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
using ctx::continuation;
|
|
|
|
using yield = boost::asio::yield_context;
|
|
|
|
|
2016-09-05 17:53:36 +02:00
|
|
|
} // namespace ircd
|