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;
|
|
|
|
int64_t notes; // norm: 0 = asleep; 1 = awake; inc by others; dec by self
|
|
|
|
ctx *adjoindre;
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
bool started() const;
|
|
|
|
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-07 23:39:41 +02:00
|
|
|
ctx(const size_t &stack_max = DEFAULT_STACK_SIZE,
|
2016-09-05 17:53:36 +02:00
|
|
|
boost::asio::io_service *const &ios = ircd::ios);
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t stack_usage_here(const ctx &) __attribute__((noinline));
|
|
|
|
|
|
|
|
inline
|
|
|
|
continuation::continuation(ctx *const &self)
|
|
|
|
:self{self}
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
strand.dispatch([this]
|
|
|
|
{
|
|
|
|
alarm.cancel();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
// notes = 1; set by continuation dtor on wakeup
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ctx::started()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return bool(yc);
|
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
|