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-05-06 06:20:25 +02:00
|
|
|
#include "ctx.h"
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2019-07-24 22:21:48 +02:00
|
|
|
/// Dedicated log facility for the ircd::ctx subsystem.
|
|
|
|
decltype(ircd::ctx::log)
|
|
|
|
ircd::ctx::log
|
|
|
|
{
|
|
|
|
"ctx"
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ctx::ctx (internal)
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Allocator instance for the ctx instance_list. This allocator will place
|
|
|
|
/// the std::list nodes in the ctx struct itself.
|
2019-04-17 05:48:00 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::ctx::ctx>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::ctx::ctx>::allocator
|
|
|
|
{};
|
|
|
|
|
2019-07-24 22:21:48 +02:00
|
|
|
/// Instance list linkage for the list of all ctx instances. All ctxs can be
|
|
|
|
/// iterated through this list. The allocator makes the overhead of this list
|
|
|
|
/// negligible.
|
2018-04-20 00:37:58 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::ctx::ctx>::list)
|
|
|
|
ircd::util::instance_list<ircd::ctx::ctx>::list
|
2019-04-17 05:48:00 +02:00
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
2018-04-20 00:37:58 +02:00
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-07-24 22:21:48 +02:00
|
|
|
/// This is a pseudo ircd::ios descriptor. We want to account for a ctx's
|
|
|
|
/// execution slice in the ircd::ios handler list. This posits the entire
|
|
|
|
/// ircd::ctx system as one ircd::ios handler type among all the others.
|
|
|
|
/// At this time it is unclear how to hook a context's execution slice in the ircd::ios system.
|
2022-06-15 18:36:03 +02:00
|
|
|
[[clang::always_destroy]]
|
2019-07-22 02:55:58 +02:00
|
|
|
decltype(ircd::ctx::ctx::ios_desc)
|
|
|
|
ircd::ctx::ctx::ios_desc
|
|
|
|
{
|
2020-12-18 05:27:44 +01:00
|
|
|
"ircd.ctx.ctx"
|
2019-07-22 02:55:58 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 13:18:41 +02:00
|
|
|
/// This is a pseudo ircd::ios handler. See ios_desc
|
|
|
|
decltype(ircd::ctx::ctx::ios_handler)
|
|
|
|
ircd::ctx::ctx::ios_handler
|
|
|
|
{
|
|
|
|
&ios_desc
|
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
/// Points to the next context to spawn (internal use)
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2020-10-14 10:34:23 +02:00
|
|
|
decltype(ircd::ctx::ctx::spawning)
|
|
|
|
ircd::ctx::ctx::spawning;
|
|
|
|
|
2020-10-28 06:51:14 +01:00
|
|
|
/// Used to notify of context completion
|
|
|
|
[[gnu::visibility("hidden")]]
|
|
|
|
decltype(ircd::ctx::ctx::adjoindre)
|
|
|
|
ircd::ctx::ctx::adjoindre;
|
|
|
|
|
2019-09-23 01:44:34 +02:00
|
|
|
/// Internal context struct ctor
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2019-09-23 01:44:34 +02:00
|
|
|
ircd::ctx::ctx::ctx(const string_view &name,
|
2020-10-14 10:34:23 +02:00
|
|
|
const ircd::ctx::stack &stack,
|
2020-02-27 19:11:59 +01:00
|
|
|
const context::flags &flags)
|
2020-11-13 10:43:44 +01:00
|
|
|
:flags
|
2020-02-27 19:11:59 +01:00
|
|
|
{
|
|
|
|
flags
|
|
|
|
}
|
|
|
|
,alarm
|
|
|
|
{
|
|
|
|
ios::get()
|
|
|
|
}
|
|
|
|
,stack
|
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
stack
|
2020-02-27 19:11:59 +01:00
|
|
|
}
|
2019-09-23 01:44:34 +02:00
|
|
|
{
|
2020-11-13 10:43:44 +01:00
|
|
|
strlcpy(this->name, name);
|
2019-09-23 01:44:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2019-07-24 22:21:48 +02:00
|
|
|
ircd::ctx::ctx::~ctx()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(yc == nullptr); // Check that the context isn't active.
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:25:22 +02:00
|
|
|
/// Internal wrapper for asio::spawn; never call directly.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2018-08-21 17:10:37 +02:00
|
|
|
void
|
2018-12-22 22:01:13 +01:00
|
|
|
IRCD_CTX_STACK_PROTECT
|
2019-07-24 22:21:48 +02:00
|
|
|
ircd::ctx::ctx::spawn(context::function func)
|
2018-08-21 17:10:37 +02:00
|
|
|
{
|
|
|
|
const boost::coroutines::attributes attrs
|
|
|
|
{
|
2019-04-06 00:56:05 +02:00
|
|
|
// Pass the requested stack size
|
2019-07-24 22:21:48 +02:00
|
|
|
stack.max,
|
2019-04-06 00:56:05 +02:00
|
|
|
|
|
|
|
// We ensure stack unwinding and cleanup out here instead.
|
|
|
|
boost::coroutines::no_stack_unwind,
|
2018-08-21 17:10:37 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
const scope_restore spawning
|
|
|
|
{
|
|
|
|
ircd::ctx::ctx::spawning, this
|
|
|
|
};
|
|
|
|
|
2018-08-21 17:10:37 +02:00
|
|
|
auto bound
|
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
std::bind(&ctx::operator(), this, ph::_1, std::move(func))
|
2018-08-21 17:10:37 +02:00
|
|
|
};
|
|
|
|
|
2020-12-13 03:06:09 +01:00
|
|
|
const auto parent_context
|
2020-08-21 13:18:41 +02:00
|
|
|
{
|
|
|
|
ircd::ctx::current
|
|
|
|
};
|
|
|
|
|
2020-12-13 03:06:09 +01:00
|
|
|
const auto parent_handler
|
2020-08-21 13:18:41 +02:00
|
|
|
{
|
|
|
|
ircd::ios::handler::current
|
|
|
|
};
|
|
|
|
|
2020-12-13 03:06:09 +01:00
|
|
|
assert(!parent_context && parent_handler); try
|
2020-08-21 13:18:41 +02:00
|
|
|
{
|
2020-12-13 03:06:09 +01:00
|
|
|
assert(!ircd::ctx::current && ios::handler::current);
|
2020-08-21 13:18:41 +02:00
|
|
|
ios::handler::leave(parent_handler);
|
2020-12-13 03:06:09 +01:00
|
|
|
|
|
|
|
assert(!ircd::ctx::current && !ios::handler::current);
|
|
|
|
boost::asio::spawn(ios::get(), ios::handle(ios_desc, std::move(bound)), attrs);
|
|
|
|
|
|
|
|
assert(!ircd::ctx::current && !ios::handler::current);
|
2020-08-21 13:18:41 +02:00
|
|
|
ios::handler::enter(parent_handler);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2020-12-13 03:06:09 +01:00
|
|
|
assert(!ircd::ctx::current && !ios::handler::current);
|
2020-08-21 13:18:41 +02:00
|
|
|
ios::handler::enter(parent_handler);
|
2020-12-13 03:06:09 +01:00
|
|
|
|
|
|
|
assert(!ircd::ctx::current && ios::handler::current == parent_handler);
|
2020-08-21 13:18:41 +02:00
|
|
|
throw;
|
|
|
|
}
|
2020-12-13 03:06:09 +01:00
|
|
|
|
|
|
|
assert(!ircd::ctx::current && ios::handler::current == parent_handler);
|
2018-08-20 03:09:04 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2016-11-29 16:23:38 +01:00
|
|
|
void
|
2018-12-22 22:01:13 +01:00
|
|
|
IRCD_CTX_STACK_PROTECT
|
2016-11-29 16:23:38 +01:00
|
|
|
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
|
|
|
{
|
2019-09-23 02:40:15 +02:00
|
|
|
assert(!ircd::ctx::current);
|
2018-12-22 22:01:40 +01:00
|
|
|
ircd::ctx::current = this;
|
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));
|
2019-03-30 21:07:52 +01:00
|
|
|
const unwind atexit{[this]
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-05-23 03:06:18 +02:00
|
|
|
adjoindre.notify_all();
|
2019-08-28 04:02:40 +02:00
|
|
|
stack.at = 0;
|
|
|
|
notes = 0;
|
2016-11-29 16:23:38 +01:00
|
|
|
this->yc = nullptr;
|
2018-12-22 22:01:40 +01:00
|
|
|
ircd::ctx::current = nullptr;
|
2019-03-30 21:07:52 +01:00
|
|
|
if(flags & context::DETACH && !std::uncaught_exceptions())
|
2016-11-29 16:23:38 +01:00
|
|
|
delete this;
|
2019-03-30 21:07:52 +01:00
|
|
|
}};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-24 23:46:41 +01:00
|
|
|
// Check for a precocious interrupt
|
2018-12-22 03:21:34 +01:00
|
|
|
interruption_point();
|
2017-03-24 23:46:41 +01:00
|
|
|
|
2020-10-09 02:54:01 +02:00
|
|
|
// Mark the point of context entry only after the interrupt check. If the
|
|
|
|
// context was interrupted without ever entering (which makes the above
|
|
|
|
// check throw) we never record any execution slice or increment the epoch
|
|
|
|
// counter for it. This can allow a parent context to assume application
|
|
|
|
// state remains unmodified by the aborted context.
|
|
|
|
mark(prof::event::ENTER);
|
|
|
|
const unwind leaver{[this]
|
|
|
|
{
|
|
|
|
mark(prof::event::LEAVE);
|
|
|
|
}};
|
|
|
|
|
2018-12-22 03:21:34 +01:00
|
|
|
// Call the user's function.
|
|
|
|
func();
|
2019-03-26 22:50:15 +01:00
|
|
|
|
|
|
|
assert(!std::uncaught_exceptions());
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2018-05-06 10:15:26 +02:00
|
|
|
catch(const ircd::ctx::interrupted &)
|
|
|
|
{
|
2019-03-26 22:50:15 +01:00
|
|
|
assert(!std::uncaught_exceptions());
|
2019-03-30 21:07:52 +01:00
|
|
|
if(flags & context::DETACH)
|
|
|
|
delete this;
|
2018-05-06 10:15:26 +02:00
|
|
|
}
|
|
|
|
catch(const ircd::ctx::terminated &)
|
|
|
|
{
|
2019-03-26 22:50:15 +01:00
|
|
|
assert(!std::uncaught_exceptions());
|
2019-03-30 21:07:52 +01:00
|
|
|
if(flags & context::DETACH)
|
|
|
|
delete this;
|
2018-05-06 10:15:26 +02:00
|
|
|
}
|
2018-03-17 04:25:56 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
2019-02-07 08:32:49 +01:00
|
|
|
log, "ctx('%s' id:%u): unhandled: %s",
|
2018-03-17 04:25:56 +01:00
|
|
|
name,
|
|
|
|
id,
|
|
|
|
e.what()
|
|
|
|
};
|
2019-03-30 21:07:52 +01:00
|
|
|
|
|
|
|
assert(!std::uncaught_exceptions());
|
|
|
|
if(flags & context::DETACH)
|
|
|
|
delete this;
|
2018-03-17 04:25:56 +01:00
|
|
|
}
|
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.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2018-12-07 21:03:25 +01:00
|
|
|
void
|
2018-12-22 22:01:13 +01:00
|
|
|
IRCD_CTX_STACK_PROTECT
|
2018-12-07 21:03:25 +01:00
|
|
|
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
|
2018-12-23 05:55:52 +01:00
|
|
|
current->notes = 0; // Unconditionally cleared here
|
|
|
|
continuation
|
2018-12-07 21:03:25 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
continuation::false_predicate, continuation::noop_interruptor, [&target]
|
2019-10-11 03:18:21 +02:00
|
|
|
(auto &yield) noexcept
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
target();
|
|
|
|
}
|
|
|
|
};
|
2018-12-07 21:03:25 +01:00
|
|
|
|
|
|
|
assert(current != this);
|
|
|
|
assert(current->notes == 1); // notes = 1; set by continuation dtor on wakeup
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2017-03-27 23:56:05 +02:00
|
|
|
bool
|
2018-12-22 22:01:13 +01:00
|
|
|
IRCD_CTX_STACK_PROTECT
|
2017-03-27 23:56:05 +02:00
|
|
|
ircd::ctx::ctx::wait()
|
|
|
|
{
|
|
|
|
namespace errc = boost::system::errc;
|
|
|
|
|
|
|
|
assert(this->yc);
|
|
|
|
assert(current == this);
|
2019-10-11 04:55:15 +02:00
|
|
|
assert(notes == 1);
|
2017-03-27 23:56:05 +02:00
|
|
|
|
2019-10-11 04:55:15 +02:00
|
|
|
// Clear the notification counter.
|
|
|
|
notes = 0;
|
2018-03-23 23:35:09 +01:00
|
|
|
|
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.
|
2020-07-12 00:28:24 +02:00
|
|
|
const predicate &predicate{[this]()
|
|
|
|
noexcept
|
2018-12-07 21:05:27 +01:00
|
|
|
{
|
|
|
|
return notes > 0;
|
|
|
|
}};
|
|
|
|
|
2019-10-11 04:55:15 +02:00
|
|
|
// An interrupt invokes this closure to force the alarm to return.
|
|
|
|
const interruptor &interruptor{[this]
|
2020-07-12 00:28:24 +02:00
|
|
|
(ctx *const &interruptor)
|
|
|
|
noexcept
|
2019-10-11 04:55:15 +02:00
|
|
|
{
|
|
|
|
wake();
|
|
|
|
}};
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
// 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.
|
2018-12-23 05:55:52 +01:00
|
|
|
boost::system::error_code ec; continuation
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2019-10-10 08:55:49 +02:00
|
|
|
predicate, interruptor, [this, &ec]
|
2020-07-12 00:28:24 +02:00
|
|
|
(auto &yield)
|
|
|
|
noexcept
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
2018-12-23 05:55:52 +01:00
|
|
|
alarm.async_wait(yield[ec]);
|
2018-12-23 05:11:00 +01:00
|
|
|
}
|
2018-12-23 05:55:52 +01:00
|
|
|
};
|
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
|
|
|
|
|
|
|
|
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.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::note()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
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)
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2018-12-07 21:03:25 +01:00
|
|
|
bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::ctx::wake()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept try
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2020-12-13 05:23:22 +01:00
|
|
|
if constexpr(ios::profile::logging)
|
|
|
|
{
|
|
|
|
assert(ios_desc.stats);
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
ios::log, log::level::DEBUG,
|
|
|
|
"QUEUE %5u %-30s [%11lu] ------[%9lu] q:%-4lu id:%-5u %-30s",
|
|
|
|
ios_desc.id,
|
|
|
|
trunc(ios_desc.name, 30),
|
2020-12-18 05:29:00 +01:00
|
|
|
uint64_t(ios_desc.stats->calls),
|
2020-12-13 05:23:22 +01:00
|
|
|
notes,
|
2020-12-18 05:29:00 +01:00
|
|
|
uint64_t(ios_desc.stats->queued),
|
2020-12-13 05:23:22 +01:00
|
|
|
id,
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
alarm.cancel();
|
2018-12-07 21:03:25 +01:00
|
|
|
return true;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
2019-04-25 01:05:01 +02:00
|
|
|
catch(const std::exception &e)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2019-04-25 01:05:01 +02:00
|
|
|
log::critical
|
2018-03-23 23:35:09 +01:00
|
|
|
{
|
2019-02-07 08:32:49 +01:00
|
|
|
log, "ctx::wake(%p): %s", this, e.what()
|
2018-03-23 23:35:09 +01:00
|
|
|
};
|
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.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2017-03-27 23:56:05 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::ctx::interruption_point()
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2019-09-22 21:49:56 +02:00
|
|
|
if(unlikely(interruption()))
|
2018-05-06 10:00:11 +02:00
|
|
|
{
|
2019-09-22 21:49:56 +02:00
|
|
|
if(termination_point(std::nothrow))
|
|
|
|
throw terminated{};
|
|
|
|
|
|
|
|
if(likely(interruption_point(std::nothrow)))
|
|
|
|
throw interrupted
|
|
|
|
{
|
|
|
|
"ctx:%lu '%s'", id, name
|
|
|
|
};
|
|
|
|
}
|
2018-05-06 10:00:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:19:02 +01:00
|
|
|
/// Returns true if this context has been flagged for termination. Does not
|
|
|
|
/// clear the flag. Sets the NOINTERRUPT flag so the context cannot be further
|
|
|
|
// interrupted which simplifies the termination process.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2018-05-06 10:00:11 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::termination_point(std::nothrow_t)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-05-06 10:00:11 +02:00
|
|
|
{
|
2019-09-22 21:49:56 +02:00
|
|
|
if(unlikely(flags & context::TERMINATED))
|
2018-05-06 10:00:11 +02:00
|
|
|
{
|
2018-12-22 22:19:02 +01:00
|
|
|
assert(~flags & context::NOINTERRUPT);
|
2018-12-16 02:02:50 +01:00
|
|
|
flags |= context::NOINTERRUPT;
|
2018-12-22 23:53:23 +01:00
|
|
|
mark(prof::event::TERMINATE);
|
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.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2017-03-27 23:56:05 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::interruption_point(std::nothrow_t)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2017-03-27 23:56:05 +02:00
|
|
|
{
|
2019-09-22 21:49:56 +02:00
|
|
|
if(unlikely(flags & context::INTERRUPTED))
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-12-22 22:19:02 +01:00
|
|
|
assert(~flags & context::NOINTERRUPT);
|
2016-11-29 16:23:38 +01:00
|
|
|
flags &= ~context::INTERRUPTED;
|
2018-12-22 23:53:23 +01:00
|
|
|
mark(prof::event::INTERRUPT);
|
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
|
|
|
|
2019-09-22 21:49:56 +02:00
|
|
|
/// True if this context has been flagged for interruption or termination
|
|
|
|
/// and interrupts are not blocked.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2019-09-22 21:49:56 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::interruption()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
static const auto &flags
|
|
|
|
{
|
|
|
|
context::TERMINATED | context::INTERRUPTED
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fast test-and-bail for the very likely case there is no interrupt.
|
|
|
|
if(likely((this->flags & flags) == 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The NOINTERRUPT flag works by pretending there is no interrupt flag
|
|
|
|
// set and also does not clear the flag. This allows the interrupt
|
|
|
|
// to remain pending until the uninterruptible section is complete.
|
|
|
|
if(this->flags & context::NOINTERRUPT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2018-08-20 01:21:33 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::started()
|
2019-08-06 01:15:56 +02:00
|
|
|
const noexcept
|
2018-08-20 01:21:33 +02:00
|
|
|
{
|
|
|
|
return stack.base != 0;
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::visibility("hidden"), gnu::hot]]
|
2018-08-20 01:21:33 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::ctx::finished()
|
2019-08-06 01:15:56 +02:00
|
|
|
const noexcept
|
2018-08-20 01:21:33 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2019-05-26 11:18:14 +02:00
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::epoch()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2019-05-26 11:18:14 +02:00
|
|
|
{
|
2020-10-09 01:45:24 +02:00
|
|
|
return ctx::ios_handler.epoch;
|
2019-05-26 11:18:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 05:50:22 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::for_each(const std::function<bool (ctx &)> &closure)
|
|
|
|
{
|
|
|
|
for(auto &ctx : ctx::list)
|
|
|
|
if(!closure(*ctx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Yield to context `ctx`.
|
|
|
|
///
|
|
|
|
///
|
2019-10-10 04:01:45 +02:00
|
|
|
[[gnu::hot]]
|
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 !!!
|
|
|
|
|
2019-10-10 04:01:45 +02:00
|
|
|
ctx.note();
|
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)
|
|
|
|
{
|
2020-07-12 00:28:24 +02:00
|
|
|
signal(ctx, [&ctx]()
|
|
|
|
noexcept
|
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)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return ctx.note();
|
|
|
|
}
|
|
|
|
|
2020-12-20 14:13:18 +01:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
[[gnu::visibility("hidden")]]
|
|
|
|
extern ios::descriptor signal_desc;
|
|
|
|
}
|
|
|
|
|
2022-06-15 18:36:03 +02:00
|
|
|
[[clang::always_destroy]]
|
2020-12-20 14:13:18 +01:00
|
|
|
decltype(ircd::ctx::signal_desc)
|
|
|
|
ircd::ctx::signal_desc
|
|
|
|
{
|
|
|
|
"ircd.ctx.signal"
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
{
|
2020-12-20 14:13:18 +01:00
|
|
|
ios::dispatch
|
|
|
|
{
|
|
|
|
signal_desc, ios::defer, std::move(func)
|
|
|
|
};
|
2017-04-03 05:52:30 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if(likely(&ctx != current && ctx.cont != nullptr))
|
2018-12-23 01:15:25 +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.
|
2019-01-18 17:55:06 +01:00
|
|
|
///
|
|
|
|
/// NOTE: If the IRCd run::level is QUIT, an interrupt() becomes a terminate().
|
2016-11-29 16:23:38 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::interrupt(ctx &ctx)
|
|
|
|
{
|
2019-01-18 17:55:06 +01:00
|
|
|
if(unlikely(run::level == run::level::QUIT))
|
2018-05-06 10:00:11 +02:00
|
|
|
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-03-23 23:35:09 +01:00
|
|
|
if(likely(&ctx != current && ctx.cont != nullptr))
|
2018-12-23 01:15:25 +01:00
|
|
|
(*ctx.cont->intr)(current);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 10:43:44 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::name(ctx &ctx,
|
|
|
|
const string_view &name)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
strlcpy(ctx.name, name);
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:40:37 +01:00
|
|
|
int8_t
|
|
|
|
ircd::ctx::nice(ctx &ctx,
|
|
|
|
const int8_t &val)
|
2020-05-19 04:13:53 +02:00
|
|
|
noexcept
|
2020-01-06 20:40:37 +01:00
|
|
|
{
|
|
|
|
ctx.nice = val;
|
|
|
|
return ctx.nice;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
ircd::ctx::ionice(ctx &ctx,
|
|
|
|
const int8_t &val)
|
2020-05-19 04:13:53 +02:00
|
|
|
noexcept
|
2020-01-06 20:40:37 +01:00
|
|
|
{
|
|
|
|
ctx.ionice = val;
|
|
|
|
return ctx.ionice;
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:04:31 +02:00
|
|
|
/// Returns writable reference to the flags of ctx
|
|
|
|
[[gnu::hot]]
|
|
|
|
uint32_t &
|
|
|
|
ircd::ctx::flags(ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return ctx.flags;
|
|
|
|
}
|
|
|
|
|
2019-07-22 03:07:26 +02:00
|
|
|
/// !running() && notes > 0
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2019-07-22 03:07:26 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::queued(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2019-07-22 03:07:26 +02:00
|
|
|
{
|
|
|
|
return !running(ctx) && notes(ctx) > 0;
|
|
|
|
}
|
|
|
|
|
2018-04-20 00:48:14 +02:00
|
|
|
/// started() && !finished() && !running
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-04-20 00:48:14 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::waiting(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-04-20 00:48:14 +02:00
|
|
|
{
|
|
|
|
return started(ctx) && !finished(ctx) && !running(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if `ctx` is the current ctx
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-04-20 00:48:14 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::running(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-04-20 00:48:14 +02:00
|
|
|
{
|
|
|
|
return &ctx == current;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Indicates if `ctx` was ever jumped to
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::started(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return ctx.started();
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Indicates if the base frame for `ctx` returned
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2016-11-29 16:23:38 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::finished(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return ctx.finished();
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:40:37 +01:00
|
|
|
/// Returns the IO priority nice-value
|
|
|
|
[[gnu::hot]]
|
|
|
|
const int8_t &
|
|
|
|
ircd::ctx::ionice(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return ctx.ionice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the context scheduling priority nice-value
|
|
|
|
[[gnu::hot]]
|
|
|
|
const int8_t &
|
|
|
|
ircd::ctx::nice(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return ctx.nice;
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:09:20 +02:00
|
|
|
/// Returns the notification count for `ctx`
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
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)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return ctx.notes;
|
|
|
|
}
|
|
|
|
|
2020-05-19 04:04:31 +02:00
|
|
|
/// Returns reference to the flags of ctx
|
|
|
|
[[gnu::hot]]
|
|
|
|
const uint32_t &
|
|
|
|
ircd::ctx::flags(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return ctx.flags;
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:43:11 +02:00
|
|
|
/// Returns the developer's optional name literal for `ctx`
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::ctx::name(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
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`)
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2016-11-29 16:23:38 +01:00
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::id(const ctx &ctx)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return ctx.id;
|
|
|
|
}
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/this_ctx.h
|
|
|
|
//
|
|
|
|
|
2022-06-15 18:36:03 +02:00
|
|
|
[[clang::always_destroy]]
|
2020-10-16 14:13:24 +02:00
|
|
|
decltype(ircd::ctx::this_ctx::courtesy_yield_desc)
|
|
|
|
ircd::ctx::this_ctx::courtesy_yield_desc
|
|
|
|
{
|
2020-12-20 14:13:18 +01:00
|
|
|
"ircd.ctx.courtesy_yield",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
true,
|
2020-10-16 14:13:24 +02:00
|
|
|
};
|
|
|
|
|
2018-03-27 01:20:47 +02:00
|
|
|
// set by the continuation object and the base frame.
|
2020-02-11 21:17:59 +01:00
|
|
|
thread_local
|
|
|
|
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
|
2019-09-23 05:02:23 +02:00
|
|
|
ircd::ctx::this_ctx::sleep_until(const system_point &tp)
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
while(!wait_until(tp, std::nothrow));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 &)
|
|
|
|
{
|
2019-09-23 05:02:23 +02:00
|
|
|
const boost::posix_time::microseconds ptime_duration
|
|
|
|
{
|
|
|
|
duration.count()
|
|
|
|
};
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
auto &c(cur());
|
2019-09-23 05:02:23 +02:00
|
|
|
c.alarm.expires_from_now(ptime_duration);
|
2018-03-25 08:50:39 +02:00
|
|
|
c.wait(); // now you're yielding with portals
|
2019-09-23 05:02:23 +02:00
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
c.alarm.expires_from_now()
|
|
|
|
};
|
2018-03-25 08:50:39 +02:00
|
|
|
|
|
|
|
// return remaining duration.
|
|
|
|
// this is > 0 if notified
|
|
|
|
// this is unchanged if a note prevented any wait at all
|
2019-09-23 05:02:23 +02:00
|
|
|
return microseconds(ret.total_microseconds());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
ircd::ctx::this_ctx::wait_until(const system_point &tp,
|
|
|
|
const std::nothrow_t &)
|
|
|
|
{
|
|
|
|
const auto &diff
|
|
|
|
{
|
|
|
|
tp - now<system_point>()
|
|
|
|
};
|
|
|
|
|
|
|
|
const boost::posix_time::microseconds duration
|
|
|
|
{
|
|
|
|
duration_cast<microseconds>(diff).count()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &expires_at
|
|
|
|
{
|
|
|
|
boost::posix_time::microsec_clock::universal_time() + duration
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &c(cur());
|
|
|
|
c.alarm.expires_at(expires_at);
|
|
|
|
c.wait(); // now you're yielding with portals
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
c.alarm.expires_from_now()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret <= boost::posix_time::microseconds(0);
|
2018-03-25 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Yield the currently running context until notified.
|
2019-10-10 04:01:45 +02:00
|
|
|
[[gnu::hot]]
|
2018-03-25 08:50:39 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::wait()
|
|
|
|
{
|
|
|
|
auto &c(cur());
|
2019-09-23 05:02:23 +02:00
|
|
|
c.alarm.expires_at(boost::posix_time::pos_infin);
|
2018-03-25 08:50:39 +02:00
|
|
|
c.wait(); // now you're yielding with portals
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::hot, gnu::noinline]]
|
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-03-25 08:50:39 +02:00
|
|
|
/// Throws interrupted if the currently running context was interrupted
|
|
|
|
/// and clears the interrupt flag.
|
2022-05-26 21:28:28 +02:00
|
|
|
[[gnu::hot]]
|
2018-03-25 08:50:39 +02:00
|
|
|
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.
|
2019-09-13 02:57:22 +02:00
|
|
|
//assert(interruptible());
|
2018-03-25 08:50:39 +02:00
|
|
|
return cur().interruption_point();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns unique ID of currently running context
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-03-25 08:50:39 +02:00
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::this_ctx::id()
|
2019-08-14 04:42:31 +02:00
|
|
|
noexcept
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
static const uint64_t zero{0};
|
|
|
|
return current? id(cur()) : zero;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2019-09-23 02:28:41 +02:00
|
|
|
extern thread_local bool critical_asserted;
|
2017-09-28 03:29:29 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 02:28:41 +02:00
|
|
|
decltype(ircd::ctx::critical_asserted)
|
|
|
|
thread_local
|
|
|
|
ircd::ctx::critical_asserted;
|
|
|
|
|
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
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::cold]]
|
2018-11-30 01:12:33 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::assert_critical()
|
|
|
|
{
|
|
|
|
if(unlikely(critical_asserted))
|
2019-01-14 00:50:04 +01:00
|
|
|
throw panic
|
2018-11-30 01:12:33 +01:00
|
|
|
{
|
|
|
|
"%lu '%s' :Illegal context switch", id(), name()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
2019-04-10 23:50:34 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-18 01:00:22 +01:00
|
|
|
//
|
|
|
|
// ctx/slice_usage_warning.h
|
|
|
|
//
|
|
|
|
|
2020-05-25 01:34:04 +02:00
|
|
|
#ifdef RB_DEBUG
|
2018-12-18 01:00:22 +01:00
|
|
|
ircd::ctx::this_ctx::slice_usage_warning::slice_usage_warning(const string_view &fmt,
|
|
|
|
va_rtti &&ap)
|
|
|
|
:fmt
|
|
|
|
{
|
|
|
|
fmt
|
|
|
|
}
|
|
|
|
,ap
|
|
|
|
{
|
|
|
|
std::move(ap)
|
|
|
|
}
|
2020-05-25 01:43:56 +02:00
|
|
|
,epoch
|
|
|
|
{
|
|
|
|
current?
|
|
|
|
ircd::ctx::epoch(cur()):
|
|
|
|
ircd::ctx::epoch()
|
|
|
|
}
|
2018-12-18 01:00:22 +01:00
|
|
|
,start
|
|
|
|
{
|
|
|
|
// Set the start value to the total number of cycles accrued by this
|
|
|
|
// context including the current time slice.
|
2019-02-12 19:50:16 +01:00
|
|
|
!current?
|
2019-04-02 20:09:37 +02:00
|
|
|
prof::cycles():
|
2018-12-19 03:08:39 +01:00
|
|
|
~cur().flags & context::SLICE_EXEMPT?
|
2019-04-13 00:57:34 +02:00
|
|
|
prof::get(cur(), prof::event::CYCLES) + prof::cur_slice_cycles():
|
2018-12-19 03:08:39 +01:00
|
|
|
0
|
2018-12-18 01:00:22 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-25 01:34:04 +02:00
|
|
|
#ifdef RB_DEBUG
|
2018-12-18 01:00:22 +01:00
|
|
|
ircd::ctx::this_ctx::slice_usage_warning::~slice_usage_warning()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-02-12 19:50:16 +01:00
|
|
|
if(current && cur().flags & context::SLICE_EXEMPT)
|
2018-12-19 03:08:39 +01:00
|
|
|
return;
|
|
|
|
|
2018-12-18 01:00:22 +01:00
|
|
|
// Set the final value by first adding the total number of cycles ever
|
|
|
|
// for this context to the current time slice. Then subtract the start
|
|
|
|
// sample. This way we're only counting the execution time of this context
|
|
|
|
// and not counting any time while it's yielding. A simple difference of
|
|
|
|
// two rdtsc() samples would be insufficient.
|
|
|
|
|
|
|
|
const auto stop
|
|
|
|
{
|
2019-02-12 19:50:16 +01:00
|
|
|
current?
|
2019-04-13 00:57:34 +02:00
|
|
|
prof::get(cur(), prof::event::CYCLES) + prof::cur_slice_cycles():
|
2019-04-02 20:09:37 +02:00
|
|
|
prof::cycles()
|
2018-12-18 01:00:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(stop >= start);
|
|
|
|
const auto total(stop - start);
|
2019-02-12 19:50:16 +01:00
|
|
|
if(likely(!prof::slice_exceeded_warning(total)))
|
2018-12-18 01:00:22 +01:00
|
|
|
return;
|
|
|
|
|
2020-05-25 01:43:56 +02:00
|
|
|
const auto span
|
|
|
|
{
|
|
|
|
current?
|
|
|
|
ircd::ctx::epoch(cur()) - this->epoch:
|
|
|
|
ircd::ctx::epoch() - this->epoch
|
|
|
|
};
|
|
|
|
|
2018-12-18 01:00:22 +01:00
|
|
|
thread_local char buf[256];
|
|
|
|
const string_view reason{fmt::vsprintf
|
|
|
|
{
|
|
|
|
buf, fmt, ap
|
|
|
|
}};
|
|
|
|
|
|
|
|
const ulong &threshold{prof::settings::slice_warning};
|
|
|
|
log::dwarning
|
|
|
|
{
|
2020-05-25 01:47:03 +02:00
|
|
|
prof::watchdog, "timeslice excessive; lim:%lu this:%lu pct:%.2lf span:%lu :%s",
|
2018-12-18 01:00:22 +01:00
|
|
|
threshold,
|
|
|
|
total,
|
|
|
|
(double(total) / double(threshold)) * 100.0,
|
2020-05-25 01:43:56 +02:00
|
|
|
span,
|
2018-12-18 01:00:22 +01:00
|
|
|
reason
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/continuation.h
|
|
|
|
//
|
|
|
|
|
2018-12-23 05:11:00 +01:00
|
|
|
decltype(ircd::ctx::continuation::true_predicate)
|
2020-07-12 00:28:24 +02:00
|
|
|
ircd::ctx::continuation::asio_predicate{[]()
|
|
|
|
noexcept -> bool
|
2018-12-23 05:11:00 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}};
|
|
|
|
|
2018-12-07 21:05:27 +01:00
|
|
|
decltype(ircd::ctx::continuation::true_predicate)
|
2020-07-12 00:28:24 +02:00
|
|
|
ircd::ctx::continuation::true_predicate{[]()
|
|
|
|
noexcept -> bool
|
2018-12-07 21:05:27 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
|
|
|
|
decltype(ircd::ctx::continuation::false_predicate)
|
2020-07-12 00:28:24 +02:00
|
|
|
ircd::ctx::continuation::false_predicate{[]()
|
|
|
|
noexcept -> bool
|
2018-12-07 21:05:27 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}};
|
|
|
|
|
|
|
|
decltype(ircd::ctx::continuation::noop_interruptor)
|
|
|
|
ircd::ctx::continuation::noop_interruptor{[]
|
2020-07-12 00:28:24 +02:00
|
|
|
(ctx *const &interruptor)
|
|
|
|
noexcept -> void
|
2018-12-07 21:05:27 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}};
|
|
|
|
|
2019-09-10 19:19:50 +02:00
|
|
|
[[gnu::hot]]
|
2019-10-11 03:18:21 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::continuation::leave()
|
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-28 03:29:29 +02:00
|
|
|
assert(self != nullptr);
|
|
|
|
assert(self->notes <= 1);
|
2018-03-24 23:53:15 +01:00
|
|
|
|
2018-12-17 20:49:38 +01:00
|
|
|
// Check here if the developer has placed a critical assertion on the stack
|
|
|
|
// but this yield is still occuring under its scope. That's bad.
|
|
|
|
assert_critical();
|
|
|
|
assert(!critical_asserted);
|
|
|
|
|
2019-07-22 00:26:44 +02:00
|
|
|
// Confirming the uncaught exception count was saved and set to zero in the
|
|
|
|
// initializer list.
|
|
|
|
assert(!std::uncaught_exceptions());
|
|
|
|
|
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());
|
|
|
|
|
2019-09-22 21:51:49 +02:00
|
|
|
// Check that we saved a valid context reference to this object for later.
|
|
|
|
assert(self->yc);
|
|
|
|
|
2018-12-17 20:49:38 +01:00
|
|
|
// Point to this continuation instance (which is on the context's stack)
|
|
|
|
// from the context's instance. This allows its features to be accessed
|
|
|
|
// while the context is asleep (i.e interruptor and predicate functions).
|
2018-12-22 22:36:48 +01:00
|
|
|
// NOTE that this pointer is not ever null'ed after being set here. It will
|
|
|
|
// remain invalid once the context resumes. You know if this is a valid
|
|
|
|
// pointer because the context is asleep; otherwise it's a trash value.
|
2018-03-23 23:35:09 +01:00
|
|
|
self->cont = this;
|
2018-12-17 20:49:38 +01:00
|
|
|
|
2018-12-22 22:36:48 +01:00
|
|
|
// Tell the profiler this is the point where the context has concluded
|
|
|
|
// its execution run and is now yielding.
|
2018-12-22 23:53:23 +01:00
|
|
|
mark(prof::event::YIELD);
|
2018-12-22 22:36:48 +01:00
|
|
|
|
2018-12-17 20:49:38 +01:00
|
|
|
// Null the fundamental current context register as the last operation
|
|
|
|
// during execution before yielding. When a context resumes it will
|
|
|
|
// restore this register; otherwise it remains null for executions on
|
|
|
|
// the program's main stack.
|
2019-09-22 21:51:49 +02:00
|
|
|
ircd::ctx::current = nullptr;
|
2019-10-11 03:18:21 +02:00
|
|
|
}
|
2019-09-22 21:51:49 +02:00
|
|
|
|
2019-10-11 03:18:21 +02:00
|
|
|
[[gnu::hot]]
|
|
|
|
void
|
|
|
|
ircd::ctx::continuation::enter()
|
|
|
|
{
|
2019-09-22 21:51:49 +02:00
|
|
|
// Restore the current context register.
|
2017-09-28 03:29:29 +02:00
|
|
|
ircd::ctx::current = self;
|
2018-12-17 20:49:38 +01:00
|
|
|
|
2019-09-22 21:51:49 +02:00
|
|
|
// Unconditionally reset the notes counter to 1 because we're awake now.
|
|
|
|
self->notes = 1;
|
|
|
|
|
|
|
|
// Restore exception state
|
2019-03-23 02:50:57 +01:00
|
|
|
assert(std::uncaught_exceptions() == 0);
|
2019-07-22 00:26:44 +02:00
|
|
|
exception_handler::uncaught_exceptions(uncaught_exceptions);
|
2019-03-23 02:50:57 +01:00
|
|
|
|
2019-09-22 21:51:49 +02:00
|
|
|
// self->continuation is not null'ed here; it remains an invalid
|
|
|
|
// pointer while the context is awake.
|
|
|
|
|
2018-12-17 20:49:38 +01:00
|
|
|
// Tell the profiler this is the point where the context is now resuming.
|
2018-12-22 23:53:23 +01:00
|
|
|
mark(prof::event::CONTINUE);
|
2018-03-23 23:35:09 +01:00
|
|
|
|
2019-09-22 21:51:49 +02:00
|
|
|
// Check for an interrupt or termination that was sent while asleep.
|
|
|
|
if(unlikely(self->interruption()))
|
|
|
|
{
|
|
|
|
self->interruption_point();
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
2018-03-23 23:35:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-23 01:15:25 +01:00
|
|
|
ircd::ctx::continuation::operator
|
|
|
|
boost::asio::yield_context &()
|
|
|
|
noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-12-23 01:15:25 +01:00
|
|
|
assert(self);
|
|
|
|
assert(self->yc);
|
2016-11-29 16:23:38 +01:00
|
|
|
return *self->yc;
|
|
|
|
}
|
|
|
|
|
2018-12-23 01:15:25 +01:00
|
|
|
ircd::ctx::continuation::operator
|
|
|
|
const boost::asio::yield_context &()
|
|
|
|
const noexcept
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-12-23 01:15:25 +01:00
|
|
|
assert(self);
|
|
|
|
assert(self->yc);
|
2016-11-29 16:23:38 +01:00
|
|
|
return *self->yc;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/context.h
|
|
|
|
//
|
|
|
|
|
2020-10-16 14:13:24 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
[[gnu::visibility("hidden")]]
|
|
|
|
extern ios::descriptor spawn_desc[3];
|
|
|
|
}
|
|
|
|
|
2022-06-15 18:36:03 +02:00
|
|
|
[[clang::always_destroy]]
|
2020-10-16 14:13:24 +02:00
|
|
|
decltype(ircd::ctx::spawn_desc)
|
|
|
|
ircd::ctx::spawn_desc
|
|
|
|
{
|
2020-12-18 05:27:44 +01:00
|
|
|
{ "ircd.ctx.spawn.post" },
|
|
|
|
{ "ircd.ctx.spawn.defer" },
|
|
|
|
{ "ircd.ctx.spawn.dispatch" },
|
2020-10-16 14:13:24 +02:00
|
|
|
};
|
|
|
|
|
2018-12-28 23:07:40 +01:00
|
|
|
decltype(ircd::ctx::DEFAULT_STACK_SIZE)
|
|
|
|
ircd::ctx::DEFAULT_STACK_SIZE
|
|
|
|
{
|
|
|
|
128_KiB
|
|
|
|
};
|
|
|
|
|
2020-10-16 14:13:24 +02:00
|
|
|
//
|
|
|
|
// context::context
|
|
|
|
//
|
|
|
|
|
2018-08-27 04:57:55 +02:00
|
|
|
// Linkage here for default construction because ctx is internal.
|
|
|
|
ircd::ctx::context::context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:40:50 +02:00
|
|
|
ircd::ctx::context::context(const string_view &name,
|
|
|
|
const size_t &stack_size,
|
|
|
|
function func,
|
|
|
|
const flags &flags)
|
|
|
|
:context
|
|
|
|
{
|
|
|
|
name, stack_size, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context::context(const string_view &name,
|
|
|
|
const flags &flags,
|
|
|
|
function func)
|
|
|
|
:context
|
|
|
|
{
|
|
|
|
name, DEFAULT_STACK_SIZE, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context::context(const string_view &name,
|
|
|
|
function func,
|
|
|
|
const flags &flags)
|
|
|
|
:context
|
|
|
|
{
|
|
|
|
name, DEFAULT_STACK_SIZE, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context::context(function func,
|
|
|
|
const flags &flags)
|
|
|
|
:context
|
|
|
|
{
|
|
|
|
"<noname>", DEFAULT_STACK_SIZE, flags, std::move(func)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-17 00:40:57 +01:00
|
|
|
ircd::ctx::context::context(const string_view &name,
|
2016-11-29 16:23:38 +01:00
|
|
|
const size_t &stack_sz,
|
|
|
|
const flags &flags,
|
2017-09-09 16:30:45 +02:00
|
|
|
function func)
|
2020-10-14 10:34:23 +02:00
|
|
|
:context
|
|
|
|
{
|
2020-10-15 11:13:28 +02:00
|
|
|
name, mutable_buffer{nullptr, stack_sz}, flags, std::move(func)
|
2020-10-14 10:34:23 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::context::context(const string_view &name,
|
|
|
|
const mutable_buffer &stack,
|
|
|
|
const flags &flags,
|
|
|
|
function func)
|
2018-08-27 04:57:55 +02:00
|
|
|
:c
|
|
|
|
{
|
2019-10-03 22:40:50 +02:00
|
|
|
std::make_unique<ctx>
|
|
|
|
(
|
|
|
|
name,
|
2020-10-14 10:34:23 +02:00
|
|
|
stack,
|
2020-02-27 19:11:59 +01:00
|
|
|
!current? flags | POST : flags
|
2019-10-03 22:40:50 +02:00
|
|
|
)
|
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
|
|
|
|
{
|
2019-07-24 22:21:48 +02:00
|
|
|
std::bind(&ctx::spawn, c.get(), std::move(func))
|
2017-10-25 22:45:24 +02:00
|
|
|
};
|
|
|
|
|
2019-03-23 02:54:33 +01:00
|
|
|
// 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.
|
2019-12-03 22:05:32 +01:00
|
|
|
const unwind_nominal release{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
2019-10-03 22:40:50 +02:00
|
|
|
assert(c);
|
|
|
|
if(c->flags & context::DETACH)
|
|
|
|
this->detach();
|
|
|
|
}};
|
2019-03-23 02:54:33 +01:00
|
|
|
|
2020-09-09 07:25:22 +02:00
|
|
|
// Indicates to the profiler that this context is spawning a child.
|
|
|
|
if(likely(ircd::ctx::current))
|
|
|
|
mark(prof::event::SPAWN);
|
|
|
|
|
|
|
|
// Branch to spawn via POST mechanism. This is an asynchronous method which
|
|
|
|
// returns immediately so this context doesn't yield. The spawning occurs
|
|
|
|
// sometime after this context next yields. This is the primary method to
|
|
|
|
// spawn contexts. Note: This is the method to spawn contexts when this
|
|
|
|
// parent is not itself a context as yielding is not possible anyway.
|
|
|
|
assert(c->flags & POST || ircd::ctx::current);
|
2019-10-03 22:40:50 +02:00
|
|
|
if(c->flags & POST)
|
2020-12-20 14:13:18 +01:00
|
|
|
ios::dispatch
|
|
|
|
{
|
|
|
|
spawn_desc[0], ios::defer, std::move(spawn)
|
|
|
|
};
|
2019-09-23 02:40:15 +02:00
|
|
|
|
2020-09-09 07:25:22 +02:00
|
|
|
// (experimental) Branch to spawn via defer mechanism.
|
|
|
|
else if(c->flags & DEFER)
|
2020-12-20 14:13:18 +01:00
|
|
|
ios::dispatch
|
|
|
|
{
|
|
|
|
spawn_desc[1], ios::defer, ios::yield, std::move(spawn)
|
|
|
|
};
|
2020-09-09 07:25:22 +02:00
|
|
|
|
|
|
|
// Branch to spawn via dispatch mechanism. This context will yield while
|
|
|
|
// the spawning takes place on this stack. This is the closest to a direct
|
|
|
|
// context switch since we don't call spawn() directly from this frame
|
|
|
|
// which allows the ctx/ios infrastructure to account for the context
|
|
|
|
// switch. Note: This is also the default method when no flags are given
|
|
|
|
// and this parent is another context.
|
|
|
|
else if(c->flags & DISPATCH || (true))
|
2020-12-20 14:13:18 +01:00
|
|
|
ios::dispatch
|
|
|
|
{
|
|
|
|
spawn_desc[2], ios::yield, std::move(spawn)
|
|
|
|
};
|
2016-09-07 23:39:41 +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;
|
2019-03-26 22:49:43 +01:00
|
|
|
|
|
|
|
// When the WAIT_JOIN flag is given we wait for the context to
|
|
|
|
// complete cooperatively before this destructs.
|
|
|
|
if(~c->flags & context::WAIT_JOIN)
|
|
|
|
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);
|
2022-07-04 22:15:53 +02:00
|
|
|
ctx::adjoindre.wait([this]() noexcept
|
2018-08-20 01:21:33 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
2018-12-17 00:41:32 +01:00
|
|
|
const ircd::string_view &
|
|
|
|
ircd::ctx::name(const pool &pool)
|
|
|
|
{
|
|
|
|
return pool.name;
|
|
|
|
}
|
|
|
|
|
2018-12-28 21:57:32 +01:00
|
|
|
decltype(ircd::ctx::pool::default_name)
|
|
|
|
ircd::ctx::pool::default_name
|
|
|
|
{
|
|
|
|
"<unnamed pool>"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::ctx::pool::default_opts)
|
|
|
|
ircd::ctx::pool::default_opts
|
|
|
|
{};
|
|
|
|
|
2018-12-17 00:41:32 +01:00
|
|
|
//
|
|
|
|
// pool::pool
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::pool::pool(const string_view &name,
|
2018-12-28 21:57:32 +01:00
|
|
|
const opts &opt)
|
2016-11-29 16:23:38 +01:00
|
|
|
:name{name}
|
2018-12-28 21:57:32 +01:00
|
|
|
,opt{&opt}
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2018-12-28 22:07:50 +01:00
|
|
|
// Can't spawn contexts when the ios isn't available. This may be the
|
|
|
|
// case for some static instances of pool: initial_ctxs value is ignored.
|
|
|
|
if(ircd::ios::available())
|
|
|
|
add(this->opt->initial_ctxs);
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
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
|
2018-12-17 00:41:32 +01:00
|
|
|
ircd::ctx::pool::join()
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2018-12-17 00:41:32 +01:00
|
|
|
set(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::pool::interrupt()
|
|
|
|
{
|
|
|
|
for(auto &context : ctxs)
|
|
|
|
context.interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::pool::terminate()
|
|
|
|
{
|
|
|
|
for(auto &context : ctxs)
|
|
|
|
context.terminate();
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-25 01:20:03 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::pool::min(const size_t &num)
|
|
|
|
{
|
|
|
|
if(size() < num)
|
|
|
|
set(num);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-12-28 21:57:32 +01:00
|
|
|
assert(opt);
|
2016-09-21 23:15:49 +02:00
|
|
|
for(size_t i(0); i < num; ++i)
|
2020-01-06 21:01:04 +01:00
|
|
|
{
|
2018-12-28 21:57:32 +01:00
|
|
|
ctxs.emplace_back(name, opt->stack_size, context::POST, std::bind(&pool::main, this));
|
2020-01-06 21:01:04 +01:00
|
|
|
|
|
|
|
assert(opt);
|
|
|
|
ionice(ctxs.back(), opt->ionice);
|
|
|
|
nice(ctxs.back(), opt->nice);
|
|
|
|
}
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 04:01:37 +02:00
|
|
|
void
|
2018-12-17 00:41:32 +01:00
|
|
|
ircd::ctx::pool::operator()(closure closure)
|
2018-08-20 00:55:35 +02:00
|
|
|
{
|
2018-12-28 21:57:32 +01:00
|
|
|
assert(opt);
|
|
|
|
if(!avail() && q.size() > size_t(opt->queue_max_soft) && opt->queue_max_dwarning)
|
2018-12-28 00:45:02 +01:00
|
|
|
log::dwarning
|
|
|
|
{
|
2019-02-07 08:32:49 +01:00
|
|
|
log, "pool(%p '%s') ctx(%p): size:%zu active:%zu queue:%zu exceeded soft max:%zu",
|
2018-12-28 00:45:02 +01:00
|
|
|
this,
|
|
|
|
name,
|
|
|
|
current,
|
|
|
|
size(),
|
|
|
|
active(),
|
|
|
|
q.size(),
|
2018-12-28 21:57:32 +01:00
|
|
|
opt->queue_max_soft
|
2018-12-28 00:45:02 +01:00
|
|
|
};
|
|
|
|
|
2018-12-28 21:57:32 +01:00
|
|
|
if(current && opt->queue_max_soft >= 0 && opt->queue_max_blocking)
|
2018-12-28 00:45:02 +01:00
|
|
|
q_max.wait([this]
|
|
|
|
{
|
2019-09-19 05:03:24 +02:00
|
|
|
return !wouldblock();
|
2018-12-28 00:45:02 +01:00
|
|
|
});
|
|
|
|
|
2018-12-28 21:57:32 +01:00
|
|
|
if(unlikely(q.size() >= size_t(opt->queue_max_hard)))
|
2018-12-28 00:45:02 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"pool(%p '%s') ctx(%p): size:%zu avail:%zu queue:%zu exceeded hard max:%zu",
|
|
|
|
this,
|
|
|
|
name,
|
|
|
|
current,
|
|
|
|
size(),
|
|
|
|
avail(),
|
|
|
|
q.size(),
|
2018-12-28 21:57:32 +01:00
|
|
|
opt->queue_max_hard
|
2018-12-28 00:45:02 +01:00
|
|
|
};
|
|
|
|
|
2018-12-17 00:41:32 +01:00
|
|
|
q.push(std::move(closure));
|
2018-08-20 00:55:35 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 05:03:24 +02:00
|
|
|
bool
|
|
|
|
ircd::ctx::pool::wouldblock()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(q.size() < size_t(opt->queue_max_soft))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!opt->queue_max_soft && q.size() < avail())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-24 21:06:05 +01:00
|
|
|
/// Main execution loop for a pool.
|
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
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
const scope_count running
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
this->running
|
|
|
|
};
|
2016-09-21 23:15:49 +02:00
|
|
|
|
2019-02-28 22:33:21 +01:00
|
|
|
q_max.notify();
|
2018-12-28 00:56:59 +01:00
|
|
|
while(!termination(cur()))
|
2018-12-24 21:06:05 +01:00
|
|
|
work();
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
catch(const interrupted &e)
|
|
|
|
{
|
2018-05-06 10:15:26 +02:00
|
|
|
// log::debug
|
|
|
|
// {
|
2019-02-07 08:32:49 +01:00
|
|
|
// log, "pool(%p) ctx(%p): %s", this, &cur(), e.what()
|
2018-05-06 10:15:26 +02:00
|
|
|
// };
|
|
|
|
}
|
|
|
|
catch(const terminated &e)
|
|
|
|
{
|
|
|
|
// log::debug
|
|
|
|
// {
|
2019-02-07 08:32:49 +01:00
|
|
|
// log, "pool(%p) ctx(%p): terminated", this, &cur()
|
2018-05-06 10:15:26 +02:00
|
|
|
// };
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-24 21:06:05 +01:00
|
|
|
ircd::ctx::pool::work()
|
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
|
|
|
{
|
2019-06-23 08:28:48 +02:00
|
|
|
q.pop()
|
2018-09-19 01:45:04 +02:00
|
|
|
};
|
2016-09-21 23:15:49 +02:00
|
|
|
|
2021-05-02 23:02:28 +02:00
|
|
|
const scope_notify notify
|
2019-02-28 22:33:21 +01:00
|
|
|
{
|
2021-05-02 23:02:28 +02:00
|
|
|
q_max
|
2019-02-28 22:33:21 +01:00
|
|
|
};
|
|
|
|
|
2021-05-02 23:02:28 +02:00
|
|
|
const scope_count working
|
2016-09-21 23:15:49 +02:00
|
|
|
{
|
2021-05-02 23:02:28 +02:00
|
|
|
this->working
|
|
|
|
};
|
2016-09-21 23:15:49 +02:00
|
|
|
|
2018-12-24 21:06:05 +01:00
|
|
|
// Execute the user's function
|
2016-09-21 23:15:49 +02:00
|
|
|
func();
|
2018-12-24 21:06:05 +01:00
|
|
|
|
|
|
|
// Check for latent interruption to this ctx. If there's anything pending
|
|
|
|
// it's best to get rid of it sooner rather than later.
|
|
|
|
interruption_point();
|
2016-09-21 23:15:49 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
2019-02-07 08:32:49 +01:00
|
|
|
log, "pool(%p '%s') ctx(%p '%s' id:%u): unhandled: %s",
|
2018-03-15 21:44:29 +01:00
|
|
|
this,
|
2018-12-24 21:06:05 +01:00
|
|
|
name,
|
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
|
|
|
|
{
|
2019-02-07 08:32:49 +01:00
|
|
|
log, "pool '%s' total: %zu avail: %zu queued: %zu active: %zu pending: %zu",
|
2018-03-17 04:25:56 +01:00
|
|
|
pool.name,
|
|
|
|
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
|
|
|
|
{
|
2022-06-14 00:08:16 +02:00
|
|
|
static thread_local ticker _total; ///< Totals kept for all contexts.
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2018-12-22 23:53:23 +01:00
|
|
|
static void check_stack();
|
|
|
|
static void check_slice();
|
2019-08-06 01:15:56 +02:00
|
|
|
static void slice_leave() noexcept;
|
2020-08-21 13:18:41 +02:00
|
|
|
static void slice_enter() noexcept;
|
2018-12-18 00:24:41 +01:00
|
|
|
|
2018-12-22 23:53:23 +01:00
|
|
|
static void handle_cur_yield();
|
|
|
|
static void handle_cur_leave();
|
2020-08-21 13:18:41 +02:00
|
|
|
static void handle_cur_continue() noexcept;
|
|
|
|
static void handle_cur_enter() noexcept;
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2019-08-06 01:15:56 +02:00
|
|
|
static void inc_ticker(const event &e) noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 01:47:03 +02:00
|
|
|
decltype(ircd::ctx::prof::watchdog)
|
|
|
|
ircd::ctx::prof::watchdog
|
|
|
|
{
|
|
|
|
"ctx.watchdog"
|
|
|
|
};
|
|
|
|
|
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 },
|
2018-12-14 01:25:42 +01:00
|
|
|
{ "persist", false },
|
2018-11-15 02:35:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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 },
|
2018-12-14 01:25:42 +01:00
|
|
|
{ "persist", false },
|
2016-09-26 00:46:54 +02:00
|
|
|
};
|
|
|
|
|
2020-08-02 15:20:22 +02:00
|
|
|
[[using gnu: flatten, always_inline]]
|
|
|
|
inline void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::mark(const event &e)
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-12-22 23:53:23 +01:00
|
|
|
inc_ticker(e);
|
|
|
|
|
2016-09-26 00:46:54 +02:00
|
|
|
switch(e)
|
|
|
|
{
|
2018-12-22 23:53:23 +01:00
|
|
|
case event::ENTER: handle_cur_enter(); break;
|
|
|
|
case event::LEAVE: handle_cur_leave(); break;
|
|
|
|
case event::YIELD: handle_cur_yield(); break;
|
|
|
|
case event::CONTINUE: handle_cur_continue(); break;
|
|
|
|
default: break;
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 23:53:23 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::prof::inc_ticker(const event &e)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-05-06 07:09:20 +02:00
|
|
|
{
|
2018-12-22 23:53:23 +01:00
|
|
|
assert(uint8_t(e) < num_of<event>());
|
2018-05-06 07:09:20 +02:00
|
|
|
|
2018-12-22 23:53:23 +01:00
|
|
|
// Increment the ticker for all contexts.
|
|
|
|
_total.event[uint8_t(e)]++;
|
2018-12-21 20:52:05 +01:00
|
|
|
|
2018-12-22 23:53:23 +01:00
|
|
|
// Increment the ticker for the context's instance
|
2020-08-02 15:42:04 +02:00
|
|
|
static uint64_t dummy;
|
|
|
|
uint64_t &ticker
|
|
|
|
{
|
|
|
|
current?
|
|
|
|
current->profile.event[uint8_t(e)]:
|
|
|
|
dummy
|
|
|
|
};
|
|
|
|
|
|
|
|
++ticker;
|
2018-05-06 07:31:52 +02:00
|
|
|
}
|
|
|
|
|
2016-09-26 00:46:54 +02:00
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::handle_cur_enter()
|
2020-08-21 13:18:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
slice_enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::prof::handle_cur_continue()
|
|
|
|
noexcept
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-12-18 00:24:41 +01:00
|
|
|
slice_enter();
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::ctx::prof::handle_cur_leave()
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-12-18 00:24:41 +01:00
|
|
|
slice_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
|
|
|
{
|
2018-12-18 00:24:41 +01:00
|
|
|
slice_leave();
|
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
|
2018-12-18 00:24:41 +01:00
|
|
|
ircd::ctx::prof::slice_enter()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
ios::handler::enter(&ctx::ios_handler);
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-18 00:24:41 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::prof::slice_leave()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-18 00:24:41 +01:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
ios::handler::leave(&ctx::ios_handler);
|
2019-09-15 03:15:09 +02:00
|
|
|
|
2020-08-21 13:18:41 +02:00
|
|
|
static constexpr auto pos
|
2019-04-13 00:57:34 +02:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
size_t(prof::event::CYCLES)
|
2019-04-13 00:57:34 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 13:18:41 +02:00
|
|
|
assert(ctx::ios_desc.stats);
|
2020-12-18 05:29:00 +01:00
|
|
|
const uint64_t &last_slice
|
2019-04-13 00:57:34 +02:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
ctx::ios_desc.stats->slice_last
|
2019-04-13 00:57:34 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 13:18:41 +02:00
|
|
|
auto &c(cur());
|
2019-07-22 02:55:58 +02:00
|
|
|
c.profile.event.at(pos) += last_slice;
|
2018-12-18 00:24:41 +01:00
|
|
|
c.stack.at = stack_at_here();
|
2020-04-23 14:09:10 +02:00
|
|
|
c.stack.peak = std::max(c.stack.at, c.stack.peak);
|
2020-08-21 13:18:41 +02:00
|
|
|
|
|
|
|
_total.event.at(pos) += last_slice;
|
2018-12-18 00:24:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-23 06:27:19 +01:00
|
|
|
#ifndef NDEBUG
|
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-12-14 01:21:54 +01:00
|
|
|
auto &c(cur());
|
2019-06-23 01:03:19 +02:00
|
|
|
const auto &slice_exempt
|
2018-12-14 01:21:54 +01:00
|
|
|
{
|
|
|
|
c.flags & context::SLICE_EXEMPT
|
|
|
|
};
|
|
|
|
|
2020-08-21 13:18:41 +02:00
|
|
|
assert(ctx::ios_desc.stats);
|
2020-12-18 05:29:00 +01:00
|
|
|
const uint64_t &last_slice
|
2018-05-06 07:09:20 +02:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
ctx::ios_desc.stats->slice_last
|
2018-05-06 07:09:20 +02:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-12-18 00:24:41 +01:00
|
|
|
// Slice warning
|
|
|
|
if(unlikely(slice_exceeded_warning(last_slice) && !slice_exempt))
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-02-05 19:47:46 +01:00
|
|
|
{
|
2020-05-25 01:47:03 +02:00
|
|
|
watchdog, "timeslice excessive; lim:%lu last:%lu pct:%.2lf",
|
2018-12-18 00:24:41 +01:00
|
|
|
ulong(settings::slice_warning),
|
|
|
|
last_slice,
|
|
|
|
((double(last_slice) / double(ulong(settings::slice_warning))) * 100.0)
|
2018-02-05 19:47:46 +01:00
|
|
|
};
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-12-18 00:24:41 +01:00
|
|
|
// Slice assertion
|
|
|
|
assert(!slice_exceeded_assertion(last_slice) || slice_exempt);
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-12-18 00:24:41 +01:00
|
|
|
// Slice interrupt
|
|
|
|
if(unlikely(slice_exceeded_interrupt(last_slice) && !slice_exempt))
|
2018-02-05 19:47:46 +01:00
|
|
|
throw interrupted
|
|
|
|
{
|
2019-07-24 02:21:17 +02:00
|
|
|
"[%s] context id:%lu watchdog interrupt; lim:%lu last:%lu total:%lu",
|
2018-02-05 19:47:46 +01:00
|
|
|
name(c),
|
|
|
|
id(c),
|
2018-12-18 00:24:41 +01:00
|
|
|
ulong(settings::slice_interrupt),
|
|
|
|
last_slice,
|
2018-05-06 07:09:20 +02:00
|
|
|
cycles(c),
|
2018-02-05 19:47:46 +01:00
|
|
|
};
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
2018-12-23 06:27:19 +01:00
|
|
|
#else
|
|
|
|
void
|
|
|
|
ircd::ctx::prof::check_slice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // NDEBUG
|
2016-09-26 00:46:54 +02:00
|
|
|
|
2018-12-23 06:27:19 +01:00
|
|
|
#ifndef NDEBUG
|
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());
|
2019-06-23 01:03:19 +02:00
|
|
|
const auto &stack_exempt
|
2018-12-14 01:21:54 +01:00
|
|
|
{
|
|
|
|
c.flags & context::STACK_EXEMPT
|
|
|
|
};
|
|
|
|
|
2018-12-18 00:24:41 +01:00
|
|
|
const auto &stack_at
|
2016-09-26 00:46:54 +02:00
|
|
|
{
|
2018-12-18 00:24:41 +01:00
|
|
|
c.stack.at
|
|
|
|
};
|
|
|
|
|
|
|
|
// Stack warning
|
|
|
|
if(unlikely(!stack_exempt && stack_exceeded_warning(stack_at)))
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-02-06 04:12:34 +01:00
|
|
|
{
|
2020-05-25 01:47:03 +02:00
|
|
|
watchdog, "stack used %zu of %zu bytes",
|
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-12-18 00:24:41 +01:00
|
|
|
// Stack assertion
|
|
|
|
assert(stack_exempt || !stack_exceeded_assertion(stack_at));
|
|
|
|
}
|
2018-12-23 06:27:19 +01:00
|
|
|
#else
|
|
|
|
void
|
|
|
|
ircd::ctx::prof::check_stack()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif // NDEBUG
|
2018-12-18 00:24:41 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::prof::stack_exceeded_assertion(const size_t &stack_at)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-18 00:24:41 +01:00
|
|
|
{
|
|
|
|
const auto &c(cur());
|
|
|
|
const auto &stack_max(c.stack.max);
|
|
|
|
const double &stack_usage_assertion(settings::stack_usage_assertion);
|
|
|
|
return stack_usage_assertion > 0.0?
|
|
|
|
stack_at >= c.stack.max * settings::stack_usage_assertion:
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::prof::stack_exceeded_warning(const size_t &stack_at)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-18 00:24:41 +01:00
|
|
|
{
|
|
|
|
const auto &c(cur());
|
|
|
|
const auto &stack_max(c.stack.max);
|
|
|
|
const double &stack_usage_warning(settings::stack_usage_warning);
|
|
|
|
return stack_usage_warning > 0.0?
|
|
|
|
stack_at >= c.stack.max * stack_usage_warning:
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::prof::slice_exceeded_interrupt(const ulong &cycles)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-18 00:24:41 +01:00
|
|
|
{
|
|
|
|
const ulong &threshold(settings::slice_interrupt);
|
|
|
|
return threshold > 0 && cycles >= threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::prof::slice_exceeded_assertion(const ulong &cycles)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-18 00:24:41 +01:00
|
|
|
{
|
|
|
|
const ulong &threshold(settings::slice_assertion);
|
|
|
|
return threshold > 0 && cycles >= threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::prof::slice_exceeded_warning(const ulong &cycles)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-18 00:24:41 +01:00
|
|
|
{
|
|
|
|
const ulong &threshold(settings::slice_warning);
|
|
|
|
return threshold > 0 && cycles >= threshold;
|
2016-09-26 00:46:54 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2020-08-21 13:18:41 +02:00
|
|
|
ulong
|
2018-12-22 23:53:23 +01:00
|
|
|
ircd::ctx::prof::cur_slice_start()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-22 23:53:23 +01:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
return ctx::ios_handler.ts;
|
2018-12-22 23:53:23 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-12-22 23:53:23 +01:00
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::prof::get(const ctx &c,
|
|
|
|
const event &e)
|
|
|
|
{
|
|
|
|
return get(c).event.at(uint8_t(e));
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-12-22 23:53:23 +01:00
|
|
|
const ircd::ctx::prof::ticker &
|
|
|
|
ircd::ctx::prof::get(const ctx &c)
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-22 23:53:23 +01:00
|
|
|
{
|
|
|
|
return c.profile;
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-12-22 23:53:23 +01:00
|
|
|
const uint64_t &
|
|
|
|
ircd::ctx::prof::get(const event &e)
|
|
|
|
{
|
|
|
|
return get().event.at(uint8_t(e));
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2018-12-22 23:53:23 +01:00
|
|
|
const ircd::ctx::prof::ticker &
|
|
|
|
ircd::ctx::prof::get()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-12-22 23:53:23 +01:00
|
|
|
{
|
|
|
|
return _total;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::ctx::prof::reflect(const event &e)
|
|
|
|
{
|
|
|
|
switch(e)
|
|
|
|
{
|
|
|
|
case event::SPAWN: return "SPAWN";
|
|
|
|
case event::JOIN: return "JOIN";
|
|
|
|
case event::JOINED: return "JOINED";
|
|
|
|
case event::ENTER: return "ENTER";
|
|
|
|
case event::LEAVE: return "LEAVE";
|
|
|
|
case event::YIELD: return "YIELD";
|
|
|
|
case event::CONTINUE: return "CONTINUE";
|
|
|
|
case event::INTERRUPT: return "INTERRUPT";
|
|
|
|
case event::TERMINATE: return "TERMINATE";
|
2019-04-13 00:57:34 +02:00
|
|
|
case event::CYCLES: return "CYCLES";
|
2018-12-22 23:53:23 +01:00
|
|
|
case event::_NUM_: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "?????";
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/promise.h
|
|
|
|
//
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
static void set_promises_state(shared_state_base &);
|
|
|
|
static void invalidate_promises(shared_state_base &);
|
|
|
|
static void append(shared_state_base &new_, shared_state_base &old);
|
|
|
|
static void update(shared_state_base &new_, shared_state_base &old);
|
|
|
|
static void remove(shared_state_base &);
|
|
|
|
static void notify(shared_state_base &);
|
|
|
|
|
|
|
|
static void set_futures_promise(promise_base &);
|
|
|
|
static void invalidate_futures(promise_base &);
|
|
|
|
static void append(promise_base &new_, promise_base &old);
|
|
|
|
static void update(promise_base &new_, promise_base &old);
|
|
|
|
static void remove(promise_base &);
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
//
|
|
|
|
// promise<void>
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::promise<void>::set_value()
|
|
|
|
{
|
2019-03-01 01:45:20 +01:00
|
|
|
if(!valid())
|
|
|
|
return;
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
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)}
|
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
update(*this, o);
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base::promise_base(const promise_base &o)
|
|
|
|
:st{o.st}
|
|
|
|
,next{nullptr}
|
|
|
|
{
|
2020-11-03 20:30:15 +01:00
|
|
|
append(*this, mutable_cast(o));
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
update(*this, o);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base::~promise_base()
|
2019-06-23 08:28:48 +02:00
|
|
|
noexcept
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-09 01:34:41 +02:00
|
|
|
if(promise_base::refcount(*this) == 1)
|
2019-06-23 08:28:48 +02:00
|
|
|
set_exception(make_exception_ptr<broken_promise>());
|
2019-09-01 08:05:06 +02:00
|
|
|
|
|
|
|
remove();
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::set_exception(std::exception_ptr eptr)
|
|
|
|
{
|
2019-03-01 01:45:20 +01:00
|
|
|
if(!valid())
|
|
|
|
return;
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
check_pending();
|
2019-09-01 08:05:06 +02:00
|
|
|
for(auto *st(shared_state_base::head(*this)); st; st = st->next)
|
|
|
|
st->eptr = eptr;
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
make_ready();
|
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::remove()
|
|
|
|
{
|
|
|
|
if(!valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ircd::ctx::remove(*this);
|
|
|
|
assert(!valid());
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::promise_base::make_ready()
|
|
|
|
{
|
2019-09-09 01:34:41 +02:00
|
|
|
const critical_assertion ca;
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
assert(valid());
|
2019-09-09 01:34:41 +02:00
|
|
|
promise_base *p
|
|
|
|
{
|
|
|
|
promise_base::head(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(p);
|
2019-09-01 08:05:06 +02:00
|
|
|
shared_state_base *next
|
|
|
|
{
|
2019-09-09 01:34:41 +02:00
|
|
|
shared_state_base::head(*p)
|
2019-09-01 08:05:06 +02:00
|
|
|
};
|
2018-10-30 19:18:03 +01:00
|
|
|
|
|
|
|
// 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
|
2019-09-01 08:05:06 +02:00
|
|
|
// same union as the promise pointer (see shared_state.h). Then
|
|
|
|
// chase the linked lists of futures and make_ready() each one.
|
|
|
|
assert(next);
|
|
|
|
invalidate_promises(*next); do
|
|
|
|
{
|
|
|
|
// Now set the shared_state to READY. We know the location of the
|
|
|
|
// shared state by saving it in this frame earlier, otherwise
|
|
|
|
// invalidate_promises() would have nulled it.
|
|
|
|
set(*next, 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(*next);
|
|
|
|
}
|
|
|
|
while((next = next->next));
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-04-15 21:18:09 +02:00
|
|
|
// At this point the promise should no longer be considered valid; no longer
|
2018-10-30 19:18:03 +01:00
|
|
|
// referring to the shared_state.
|
2019-09-04 02:35:46 +02:00
|
|
|
this->st = nullptr;
|
2018-10-30 19:18:03 +01:00
|
|
|
assert(!valid());
|
|
|
|
}
|
|
|
|
|
2019-09-09 01:33:38 +02:00
|
|
|
/// If no shared state anymore: refcount=0; otherwise the promise
|
|
|
|
/// list head from p.st->p resolves to at least &p which means refcount>=1
|
|
|
|
size_t
|
|
|
|
ircd::ctx::promise_base::refcount(const promise_base &p)
|
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
p.st? refcount(*p.st): 0UL
|
|
|
|
};
|
|
|
|
|
|
|
|
assert((p.st && ret >= 1) || (!p.st && !ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
/// 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::promise_base::refcount(const shared_state_base &st)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
size_t ret{0};
|
|
|
|
if(!is(st, future_state::PENDING))
|
|
|
|
return ret;
|
|
|
|
|
2019-09-05 00:34:02 +02:00
|
|
|
for(const auto *next(head(st)); next; next = next->next)
|
2019-09-01 08:05:06 +02:00
|
|
|
++ret;
|
|
|
|
|
|
|
|
return ret;
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::promise_base *
|
|
|
|
ircd::ctx::promise_base::head(promise_base &p)
|
|
|
|
{
|
|
|
|
return p.st && head(*p.st)?
|
|
|
|
head(*p.st):
|
|
|
|
std::addressof(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::promise_base *
|
|
|
|
ircd::ctx::promise_base::head(shared_state_base &st)
|
|
|
|
{
|
|
|
|
return is(st, future_state::PENDING)?
|
|
|
|
st.p:
|
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::ctx::promise_base *
|
|
|
|
ircd::ctx::promise_base::head(const promise_base &p)
|
|
|
|
{
|
|
|
|
return p.st && head(*p.st)?
|
|
|
|
head(*p.st):
|
2019-09-09 01:34:41 +02:00
|
|
|
std::addressof(p);
|
2019-09-01 08:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::ctx::promise_base *
|
|
|
|
ircd::ctx::promise_base::head(const shared_state_base &st)
|
|
|
|
{
|
|
|
|
return is(st, future_state::PENDING)?
|
|
|
|
st.p:
|
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// internal
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Internal semantics; removes the promise from the linked list of promises.
|
2018-10-30 19:18:03 +01:00
|
|
|
void
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::remove(promise_base &p)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
promise_base *last
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
promise_base::head(p)
|
|
|
|
};
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-09-04 02:35:46 +02:00
|
|
|
if(last == &p)
|
|
|
|
{
|
|
|
|
if(p.next)
|
|
|
|
set_futures_promise(*p.next);
|
|
|
|
else
|
|
|
|
invalidate_futures(p);
|
|
|
|
}
|
2019-09-08 22:02:32 +02:00
|
|
|
else if(last)
|
2019-09-05 00:34:02 +02:00
|
|
|
for(auto *next{last->next}; next; last = next, next = next->next)
|
2019-09-01 08:05:06 +02:00
|
|
|
if(next == &p)
|
|
|
|
{
|
2019-09-05 00:34:02 +02:00
|
|
|
last->next = next->next;
|
2019-09-01 08:05:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.st = nullptr;
|
|
|
|
p.next = nullptr;
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal semantics; updates the location of a promise within the linked
|
|
|
|
/// list of related promises (for move semantic).
|
|
|
|
void
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::update(promise_base &new_,
|
|
|
|
promise_base &old)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
new_.next = old.next;
|
|
|
|
promise_base *last
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
promise_base::head(old)
|
|
|
|
};
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
if(last == &old)
|
|
|
|
set_futures_promise(new_);
|
|
|
|
|
2019-09-08 22:02:32 +02:00
|
|
|
else if(last)
|
2019-09-01 08:05:06 +02:00
|
|
|
for(auto *next{last->next}; next; last = next, next = last->next)
|
|
|
|
if(next == &old)
|
|
|
|
{
|
|
|
|
last->next = &new_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
old.st = nullptr;
|
|
|
|
old.next = nullptr;
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
/// Internal semantics; chases the linked list of promises and adds a reference
|
|
|
|
/// to a new copy at the end (for copy semantic).
|
2018-10-30 19:18:03 +01:00
|
|
|
void
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::append(promise_base &new_,
|
|
|
|
promise_base &old)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
assert(new_.st);
|
2019-09-08 22:02:32 +02:00
|
|
|
assert(old.st);
|
2019-09-01 08:05:06 +02:00
|
|
|
if(!old.next)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
old.next = &new_;
|
2018-10-30 19:18:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
promise_base *next{old.next};
|
|
|
|
for(; next->next; next = next->next);
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
assert(!next->next);
|
|
|
|
next->next = &new_;
|
|
|
|
}
|
2018-08-29 03:56:41 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
void
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::set_futures_promise(promise_base &p)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
auto *next
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
shared_state_base::head(p)
|
|
|
|
};
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
for(; next; next = next->next)
|
2019-09-08 22:02:32 +02:00
|
|
|
{
|
|
|
|
assert(is(*next, future_state::PENDING));
|
|
|
|
next->p = std::addressof(p);
|
|
|
|
}
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::invalidate_futures(promise_base &p)
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
auto *next
|
|
|
|
{
|
|
|
|
shared_state_base::head(p)
|
|
|
|
};
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
for(; next; next = next->next)
|
2019-09-08 22:02:32 +02:00
|
|
|
{
|
|
|
|
assert(is(*next, future_state::PENDING));
|
|
|
|
next->p = nullptr;
|
|
|
|
}
|
2018-10-30 19:18:03 +01:00
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx/shared_shared.h
|
|
|
|
//
|
2018-10-30 19:18:03 +01:00
|
|
|
|
|
|
|
/// 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:
|
2019-09-03 22:52:05 +02:00
|
|
|
st.p = nullptr;
|
2018-10-30 19:18:03 +01:00
|
|
|
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_)
|
2019-09-07 22:24:02 +02:00
|
|
|
noexcept
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
|
|
|
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:
|
2019-09-04 07:08:15 +02:00
|
|
|
return uintptr_t(st.p) >= ircd::info::page_size;
|
2018-10-30 19:18:03 +01:00
|
|
|
|
|
|
|
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)
|
2019-09-07 22:24:02 +02:00
|
|
|
noexcept
|
2018-10-30 19:18:03 +01:00
|
|
|
{
|
|
|
|
return uintptr_t(st.p) >= ircd::info::page_size?
|
|
|
|
future_state::PENDING:
|
|
|
|
st.st;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// shared_state_base::shared_state_base
|
|
|
|
//
|
|
|
|
|
2019-09-07 22:24:02 +02:00
|
|
|
ircd::ctx::shared_state_base::shared_state_base()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
ircd::ctx::shared_state_base::shared_state_base(already_t)
|
2019-09-07 22:24:02 +02:00
|
|
|
noexcept
|
2019-09-01 08:05:06 +02:00
|
|
|
{
|
|
|
|
set(*this, future_state::READY);
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
ircd::ctx::shared_state_base::shared_state_base(promise_base &p)
|
2018-08-29 03:56:41 +02:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
// assign the promise pointer in our new shared_state contained
|
|
|
|
// in the future. If the promise already has a shared_state, that
|
|
|
|
// means this is a shared future.
|
|
|
|
this->p = promise_base::head(p);
|
|
|
|
assert(!this->next);
|
|
|
|
|
|
|
|
// Add this future (shared_state) to the end of the list of futures. Else
|
|
|
|
// this is not a shared future, this is the head of the futures list told
|
|
|
|
// to all shared promises.
|
|
|
|
if(!p.st)
|
|
|
|
{
|
|
|
|
p.st = this;
|
|
|
|
set_promises_state(*this);
|
|
|
|
}
|
|
|
|
else append(*this, *p.st);
|
|
|
|
|
|
|
|
assert(p.valid());
|
|
|
|
assert(is(*this, future_state::PENDING));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base::shared_state_base(shared_state_base &&o)
|
|
|
|
noexcept
|
|
|
|
:cond{std::move(o.cond)}
|
|
|
|
,eptr{std::move(o.eptr)}
|
|
|
|
,then{std::move(o.then)}
|
|
|
|
,next{std::move(o.next)}
|
|
|
|
,p{std::move(o.p)}
|
|
|
|
{
|
|
|
|
update(*this, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base::shared_state_base(const shared_state_base &o)
|
|
|
|
:p{o.p}
|
|
|
|
{
|
|
|
|
append(*this, mutable_cast(o));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::shared_state_base::operator=(promise_base &p)
|
|
|
|
{
|
|
|
|
this->~shared_state_base();
|
|
|
|
new (this) shared_state_base{p};
|
2019-09-08 22:02:32 +02:00
|
|
|
|
|
|
|
assert(p.valid());
|
|
|
|
assert(is(*this, future_state::PENDING));
|
2019-09-01 08:05:06 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::shared_state_base::operator=(shared_state_base &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~shared_state_base();
|
|
|
|
eptr = std::move(o.eptr);
|
|
|
|
then = std::move(o.then);
|
|
|
|
next = std::move(o.next);
|
|
|
|
p = std::move(o.p);
|
|
|
|
update(*this, o);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::shared_state_base::operator=(const shared_state_base &o)
|
|
|
|
{
|
|
|
|
this->~shared_state_base();
|
|
|
|
eptr = o.eptr;
|
|
|
|
then = o.then;
|
|
|
|
p = o.p;
|
|
|
|
append(*this, mutable_cast(o));
|
|
|
|
return *this;
|
2018-08-29 03:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ctx::shared_state_base::~shared_state_base()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
const auto refcount
|
|
|
|
{
|
2019-09-05 00:34:02 +02:00
|
|
|
shared_state_base::refcount(*this)
|
2019-09-01 08:05:06 +02:00
|
|
|
};
|
|
|
|
|
2019-09-09 01:34:41 +02:00
|
|
|
if(refcount == 1)
|
2019-09-01 08:05:06 +02:00
|
|
|
invalidate_promises(*this);
|
2019-09-09 01:34:41 +02:00
|
|
|
else if(refcount > 1)
|
|
|
|
remove(*this);
|
2019-09-01 08:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Count the number of associated futures
|
|
|
|
size_t
|
|
|
|
ircd::ctx::shared_state_base::refcount(const shared_state_base &st)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
2019-09-09 01:34:41 +02:00
|
|
|
if(!is(st, future_state::PENDING))
|
|
|
|
return ret;
|
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
for(const auto *next(head(st)); next; next = next->next)
|
|
|
|
++ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the head of the futures from any associated promise
|
|
|
|
ircd::ctx::shared_state_base *
|
|
|
|
ircd::ctx::shared_state_base::head(promise_base &p)
|
|
|
|
{
|
|
|
|
return p.st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the head of the futures from any associated future
|
|
|
|
ircd::ctx::shared_state_base *
|
|
|
|
ircd::ctx::shared_state_base::head(shared_state_base &st)
|
|
|
|
{
|
|
|
|
return is(st, future_state::PENDING) && head(*st.p)?
|
|
|
|
head(*st.p):
|
|
|
|
std::addressof(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the head of the futures from any associated promise
|
|
|
|
const ircd::ctx::shared_state_base *
|
|
|
|
ircd::ctx::shared_state_base::head(const promise_base &p)
|
|
|
|
{
|
|
|
|
return p.st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the head of the futures from any associated future
|
|
|
|
const ircd::ctx::shared_state_base *
|
|
|
|
ircd::ctx::shared_state_base::head(const shared_state_base &st)
|
|
|
|
{
|
|
|
|
return is(st, future_state::PENDING) && head(*st.p)?
|
|
|
|
head(*st.p):
|
|
|
|
std::addressof(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// internal
|
|
|
|
//
|
|
|
|
|
|
|
|
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);
|
|
|
|
st.then = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stack_usage_assertion sua;
|
|
|
|
st.cond.notify_all();
|
|
|
|
assert(bool(st.then));
|
|
|
|
st.then(st);
|
|
|
|
st.then = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the future from the list of futures.
|
|
|
|
void
|
|
|
|
ircd::ctx::remove(shared_state_base &st)
|
|
|
|
{
|
|
|
|
shared_state_base *last
|
|
|
|
{
|
|
|
|
shared_state_base::head(st)
|
|
|
|
};
|
|
|
|
|
2019-09-08 22:02:32 +02:00
|
|
|
assert(last);
|
2019-09-01 08:05:06 +02:00
|
|
|
if(last == &st && is(st, future_state::PENDING))
|
|
|
|
{
|
|
|
|
if(last->next)
|
|
|
|
set_promises_state(*last->next);
|
|
|
|
else
|
|
|
|
invalidate_promises(st);
|
|
|
|
}
|
2019-09-09 01:34:41 +02:00
|
|
|
else for(auto *next(last->next); next; last = next, next = next->next)
|
2019-09-01 08:05:06 +02:00
|
|
|
if(next == &st)
|
|
|
|
{
|
2019-09-05 00:34:02 +02:00
|
|
|
last->next = next->next;
|
2019-09-01 08:05:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
st.next = nullptr;
|
|
|
|
st.p = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace associated future old with new_. This is used to implement the
|
|
|
|
/// object move semantics.
|
|
|
|
void
|
|
|
|
ircd::ctx::update(shared_state_base &new_,
|
|
|
|
shared_state_base &old)
|
|
|
|
{
|
|
|
|
shared_state_base *last
|
|
|
|
{
|
|
|
|
shared_state_base::head(old)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(last);
|
|
|
|
if(last == &old && is(*last, future_state::PENDING))
|
|
|
|
set_promises_state(new_);
|
|
|
|
|
|
|
|
new_.next = old.next;
|
|
|
|
for(auto *next{last->next}; next; last = next, next = last->next)
|
|
|
|
if(next == &old)
|
|
|
|
{
|
|
|
|
last->next = &new_;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
old.p = nullptr;
|
|
|
|
old.next = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a new future sharing the list
|
|
|
|
void
|
|
|
|
ircd::ctx::append(shared_state_base &new_,
|
|
|
|
shared_state_base &old)
|
|
|
|
{
|
|
|
|
assert(!new_.next);
|
|
|
|
assert(is(new_, future_state::PENDING));
|
|
|
|
if(!old.next)
|
|
|
|
{
|
|
|
|
old.next = &new_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_state_base *next{old.next};
|
|
|
|
for(; next->next; next = next->next);
|
|
|
|
|
|
|
|
assert(!next->next);
|
|
|
|
next->next = &new_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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::set_promises_state(shared_state_base &st)
|
|
|
|
{
|
|
|
|
assert(is(st, future_state::PENDING));
|
|
|
|
promise_base *next
|
|
|
|
{
|
|
|
|
promise_base::head(st)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(next);
|
|
|
|
for(; next; next = next->next)
|
|
|
|
next->st = std::addressof(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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_promises(shared_state_base &st)
|
|
|
|
{
|
|
|
|
promise_base *next
|
|
|
|
{
|
|
|
|
promise_base::head(st)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; next; next = next->next)
|
|
|
|
next->st = nullptr;
|
2018-08-29 03:56:41 +02:00
|
|
|
}
|
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2020-05-03 21:53:21 +02:00
|
|
|
// condition_variable
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Wake up the next context waiting on the condition_variable
|
|
|
|
///
|
|
|
|
/// Unlike notify_one(), the next context in the queue is repositioned in the
|
|
|
|
/// back before being woken up for fairness.
|
|
|
|
void
|
|
|
|
ircd::ctx::condition_variable::notify()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
ctx *c;
|
|
|
|
if(!(c = q.pop_front()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
q.push_back(c);
|
|
|
|
ircd::ctx::notify(*c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake up the next context waiting on the dock
|
|
|
|
void
|
|
|
|
ircd::ctx::condition_variable::notify_one()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(q.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ircd::ctx::notify(*q.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake up all contexts waiting on the condition_variable.
|
|
|
|
///
|
|
|
|
/// We post all notifications without requesting direct context
|
|
|
|
/// switches. This ensures everyone gets notified in a single
|
|
|
|
/// transaction without any interleaving during this process.
|
|
|
|
void
|
|
|
|
ircd::ctx::condition_variable::notify_all()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.for_each([this](ctx &c)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2020-05-03 21:53:21 +02:00
|
|
|
{
|
|
|
|
ircd::ctx::notify(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake up all contexts waiting on the condition_variable to throw an
|
|
|
|
/// interrupt exception.
|
|
|
|
void
|
|
|
|
ircd::ctx::condition_variable::interrupt_all()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.for_each([this](ctx &c)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2020-05-03 21:53:21 +02:00
|
|
|
{
|
|
|
|
ircd::ctx::interrupt(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake up all contexts waiting on the condition_variable to throw an
|
|
|
|
/// interrupt exception.
|
|
|
|
void
|
|
|
|
ircd::ctx::condition_variable::terminate_all()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.for_each([this](ctx &c)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2020-05-03 21:53:21 +02:00
|
|
|
{
|
|
|
|
ircd::ctx::terminate(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The number of contexts waiting in the queue.
|
|
|
|
bool
|
|
|
|
ircd::ctx::condition_variable::waiting(const ctx &a)
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
// for_each returns false if a was found
|
2020-07-12 00:28:24 +02:00
|
|
|
return !q.for_each(list::closure_bool_const{[&a](const ctx &b)
|
|
|
|
noexcept
|
2020-05-03 21:53:21 +02:00
|
|
|
{
|
|
|
|
// return false to break on equal
|
|
|
|
return std::addressof(a) != std::addressof(b);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-01-13 22:57:34 +01:00
|
|
|
// dock.h
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Wake up the next context waiting on the dock
|
|
|
|
///
|
|
|
|
/// Unlike notify_one(), the next context in the queue is repositioned in the
|
|
|
|
/// back before being woken up for fairness.
|
|
|
|
void
|
|
|
|
ircd::ctx::dock::notify()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
ctx *c;
|
|
|
|
if(!(c = q.pop_front()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
q.push_back(c);
|
2019-10-10 04:01:45 +02:00
|
|
|
ircd::ctx::notify(*c);
|
2019-01-13 22:57:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake up all contexts waiting on the dock.
|
|
|
|
///
|
|
|
|
/// We post all notifications without requesting direct context
|
|
|
|
/// switches. This ensures everyone gets notified in a single
|
|
|
|
/// transaction without any interleaving during this process.
|
|
|
|
void
|
|
|
|
ircd::ctx::dock::notify_all()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.for_each([this](ctx &c)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2019-01-13 22:57:34 +01:00
|
|
|
{
|
2019-10-10 04:01:45 +02:00
|
|
|
ircd::ctx::notify(c);
|
2019-01-13 22:57:34 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-20 01:16:53 +01:00
|
|
|
/// Wake up all contexts waiting on the dock to throw an interrupt exception.
|
|
|
|
void
|
|
|
|
ircd::ctx::dock::interrupt_all()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.for_each([this](ctx &c)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2020-02-20 01:16:53 +01:00
|
|
|
{
|
|
|
|
ircd::ctx::interrupt(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wake up all contexts waiting on the dock to throw an interrupt exception.
|
|
|
|
void
|
|
|
|
ircd::ctx::dock::terminate_all()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.for_each([this](ctx &c)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2020-02-20 01:16:53 +01:00
|
|
|
{
|
|
|
|
ircd::ctx::terminate(c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
void
|
|
|
|
ircd::ctx::dock::wait()
|
|
|
|
{
|
|
|
|
assert(current);
|
2019-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional renotify{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
const unwind remove{[this]
|
|
|
|
{
|
|
|
|
q.remove(current);
|
|
|
|
}};
|
|
|
|
|
|
|
|
q.push_back(current);
|
2020-02-25 20:20:16 +01:00
|
|
|
this_ctx::wait();
|
2019-01-13 22:57:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::dock::wait(const predicate &pred)
|
|
|
|
{
|
|
|
|
if(pred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(current);
|
2019-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional renotify{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
const unwind remove{[this]
|
|
|
|
{
|
|
|
|
q.remove(current);
|
|
|
|
}};
|
|
|
|
|
|
|
|
q.push_back(current); do
|
|
|
|
{
|
2020-02-25 20:20:16 +01:00
|
|
|
this_ctx::wait();
|
2019-01-13 22:57:34 +01:00
|
|
|
}
|
|
|
|
while(!pred());
|
|
|
|
}
|
|
|
|
|
2022-06-29 22:03:04 +02:00
|
|
|
template<>
|
2019-09-10 19:42:55 +02:00
|
|
|
bool
|
2022-06-29 22:03:04 +02:00
|
|
|
ircd::ctx::dock::wait_for(const microseconds dur)
|
2019-09-10 19:42:55 +02:00
|
|
|
{
|
2022-06-29 22:03:04 +02:00
|
|
|
static const microseconds zero {0};
|
|
|
|
|
|
|
|
assert(current);
|
|
|
|
const unwind_exceptional renotify{[this]
|
2019-09-10 19:42:55 +02:00
|
|
|
{
|
2022-06-29 22:03:04 +02:00
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
|
|
|
const unwind remove{[this]
|
|
|
|
{
|
|
|
|
q.remove(current);
|
|
|
|
}};
|
|
|
|
|
|
|
|
q.push_back(current);
|
|
|
|
return ircd::ctx::wait<std::nothrow_t>(dur) > zero;
|
2019-09-10 19:42:55 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 22:03:04 +02:00
|
|
|
template<>
|
|
|
|
bool
|
|
|
|
ircd::ctx::dock::wait_for(const microseconds dur,
|
|
|
|
const predicate &pred)
|
2019-01-13 22:57:34 +01:00
|
|
|
{
|
2022-06-29 22:03:04 +02:00
|
|
|
static const microseconds zero {0};
|
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(current);
|
|
|
|
const unwind_exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
|
|
|
const unwind remove{[this]
|
|
|
|
{
|
|
|
|
q.remove(current);
|
|
|
|
}};
|
|
|
|
|
|
|
|
q.push_back(current); do
|
|
|
|
{
|
|
|
|
const bool expired
|
|
|
|
{
|
|
|
|
ircd::ctx::wait<std::nothrow_t>(dur) <= zero
|
|
|
|
};
|
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(expired)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ctx::dock::wait_until(const system_point tp)
|
|
|
|
{
|
|
|
|
assert(current);
|
|
|
|
const unwind_exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
|
|
|
const unwind remove{[this]
|
|
|
|
{
|
|
|
|
q.remove(current);
|
|
|
|
}};
|
|
|
|
|
|
|
|
q.push_back(current);
|
|
|
|
return !ircd::ctx::wait_until<std::nothrow_t>(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if predicate passed; false if timed out
|
|
|
|
bool
|
|
|
|
ircd::ctx::dock::wait_until(const system_point tp,
|
|
|
|
const predicate &pred)
|
|
|
|
{
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(current);
|
|
|
|
const unwind_exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
|
|
|
const unwind remove{[this]
|
|
|
|
{
|
|
|
|
q.remove(current);
|
|
|
|
}};
|
|
|
|
|
|
|
|
q.push_back(current); do
|
|
|
|
{
|
|
|
|
const bool expired
|
|
|
|
{
|
|
|
|
ircd::ctx::wait_until<std::nothrow_t>(tp)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(expired)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(1);
|
2019-01-13 22:57:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The number of contexts waiting in the queue.
|
|
|
|
bool
|
2022-06-29 22:03:04 +02:00
|
|
|
ircd::ctx::dock::waiting(const ctx &a)
|
2019-09-10 19:19:50 +02:00
|
|
|
const noexcept
|
2019-01-13 22:57:34 +01:00
|
|
|
{
|
2022-06-29 22:03:04 +02:00
|
|
|
// for_each returns false if a was found
|
|
|
|
return !q.for_each(list::closure_bool_const{[&a](const ctx &b)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
// return false to break on equal
|
|
|
|
return std::addressof(a) != std::addressof(b);
|
|
|
|
}});
|
2019-01-13 22:57:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 08:10:44 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ctx_list.h
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ctx::list::remove(ctx *const &c)
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
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()
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
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()
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
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)
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
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)
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-10 19:19:50 +02:00
|
|
|
size_t
|
|
|
|
ircd::ctx::list::size()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
size_t i{0};
|
|
|
|
for_each([&i](const ctx &)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2019-09-10 19:19:50 +02:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
});
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-01-14 08:10:44 +01:00
|
|
|
void
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::rfor_each(const closure &closure)
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
|
|
|
for(ctx *tail{this->tail}; tail; tail = prev(tail))
|
|
|
|
closure(*tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::rfor_each(const closure_const &closure)
|
2018-01-14 08:10:44 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const ctx *tail{this->tail}; tail; tail = prev(tail))
|
|
|
|
closure(*tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::rfor_each(const closure_bool &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
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::rfor_each(const closure_bool_const &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
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::for_each(const closure &closure)
|
2018-04-19 23:47:56 +02:00
|
|
|
{
|
|
|
|
for(ctx *head{this->head}; head; head = next(head))
|
|
|
|
closure(*head);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::for_each(const closure_const &closure)
|
2018-04-19 23:47:56 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
for(const ctx *head{this->head}; head; head = next(head))
|
|
|
|
closure(*head);
|
|
|
|
}
|
|
|
|
|
2018-01-14 08:10:44 +01:00
|
|
|
bool
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::for_each(const closure_bool &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
|
2019-09-10 19:42:25 +02:00
|
|
|
ircd::ctx::list::for_each(const closure_bool_const &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;
|
|
|
|
}
|
|
|
|
|
2020-08-23 04:37:51 +02:00
|
|
|
[[gnu::hot]]
|
2020-07-12 03:21:38 +02:00
|
|
|
ircd::ctx::list::node &
|
|
|
|
ircd::ctx::list::get(ctx &c)
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
2020-07-12 03:21:38 +02:00
|
|
|
return c.node;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
|
|
|
|
2020-08-23 04:37:51 +02:00
|
|
|
[[gnu::hot]]
|
2020-07-12 03:21:38 +02:00
|
|
|
const ircd::ctx::list::node &
|
|
|
|
ircd::ctx::list::get(const ctx &c)
|
2019-09-10 19:19:50 +02:00
|
|
|
noexcept
|
2018-01-14 08:10:44 +01:00
|
|
|
{
|
2020-07-12 03:21:38 +02:00
|
|
|
return c.node;
|
2018-01-14 08:10:44 +01:00
|
|
|
}
|
2020-02-25 19:24:37 +01:00
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2020-10-14 04:48:55 +02:00
|
|
|
//
|
2020-10-14 10:34:23 +02:00
|
|
|
// ctx/stack.h
|
2020-10-14 04:48:55 +02:00
|
|
|
//
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
[[gnu::hot]]
|
|
|
|
ircd::ctx::stack &
|
|
|
|
ircd::ctx::stack::get(ctx &ctx)
|
|
|
|
noexcept
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
return ctx.stack;
|
2020-10-14 04:48:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
[[gnu::hot]]
|
|
|
|
const ircd::ctx::stack &
|
|
|
|
ircd::ctx::stack::get(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return ctx.stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stack::stack
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ctx::stack::stack(const mutable_buffer &buf)
|
|
|
|
noexcept
|
|
|
|
:buf
|
|
|
|
{
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
,max
|
|
|
|
{
|
2020-10-15 11:13:28 +02:00
|
|
|
//note: ircd::size() asserts because begin(buf) is nullptr, but that's ok
|
|
|
|
//ircd::size(buf)
|
|
|
|
size_t(std::distance(begin(buf), end(buf)))
|
2020-10-14 10:34:23 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// stack::allocator
|
|
|
|
//
|
|
|
|
|
2020-10-14 04:48:55 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
2020-10-14 10:34:23 +02:00
|
|
|
ircd::ctx::stack::allocator
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
|
|
|
using stack_context = boost::coroutines::stack_context;
|
|
|
|
|
|
|
|
mutable_buffer &buf;
|
2020-10-14 10:34:23 +02:00
|
|
|
bool owner {false};
|
2020-10-14 04:48:55 +02:00
|
|
|
|
|
|
|
void allocate(stack_context &, size_t size);
|
|
|
|
void deallocate(stack_context &);
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2020-10-14 10:34:23 +02:00
|
|
|
ircd::ctx::stack::allocator::allocate(stack_context &c,
|
|
|
|
size_t size)
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
|
|
|
static const auto &alignment
|
|
|
|
{
|
|
|
|
info::page_size
|
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
unique_mutable_buffer umb
|
|
|
|
{
|
|
|
|
null(this->buf)? size: 0, alignment
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer &buf
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
umb? umb: this->buf
|
2020-10-14 04:48:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
c.size = ircd::size(buf);
|
|
|
|
c.sp = ircd::data(buf) + c.size;
|
|
|
|
|
|
|
|
#if defined(BOOST_USE_VALGRIND)
|
2020-10-16 19:02:16 +02:00
|
|
|
if(vg::active)
|
|
|
|
c.valgrind_stack_id = vg::stack::add(buf);
|
2020-10-14 04:48:55 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
this->owner = bool(umb);
|
|
|
|
this->buf = umb? umb.release(): this->buf;
|
2020-10-14 04:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-14 10:34:23 +02:00
|
|
|
ircd::ctx::stack::allocator::deallocate(stack_context &c)
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
|
|
|
assert(c.sp);
|
|
|
|
|
|
|
|
#if defined(BOOST_USE_VALGRIND)
|
2020-10-16 19:02:16 +02:00
|
|
|
if(vg::active)
|
|
|
|
vg::stack::del(c.valgrind_stack_id);
|
2020-10-14 04:48:55 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
const auto base
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
(reinterpret_cast<uintptr_t>(c.sp) - c.size)
|
|
|
|
& boolmask<uintptr_t>(owner)
|
2020-10-14 04:48:55 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
std::free(reinterpret_cast<void *>(base));
|
2020-10-14 04:48:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 19:24:37 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// (internal) boost::asio
|
|
|
|
//
|
|
|
|
|
2020-10-14 04:48:55 +02:00
|
|
|
template<class Function>
|
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
boost::asio::detail::spawn_data
|
2020-10-14 02:40:06 +02:00
|
|
|
<
|
2020-10-14 04:48:55 +02:00
|
|
|
boost::asio::executor_binder<void (*)(), boost::asio::strand<boost::asio::executor>>,
|
|
|
|
Function
|
|
|
|
>
|
|
|
|
{
|
|
|
|
using Handler = boost::asio::executor_binder<void (*)(), boost::asio::strand<boost::asio::executor>>;
|
|
|
|
using caller_type = typename basic_yield_context<Handler>::caller_type;
|
|
|
|
using callee_type = typename basic_yield_context<Handler>::callee_type;
|
|
|
|
|
|
|
|
weak_ptr<callee_type> coro_;
|
|
|
|
Function function_;
|
|
|
|
Handler handler_;
|
2020-10-14 10:34:23 +02:00
|
|
|
ircd::ctx::ctx *ctrl;
|
2020-10-14 04:48:55 +02:00
|
|
|
|
|
|
|
template<class H,
|
|
|
|
class F>
|
|
|
|
spawn_data(H&& handler, bool call_handler, F&& function)
|
|
|
|
:function_(std::move(function))
|
|
|
|
,handler_(std::move(handler))
|
2020-10-14 10:34:23 +02:00
|
|
|
,ctrl{ircd::ctx::ctx::spawning}
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
|
|
|
assert(call_handler);
|
2020-10-14 10:34:23 +02:00
|
|
|
assert(ctrl);
|
2020-10-14 04:48:55 +02:00
|
|
|
}
|
|
|
|
};
|
2020-10-14 02:40:06 +02:00
|
|
|
|
2020-10-14 04:15:09 +02:00
|
|
|
template<class Function>
|
|
|
|
struct [[gnu::visibility("hidden")]]
|
2020-10-14 10:34:23 +02:00
|
|
|
boost::asio::detail::coro_entry_point
|
2020-10-14 04:15:09 +02:00
|
|
|
<
|
|
|
|
boost::asio::executor_binder<void (*)(), boost::asio::strand<boost::asio::executor>>,
|
|
|
|
Function
|
|
|
|
>
|
|
|
|
{
|
|
|
|
using Handler = boost::asio::executor_binder<void (*)(), boost::asio::strand<boost::asio::executor>>;
|
2020-10-14 10:34:23 +02:00
|
|
|
using caller_type = typename basic_yield_context<Handler>::caller_type;
|
2020-10-14 04:15:09 +02:00
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
void operator()(caller_type &ca) // pull
|
2020-10-14 04:15:09 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
const auto data
|
2020-10-14 04:48:55 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
this->data
|
2020-10-14 04:48:55 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
basic_yield_context<Handler> yc
|
2020-10-14 04:15:09 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
data->coro_, ca, data->handler_
|
2020-10-14 04:15:09 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
(data->function_)(yc);
|
|
|
|
(data->handler_)();
|
2020-10-14 04:15:09 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
shared_ptr<spawn_data<Handler, Function>> data;
|
2020-10-14 04:15:09 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
// Hooks the first push phase of coroutine spawn to supply our own stack
|
|
|
|
// allocator.
|
2020-10-14 04:15:09 +02:00
|
|
|
template<class Function>
|
|
|
|
struct [[gnu::visibility("hidden")]]
|
2020-10-14 10:34:23 +02:00
|
|
|
boost::asio::detail::spawn_helper
|
2020-10-14 04:15:09 +02:00
|
|
|
<
|
|
|
|
boost::asio::executor_binder<void (*)(), boost::asio::strand<boost::asio::executor>>,
|
|
|
|
Function
|
|
|
|
>
|
|
|
|
{
|
|
|
|
using Handler = boost::asio::executor_binder<void (*)(), boost::asio::strand<boost::asio::executor>>;
|
2020-10-14 10:34:23 +02:00
|
|
|
using callee_type = typename basic_yield_context<Handler>::callee_type;
|
2020-10-14 04:15:09 +02:00
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
void operator()() // push
|
2020-10-14 04:15:09 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
const auto coro
|
2020-10-14 04:15:09 +02:00
|
|
|
{
|
2020-10-14 10:34:23 +02:00
|
|
|
std::make_shared<callee_type>
|
|
|
|
(
|
|
|
|
coro_entry_point<Handler, Function>{data_},
|
|
|
|
attributes_,
|
|
|
|
ircd::ctx::stack::allocator{data_->ctrl->stack.buf}
|
|
|
|
)
|
2020-10-14 04:15:09 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
data_->coro_ = coro;
|
|
|
|
(*coro)();
|
2020-10-14 04:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<spawn_data<Handler, Function>> data_;
|
2020-10-14 10:34:23 +02:00
|
|
|
boost::coroutines::attributes attributes_;
|
2020-10-14 04:15:09 +02:00
|
|
|
};
|
|
|
|
|
2020-10-14 04:48:55 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// (internal) boost::asio
|
|
|
|
//
|
2020-10-14 02:40:06 +02:00
|
|
|
|
2020-02-25 19:24:37 +01:00
|
|
|
//
|
|
|
|
// Optimize ctx::wake() by reimplementing the timer cancel's op scheduler to
|
|
|
|
// enqueue as a defer (private/priority queue) rather than to the post queue.
|
|
|
|
// This interposes the function for all callstacks in this translation unit,
|
|
|
|
// including primarily ctx::wake().
|
|
|
|
//
|
|
|
|
#if defined(BOOST_ASIO_HAS_EPOLL)
|
|
|
|
using epoll_time_traits = boost::asio::time_traits<boost::posix_time::ptime>;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
std::size_t
|
|
|
|
__attribute__((visibility("internal")))
|
|
|
|
boost::asio::detail::epoll_reactor::cancel_timer(timer_queue<epoll_time_traits> &queue,
|
|
|
|
typename timer_queue<epoll_time_traits>::per_timer_data &t,
|
|
|
|
std::size_t max)
|
|
|
|
{
|
2020-12-13 07:24:14 +01:00
|
|
|
constexpr bool post_priv {true};
|
|
|
|
constexpr bool post_defer {false};
|
|
|
|
constexpr bool do_dispatch {false};
|
|
|
|
constexpr bool poll_dispatch {false};
|
|
|
|
constexpr bool is_continuation {false};
|
|
|
|
|
|
|
|
auto *const thread_info
|
|
|
|
{
|
2022-07-25 21:10:59 +02:00
|
|
|
#if BOOST_VERSION >= 107600
|
|
|
|
static_cast<scheduler_thread_info *>(scheduler::top_of_thread_call_stack())
|
|
|
|
#else
|
|
|
|
static_cast<scheduler_thread_info *>(scheduler::thread_call_stack::top())
|
|
|
|
#endif
|
2020-12-13 07:24:14 +01:00
|
|
|
};
|
|
|
|
|
2020-02-25 19:24:37 +01:00
|
|
|
std::size_t ret;
|
|
|
|
op_queue<operation> ops;
|
|
|
|
{
|
|
|
|
const mutex::scoped_lock lock(mutex_);
|
|
|
|
ret = queue.cancel_timer(t, ops, max);
|
|
|
|
}
|
|
|
|
|
2022-07-25 21:10:59 +02:00
|
|
|
assert(thread_info);
|
2020-12-13 07:24:14 +01:00
|
|
|
if constexpr(post_priv)
|
|
|
|
thread_info->private_op_queue.push(ops);
|
|
|
|
|
|
|
|
else if constexpr(post_defer)
|
|
|
|
scheduler_.post_deferred_completions(ops);
|
2020-02-25 19:24:37 +01:00
|
|
|
|
2020-12-13 07:24:14 +01:00
|
|
|
else for(auto *op(ops.front()); op; ops.pop(), op = ops.front())
|
2020-02-25 19:24:37 +01:00
|
|
|
{
|
2020-12-13 07:24:14 +01:00
|
|
|
if constexpr(do_dispatch)
|
|
|
|
{
|
|
|
|
scheduler_.do_dispatch(op);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-25 19:24:37 +01:00
|
|
|
scheduler_.post_immediate_completion(op, is_continuation);
|
|
|
|
thread_info->private_outstanding_work -= is_continuation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|