0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00
construct/ircd/ctx.cc

235 lines
4.9 KiB
C++
Raw Normal View History

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.
*/
#include <ircd/ctx_ctx.h>
using namespace ircd;
2016-09-10 07:23:07 +02:00
///////////////////////////////////////////////////////////////////////////////
//
// ctx.h
//
2016-09-05 17:53:36 +02:00
__thread ctx::ctx *ctx::current;
void
ctx::sleep_until(const std::chrono::steady_clock::time_point &tp)
{
while(!wait_until(tp, std::nothrow));
}
2016-09-10 07:23:07 +02:00
bool
ctx::wait_until(const std::chrono::steady_clock::time_point &tp,
const std::nothrow_t &)
2016-09-05 17:53:36 +02:00
{
2016-09-10 07:23:07 +02:00
auto &c(cur());
c.alarm.expires_at(tp);
c.wait(); // now you're yielding with portals
return std::chrono::steady_clock::now() >= tp;
}
std::chrono::microseconds
2016-09-10 07:23:07 +02:00
ctx::wait(const std::chrono::microseconds &duration,
const std::nothrow_t &)
{
auto &c(cur());
c.alarm.expires_from_now(duration);
c.wait(); // now you're yielding with portals
2016-09-10 07:23:07 +02:00
const auto ret(c.alarm.expires_from_now());
2016-09-05 17:53:36 +02:00
// return remaining duration.
// this is > 0 if notified or interrupted
// this is unchanged if a note prevented any wait at all
return std::chrono::duration_cast<std::chrono::microseconds>(ret);
2016-09-05 17:53:36 +02:00
}
void
ctx::wait()
{
auto &c(cur());
c.alarm.expires_at(std::chrono::steady_clock::time_point::max());
c.wait(); // now you're yielding with portals
}
ircd::ctx::context::context(const size_t &stack_sz,
std::function<void ()> func,
2016-09-10 07:23:07 +02:00
const enum flags &flags)
:c{std::make_unique<ctx>(stack_sz, flags, ircd::ios)}
{
auto spawn([stack_sz, c(c.get()), func(std::move(func))]
{
auto bound(std::bind(&ctx::operator(), c, ph::_1, std::move(func)));
const boost::coroutines::attributes attrs
{
stack_sz,
boost::coroutines::stack_unwind
};
boost::asio::spawn(*ios, std::move(bound), attrs);
});
// The current context must be reasserted if spawn returns here
const scope recurrent([current(ircd::ctx::current)]
{
ircd::ctx::current = current;
});
if(flags & DEFER_POST)
ios->post(std::move(spawn));
else if(flags & DEFER_DISPATCH)
ios->dispatch(std::move(spawn));
else
spawn();
2016-09-10 07:23:07 +02:00
if(flags & SELF_DESTRUCT)
c.release();
}
ircd::ctx::context::context(std::function<void ()> func,
2016-09-10 07:23:07 +02:00
const enum flags &flags)
2016-09-05 17:53:36 +02:00
:context
{
DEFAULT_STACK_SIZE,
std::move(func),
flags
2016-09-05 17:53:36 +02:00
}
{
}
ircd::ctx::context::~context()
noexcept
{
if(!c)
return;
// Can't join to bare metal, only from within another context.
if(!current)
return;
2016-09-10 07:23:07 +02:00
interrupt();
2016-09-05 17:53:36 +02:00
join();
}
void
ircd::ctx::context::join()
{
if(joined())
return;
2016-09-05 17:53:36 +02:00
2016-09-10 07:23:07 +02:00
// Set the target context to notify this context when it finishes
assert(!c->adjoindre);
c->adjoindre = &cur();
wait();
2016-09-05 17:53:36 +02:00
}
ircd::ctx::ctx *
ircd::ctx::context::detach()
{
return c.release();
}
bool
ircd::ctx::notify(ctx &ctx)
{
return ctx.note();
}
2016-09-10 07:23:07 +02:00
void
ircd::ctx::interrupt(ctx &ctx)
{
ctx.flags |= INTERRUPTED;
ctx.wake();
}
bool
ircd::ctx::started(const ctx &ctx)
{
return ctx.started();
}
2016-09-10 07:23:07 +02:00
bool
ircd::ctx::finished(const ctx &ctx)
{
return ctx.finished();
}
const enum ctx::flags &
ircd::ctx::flags(const ctx &ctx)
{
return ctx.flags;
}
const int64_t &
ircd::ctx::notes(const ctx &ctx)
{
return ctx.notes;
}
2016-09-10 07:23:07 +02:00
///////////////////////////////////////////////////////////////////////////////
//
// ctx_ctx.h
//
2016-09-05 17:53:36 +02:00
ctx::ctx::ctx(const size_t &stack_max,
2016-09-10 07:23:07 +02:00
const enum flags &flags,
2016-09-05 17:53:36 +02:00
boost::asio::io_service *const &ios)
:alarm{*ios}
2016-09-05 17:53:36 +02:00
,yc{nullptr}
,stack_base{0}
,stack_max{stack_max}
,notes{1}
,adjoindre{nullptr}
2016-09-10 07:23:07 +02:00
,flags{flags}
2016-09-05 17:53:36 +02:00
{
}
void
ctx::ctx::operator()(boost::asio::yield_context yc,
const std::function<void ()> func)
noexcept
2016-09-05 17:53:36 +02:00
{
this->yc = &yc;
notes = 1;
stack_base = uintptr_t(__builtin_frame_address(0));
2016-09-05 17:53:36 +02:00
ircd::ctx::current = this;
2016-09-10 07:23:07 +02:00
const scope atexit([this]
{
ircd::ctx::current = nullptr;
this->yc = nullptr;
if(adjoindre)
notify(*adjoindre);
if(flags & SELF_DESTRUCT)
delete this;
});
2016-09-05 17:53:36 +02:00
if(likely(bool(func)))
func();
}
size_t
ctx::stack_usage_here(const ctx &ctx)
{
return ctx.stack_base - uintptr_t(__builtin_frame_address(0));
}