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-05 17:53:36 +02:00
|
|
|
|
2018-02-05 19:47:46 +01:00
|
|
|
#include <RB_INC_X86INTRIN_H
|
2018-03-25 01:41:16 +01:00
|
|
|
#include <cxxabi.h>
|
2017-09-21 04:31:54 +02:00
|
|
|
#include <ircd/asio.h>
|
2018-05-06 06:20:25 +02:00
|
|
|
#include "ctx.h"
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-04-20 00:37:58 +02:00
|
|
|
/// Instance list linkage for the list of all ctx instances.
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::ctx::ctx>::list)
|
|
|
|
ircd::util::instance_list<ircd::ctx::ctx>::list
|
|
|
|
{};
|
|
|
|
|
|
|
|
/// Public interface linkage for the list of all ctx instances
|
|
|
|
decltype(ircd::ctx::ctxs)
|
|
|
|
ircd::ctx::ctxs
|
|
|
|
{
|
|
|
|
ctx::ctx::list
|
|
|
|
};
|
|
|
|
|
2017-12-12 21:12:14 +01:00
|
|
|
/// Monotonic ctx id counter state. This counter is incremented for each
|
|
|
|
/// newly created context.
|
2016-11-29 16:23:38 +01:00
|
|
|
decltype(ircd::ctx::ctx::id_ctr)
|
|
|
|
ircd::ctx::ctx::id_ctr
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2018-08-21 17:10:37 +02:00
|
|
|
/// Spawn (internal)
|
|
|
|
void
|
|
|
|
ircd::ctx::spawn(ctx *const c,
|
|
|
|
context::function func)
|
|
|
|
{
|
|
|
|
const boost::coroutines::attributes attrs
|
|
|
|
{
|
|
|
|
c->stack.max,
|
|
|
|
boost::coroutines::stack_unwind
|
|
|
|
};
|
|
|
|
|
|
|
|
auto bound
|
|
|
|
{
|
|
|
|
std::bind(&ctx::operator(), c, ph::_1, std::move(func))
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::asio::spawn(c->strand, std::move(bound), attrs);
|
|
|
|
}
|
|
|
|
|
2018-08-20 03:09:04 +02:00
|
|
|
// linkage for dtor
|
|
|
|
ircd::ctx::ctx::~ctx()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Base frame for a context.
|
|
|
|
///
|
|
|
|
/// This function is the first thing executed on the new context's stack
|
|
|
|
/// and calls the user's function.
|
2016-11-29 16:23:38 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::ctx::operator()(boost::asio::yield_context yc,
|
|
|
|
const std::function<void ()> func)
|
2018-03-17 04:25:56 +01:00
|
|
|
noexcept try
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
this->yc = &yc;
|
|
|
|
notes = 1;
|
2018-05-23 03:03:18 +02:00
|
|
|
stack.base = uintptr_t(__builtin_frame_address(0));
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::current = this;
|
|
|
|
mark(prof::event::CUR_ENTER);
|
|
|
|
|
2017-09-22 22:57:43 +02:00
|
|
|
const unwind atexit([this]
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-24 23:45:35 +01:00
|
|
|
mark(prof::event::CUR_LEAVE);
|
2018-05-23 03:06:18 +02:00
|
|
|
adjoindre.notify_all();
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::current = nullptr;
|
|
|
|
this->yc = nullptr;
|
|
|
|
|
|
|
|
if(flags & context::DETACH)
|
|
|
|
delete this;
|
|
|
|
});
|
|
|
|
|
2017-03-24 23:46:41 +01:00
|
|
|
// Check for a precocious interrupt
|
2018-05-06 10:00:11 +02:00
|
|
|
if(unlikely(flags & (context::INTERRUPTED | context::TERMINATED)))
|
2017-03-24 23:46:41 +01:00
|
|
|
return;
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
if(likely(bool(func)))
|
|
|
|
func();
|
|
|
|
}
|
2018-05-06 10:15:26 +02:00
|
|
|
catch(const ircd::ctx::interrupted &)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch(const ircd::ctx::terminated &)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-03-17 04:25:56 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2018-05-06 10:15:26 +02:00
|
|
|
"ctx('%s' #%u): unhandled: %s",
|
2018-03-17 04:25:56 +01:00
|
|
|
name,
|
|
|
|
id,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Preserving the stacktrace from the throw point here is hopeless.
|
|
|
|
// We can terminate for developer nuisance but we will never know
|
|
|
|
// where this exception came from and where it is going. Bottom line
|
|
|
|
// is that #ifdef'ing away this handler or rethrowing isn't as useful as
|
|
|
|
// handling the exception here with a log message and calling it a day.
|
|
|
|
return;
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-12-07 21:03:25 +01:00
|
|
|
/// Direct context switch to this context.
|
|
|
|
///
|
|
|
|
/// This currently doesn't work yet because the suspension state of this
|
|
|
|
/// context has to be ready to be jumped to and that isn't implemented yet.
|
|
|
|
void
|
|
|
|
ircd::ctx::ctx::jump()
|
|
|
|
{
|
|
|
|
assert(this->yc);
|
|
|
|
assert(current != this); // can't jump to self
|
|
|
|
|
|
|
|
auto &yc(*this->yc);
|
|
|
|
auto &target(*yc.coro_.lock());
|
|
|
|
|
|
|
|
// Jump from the currently running context (source) to *this (target)
|
|
|
|
// with continuation of source after target
|
|
|
|
{
|
|
|
|
current->notes = 0; // Unconditionally cleared here
|
|
|
|
const continuation continuation;
|
|
|
|
target();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(current != this);
|
|
|
|
assert(current->notes == 1); // notes = 1; set by continuation dtor on wakeup
|
|
|
|
|
|
|
|
interruption_point();
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Yield (suspend) this context until notified.
|
|
|
|
///
|
|
|
|
/// This context must be currently running otherwise bad things. Returns false
|
|
|
|
/// if the context was notified before actually suspending; the note is then
|
|
|
|
/// considered handled an another attempt to `wait()` can be made. Returns true
|
|
|
|
/// if the context suspended and was notified. When a context wakes up the
|
|
|
|
/// note counter is reset.
|
2017-03-27 23:56:05 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::wait()
|
|
|
|
{
|
|
|
|
namespace errc = boost::system::errc;
|
|
|
|
|
|
|
|
assert(this->yc);
|
|
|
|
assert(current == this);
|
|
|
|
|
|
|
|
if(--notes > 0)
|
|
|
|
return false;
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
// An interrupt invokes this closure to force the alarm to return.
|
|
|
|
const interruptor interruptor{[this]
|
2018-03-23 23:35:09 +01:00
|
|
|
(ctx *const &interruptor) noexcept
|
|
|
|
{
|
|
|
|
wake();
|
|
|
|
}};
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
// This is currently a dummy predicate; this is where we can take the
|
|
|
|
// user's real wakeup condition (i.e from a ctx::dock) and use it with
|
|
|
|
// an internal scheduler.
|
|
|
|
const predicate &predicate{[this]
|
|
|
|
{
|
|
|
|
return notes > 0;
|
|
|
|
}};
|
|
|
|
|
|
|
|
// The register switch itself occurs inside the alarm.async_wait() call.
|
|
|
|
// The construction of the arguments to the call on this stack comprise
|
|
|
|
// our final control before the context switch. The destruction of the
|
|
|
|
// arguments comprise the initial control after the context switch.
|
2017-03-27 23:56:05 +02:00
|
|
|
boost::system::error_code ec;
|
2018-12-07 21:05:27 +01:00
|
|
|
alarm.async_wait(yield_context{continuation{predicate, interruptor}}[ec]);
|
2017-03-27 23:56:05 +02:00
|
|
|
|
|
|
|
assert(ec == errc::operation_canceled || ec == errc::success);
|
|
|
|
assert(current == this);
|
|
|
|
assert(notes == 1); // notes = 1; set by continuation dtor on wakeup
|
|
|
|
|
|
|
|
interruption_point();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Notifies this context to resume (wake up from waiting).
|
|
|
|
///
|
|
|
|
/// Returns true if this note was the first note received by this context
|
|
|
|
/// while it's been suspended or false if it's already been notified.
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::note()
|
|
|
|
{
|
|
|
|
if(notes++ > 0)
|
|
|
|
return false;
|
|
|
|
|
2018-12-07 21:03:25 +01:00
|
|
|
if(this == current)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return wake();
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Wakes a context without a note (internal)
|
2018-12-07 21:03:25 +01:00
|
|
|
bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ctx::wake()
|
|
|
|
try
|
|
|
|
{
|
|
|
|
alarm.cancel();
|
2018-12-07 21:03:25 +01:00
|
|
|
return true;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
2018-03-23 23:35:09 +01:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"ctx::wake(%p): %s", this, e.what()
|
|
|
|
};
|
2018-12-07 21:03:25 +01:00
|
|
|
|
|
|
|
return false;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Throws if this context has been flagged for interruption and clears
|
|
|
|
/// the flag.
|
2017-03-27 23:56:05 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::ctx::interruption_point()
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-05-06 10:00:11 +02:00
|
|
|
static const auto &flags
|
|
|
|
{
|
|
|
|
context::TERMINATED | context::INTERRUPTED
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely((this->flags & flags) == 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(unlikely(termination_point(std::nothrow)))
|
|
|
|
throw terminated{};
|
|
|
|
|
2017-03-27 23:56:05 +02:00
|
|
|
if(unlikely(interruption_point(std::nothrow)))
|
2018-05-06 10:00:11 +02:00
|
|
|
throw interrupted
|
|
|
|
{
|
|
|
|
"ctx(%p) '%s'", (const void *)this, name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if this context has been flagged for termination.
|
|
|
|
/// Does not clear the flag.
|
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::termination_point(std::nothrow_t)
|
|
|
|
{
|
|
|
|
if(unlikely(flags & context::TERMINATED))
|
|
|
|
{
|
2018-05-22 09:24:48 +02:00
|
|
|
// see: interruption_point().
|
|
|
|
if(flags & context::NOINTERRUPT)
|
|
|
|
return false;
|
|
|
|
|
2018-05-06 10:00:11 +02:00
|
|
|
mark(prof::event::CUR_TERMINATE);
|
2018-12-07 21:45:05 +01:00
|
|
|
assert(flags & ~context::NOINTERRUPT);
|
2018-05-06 10:00:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2017-03-27 23:56:05 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Returns true if this context has been flagged for interruption and
|
|
|
|
/// clears the flag.
|
2017-03-27 23:56:05 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::interruption_point(std::nothrow_t)
|
|
|
|
{
|
2016-11-29 16:23:38 +01:00
|
|
|
// Interruption shouldn't be used for normal operation,
|
|
|
|
// so please eat this branch misprediction.
|
|
|
|
if(unlikely(flags & context::INTERRUPTED))
|
|
|
|
{
|
2018-05-22 09:00:26 +02:00
|
|
|
// The NOINTERRUPT flag works by pretending there is no INTERRUPTED
|
|
|
|
// flag set and also does not clear the flag. This allows the interrupt
|
|
|
|
// to remaing pending until the uninterruptible section is complete.
|
|
|
|
if(flags & context::NOINTERRUPT)
|
|
|
|
return false;
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
flags &= ~context::INTERRUPTED;
|
2018-12-07 21:45:05 +01:00
|
|
|
mark(prof::event::CUR_INTERRUPT);
|
|
|
|
assert(~flags & context::NOINTERRUPT);
|
2017-03-27 23:56:05 +02:00
|
|
|
return true;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2017-03-27 23:56:05 +02:00
|
|
|
else return false;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2018-08-20 01:21:33 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::started()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return stack.base != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::finished()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return started() && yc == nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:23:07 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2016-11-29 16:23:38 +01:00
|
|
|
// ctx/ctx.h
|
2016-09-10 07:23:07 +02:00
|
|
|
//
|
2017-12-12 21:12:14 +01:00
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Yield to context `ctx`.
|
|
|
|
///
|
|
|
|
///
|
2017-03-27 23:56:05 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::yield(ctx &ctx)
|
|
|
|
{
|
|
|
|
assert(current);
|
2017-08-19 00:54:34 +02:00
|
|
|
|
|
|
|
//ctx.jump();
|
|
|
|
// !!! TODO !!!
|
|
|
|
// XXX: We can't jump directly to a context if it's waiting on its alarm, and
|
|
|
|
// we don't know whether it's waiting on its alarm. We can add another flag to
|
|
|
|
// inform us of that, but most contexts are usually waiting on their alarm anyway.
|
|
|
|
//
|
|
|
|
// Perhaps a better way to do this would be to centralize the alarms into a single
|
|
|
|
// context with the sole job of waiting on a single alarm. Then it can schedule
|
|
|
|
// things allowing for more direct jumps until all work is complete.
|
|
|
|
// !!! TODO !!!
|
|
|
|
|
|
|
|
notify(ctx);
|
2017-03-27 23:56:05 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Notifies `ctx` to wake up from another std::thread
|
2017-04-03 05:52:30 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::notify(ctx &ctx,
|
|
|
|
threadsafe_t)
|
|
|
|
{
|
2017-09-20 04:01:37 +02:00
|
|
|
signal(ctx, [&ctx]
|
2017-04-03 05:52:30 +02:00
|
|
|
{
|
|
|
|
notify(ctx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Notifies `ctx` to wake up. This will enqueue the resumption, not jump
|
|
|
|
/// directly to `ctx`.
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::notify(ctx &ctx)
|
|
|
|
{
|
|
|
|
return ctx.note();
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Executes `func` sometime between executions of `ctx` with thread-safety
|
|
|
|
/// so `func` and `ctx` are never executed concurrently no matter how many
|
|
|
|
/// threads the io_service has available to execute events on.
|
2017-04-03 05:52:30 +02:00
|
|
|
void
|
2017-09-20 04:01:37 +02:00
|
|
|
ircd::ctx::signal(ctx &ctx,
|
2017-04-03 05:52:30 +02:00
|
|
|
std::function<void ()> func)
|
|
|
|
{
|
|
|
|
ctx.strand.post(std::move(func));
|
|
|
|
}
|
|
|
|
|
2018-05-06 10:00:11 +02:00
|
|
|
/// Marks `ctx` for termination. Terminate is similar to interrupt() but the
|
|
|
|
/// exception thrown is ctx::terminate which does not participate in the
|
|
|
|
/// std::exception hierarchy. Project code is unlikely to catch this.
|
|
|
|
void
|
|
|
|
ircd::ctx::terminate(ctx &ctx)
|
|
|
|
{
|
|
|
|
if(finished(ctx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(termination(ctx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx.flags |= context::TERMINATED;
|
2018-12-07 04:07:36 +01:00
|
|
|
if(!interruptible(ctx))
|
|
|
|
return;
|
|
|
|
|
2018-05-06 10:00:11 +02:00
|
|
|
if(likely(&ctx != current && ctx.cont != nullptr))
|
2018-12-07 21:05:27 +01:00
|
|
|
ctx.cont->intr(current);
|
2018-05-06 10:00:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Marks `ctx` for interruption and enqueues it for resumption to receive the
|
|
|
|
/// interrupt which will be an exception coming out of the point where the
|
|
|
|
/// `ctx` was yielding.
|
2016-11-29 16:23:38 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::interrupt(ctx &ctx)
|
|
|
|
{
|
2018-05-06 10:00:11 +02:00
|
|
|
if(unlikely(ircd::runlevel == runlevel::QUIT))
|
|
|
|
return terminate(ctx);
|
|
|
|
|
2018-08-18 06:31:54 +02:00
|
|
|
if(finished(ctx))
|
|
|
|
return;
|
|
|
|
|
2018-03-23 23:35:09 +01:00
|
|
|
if(interruption(ctx))
|
|
|
|
return;
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ctx.flags |= context::INTERRUPTED;
|
2018-12-07 04:07:36 +01:00
|
|
|
if(!interruptible(ctx))
|
|
|
|
return;
|
|
|
|
|
2018-03-23 23:35:09 +01:00
|
|
|
if(likely(&ctx != current && ctx.cont != nullptr))
|
2018-12-07 21:05:27 +01:00
|
|
|
ctx.cont->intr(current);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 09:00:26 +02:00
|
|
|
/// Marks `ctx` for whether to allow or suppress interruption. Suppression
|
|
|
|
/// does not ignore an interrupt itself, it only ignores the interruption
|
|
|
|
/// points. Thus when a suppression ends if the interrupt flag was ever set
|
|
|
|
/// the next interruption point will throw as expected.
|
|
|
|
void
|
|
|
|
ircd::ctx::interruptible(ctx &ctx,
|
|
|
|
const bool &b)
|
|
|
|
{
|
|
|
|
if(b)
|
|
|
|
ctx.flags &= ~context::NOINTERRUPT;
|
|
|
|
else
|
|
|
|
ctx.flags |= context::NOINTERRUPT;
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:48:14 +02:00
|
|
|
/// started() && !finished() && !running
|
|
|
|
bool
|
|
|
|
ircd::ctx::waiting(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return started(ctx) && !finished(ctx) && !running(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if `ctx` is the current ctx
|
|
|
|
bool
|
|
|
|
ircd::ctx::running(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return &ctx == current;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Indicates if `ctx` was ever jumped to
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::started(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return ctx.started();
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Indicates if the base frame for `ctx` returned
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::finished(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return ctx.finished();
|
|
|
|
}
|
|
|
|
|
2018-05-06 10:00:11 +02:00
|
|
|
/// Indicates if `ctx` was terminated; does not clear the flag
|
|
|
|
bool
|
|
|
|
ircd::ctx::termination(const ctx &c)
|
2018-11-11 23:13:37 +01:00
|
|
|
noexcept
|
2018-05-06 10:00:11 +02:00
|
|
|
{
|
|
|
|
return c.flags & context::TERMINATED;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Indicates if `ctx` was interrupted; does not clear the flag
|
2017-09-20 04:01:37 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::interruption(const ctx &c)
|
2018-11-11 23:13:37 +01:00
|
|
|
noexcept
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
|
|
|
return c.flags & context::INTERRUPTED;
|
|
|
|
}
|
|
|
|
|
2018-05-22 09:00:26 +02:00
|
|
|
/// Indicates if `ctx` will suppress any interrupts.
|
|
|
|
bool
|
|
|
|
ircd::ctx::interruptible(const ctx &c)
|
2018-11-11 23:13:37 +01:00
|
|
|
noexcept
|
2018-05-22 09:00:26 +02:00
|
|
|
{
|
2018-12-07 21:45:05 +01:00
|
|
|
return ~c.flags & context::NOINTERRUPT;
|
2018-05-22 09:00:26 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:37:44 +02:00
|
|
|
/// Returns the cycle count for `ctx`
|
2018-05-06 07:09:20 +02:00
|
|
|
const ulong &
|
|
|
|
ircd::ctx::cycles(const ctx &ctx)
|
|
|
|
{
|
2018-05-23 03:03:18 +02:00
|
|
|
return ctx.profile.cycles;
|
2018-05-06 07:09:20 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:36:33 +02:00
|
|
|
/// Returns the yield count for `ctx`
|
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::yields(const ctx &ctx)
|
|
|
|
{
|
2018-05-23 03:03:18 +02:00
|
|
|
return ctx.profile.yields;
|
2018-05-07 21:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 07:09:20 +02:00
|
|
|
/// Returns the notification count for `ctx`
|
2018-12-07 19:13:29 +01:00
|
|
|
const int32_t &
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::notes(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return ctx.notes;
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:58:25 +02:00
|
|
|
/// Returns the notification count for `ctx`
|
|
|
|
const size_t &
|
|
|
|
ircd::ctx::stack_at(const ctx &ctx)
|
|
|
|
{
|
2018-05-23 03:03:18 +02:00
|
|
|
return ctx.stack.at;
|
2018-05-06 07:58:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 07:09:20 +02:00
|
|
|
/// Returns the notification count for `ctx`
|
2018-04-20 01:07:46 +02:00
|
|
|
const size_t &
|
|
|
|
ircd::ctx::stack_max(const ctx &ctx)
|
|
|
|
{
|
2018-05-23 03:03:18 +02:00
|
|
|
return ctx.stack.max;
|
2018-04-20 01:07:46 +02:00
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Returns the developer's optional name literal for `ctx`
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::ctx::name(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return ctx.name;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Returns a reference to unique ID for `ctx` (which will go away with `ctx`)
|
2016-11-29 16:23:38 +01:00
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::id(const ctx &ctx)
|
|
|
|
{
|
|
|
|
return ctx.id;
|
|
|
|
}
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/this_ctx.h
|
|
|
|
//
|
|
|
|
|
2018-03-27 01:20:47 +02:00
|
|
|
// set by the continuation object and the base frame.
|
2018-03-25 08:50:39 +02:00
|
|
|
__thread ircd::ctx::ctx *
|
2018-03-27 01:20:47 +02:00
|
|
|
ircd::ctx::current;
|
2018-03-25 08:50:39 +02:00
|
|
|
|
|
|
|
/// Yield the currently running context until `time_point` ignoring notes
|
|
|
|
void
|
2018-12-07 20:01:16 +01:00
|
|
|
ircd::ctx::this_ctx::sleep_until(const steady_clock::time_point &tp)
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
while(!wait_until(tp, std::nothrow));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Yield the currently running context until notified or `time_point`.
|
|
|
|
///
|
|
|
|
/// Returns true if this function returned because `time_point` was hit or
|
|
|
|
/// false because this context was notified.
|
|
|
|
bool
|
2018-12-07 20:01:16 +01:00
|
|
|
ircd::ctx::this_ctx::wait_until(const steady_clock::time_point &tp,
|
2018-03-25 08:50:39 +02:00
|
|
|
const std::nothrow_t &)
|
|
|
|
{
|
|
|
|
auto &c(cur());
|
|
|
|
c.alarm.expires_at(tp);
|
|
|
|
c.wait(); // now you're yielding with portals
|
|
|
|
|
2018-12-07 20:01:16 +01:00
|
|
|
return steady_clock::now() >= tp;
|
2018-03-25 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Yield the currently running context for `duration` or until notified.
|
|
|
|
///
|
|
|
|
/// Returns the duration remaining if notified, or <= 0 if suspended for
|
|
|
|
/// the full duration, or unchanged if no suspend ever took place.
|
2018-12-07 20:01:16 +01:00
|
|
|
ircd::microseconds
|
|
|
|
ircd::ctx::this_ctx::wait(const microseconds &duration,
|
2018-03-25 08:50:39 +02:00
|
|
|
const std::nothrow_t &)
|
|
|
|
{
|
|
|
|
auto &c(cur());
|
|
|
|
c.alarm.expires_from_now(duration);
|
|
|
|
c.wait(); // now you're yielding with portals
|
|
|
|
const auto ret(c.alarm.expires_from_now());
|
|
|
|
|
|
|
|
// return remaining duration.
|
|
|
|
// this is > 0 if notified
|
|
|
|
// this is unchanged if a note prevented any wait at all
|
2018-12-07 20:01:16 +01:00
|
|
|
return duration_cast<microseconds>(ret);
|
2018-03-25 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Yield the currently running context until notified.
|
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::wait()
|
|
|
|
{
|
|
|
|
auto &c(cur());
|
2018-12-07 20:01:16 +01:00
|
|
|
c.alarm.expires_at(steady_clock::time_point::max());
|
2018-03-25 08:50:39 +02:00
|
|
|
c.wait(); // now you're yielding with portals
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Post the currently running context to the event queue and then suspend to
|
|
|
|
/// allow other contexts in the queue to run.
|
|
|
|
///
|
|
|
|
/// Until we have our own queue the ios queue makes no guarantees if the queue
|
|
|
|
/// is FIFO or LIFO etc :-/ It is generally bad practice to use this function,
|
|
|
|
/// as one should make the effort to devise a specific cooperative strategy for
|
|
|
|
/// how context switching occurs rather than this coarse/brute technique.
|
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::yield()
|
|
|
|
{
|
|
|
|
bool done(false);
|
|
|
|
const auto restore([&done, &me(cur())]
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
notify(me);
|
|
|
|
});
|
|
|
|
|
|
|
|
// All spurious notifications are ignored until `done`
|
2018-10-17 14:12:10 +02:00
|
|
|
ircd::post(restore); do
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
while(!done);
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:58:25 +02:00
|
|
|
ulong
|
|
|
|
ircd::ctx::this_ctx::cycles_here()
|
|
|
|
{
|
|
|
|
assert(current);
|
|
|
|
return cycles(cur()) + prof::cur_slice_cycles();
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:04:31 +02:00
|
|
|
size_t
|
2018-05-06 07:58:25 +02:00
|
|
|
ircd::ctx::this_ctx::stack_at_here()
|
2018-04-28 04:04:31 +02:00
|
|
|
{
|
|
|
|
assert(current);
|
2018-05-23 03:03:18 +02:00
|
|
|
return cur().stack.base - uintptr_t(__builtin_frame_address(0));
|
2018-04-28 04:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 09:00:26 +02:00
|
|
|
/// Throws interrupted if the currently running context was interrupted
|
|
|
|
/// and clears the interrupt flag.
|
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::interruptible(const bool &b)
|
|
|
|
{
|
2018-08-20 05:10:51 +02:00
|
|
|
const bool theirs
|
|
|
|
{
|
|
|
|
interruptible(cur())
|
|
|
|
};
|
|
|
|
|
|
|
|
if(theirs && !b)
|
|
|
|
interruption_point();
|
|
|
|
|
2018-08-20 04:27:59 +02:00
|
|
|
interruptible(cur(), b);
|
2018-08-20 05:10:51 +02:00
|
|
|
|
|
|
|
if(!theirs && b)
|
|
|
|
interruption_point();
|
2018-05-22 09:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::interruptible(const bool &b,
|
|
|
|
std::nothrow_t)
|
|
|
|
noexcept
|
|
|
|
{
|
2018-08-20 04:27:59 +02:00
|
|
|
interruptible(cur(), b);
|
2018-05-22 09:00:26 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 23:13:37 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::this_ctx::interruptible()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return interruptible(cur());
|
|
|
|
}
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
/// Throws interrupted if the currently running context was interrupted
|
|
|
|
/// and clears the interrupt flag.
|
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::interruption_point()
|
|
|
|
{
|
2018-12-07 04:07:36 +01:00
|
|
|
// Asserting to know if this call is useless as it's being made in
|
|
|
|
// an uninterruptible scope anyway. It's okay to relax this assertion.
|
2018-12-07 04:04:31 +01:00
|
|
|
assert(interruptible());
|
2018-03-25 08:50:39 +02:00
|
|
|
return cur().interruption_point();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the currently running context was interrupted and clears
|
|
|
|
/// the interrupt flag.
|
|
|
|
bool
|
|
|
|
ircd::ctx::this_ctx::interruption_requested()
|
|
|
|
{
|
2018-05-06 10:00:11 +02:00
|
|
|
return interruption(cur()) || termination(cur());
|
2018-03-25 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns unique ID of currently running context
|
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::this_ctx::id()
|
|
|
|
{
|
|
|
|
static const uint64_t zero{0};
|
|
|
|
return current? id(cur()) : zero;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns optional developer-given name for currently running context
|
|
|
|
ircd::string_view
|
|
|
|
ircd::ctx::this_ctx::name()
|
|
|
|
{
|
|
|
|
static const string_view nada{"*"};
|
|
|
|
return current? name(cur()) : nada;
|
|
|
|
}
|
|
|
|
|
2018-05-22 09:00:26 +02:00
|
|
|
//
|
|
|
|
// uinterruptible
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::this_ctx::uninterruptible::uninterruptible()
|
2018-08-20 04:27:59 +02:00
|
|
|
:theirs
|
|
|
|
{
|
|
|
|
interruptible(cur())
|
|
|
|
}
|
2018-05-22 09:00:26 +02:00
|
|
|
{
|
|
|
|
interruptible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::this_ctx::uninterruptible::~uninterruptible()
|
|
|
|
noexcept(false)
|
|
|
|
{
|
2018-08-20 04:27:59 +02:00
|
|
|
interruptible(theirs);
|
2018-05-22 09:00:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// uninterruptible::nothrow
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::this_ctx::uninterruptible::nothrow::nothrow()
|
|
|
|
noexcept
|
2018-08-20 04:27:59 +02:00
|
|
|
:theirs
|
|
|
|
{
|
|
|
|
interruptible(cur())
|
|
|
|
}
|
2018-05-22 09:00:26 +02:00
|
|
|
{
|
|
|
|
interruptible(false, std::nothrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::this_ctx::uninterruptible::nothrow::~nothrow()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-08-20 04:27:59 +02:00
|
|
|
interruptible(theirs, std::nothrow);
|
2018-05-22 09:00:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 01:41:16 +01:00
|
|
|
//
|
|
|
|
// exception_handler
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::this_ctx::exception_handler::exception_handler()
|
|
|
|
noexcept
|
|
|
|
:std::exception_ptr{std::current_exception()}
|
|
|
|
{
|
|
|
|
assert(bool(*this));
|
2018-03-28 22:02:49 +02:00
|
|
|
//assert(!std::uncaught_exceptions());
|
2018-03-25 01:41:16 +01:00
|
|
|
__cxa_end_catch();
|
2018-04-07 20:08:07 +02:00
|
|
|
|
|
|
|
// We don't yet support more levels of exceptions; after ending this
|
|
|
|
// catch we can't still be in another one. This doesn't apply if we're
|
|
|
|
// not on any ctx currently.
|
|
|
|
assert(!current || !std::current_exception());
|
2018-03-25 01:41:16 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 03:29:29 +02:00
|
|
|
//
|
2018-03-25 08:50:39 +02:00
|
|
|
// critical_assertion
|
2017-09-28 03:29:29 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
bool critical_asserted;
|
2018-11-30 01:12:33 +01:00
|
|
|
|
|
|
|
void assert_critical();
|
2017-09-28 03:29:29 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 01:12:33 +01:00
|
|
|
#ifndef NDEBUG
|
2017-09-28 03:29:29 +02:00
|
|
|
ircd::ctx::this_ctx::critical_assertion::critical_assertion()
|
|
|
|
:theirs{critical_asserted}
|
|
|
|
{
|
|
|
|
critical_asserted = true;
|
|
|
|
}
|
2018-11-30 01:12:33 +01:00
|
|
|
#endif
|
2017-09-28 03:29:29 +02:00
|
|
|
|
2018-11-30 01:12:33 +01:00
|
|
|
#ifndef NDEBUG
|
2017-09-28 03:29:29 +02:00
|
|
|
ircd::ctx::this_ctx::critical_assertion::~critical_assertion()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(critical_asserted);
|
|
|
|
critical_asserted = theirs;
|
|
|
|
}
|
2018-11-30 01:12:33 +01:00
|
|
|
#endif
|
2017-09-28 03:29:29 +02:00
|
|
|
|
2018-11-30 01:12:33 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void
|
|
|
|
ircd::ctx::assert_critical()
|
|
|
|
{
|
|
|
|
if(unlikely(critical_asserted))
|
|
|
|
throw ircd::assertive
|
|
|
|
{
|
|
|
|
"%lu '%s' :Illegal context switch", id(), name()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
ircd::ctx::assert_critical()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-28 04:16:56 +02:00
|
|
|
|
2018-04-28 04:04:31 +02:00
|
|
|
//
|
|
|
|
// stack_usage_assertion
|
|
|
|
//
|
|
|
|
|
2018-11-30 01:12:33 +01:00
|
|
|
#ifndef NDEBUG
|
2018-04-28 04:04:31 +02:00
|
|
|
ircd::ctx::this_ctx::stack_usage_assertion::stack_usage_assertion()
|
|
|
|
{
|
2018-05-06 07:58:25 +02:00
|
|
|
const auto stack_usage(stack_at_here());
|
2018-11-15 02:35:23 +01:00
|
|
|
assert(stack_usage < cur().stack.max * double(prof::settings::stack_usage_assertion));
|
2018-04-28 04:04:31 +02:00
|
|
|
}
|
2018-11-30 01:12:33 +01:00
|
|
|
#endif
|
2018-04-28 04:04:31 +02:00
|
|
|
|
2018-11-30 01:12:33 +01:00
|
|
|
#ifndef NDEBUG
|
2018-04-28 04:04:31 +02:00
|
|
|
ircd::ctx::this_ctx::stack_usage_assertion::~stack_usage_assertion()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-05-06 07:58:25 +02:00
|
|
|
const auto stack_usage(stack_at_here());
|
2018-11-15 02:35:23 +01:00
|
|
|
assert(stack_usage < cur().stack.max * double(prof::settings::stack_usage_assertion));
|
2018-04-28 04:04:31 +02:00
|
|
|
}
|
2018-11-30 01:12:33 +01:00
|
|
|
#endif
|
2018-04-28 04:16:56 +02:00
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/continuation.h
|
|
|
|
//
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
decltype(ircd::ctx::continuation::true_predicate)
|
|
|
|
ircd::ctx::continuation::true_predicate{[]
|
|
|
|
() -> bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
|
|
|
|
decltype(ircd::ctx::continuation::false_predicate)
|
|
|
|
ircd::ctx::continuation::false_predicate{[]
|
|
|
|
() -> bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}};
|
|
|
|
|
|
|
|
decltype(ircd::ctx::continuation::noop_interruptor)
|
|
|
|
ircd::ctx::continuation::noop_interruptor{[]
|
|
|
|
(ctx *const &interruptor) -> void
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}};
|
|
|
|
|
2017-09-28 03:29:29 +02:00
|
|
|
//
|
|
|
|
// continuation
|
|
|
|
//
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
ircd::ctx::continuation::continuation(const predicate &pred,
|
|
|
|
const interruptor &intr)
|
2018-05-26 08:50:20 +02:00
|
|
|
:self
|
|
|
|
{
|
|
|
|
ircd::ctx::current
|
|
|
|
}
|
2018-12-07 21:05:27 +01:00
|
|
|
,pred
|
|
|
|
{
|
|
|
|
pred
|
|
|
|
}
|
|
|
|
,intr
|
|
|
|
{
|
|
|
|
intr
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-28 03:29:29 +02:00
|
|
|
mark(prof::event::CUR_YIELD);
|
2018-11-30 01:12:33 +01:00
|
|
|
|
|
|
|
assert_critical();
|
2017-09-28 03:29:29 +02:00
|
|
|
assert(!critical_asserted);
|
|
|
|
assert(self != nullptr);
|
|
|
|
assert(self->notes <= 1);
|
2018-03-24 23:53:15 +01:00
|
|
|
|
2018-03-25 01:41:16 +01:00
|
|
|
// Note: Construct an instance of ctx::exception_handler to enable yielding
|
|
|
|
// in your catch block.
|
|
|
|
//
|
2018-03-24 23:53:15 +01:00
|
|
|
// GNU cxxabi uses a singly-linked forward list (aka the 'exception
|
|
|
|
// stack') for pending exception activities. Due to this limitation we
|
|
|
|
// cannot interleave _cxa_begin_catch() and __cxa_end_catch() by yielding
|
|
|
|
// the ircd::ctx in an exception handler.
|
|
|
|
assert(!std::current_exception());
|
2018-03-28 22:02:49 +02:00
|
|
|
//assert(!std::uncaught_exceptions());
|
2018-03-24 23:53:15 +01:00
|
|
|
|
2018-05-23 03:03:18 +02:00
|
|
|
self->profile.yields++;
|
2018-03-23 23:35:09 +01:00
|
|
|
self->cont = this;
|
2017-09-28 03:29:29 +02:00
|
|
|
ircd::ctx::current = nullptr;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::continuation::~continuation()
|
|
|
|
noexcept
|
|
|
|
{
|
2017-09-28 03:29:29 +02:00
|
|
|
ircd::ctx::current = self;
|
|
|
|
self->notes = 1;
|
|
|
|
mark(prof::event::CUR_CONTINUE);
|
2018-03-23 23:35:09 +01:00
|
|
|
|
|
|
|
// self->continuation is not null'ed here; it remains an invalid
|
|
|
|
// pointer while the context is awake.
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::continuation::operator boost::asio::yield_context &()
|
|
|
|
{
|
|
|
|
return *self->yc;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::continuation::operator const boost::asio::yield_context &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return *self->yc;
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:35:09 +01:00
|
|
|
//
|
|
|
|
// to_asio
|
|
|
|
//
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
ircd::ctx::to_asio::to_asio(const interruptor &intr)
|
|
|
|
:continuation
|
|
|
|
{
|
|
|
|
false_predicate, intr
|
|
|
|
}
|
2018-03-23 23:35:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/context.h
|
|
|
|
//
|
|
|
|
|
2018-08-27 04:57:55 +02:00
|
|
|
// Linkage here for default construction because ctx is internal.
|
|
|
|
ircd::ctx::context::context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::context::context(const char *const &name,
|
|
|
|
const size_t &stack_sz,
|
|
|
|
const flags &flags,
|
2017-09-09 16:30:45 +02:00
|
|
|
function func)
|
2018-08-27 04:57:55 +02:00
|
|
|
:c
|
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
std::make_unique<ctx>(name, stack_sz, flags, ios::get())
|
2018-08-27 04:57:55 +02:00
|
|
|
}
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2017-10-25 22:45:24 +02:00
|
|
|
auto spawn
|
|
|
|
{
|
|
|
|
std::bind(&ircd::ctx::spawn, c.get(), std::move(func))
|
|
|
|
};
|
|
|
|
|
2017-11-06 21:10:55 +01:00
|
|
|
// The profiler is told about the spawn request here, not inside the closure
|
|
|
|
// which is probably the same event-slice as event::CUR_ENTER and not as useful.
|
|
|
|
mark(prof::event::SPAWN);
|
|
|
|
|
|
|
|
// When the user passes the DETACH flag we want to release the unique_ptr
|
|
|
|
// of the ctx if and only if that ctx is committed to freeing itself. Our
|
|
|
|
// commitment ends at the 180 of this function. If no exception was thrown
|
|
|
|
// we expect the context to be committed to entry. If the POST flag is
|
|
|
|
// supplied and it gets lost in the asio queue it will not be entered, and
|
|
|
|
// will not be able to free itself; that will leak.
|
|
|
|
const unwind::nominal release
|
2017-11-01 23:56:40 +01:00
|
|
|
{
|
2017-11-06 21:10:55 +01:00
|
|
|
[this, &flags]
|
|
|
|
{
|
|
|
|
if(flags & context::DETACH)
|
|
|
|
this->detach();
|
|
|
|
}
|
|
|
|
};
|
2017-11-01 23:56:40 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
if(flags & POST)
|
2017-11-01 23:56:40 +01:00
|
|
|
{
|
2018-10-17 14:12:10 +02:00
|
|
|
ios::post(std::move(spawn));
|
2017-11-01 23:56:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current context must be reasserted if spawn returns here
|
2018-03-12 23:44:55 +01:00
|
|
|
auto *const theirs(ircd::ctx::current);
|
|
|
|
const unwind recurrent([&theirs]
|
2017-10-25 22:45:24 +02:00
|
|
|
{
|
2018-03-12 23:44:55 +01:00
|
|
|
ircd::ctx::current = theirs;
|
2017-11-01 23:56:40 +01:00
|
|
|
});
|
2017-10-25 22:45:24 +02:00
|
|
|
|
2017-11-01 23:56:40 +01:00
|
|
|
if(flags & DISPATCH)
|
2018-10-17 14:12:10 +02:00
|
|
|
ios::dispatch(std::move(spawn));
|
2017-11-01 23:56:40 +01:00
|
|
|
else
|
2016-09-07 23:39:41 +02:00
|
|
|
spawn();
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::context::context(const char *const &name,
|
|
|
|
const size_t &stack_size,
|
2017-09-09 16:30:45 +02:00
|
|
|
function func,
|
2016-11-29 16:23:38 +01:00
|
|
|
const flags &flags)
|
|
|
|
:context
|
|
|
|
{
|
|
|
|
name, stack_size, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context::context(const char *const &name,
|
|
|
|
const flags &flags,
|
2017-09-09 16:30:45 +02:00
|
|
|
function func)
|
2016-11-29 16:23:38 +01:00
|
|
|
:context
|
|
|
|
{
|
|
|
|
name, DEFAULT_STACK_SIZE, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context::context(const char *const &name,
|
2017-09-09 16:30:45 +02:00
|
|
|
function func,
|
2016-11-29 16:23:38 +01:00
|
|
|
const flags &flags)
|
|
|
|
:context
|
|
|
|
{
|
|
|
|
name, DEFAULT_STACK_SIZE, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-09 16:30:45 +02:00
|
|
|
ircd::ctx::context::context(function func,
|
2016-11-29 16:23:38 +01:00
|
|
|
const flags &flags)
|
2016-09-05 17:53:36 +02:00
|
|
|
:context
|
|
|
|
{
|
2017-10-16 06:12:58 +02:00
|
|
|
"<noname>", DEFAULT_STACK_SIZE, flags, std::move(func)
|
2016-09-05 17:53:36 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-27 04:57:55 +02:00
|
|
|
ircd::ctx::context::context(context &&other)
|
|
|
|
noexcept
|
|
|
|
:c{std::move(other.c)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context &
|
|
|
|
ircd::ctx::context::operator=(context &&other)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
std::swap(this->c, other.c);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:53:36 +02:00
|
|
|
ircd::ctx::context::~context()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-08-20 03:09:04 +02:00
|
|
|
if(!c)
|
|
|
|
return;
|
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
// Can't join to bare metal, only from within another context.
|
2018-08-20 03:09:04 +02:00
|
|
|
if(current)
|
2017-10-25 22:45:24 +02:00
|
|
|
{
|
2018-08-28 23:04:43 +02:00
|
|
|
const uninterruptible::nothrow ui;
|
2018-10-23 19:16:15 +02:00
|
|
|
terminate();
|
2017-10-25 22:45:24 +02:00
|
|
|
join();
|
2018-05-28 12:12:11 +02:00
|
|
|
return;
|
2017-10-25 22:45:24 +02:00
|
|
|
}
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2017-10-25 22:45:24 +02:00
|
|
|
// because *this uses unique_ptr's, if we dtor the ircd::ctx from
|
|
|
|
// right here and ircd::ctx hasn't been entered yet because the user
|
|
|
|
// passed the POST flag, the ctx::spawn() is still sitting in the ios
|
|
|
|
// queue.
|
2018-08-20 03:09:04 +02:00
|
|
|
if(!started(*c))
|
2017-10-25 22:45:24 +02:00
|
|
|
{
|
2018-05-28 12:12:11 +02:00
|
|
|
detach();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When this is bare metal the above join branch will not have been
|
|
|
|
// taken. In that case we should detach the context so it frees itself,
|
|
|
|
// but only if the context has not already finished.
|
2018-08-20 03:09:04 +02:00
|
|
|
if(!current && !finished(*c))
|
2018-05-28 12:12:11 +02:00
|
|
|
{
|
|
|
|
detach();
|
|
|
|
return;
|
2017-10-25 22:45:24 +02:00
|
|
|
}
|
2016-09-05 17:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::context::join()
|
|
|
|
{
|
2016-09-07 23:39:41 +02:00
|
|
|
if(joined())
|
|
|
|
return;
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2017-11-06 21:10:55 +01:00
|
|
|
assert(bool(c));
|
2018-08-20 01:21:33 +02:00
|
|
|
mark(prof::event::JOIN);
|
|
|
|
c->adjoindre.wait([this]
|
|
|
|
{
|
|
|
|
return joined();
|
|
|
|
});
|
|
|
|
|
2016-09-26 00:46:54 +02:00
|
|
|
mark(prof::event::JOINED);
|
2016-09-05 17:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::ctx *
|
|
|
|
ircd::ctx::context::detach()
|
|
|
|
{
|
2017-11-06 21:10:55 +01:00
|
|
|
assert(bool(c));
|
2016-11-29 16:23:38 +01:00
|
|
|
c->flags |= DETACH;
|
2016-09-05 17:53:36 +02:00
|
|
|
return c.release();
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:15:49 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx_pool.h
|
|
|
|
//
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::pool(const char *const &name,
|
|
|
|
const size_t &stack_size,
|
|
|
|
const size_t &size)
|
|
|
|
:name{name}
|
|
|
|
,stack_size{stack_size}
|
2017-11-26 03:21:21 +01:00
|
|
|
,running{0}
|
|
|
|
,working{0}
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
|
|
|
add(size);
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::~pool()
|
2016-09-21 23:15:49 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2018-08-28 23:04:43 +02:00
|
|
|
terminate();
|
2018-08-20 03:09:04 +02:00
|
|
|
join();
|
2018-08-28 23:04:43 +02:00
|
|
|
|
2018-08-20 03:09:04 +02:00
|
|
|
assert(ctxs.empty());
|
2018-09-19 01:45:04 +02:00
|
|
|
assert(q.empty());
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::operator()(closure closure)
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2018-09-19 06:56:36 +02:00
|
|
|
q.push(std::move(closure));
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:08:03 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::pool::set(const size_t &num)
|
|
|
|
{
|
|
|
|
if(size() > num)
|
|
|
|
del(size() - num);
|
|
|
|
else
|
|
|
|
add(num - size());
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:15:49 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::del(const size_t &num)
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2018-08-20 00:55:35 +02:00
|
|
|
const auto requested
|
|
|
|
{
|
|
|
|
ssize_t(size()) - ssize_t(num)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto target
|
|
|
|
{
|
|
|
|
size_t(std::max(requested, 0L))
|
|
|
|
};
|
|
|
|
|
2016-09-21 23:15:49 +02:00
|
|
|
while(ctxs.size() > target)
|
|
|
|
ctxs.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::add(const size_t &num)
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
|
|
|
for(size_t i(0); i < num; ++i)
|
2016-11-29 16:23:38 +01:00
|
|
|
ctxs.emplace_back(name, stack_size, context::POST, std::bind(&pool::main, this));
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 04:01:37 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::pool::join()
|
|
|
|
{
|
2018-08-22 23:08:03 +02:00
|
|
|
set(0);
|
2017-09-20 04:01:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:15:49 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::interrupt()
|
|
|
|
{
|
|
|
|
for(auto &context : ctxs)
|
|
|
|
context.interrupt();
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:55:35 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::pool::terminate()
|
|
|
|
{
|
|
|
|
for(auto &context : ctxs)
|
|
|
|
context.terminate();
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::pool::main()
|
2018-03-15 21:44:29 +01:00
|
|
|
noexcept try
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2017-11-26 03:21:21 +01:00
|
|
|
++running;
|
2017-09-22 22:57:43 +02:00
|
|
|
const unwind avail([this]
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2017-11-26 03:21:21 +01:00
|
|
|
--running;
|
2016-09-21 23:15:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
catch(const interrupted &e)
|
|
|
|
{
|
2018-05-06 10:15:26 +02:00
|
|
|
// log::debug
|
|
|
|
// {
|
|
|
|
// "pool(%p) ctx(%p): %s", this, &cur(), e.what()
|
|
|
|
// };
|
|
|
|
}
|
|
|
|
catch(const terminated &e)
|
|
|
|
{
|
|
|
|
// log::debug
|
|
|
|
// {
|
|
|
|
// "pool(%p) ctx(%p): %s", this, &cur(), e.what()
|
|
|
|
// };
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::pool::next()
|
2016-09-21 23:15:49 +02:00
|
|
|
try
|
|
|
|
{
|
2018-09-19 01:45:04 +02:00
|
|
|
const auto func
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2018-09-19 01:45:04 +02:00
|
|
|
std::move(q.pop())
|
|
|
|
};
|
2016-09-21 23:15:49 +02:00
|
|
|
|
2017-11-26 03:21:21 +01:00
|
|
|
++working;
|
2017-09-22 22:57:43 +02:00
|
|
|
const unwind avail([this]
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2017-11-26 03:21:21 +01:00
|
|
|
--working;
|
2016-09-21 23:15:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
catch(const interrupted &e)
|
|
|
|
{
|
2018-05-22 14:46:32 +02:00
|
|
|
// Interrupt is stopped here so this ctx can be reused for a new job.
|
|
|
|
return;
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-03-15 21:44:29 +01:00
|
|
|
log::critical
|
|
|
|
{
|
2018-03-17 04:25:56 +01:00
|
|
|
"pool(%p) ctx(%p '%s' #%u): unhandled: %s",
|
2018-03-15 21:44:29 +01:00
|
|
|
this,
|
2018-03-17 04:25:56 +01:00
|
|
|
current,
|
|
|
|
ircd::ctx::name(cur()),
|
|
|
|
ircd::ctx::id(cur()),
|
2018-03-15 21:44:29 +01:00
|
|
|
e.what()
|
|
|
|
};
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2017-10-19 09:58:43 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::debug_stats(const pool &pool)
|
|
|
|
{
|
2018-03-17 04:25:56 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"pool '%s' (stack size: %zu) total: %zu avail: %zu queued: %zu active: %zu pending: %zu",
|
|
|
|
pool.name,
|
|
|
|
pool.stack_size,
|
|
|
|
pool.size(),
|
|
|
|
pool.avail(),
|
|
|
|
pool.queued(),
|
|
|
|
pool.active(),
|
|
|
|
pool.pending()
|
|
|
|
};
|
2017-10-19 09:58:43 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:46:54 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx_prof.h
|
|
|
|
//
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx::prof
|
|
|
|
{
|
2018-05-06 07:09:20 +02:00
|
|
|
ulong _slice_start; // Time slice state
|
2018-05-06 07:31:52 +02:00
|
|
|
ulong _slice_total; // Monotonic accumulator
|
2017-08-28 23:51:22 +02:00
|
|
|
|
|
|
|
void check_stack();
|
|
|
|
void check_slice();
|
|
|
|
void slice_start();
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
void handle_cur_continue();
|
|
|
|
void handle_cur_yield();
|
|
|
|
void handle_cur_leave();
|
|
|
|
void handle_cur_enter();
|
|
|
|
}
|
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
// stack_usage_warning at 1/3 engineering tolerance
|
|
|
|
decltype(ircd::ctx::prof::settings::stack_usage_warning)
|
|
|
|
ircd::ctx::prof::settings::stack_usage_warning
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-11-15 02:35:23 +01:00
|
|
|
{ "name", "ircd.ctx.prof.stack_usage_warning" },
|
|
|
|
{ "default", 0.33 },
|
|
|
|
};
|
|
|
|
|
|
|
|
// stack_usage_assertion at 1/2 engineering tolerance
|
|
|
|
decltype(ircd::ctx::prof::settings::stack_usage_assertion)
|
|
|
|
ircd::ctx::prof::settings::stack_usage_assertion
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ctx.prof.stack_usage_assertion" },
|
|
|
|
{ "default", 0.50 },
|
|
|
|
};
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
// slice_warning after this number of tsc ticks...
|
|
|
|
decltype(ircd::ctx::prof::settings::slice_warning)
|
|
|
|
ircd::ctx::prof::settings::slice_warning
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ctx.prof.slice_warning" },
|
|
|
|
{ "default", 280 * 1000000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
// slice_interrupt after this number of tsc ticks...
|
|
|
|
decltype(ircd::ctx::prof::settings::slice_interrupt)
|
|
|
|
ircd::ctx::prof::settings::slice_interrupt
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ctx.prof.slice_interrupt" },
|
|
|
|
{ "default", 0L },
|
|
|
|
};
|
|
|
|
|
|
|
|
// slice_assertion after this number of tsc ticks...
|
|
|
|
decltype(ircd::ctx::prof::settings::slice_assertion)
|
|
|
|
ircd::ctx::prof::settings::slice_assertion
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ctx.prof.slice_assertion" },
|
|
|
|
{ "default", 0L },
|
2016-09-26 00:46:54 +02:00
|
|
|
};
|
|
|
|
|
2018-02-05 19:48:46 +01:00
|
|
|
#ifdef RB_DEBUG
|
2016-09-26 00:46:54 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::mark(const event &e)
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
switch(e)
|
|
|
|
{
|
|
|
|
case event::CUR_ENTER: handle_cur_enter(); break;
|
|
|
|
case event::CUR_LEAVE: handle_cur_leave(); break;
|
|
|
|
case event::CUR_YIELD: handle_cur_yield(); break;
|
|
|
|
case event::CUR_CONTINUE: handle_cur_continue(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 19:48:46 +01:00
|
|
|
#else
|
|
|
|
void
|
|
|
|
ircd::ctx::prof::mark(const event &e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-05-06 07:09:20 +02:00
|
|
|
ulong
|
|
|
|
ircd::ctx::prof::cur_slice_cycles()
|
|
|
|
{
|
2018-11-15 01:55:46 +01:00
|
|
|
return rdtsc() - cur_slice_start();
|
2018-05-06 07:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ulong &
|
|
|
|
ircd::ctx::prof::cur_slice_start()
|
|
|
|
{
|
|
|
|
return _slice_start;
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:31:52 +02:00
|
|
|
const ulong &
|
|
|
|
ircd::ctx::prof::total_slice_cycles()
|
|
|
|
{
|
|
|
|
return _slice_total;
|
|
|
|
}
|
|
|
|
|
2016-09-26 00:46:54 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::handle_cur_enter()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
slice_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::handle_cur_leave()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
check_slice();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::handle_cur_yield()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
check_slice();
|
2018-05-06 07:31:52 +02:00
|
|
|
check_stack();
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::handle_cur_continue()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
slice_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::slice_start()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-11-15 01:55:46 +01:00
|
|
|
_slice_start = rdtsc();
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::check_slice()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-05-06 07:09:20 +02:00
|
|
|
const auto &last_cycles
|
|
|
|
{
|
|
|
|
cur_slice_cycles()
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-02-05 19:47:46 +01:00
|
|
|
auto &c(cur());
|
2018-05-23 03:03:18 +02:00
|
|
|
c.profile.cycles += last_cycles;
|
2018-05-06 07:31:52 +02:00
|
|
|
_slice_total += last_cycles;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
const ulong &slice_warning(settings::slice_warning);
|
|
|
|
if(unlikely(slice_warning > 0 && last_cycles >= slice_warning))
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-02-05 19:47:46 +01:00
|
|
|
{
|
2018-05-06 07:09:20 +02:00
|
|
|
"context timeslice exceeded '%s' #%lu total: %lu last: %lu",
|
2018-02-05 19:47:46 +01:00
|
|
|
name(c),
|
|
|
|
id(c),
|
2018-05-06 07:09:20 +02:00
|
|
|
cycles(c),
|
|
|
|
last_cycles
|
2018-02-05 19:47:46 +01:00
|
|
|
};
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
const ulong &slice_assertion(settings::slice_assertion);
|
|
|
|
assert(slice_assertion == 0 || last_cycles < slice_assertion);
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
const ulong &slice_interrupt(settings::slice_interrupt);
|
|
|
|
if(unlikely(slice_interrupt > 0 && last_cycles >= slice_interrupt))
|
2018-02-05 19:47:46 +01:00
|
|
|
throw interrupted
|
|
|
|
{
|
2018-05-06 07:09:20 +02:00
|
|
|
"context '%s' #%lu watchdog interrupt (total: %lu last: %lu)",
|
2018-02-05 19:47:46 +01:00
|
|
|
name(c),
|
|
|
|
id(c),
|
2018-05-06 07:09:20 +02:00
|
|
|
cycles(c),
|
|
|
|
last_cycles
|
2018-02-05 19:47:46 +01:00
|
|
|
};
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::check_stack()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
|
|
|
auto &c(cur());
|
2018-05-23 03:03:18 +02:00
|
|
|
const double &stack_max(c.stack.max);
|
2018-05-06 07:58:25 +02:00
|
|
|
const auto &stack_at(stack_at_here());
|
2018-05-23 03:03:18 +02:00
|
|
|
c.stack.at = stack_at;
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
const double &stack_usage_assertion(settings::stack_usage_assertion);
|
|
|
|
if(unlikely(stack_at > stack_max * stack_usage_assertion))
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-02-06 04:12:34 +01:00
|
|
|
{
|
|
|
|
"context stack usage ctx '%s' #%lu used %zu of %zu bytes",
|
|
|
|
name(c),
|
|
|
|
id(c),
|
2018-05-06 07:58:25 +02:00
|
|
|
stack_at,
|
2018-05-23 03:03:18 +02:00
|
|
|
c.stack.max
|
2018-02-06 04:12:34 +01:00
|
|
|
};
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-11-15 02:35:23 +01:00
|
|
|
const double &stack_usage_assertion(settings::stack_usage_assertion);
|
|
|
|
assert(stack_at < c.stack.max * double(settings::stack_usage_assertion));
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:10:19 +01:00
|
|
|
#ifdef HAVE_X86INTRIN_H
|
2018-11-15 01:55:46 +01:00
|
|
|
ulong
|
|
|
|
ircd::ctx::prof::rdtsc()
|
|
|
|
{
|
|
|
|
return __rdtsc();
|
|
|
|
}
|
2018-11-15 03:10:19 +01:00
|
|
|
#else
|
|
|
|
ulong
|
|
|
|
ircd::ctx::prof::rdtsc()
|
|
|
|
{
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
0, "TODO: Implement fallback here"
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-15 01:55:46 +01:00
|
|
|
|
2016-09-27 10:40:33 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx_ole.h
|
|
|
|
//
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx::ole
|
|
|
|
{
|
2018-09-13 12:06:17 +02:00
|
|
|
using closure = std::function<void ()>;
|
2016-09-27 10:40:33 +02:00
|
|
|
|
2018-05-26 05:21:36 +02:00
|
|
|
extern conf::item<size_t> thread_max;
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable cond;
|
2018-05-26 05:21:36 +02:00
|
|
|
bool termination;
|
2017-08-28 23:51:22 +02:00
|
|
|
std::deque<closure> queue;
|
2018-05-26 05:21:36 +02:00
|
|
|
std::vector<std::thread> threads;
|
2016-09-27 10:40:33 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
closure pop();
|
|
|
|
void push(closure &&);
|
2018-05-26 05:21:36 +02:00
|
|
|
void worker() noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-09-27 10:40:33 +02:00
|
|
|
|
2018-05-26 05:21:36 +02:00
|
|
|
decltype(ircd::ctx::ole::thread_max)
|
|
|
|
ircd::ctx::ole::thread_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ctx.ole.thread.max" },
|
|
|
|
{ "default", int64_t(1) },
|
|
|
|
};
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ole::init::init()
|
2016-09-27 10:40:33 +02:00
|
|
|
{
|
2018-05-26 05:21:36 +02:00
|
|
|
assert(threads.empty());
|
|
|
|
termination = false;
|
2016-09-27 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ole::init::~init()
|
2016-09-27 10:40:33 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2018-05-26 05:21:36 +02:00
|
|
|
std::unique_lock<decltype(mutex)> lock(mutex);
|
|
|
|
termination = true;
|
|
|
|
cond.notify_all();
|
|
|
|
cond.wait(lock, []
|
|
|
|
{
|
|
|
|
return threads.empty();
|
|
|
|
});
|
2016-09-27 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ole::offload(const std::function<void ()> &func)
|
2016-09-27 10:40:33 +02:00
|
|
|
{
|
2017-03-29 07:44:01 +02:00
|
|
|
bool done(false);
|
|
|
|
auto *const context(current);
|
2017-03-31 06:12:36 +02:00
|
|
|
const auto kick([&context, &done]
|
2017-03-29 07:44:01 +02:00
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
notify(*context);
|
|
|
|
});
|
|
|
|
|
2016-09-27 10:40:33 +02:00
|
|
|
std::exception_ptr eptr;
|
2017-03-31 06:12:36 +02:00
|
|
|
auto closure([&func, &eptr, &context, &kick]
|
2016-09-27 10:40:33 +02:00
|
|
|
() noexcept
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
eptr = std::current_exception();
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:59:27 +02:00
|
|
|
// To wake the context on the IRCd thread we give it the kick
|
2017-09-20 04:01:37 +02:00
|
|
|
signal(*context, kick);
|
2016-09-27 10:40:33 +02:00
|
|
|
});
|
|
|
|
|
2018-05-22 09:18:46 +02:00
|
|
|
// interrupt(ctx) is suppressed while this context has offloaded some work
|
|
|
|
// to another thread. This context must stay right here and not disappear
|
|
|
|
// until the other thread signals back. Note that the destructor is
|
|
|
|
// capable of throwing an interrupt that was received during this scope.
|
2018-08-20 03:09:04 +02:00
|
|
|
const uninterruptible uninterruptible;
|
2018-05-22 09:18:46 +02:00
|
|
|
|
2016-09-27 10:40:33 +02:00
|
|
|
push(std::move(closure)); do
|
|
|
|
{
|
|
|
|
wait();
|
|
|
|
}
|
2017-03-29 07:44:01 +02:00
|
|
|
while(!done);
|
2016-09-27 10:40:33 +02:00
|
|
|
|
2018-05-22 09:18:46 +02:00
|
|
|
// Don't throw any exception if there is a pending interrupt for this ctx.
|
|
|
|
// Two exceptions will be thrown in that case and if there's an interrupt
|
|
|
|
// we don't care about eptr anyway.
|
|
|
|
if(eptr && likely(!interruption_requested()))
|
2016-09-27 10:40:33 +02:00
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ole::push(closure &&func)
|
2016-09-27 10:40:33 +02:00
|
|
|
{
|
2018-05-26 05:21:36 +02:00
|
|
|
if(unlikely(threads.size() < size_t(thread_max)))
|
|
|
|
threads.emplace_back(&worker);
|
2018-02-07 20:37:11 +01:00
|
|
|
|
2016-09-27 10:40:33 +02:00
|
|
|
const std::lock_guard<decltype(mutex)> lock(mutex);
|
|
|
|
queue.emplace_back(std::move(func));
|
2018-05-26 05:21:36 +02:00
|
|
|
cond.notify_all();
|
2016-09-27 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ole::worker()
|
2016-09-27 10:40:33 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
const auto func(pop());
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const interrupted &)
|
|
|
|
{
|
2018-05-26 05:21:36 +02:00
|
|
|
std::unique_lock<decltype(mutex)> lock(mutex);
|
|
|
|
const auto it(std::find_if(begin(threads), end(threads), []
|
|
|
|
(const auto &thread)
|
|
|
|
{
|
|
|
|
return thread.get_id() == std::this_thread::get_id();
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert(it != end(threads));
|
|
|
|
auto &this_thread(*it);
|
|
|
|
this_thread.detach();
|
|
|
|
threads.erase(it);
|
|
|
|
cond.notify_all();
|
2016-09-27 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ole::closure
|
|
|
|
ircd::ctx::ole::pop()
|
2016-09-27 10:40:33 +02:00
|
|
|
{
|
|
|
|
std::unique_lock<decltype(mutex)> lock(mutex);
|
|
|
|
cond.wait(lock, []
|
|
|
|
{
|
|
|
|
if(!queue.empty())
|
|
|
|
return true;
|
|
|
|
|
2018-05-26 05:21:36 +02:00
|
|
|
if(unlikely(termination))
|
|
|
|
throw interrupted{};
|
2016-09-27 10:40:33 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto c(std::move(queue.front()));
|
|
|
|
queue.pop_front();
|
|
|
|
return std::move(c);
|
|
|
|
}
|
2017-10-12 02:38:24 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/promise.h
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// promise<void>
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::promise<void>::set_value()
|
|
|
|
{
|
|
|
|
check_pending();
|
|
|
|
make_ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state<void> &
|
|
|
|
ircd::ctx::promise<void>::state()
|
|
|
|
{
|
|
|
|
return promise_base::state<void>();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::ctx::shared_state<void> &
|
|
|
|
ircd::ctx::promise<void>::state()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return promise_base::state<void>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// promise_base::promise_base
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::promise_base::promise_base(promise_base &&o)
|
|
|
|
noexcept
|
|
|
|
:st{std::move(o.st)}
|
|
|
|
,next{std::move(o.next)}
|
|
|
|
{
|
|
|
|
if(st)
|
|
|
|
{
|
|
|
|
update(*this, o);
|
|
|
|
o.st = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base::promise_base(const promise_base &o)
|
|
|
|
:st{o.st}
|
|
|
|
,next{nullptr}
|
|
|
|
{
|
|
|
|
append(*this, const_cast<promise_base &>(o));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base &
|
|
|
|
ircd::ctx::promise_base::operator=(promise_base &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~promise_base();
|
|
|
|
st = std::move(o.st);
|
|
|
|
next = std::move(o.next);
|
|
|
|
if(!st)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
update(*this, o);
|
|
|
|
o.st = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base::~promise_base()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(!valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(refcount(state()) == 1)
|
|
|
|
set_exception(std::make_exception_ptr(broken_promise()));
|
|
|
|
else
|
|
|
|
remove(state(), *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::set_exception(std::exception_ptr eptr)
|
|
|
|
{
|
|
|
|
check_pending();
|
|
|
|
state().eptr = std::move(eptr);
|
|
|
|
make_ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::make_ready()
|
|
|
|
{
|
|
|
|
auto &st(state());
|
|
|
|
|
|
|
|
// First we have to chase the linked list of promises reachable
|
|
|
|
// from this shared_state. invalidate() will null their pointer
|
|
|
|
// to the shared_state indicating the promise was already satisfied.
|
|
|
|
// This is done first because the set() to the READY writes to the
|
|
|
|
// same union as the promise pointer (see shared_state.h).
|
|
|
|
invalidate(st);
|
|
|
|
|
|
|
|
// Now set the shared_state to READY. We know the location of the
|
|
|
|
// shared state by saving it in this frame earlier, otherwise invalidate()
|
|
|
|
// would have nulled it.
|
|
|
|
set(st, future_state::READY);
|
|
|
|
|
|
|
|
// Finally call the notify() routine which will tell the future the promise
|
|
|
|
// was satisfied and the value/exception is ready for them. This call may
|
|
|
|
// notify an ircd::ctx and/or post a function to the ircd::ios for a then()
|
|
|
|
// callback etc.
|
|
|
|
notify(st);
|
|
|
|
|
2018-11-07 04:46:28 +01:00
|
|
|
// At this point the future should no longer be considered valid; no longer
|
2018-10-30 19:18:03 +01:00
|
|
|
// referring to the shared_state.
|
|
|
|
assert(!valid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::check_pending()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
if(unlikely(!is(state(), future_state::PENDING)))
|
|
|
|
throw promise_already_satisfied{};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::promise_base::operator!()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base::operator bool()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::promise_base::valid()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return bool(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::promise_base::state()
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
return *st;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::promise_base::state()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
return *st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal semantics; chases the linked list of promises and adds a reference
|
|
|
|
/// to a new copy at the end (for copy semantic).
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::append(promise_base &new_,
|
|
|
|
promise_base &old)
|
|
|
|
{
|
|
|
|
if(!old.next)
|
|
|
|
{
|
|
|
|
old.next = &new_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_base *next{old.next};
|
|
|
|
for(; next->next; next = next->next);
|
|
|
|
next->next = &new_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal semantics; updates the location of a promise within the linked
|
|
|
|
/// list of related promises (for move semantic).
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::update(promise_base &new_,
|
|
|
|
promise_base &old)
|
|
|
|
{
|
|
|
|
assert(old.st);
|
|
|
|
auto &st{*old.st};
|
|
|
|
if(!is(st, future_state::PENDING))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(st.p == &old)
|
|
|
|
{
|
|
|
|
st.p = &new_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_base *last{st.p};
|
|
|
|
for(promise_base *next{last->next}; next; last = next, next = last->next)
|
|
|
|
if(next == &old)
|
|
|
|
{
|
|
|
|
last->next = &new_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal semantics; removes the promise from the linked list of promises.
|
|
|
|
/// Because the linked list of promises is a forward singly-linked list this
|
|
|
|
/// operation requires a reference to the list's head in shared_state_base
|
|
|
|
/// (for dtor semantic).
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::remove(shared_state_base &st,
|
|
|
|
promise_base &p)
|
|
|
|
{
|
|
|
|
if(!is(st, future_state::PENDING))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(st.p == &p)
|
|
|
|
{
|
|
|
|
st.p = p.next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_base *last{st.p};
|
|
|
|
for(promise_base *next{last->next}; next; last = next, next = last->next)
|
|
|
|
if(next == &p)
|
|
|
|
{
|
|
|
|
last->next = p.next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 03:56:41 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/shared_shared.h
|
|
|
|
//
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
/// Internal use
|
|
|
|
void
|
|
|
|
ircd::ctx::notify(shared_state_base &st)
|
|
|
|
{
|
|
|
|
if(!st.then)
|
|
|
|
{
|
|
|
|
st.cond.notify_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!current)
|
|
|
|
{
|
|
|
|
st.cond.notify_all();
|
|
|
|
assert(bool(st.then));
|
|
|
|
st.then(st);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stack_usage_assertion sua;
|
|
|
|
st.cond.notify_all();
|
|
|
|
assert(bool(st.then));
|
|
|
|
st.then(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal use; chases the linked list of promises starting from the head
|
|
|
|
/// in the shared_state and invalidates all of their references to the shared
|
|
|
|
/// state. This will cause the promise to no longer be valid().
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::ctx::invalidate(shared_state_base &st)
|
|
|
|
{
|
|
|
|
if(is(st, future_state::PENDING))
|
|
|
|
for(promise_base *p{st.p}; p; p = p->next)
|
|
|
|
p->st = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal use; chases the linked list of promises starting from the head in
|
|
|
|
/// the shared_state and updates the location of the shared_state within each
|
|
|
|
/// promise. This is used to tell the promises when the shared_state itself
|
|
|
|
/// has relocated.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
ircd::ctx::update(shared_state_base &st)
|
|
|
|
{
|
|
|
|
if(is(st, future_state::PENDING))
|
|
|
|
for(promise_base *p{st.p}; p; p = p->next)
|
|
|
|
p->st = &st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal use; returns the number of copies of the promise reachable from
|
|
|
|
/// the linked list headed by the shared state. This is used to indicate when
|
|
|
|
/// the last copy has destructed which may result in a broken_promise exception
|
|
|
|
/// being sent to the future.
|
|
|
|
size_t
|
|
|
|
ircd::ctx::refcount(const shared_state_base &st)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
if(is(st, future_state::PENDING))
|
|
|
|
for(const promise_base *p{st.p}; p; p = p->next)
|
|
|
|
++ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal use; sets the state indicator within the shared_state object. Take
|
|
|
|
/// special note that this data is unionized. Setting a state here will clobber
|
|
|
|
/// the shared_state's reference to its promise.
|
|
|
|
void
|
|
|
|
ircd::ctx::set(shared_state_base &st,
|
|
|
|
const future_state &state)
|
|
|
|
{
|
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case future_state::INVALID: assert(0); return;
|
|
|
|
case future_state::PENDING: assert(0); return;
|
|
|
|
case future_state::OBSERVED:
|
|
|
|
case future_state::READY:
|
|
|
|
case future_state::RETRIEVED:
|
|
|
|
default:
|
|
|
|
st.st = state;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal; check if the current state is something; safe but unnecessary
|
|
|
|
/// for public use. Take special note here that the state value is unionized.
|
|
|
|
///
|
|
|
|
/// A PENDING state returned here does not mean the state contains the
|
|
|
|
/// enumerated PENDING value itself, but instead contains a valid pointer
|
|
|
|
/// to a promise.
|
|
|
|
///
|
|
|
|
/// An INVALID state shares a zero/null value in the unionized data.
|
|
|
|
bool
|
|
|
|
ircd::ctx::is(const shared_state_base &st,
|
|
|
|
const future_state &state_)
|
|
|
|
{
|
|
|
|
switch(st.st)
|
|
|
|
{
|
|
|
|
case future_state::READY:
|
|
|
|
case future_state::OBSERVED:
|
|
|
|
case future_state::RETRIEVED:
|
|
|
|
return state_ == st.st;
|
|
|
|
|
|
|
|
default: switch(state_)
|
|
|
|
{
|
|
|
|
case future_state::INVALID:
|
|
|
|
return st.p == nullptr;
|
|
|
|
|
|
|
|
case future_state::PENDING:
|
|
|
|
return uintptr_t(st.p) >= 0x1000;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal; get the current state of the shared_state; safe but unnecessary
|
|
|
|
/// for public use.
|
|
|
|
///
|
|
|
|
/// NOTE: This operates over a union of a pointer and an enum class. The
|
|
|
|
/// way we determine whether the data is a pointer or an enum value is
|
|
|
|
/// with a test of the value being >= the system's page size. This assumes
|
|
|
|
/// the system does not use the first page of a process's address space
|
|
|
|
/// to fault on null pointer dereference. This assumption may not hold on
|
|
|
|
/// all systems or in all environments.
|
|
|
|
///
|
|
|
|
/// Alternatively, we can switch this to checking whether the value is simply
|
|
|
|
/// above the few low-numbered enum values.
|
|
|
|
ircd::ctx::future_state
|
|
|
|
ircd::ctx::state(const shared_state_base &st)
|
|
|
|
{
|
|
|
|
return uintptr_t(st.p) >= ircd::info::page_size?
|
|
|
|
future_state::PENDING:
|
|
|
|
st.st;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// shared_state_base::shared_state_base
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base::shared_state_base(promise_base &p)
|
|
|
|
:p{&p}
|
2018-08-29 03:56:41 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linkage
|
|
|
|
ircd::ctx::shared_state_base::~shared_state_base()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
then = {};
|
|
|
|
}
|
|
|
|
|
2018-01-14 08:10:44 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx_list.h
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::remove(ctx *const &c)
|
|
|
|
{
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
if(c == head)
|
|
|
|
{
|
|
|
|
pop_front();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c == tail)
|
|
|
|
{
|
|
|
|
pop_back();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(next(c) && prev(c));
|
|
|
|
prev(next(c)) = prev(c);
|
|
|
|
next(prev(c)) = next(c);
|
|
|
|
next(c) = nullptr;
|
|
|
|
prev(c) = nullptr;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::ctx *
|
|
|
|
ircd::ctx::list::pop_back()
|
|
|
|
{
|
|
|
|
const auto tail
|
|
|
|
{
|
|
|
|
this->tail
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!tail)
|
2018-08-27 10:45:31 +02:00
|
|
|
{
|
|
|
|
assert(!head);
|
2018-01-14 08:10:44 +01:00
|
|
|
return tail;
|
2018-08-27 10:45:31 +02:00
|
|
|
}
|
2018-01-14 08:10:44 +01:00
|
|
|
|
2018-08-27 10:45:31 +02:00
|
|
|
assert(head);
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(!next(tail));
|
|
|
|
if(!prev(tail))
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
this->head = nullptr;
|
|
|
|
this->tail = nullptr;
|
|
|
|
} else {
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(next(prev(tail)) == tail);
|
|
|
|
next(prev(tail)) = nullptr;
|
|
|
|
this->tail = prev(tail);
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
prev(tail) = nullptr;
|
|
|
|
next(tail) = nullptr;
|
2018-01-14 08:10:44 +01:00
|
|
|
return tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::ctx *
|
|
|
|
ircd::ctx::list::pop_front()
|
|
|
|
{
|
|
|
|
const auto head
|
|
|
|
{
|
|
|
|
this->head
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!head)
|
2018-08-27 10:45:31 +02:00
|
|
|
{
|
|
|
|
assert(!tail);
|
2018-01-14 08:10:44 +01:00
|
|
|
return head;
|
2018-08-27 10:45:31 +02:00
|
|
|
}
|
2018-01-14 08:10:44 +01:00
|
|
|
|
2018-08-27 10:45:31 +02:00
|
|
|
assert(tail);
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(!prev(head));
|
|
|
|
if(!next(head))
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
this->head = nullptr;
|
|
|
|
this->tail = nullptr;
|
|
|
|
} else {
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(prev(next(head)) == head);
|
|
|
|
prev(next(head)) = nullptr;
|
|
|
|
this->head = next(head);
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
prev(head) = nullptr;
|
|
|
|
next(head) = nullptr;
|
2018-01-14 08:10:44 +01:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::push_front(ctx *const &c)
|
|
|
|
{
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(next(c) == nullptr);
|
|
|
|
assert(prev(c) == nullptr);
|
2018-01-14 08:10:44 +01:00
|
|
|
|
|
|
|
if(!head)
|
|
|
|
{
|
2018-08-27 10:45:31 +02:00
|
|
|
assert(!tail);
|
2018-01-14 08:10:44 +01:00
|
|
|
head = c;
|
|
|
|
tail = c;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(prev(head) == nullptr);
|
|
|
|
prev(head) = c;
|
|
|
|
next(c) = head;
|
2018-01-14 08:10:44 +01:00
|
|
|
head = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::push_back(ctx *const &c)
|
|
|
|
{
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(next(c) == nullptr);
|
|
|
|
assert(prev(c) == nullptr);
|
2018-01-14 08:10:44 +01:00
|
|
|
|
|
|
|
if(!tail)
|
|
|
|
{
|
|
|
|
assert(!head);
|
|
|
|
head = c;
|
|
|
|
tail = c;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
assert(next(tail) == nullptr);
|
|
|
|
next(tail) = c;
|
|
|
|
prev(c) = tail;
|
2018-01-14 08:10:44 +01:00
|
|
|
tail = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::rfor_each(const std::function<void (ctx &)> &closure)
|
|
|
|
{
|
|
|
|
for(ctx *tail{this->tail}; tail; tail = prev(tail))
|
|
|
|
closure(*tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::rfor_each(const std::function<void (const ctx &)> &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const ctx *tail{this->tail}; tail; tail = prev(tail))
|
|
|
|
closure(*tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-04-19 23:47:56 +02:00
|
|
|
ircd::ctx::list::rfor_each(const std::function<bool (ctx &)> &closure)
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
for(ctx *tail{this->tail}; tail; tail = prev(tail))
|
|
|
|
if(!closure(*tail))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-04-19 23:47:56 +02:00
|
|
|
ircd::ctx::list::rfor_each(const std::function<bool (const ctx &)> &closure)
|
2018-01-14 08:10:44 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const ctx *tail{this->tail}; tail; tail = prev(tail))
|
|
|
|
if(!closure(*tail))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-19 23:47:56 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::list::for_each(const std::function<void (ctx &)> &closure)
|
|
|
|
{
|
|
|
|
for(ctx *head{this->head}; head; head = next(head))
|
|
|
|
closure(*head);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::for_each(const std::function<void (const ctx &)> &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const ctx *head{this->head}; head; head = next(head))
|
|
|
|
closure(*head);
|
|
|
|
}
|
|
|
|
|
2018-01-14 08:10:44 +01:00
|
|
|
bool
|
2018-04-19 23:47:56 +02:00
|
|
|
ircd::ctx::list::for_each(const std::function<bool (ctx &)> &closure)
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
for(ctx *head{this->head}; head; head = next(head))
|
|
|
|
if(!closure(*head))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-04-19 23:47:56 +02:00
|
|
|
ircd::ctx::list::for_each(const std::function<bool (const ctx &)> &closure)
|
2018-01-14 08:10:44 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const ctx *head{this->head}; head; head = next(head))
|
|
|
|
if(!closure(*head))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
ircd::ctx::ctx *&
|
2018-01-14 08:10:44 +01:00
|
|
|
ircd::ctx::list::prev(ctx *const &c)
|
|
|
|
{
|
|
|
|
assert(c);
|
2018-04-20 00:14:49 +02:00
|
|
|
return c->node.prev;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-20 00:14:49 +02:00
|
|
|
ircd::ctx::ctx *&
|
2018-01-14 08:10:44 +01:00
|
|
|
ircd::ctx::list::next(ctx *const &c)
|
|
|
|
{
|
|
|
|
assert(c);
|
2018-04-20 00:14:49 +02:00
|
|
|
return c->node.next;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 12:20:29 +02:00
|
|
|
const ircd::ctx::ctx *
|
2018-01-14 08:10:44 +01:00
|
|
|
ircd::ctx::list::prev(const ctx *const &c)
|
|
|
|
{
|
|
|
|
assert(c);
|
2018-04-28 12:20:29 +02:00
|
|
|
return c->node.prev;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
2018-04-28 12:20:29 +02:00
|
|
|
const ircd::ctx::ctx *
|
2018-01-14 08:10:44 +01:00
|
|
|
ircd::ctx::list::next(const ctx *const &c)
|
|
|
|
{
|
|
|
|
assert(c);
|
2018-04-28 12:20:29 +02:00
|
|
|
return c->node.next;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|