2018-05-06 06:20:25 +02: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.
|
|
|
|
|
2019-07-11 23:04:49 +02:00
|
|
|
/// We make a special use of the stack-protector canary in certain places as
|
|
|
|
/// another tool to detect corruption of a context's stack, specifically
|
|
|
|
/// during yield and resume. This use is not really to provide security; just
|
|
|
|
/// a kind of extra assertion, so we eliminate its emission during release.
|
|
|
|
#if !defined(NDEBUG) && !defined(__clang__)
|
|
|
|
#define IRCD_CTX_STACK_PROTECT __attribute__((stack_protect))
|
|
|
|
#else
|
|
|
|
#define IRCD_CTX_STACK_PROTECT
|
|
|
|
#endif
|
|
|
|
|
2019-05-26 11:07:43 +02:00
|
|
|
namespace ircd::ctx::prof
|
|
|
|
{
|
2020-08-02 15:20:22 +02:00
|
|
|
static void mark(const event &);
|
2019-05-26 11:07:43 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 06:20:25 +02:00
|
|
|
/// Internal context implementation
|
|
|
|
///
|
|
|
|
struct ircd::ctx::ctx
|
|
|
|
:instance_list<ctx>
|
|
|
|
{
|
2020-05-19 04:04:31 +02:00
|
|
|
using flags_type = std::underlying_type<context::flags>::type;
|
|
|
|
|
2018-05-06 06:20:25 +02:00
|
|
|
static uint64_t id_ctr; // monotonic
|
2019-07-22 02:55:58 +02:00
|
|
|
static ios::descriptor ios_desc;
|
2020-08-21 13:18:41 +02:00
|
|
|
static ios::handler ios_handler;
|
2020-10-14 10:34:23 +02:00
|
|
|
static ctx *spawning;
|
2020-10-28 06:51:14 +01:00
|
|
|
static dock adjoindre; // contexts waiting for join
|
2018-05-06 06:20:25 +02:00
|
|
|
|
|
|
|
uint64_t id {++id_ctr}; // Unique runtime ID
|
2018-12-17 00:40:57 +01:00
|
|
|
string_view name; // User given name (optional)
|
2020-05-19 04:04:31 +02:00
|
|
|
flags_type flags; // User given flags
|
2020-01-06 20:40:37 +01:00
|
|
|
int8_t nice {0}; // Scheduling priority nice-value
|
|
|
|
int8_t ionice {0}; // IO priority nice-value (defaults for fs::opts)
|
2018-12-07 19:13:29 +01:00
|
|
|
int32_t notes {0}; // norm: 0 = asleep; 1 = awake; inc by others; dec by self
|
2019-09-23 05:02:23 +02:00
|
|
|
boost::asio::deadline_timer alarm; // acting semaphore (64B)
|
2018-05-06 06:20:25 +02:00
|
|
|
boost::asio::yield_context *yc {nullptr}; // boost interface
|
2018-05-23 03:06:18 +02:00
|
|
|
continuation *cont {nullptr}; // valid when asleep; invalid when awake
|
2018-05-06 06:20:25 +02:00
|
|
|
list::node node; // node for ctx::list
|
2018-12-22 23:53:23 +01:00
|
|
|
ircd::ctx::stack stack; // stack related structure
|
|
|
|
prof::ticker profile; // prof related structure
|
2018-05-06 06:20:25 +02:00
|
|
|
|
2019-08-06 01:15:56 +02:00
|
|
|
bool started() const noexcept; // context was ever entered
|
|
|
|
bool finished() const noexcept; // context will not be further entered.
|
2019-09-22 21:49:56 +02:00
|
|
|
bool interruption() const noexcept;
|
2018-05-06 06:20:25 +02:00
|
|
|
|
2019-08-06 01:15:56 +02:00
|
|
|
bool interruption_point(std::nothrow_t) noexcept;
|
|
|
|
bool termination_point(std::nothrow_t) noexcept;
|
|
|
|
void interruption_point();
|
2018-05-06 06:20:25 +02:00
|
|
|
|
2019-08-06 01:15:56 +02:00
|
|
|
bool wake() noexcept; // jump to context by queueing with ios (use note())
|
|
|
|
bool note() noexcept; // properly request wake()
|
2018-12-07 19:13:29 +01:00
|
|
|
bool wait(); // yield context to ios queue (returns on this resume)
|
2018-12-07 21:03:25 +01:00
|
|
|
void jump(); // jump to context directly (returns on your resume)
|
2018-05-06 06:20:25 +02:00
|
|
|
|
2020-10-14 10:34:23 +02:00
|
|
|
void operator()(boost::asio::yield_context, const std::function<void ()>) noexcept;
|
2019-07-24 22:21:48 +02:00
|
|
|
void spawn(context::function func);
|
2018-05-06 06:20:25 +02:00
|
|
|
|
2020-02-27 19:11:59 +01:00
|
|
|
ctx(const string_view &name = "<noname>"_sv,
|
2020-10-14 10:34:23 +02:00
|
|
|
const ircd::ctx::stack & = mutable_buffer{nullptr, DEFAULT_STACK_SIZE},
|
2020-02-27 19:11:59 +01:00
|
|
|
const context::flags & = (context::flags)0U);
|
2018-05-06 06:20:25 +02:00
|
|
|
|
|
|
|
ctx(ctx &&) = delete;
|
|
|
|
ctx(const ctx &) = delete;
|
|
|
|
ctx &operator=(ctx &&) = delete;
|
|
|
|
ctx &operator=(const ctx &) = delete;
|
2018-08-20 03:09:04 +02:00
|
|
|
~ctx() noexcept;
|
2018-05-06 06:20:25 +02:00
|
|
|
};
|