2018-03-25 08:50:39 +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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_THIS_CTX_H
|
|
|
|
|
|
|
|
/// Interface to the currently running context
|
2019-06-23 08:08:46 +02:00
|
|
|
namespace ircd::ctx {
|
2018-03-25 08:50:39 +02:00
|
|
|
inline namespace this_ctx
|
|
|
|
{
|
2019-08-06 01:15:56 +02:00
|
|
|
struct ctx &cur() noexcept; ///< Assumptional reference to *current
|
2019-08-14 04:42:31 +02:00
|
|
|
const uint64_t &id() noexcept; // Unique ID for cur ctx
|
|
|
|
string_view name() noexcept; // Optional label for cur ctx
|
2019-08-06 01:15:56 +02:00
|
|
|
ulong cycles() noexcept; // misc profiling related
|
|
|
|
bool interruption_requested() noexcept; // interruption(cur())
|
2018-03-25 08:50:39 +02:00
|
|
|
|
2019-08-14 04:42:31 +02:00
|
|
|
void interruption_point(); // throws if interruption_requested()
|
2018-11-11 23:13:37 +01:00
|
|
|
void wait(); // Returns when context is woken up.
|
|
|
|
void yield(); // Allow other contexts to run before returning.
|
2018-04-28 04:04:31 +02:00
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
// Return remaining time if notified; or <= 0 if not, and timeout thrown on throw overloads
|
|
|
|
microseconds wait(const microseconds &, const std::nothrow_t &);
|
|
|
|
template<class E, class duration> nothrow_overload<E, duration> wait(const duration &);
|
|
|
|
template<class E = timeout, class duration> throw_overload<E, duration> wait(const duration &);
|
|
|
|
|
|
|
|
// Returns false if notified; true if time point reached, timeout thrown on throw_overloads
|
2018-12-07 19:12:29 +01:00
|
|
|
bool wait_until(const steady_point &tp, const std::nothrow_t &);
|
|
|
|
template<class E> nothrow_overload<E, bool> wait_until(const steady_point &tp);
|
|
|
|
template<class E = timeout> throw_overload<E> wait_until(const steady_point &tp);
|
2018-03-25 08:50:39 +02:00
|
|
|
|
|
|
|
// Ignores notes. Throws if interrupted.
|
2018-12-07 19:12:29 +01:00
|
|
|
void sleep_until(const steady_point &tp);
|
2018-03-25 08:50:39 +02:00
|
|
|
template<class duration> void sleep(const duration &);
|
|
|
|
void sleep(const int &secs);
|
|
|
|
}}
|
|
|
|
|
2018-03-27 01:20:47 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
/// Points to the currently running context or null for main stack (do not modify)
|
|
|
|
extern __thread ctx *current;
|
|
|
|
}
|
|
|
|
|
2019-08-11 17:00:17 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
namespace this_ctx = ctx::this_ctx;
|
|
|
|
}
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
/// This overload matches ::sleep() and acts as a drop-in for ircd contexts.
|
|
|
|
/// interruption point.
|
|
|
|
inline void
|
|
|
|
ircd::ctx::this_ctx::sleep(const int &secs)
|
|
|
|
{
|
|
|
|
sleep(seconds(secs));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Yield the context for a period of time and ignore notifications. sleep()
|
|
|
|
/// is like wait() but it only returns after the timeout and not because of a
|
|
|
|
/// note.
|
|
|
|
/// interruption point.
|
|
|
|
template<class duration>
|
|
|
|
void
|
|
|
|
ircd::ctx::this_ctx::sleep(const duration &d)
|
|
|
|
{
|
|
|
|
sleep_until(steady_clock::now() + d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for a notification until a point in time. If there is a notification
|
|
|
|
/// then context continues normally. If there's never a notification then an
|
|
|
|
/// exception (= timeout) is thrown.
|
|
|
|
/// interruption point.
|
|
|
|
template<class E>
|
|
|
|
ircd::throw_overload<E>
|
2018-12-07 19:12:29 +01:00
|
|
|
ircd::ctx::this_ctx::wait_until(const steady_point &tp)
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
if(wait_until<std::nothrow_t>(tp))
|
2018-12-07 19:12:29 +01:00
|
|
|
throw E{};
|
2018-03-25 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for a notification until a point in time. If there is a notification
|
|
|
|
/// then returns true. If there's never a notification then returns false.
|
|
|
|
/// interruption point. this is not noexcept.
|
|
|
|
template<class E>
|
|
|
|
ircd::nothrow_overload<E, bool>
|
2018-12-07 19:12:29 +01:00
|
|
|
ircd::ctx::this_ctx::wait_until(const steady_point &tp)
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
return wait_until(tp, std::nothrow);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for a notification for at most some amount of time. If the duration is
|
|
|
|
/// reached without a notification then E (= timeout) is thrown. Otherwise,
|
|
|
|
/// returns the time remaining on the duration.
|
|
|
|
/// interruption point
|
|
|
|
template<class E,
|
|
|
|
class duration>
|
|
|
|
ircd::throw_overload<E, duration>
|
|
|
|
ircd::ctx::this_ctx::wait(const duration &d)
|
|
|
|
{
|
2018-12-07 21:03:25 +01:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
wait<std::nothrow_t>(d)
|
|
|
|
};
|
|
|
|
|
2018-12-07 19:12:29 +01:00
|
|
|
return ret <= duration(0)?
|
|
|
|
throw E{}:
|
|
|
|
ret;
|
2018-03-25 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait for a notification for some amount of time. This function returns
|
|
|
|
/// when a context is notified. It always returns the duration remaining which
|
|
|
|
/// will be <= 0 to indicate a timeout without notification.
|
|
|
|
/// interruption point. this is not noexcept.
|
|
|
|
template<class E,
|
|
|
|
class duration>
|
|
|
|
ircd::nothrow_overload<E, duration>
|
|
|
|
ircd::ctx::this_ctx::wait(const duration &d)
|
|
|
|
{
|
2018-12-07 21:03:25 +01:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
wait(duration_cast<microseconds>(d), std::nothrow)
|
|
|
|
};
|
2018-03-25 08:50:39 +02:00
|
|
|
|
|
|
|
return duration_cast<duration>(ret);
|
|
|
|
}
|
|
|
|
|
2019-08-14 04:42:31 +02:00
|
|
|
/// View the name of the currently running context, or "*" if no context is
|
|
|
|
/// currently running.
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::ctx::this_ctx::name()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return current? name(cur()) : "*"_sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate the current TSC (reference cycle count) accumulated for this
|
|
|
|
/// context only. This is done by first calculating a cycle count for the
|
|
|
|
/// current slice/epoch (see: ctx/prof.h) which is where the RDTSC sample
|
|
|
|
/// occurs. This count is added to an accumulator value saved in the ctx
|
|
|
|
/// structure. The accumulator value is updated at the end of each execution
|
|
|
|
/// slice, thus giving us the cycle count for this ctx only, up to this point.
|
|
|
|
extern inline ulong
|
|
|
|
__attribute__((flatten, always_inline, gnu_inline, artificial))
|
2019-07-24 01:21:22 +02:00
|
|
|
ircd::ctx::this_ctx::cycles()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2019-07-24 01:21:22 +02:00
|
|
|
{
|
2019-08-14 04:42:31 +02:00
|
|
|
const auto slice(prof::cur_slice_cycles());
|
|
|
|
const auto accumulated(cycles(cur()));
|
|
|
|
return accumulated + slice;
|
2019-07-24 01:21:22 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
/// Reference to the currently running context. Call if you expect to be in a
|
|
|
|
/// context. Otherwise use the ctx::current pointer.
|
|
|
|
inline ircd::ctx::ctx &
|
2019-08-14 04:42:31 +02:00
|
|
|
__attribute__((always_inline))
|
2018-03-25 08:50:39 +02:00
|
|
|
ircd::ctx::this_ctx::cur()
|
2019-08-06 01:15:56 +02:00
|
|
|
noexcept
|
2018-03-25 08:50:39 +02:00
|
|
|
{
|
|
|
|
assert(current);
|
|
|
|
return *current;
|
|
|
|
}
|