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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_H
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Userspace Contexts: cooperative threading from stackful coroutines.
|
|
|
|
///
|
|
|
|
/// This is the public interface to the userspace context system. No 3rd party
|
|
|
|
/// symbols are included from here. This file is included automatically in stdinc.h
|
|
|
|
/// and you do not have to include it manually.
|
|
|
|
///
|
|
|
|
/// There are two primary objects at work in the context system:
|
|
|
|
///
|
|
|
|
/// `struct context` <ircd/ctx/context.h>
|
|
|
|
/// Public interface emulating std::thread; included automatically from here.
|
|
|
|
/// To spawn and manipulate contexts, deal with this object.
|
|
|
|
///
|
|
|
|
/// `struct ctx` (ircd/ctx.cc)
|
|
|
|
/// Internal implementation of the context. This is not included here.
|
|
|
|
/// Several low-level functions are exposed for library creators. This file is usually
|
|
|
|
/// included when boost/asio.hpp is also included and calls are actually made into boost.
|
|
|
|
///
|
2017-09-21 04:31:54 +02:00
|
|
|
/// boost::asio is not included from here. To access that include boost in a
|
|
|
|
/// definition file with #include <ircd/asio.h>. That include contains some
|
|
|
|
/// devices we use to yield a context to asio.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
2017-09-20 04:01:37 +02:00
|
|
|
struct ctx;
|
|
|
|
|
2018-01-08 21:40:09 +01:00
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
IRCD_EXCEPTION(error, interrupted)
|
|
|
|
IRCD_EXCEPTION(error, timeout)
|
2020-05-19 04:13:53 +02:00
|
|
|
struct terminated {}; // Special exception
|
2018-01-08 21:40:09 +01:00
|
|
|
|
|
|
|
IRCD_OVERLOAD(threadsafe)
|
2020-05-05 01:07:30 +02:00
|
|
|
bool is_main_thread() noexcept;
|
|
|
|
void assert_main_thread();
|
2018-01-08 21:40:09 +01:00
|
|
|
|
2020-05-19 04:13:53 +02:00
|
|
|
const uint64_t &id(const ctx &) noexcept; // Unique ID for context
|
|
|
|
string_view name(const ctx &) noexcept; // User's optional label for context
|
|
|
|
const uint32_t &flags(const ctx &) noexcept; // Direct flags access
|
|
|
|
const int32_t ¬es(const ctx &) noexcept; // Peeks at internal semaphore count
|
|
|
|
const uint64_t &epoch(const ctx &) noexcept; // Context switching counter
|
|
|
|
const ulong &cycles(const ctx &) noexcept; // Accumulated tsc (not counting cur slice)
|
|
|
|
const int8_t &ionice(const ctx &) noexcept; // IO priority nice-value
|
|
|
|
const int8_t &nice(const ctx &) noexcept; // Scheduling priority nice-value
|
|
|
|
bool interruptible(const ctx &) noexcept; // Context can throw at interruption point
|
|
|
|
bool interruption(const ctx &) noexcept; // Context was marked for interruption
|
|
|
|
bool termination(const ctx &) noexcept; // Context was marked for termination
|
|
|
|
bool finished(const ctx &) noexcept; // Context function returned (or exception).
|
|
|
|
bool started(const ctx &) noexcept; // Context was ever entered.
|
|
|
|
bool running(const ctx &) noexcept; // Context is the currently running ctx.
|
|
|
|
bool waiting(const ctx &) noexcept; // started() && !finished() && !running()
|
|
|
|
bool queued(const ctx &) noexcept; // !running() && notes() > 0
|
2017-09-20 04:01:37 +02:00
|
|
|
|
2020-05-19 04:13:53 +02:00
|
|
|
uint32_t &flags(ctx &) noexcept; // Direct flags access
|
|
|
|
int8_t ionice(ctx &, const int8_t &) noexcept; // IO priority nice-value
|
|
|
|
int8_t nice(ctx &, const int8_t &) noexcept; // Scheduling priority nice-value
|
2020-11-13 10:43:44 +01:00
|
|
|
void name(ctx &, const string_view &) noexcept; // Change the name (truncates to 15 chars)
|
2020-05-19 04:13:53 +02:00
|
|
|
void interruptible(ctx &, const bool &) noexcept; // False for interrupt suppression.
|
|
|
|
void interrupt(ctx &); // Interrupt the context.
|
|
|
|
void terminate(ctx &); // Interrupt for termination.
|
|
|
|
void signal(ctx &, std::function<void ()>); // Post function to context strand
|
|
|
|
void notify(ctx &, threadsafe_t); // Notify context with threadsafety.
|
|
|
|
bool notify(ctx &) noexcept; // Queue a context switch to arg
|
|
|
|
void yield(ctx &); // Direct context switch to arg
|
2019-04-20 05:50:22 +02:00
|
|
|
|
|
|
|
bool for_each(const std::function<bool (ctx &)> &);
|
2019-08-06 01:15:56 +02:00
|
|
|
const uint64_t &epoch() noexcept;
|
2019-05-26 11:18:14 +02:00
|
|
|
|
|
|
|
extern log::log log;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2018-12-18 01:00:22 +01:00
|
|
|
#include "prof.h"
|
2018-03-25 08:50:39 +02:00
|
|
|
#include "this_ctx.h"
|
2020-05-19 04:22:18 +02:00
|
|
|
#include "context.h"
|
2020-02-28 18:11:06 +01:00
|
|
|
#include "wait.h"
|
|
|
|
#include "sleep.h"
|
2020-04-23 14:05:57 +02:00
|
|
|
#include "stack.h"
|
2018-11-11 23:13:37 +01:00
|
|
|
#include "stack_usage_assertion.h"
|
2018-12-18 01:00:22 +01:00
|
|
|
#include "slice_usage_warning.h"
|
2018-11-11 23:13:37 +01:00
|
|
|
#include "critical_assertion.h"
|
|
|
|
#include "critical_indicator.h"
|
|
|
|
#include "exception_handler.h"
|
|
|
|
#include "uninterruptible.h"
|
2018-01-14 08:10:44 +01:00
|
|
|
#include "list.h"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "dock.h"
|
2018-08-19 04:28:39 +02:00
|
|
|
#include "latch.h"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "queue.h"
|
|
|
|
#include "shared_mutex.h"
|
2018-08-27 03:01:43 +02:00
|
|
|
#include "upgrade_lock.h"
|
2018-03-29 04:19:34 +02:00
|
|
|
#include "unlock_guard.h"
|
2018-06-01 19:37:51 +02:00
|
|
|
#include "condition_variable.h"
|
2019-03-21 23:50:42 +01:00
|
|
|
#include "scope_notify.h"
|
2020-10-29 08:08:06 +01:00
|
|
|
#include "mutex.h"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "view.h"
|
|
|
|
#include "shared_state.h"
|
|
|
|
#include "promise.h"
|
|
|
|
#include "future.h"
|
2018-03-14 19:25:11 +01:00
|
|
|
#include "when.h"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "async.h"
|
|
|
|
#include "pool.h"
|
|
|
|
#include "ole.h"
|
|
|
|
#include "fault.h"
|
2019-07-13 00:18:26 +02:00
|
|
|
#include "concurrent.h"
|
2019-07-14 22:40:04 +02:00
|
|
|
#include "concurrent_for_each.h"
|
2019-09-30 21:30:57 +02:00
|
|
|
#include "trit.h"
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2018-01-08 21:40:09 +01:00
|
|
|
// Exports to ircd::
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
2017-09-20 04:01:37 +02:00
|
|
|
//using yield = boost::asio::yield_context;
|
2018-12-07 04:16:47 +01:00
|
|
|
namespace this_ctx = ctx::this_ctx;
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
using ctx::timeout;
|
|
|
|
using ctx::context;
|
|
|
|
using ctx::sleep;
|
2017-10-12 02:44:32 +02:00
|
|
|
|
|
|
|
using ctx::promise;
|
|
|
|
using ctx::future;
|
2018-01-08 21:40:09 +01:00
|
|
|
|
|
|
|
using ctx::use_future_t;
|
|
|
|
using ctx::use_future;
|
2018-01-12 03:37:08 +01:00
|
|
|
|
|
|
|
using ctx::critical_assertion;
|
2018-05-07 21:38:24 +02:00
|
|
|
using ctx::critical_indicator;
|
2020-05-05 01:07:30 +02:00
|
|
|
|
|
|
|
using ctx::is_main_thread;
|
|
|
|
using ctx::assert_main_thread;
|
|
|
|
}
|
|
|
|
|
2020-06-18 12:20:14 +02:00
|
|
|
/// Marks `ctx` for whether to allow or suppress interruption. Suppression
|
|
|
|
/// does not ignore an interrupt itself, it only ignores the interruption
|
|
|
|
/// points. Thus when a suppression ends if the interrupt flag was ever set
|
|
|
|
/// the next interruption point will throw as expected.
|
|
|
|
inline void
|
|
|
|
ircd::ctx::interruptible(ctx &ctx,
|
|
|
|
const bool &b)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
flags(ctx) ^= (flags(ctx) ^ (ulong(b) - 1)) & context::NOINTERRUPT;
|
|
|
|
assert(bool(flags(ctx) & context::NOINTERRUPT) == !b);
|
|
|
|
assert(interruptible(ctx) == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if `ctx` was terminated; does not clear the flag
|
|
|
|
inline bool
|
|
|
|
ircd::ctx::termination(const ctx &c)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return flags(c) & context::TERMINATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if `ctx` was interrupted; does not clear the flag
|
|
|
|
inline bool
|
|
|
|
ircd::ctx::interruption(const ctx &c)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return flags(c) & context::INTERRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::ctx::interruptible(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return ~flags(ctx) & context::NOINTERRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cycle count for `ctx`
|
|
|
|
inline const ulong &
|
|
|
|
ircd::ctx::cycles(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return prof::get(ctx, prof::event::CYCLES);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the yield count for `ctx`
|
|
|
|
inline const uint64_t &
|
|
|
|
ircd::ctx::epoch(const ctx &ctx)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return prof::get(ctx, prof::event::YIELD);
|
|
|
|
}
|
|
|
|
|
2020-05-05 01:07:30 +02:00
|
|
|
inline void
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::ctx::assert_main_thread()
|
|
|
|
{
|
|
|
|
assert(is_main_thread());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
__attribute__((always_inline))
|
|
|
|
ircd::ctx::is_main_thread()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return current ||
|
|
|
|
std::this_thread::get_id() == ios::main_thread_id;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|