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
|
|
|
using std::chrono::steady_clock;
|
|
|
|
using time_point = steady_clock::time_point;
|
|
|
|
|
|
|
|
struct ctx;
|
|
|
|
|
2018-01-08 21:40:09 +01:00
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
IRCD_EXCEPTION(error, interrupted)
|
|
|
|
IRCD_EXCEPTION(error, timeout)
|
2018-05-06 10:00:11 +02:00
|
|
|
struct terminated {}; // Special exception
|
2018-01-08 21:40:09 +01:00
|
|
|
|
|
|
|
IRCD_OVERLOAD(threadsafe)
|
|
|
|
|
2018-04-20 00:37:58 +02:00
|
|
|
extern const std::list<ctx *> &ctxs; // List of all ctx instances
|
|
|
|
|
2017-09-20 04:01:37 +02:00
|
|
|
const uint64_t &id(const ctx &); // Unique ID for context
|
|
|
|
string_view name(const ctx &); // User's optional label for context
|
2018-04-20 01:07:46 +02:00
|
|
|
const size_t &stack_max(const ctx &); // Returns stack size allocated for ctx
|
2018-05-06 07:58:25 +02:00
|
|
|
const size_t &stack_at(const ctx &); // Stack at last sleep (also see this_ctx.h)
|
2017-09-20 04:01:37 +02:00
|
|
|
const int64_t ¬es(const ctx &); // Peeks at internal semaphore count
|
2018-05-07 21:36:33 +02:00
|
|
|
const uint64_t &yields(const ctx &); // Context switching counter
|
2018-05-06 07:09:20 +02:00
|
|
|
const ulong &cycles(const ctx &); // Accumulated tsc (not counting cur slice)
|
2018-05-22 09:00:26 +02:00
|
|
|
bool interruptible(const ctx &); // Context can throw at interruption point
|
2017-09-20 04:01:37 +02:00
|
|
|
bool interruption(const ctx &); // Context was marked for interruption
|
2018-05-06 10:00:11 +02:00
|
|
|
bool termination(const ctx &); // Context was marked for termination
|
2017-09-20 04:01:37 +02:00
|
|
|
bool finished(const ctx &); // Context function returned (or exception).
|
|
|
|
bool started(const ctx &); // Context was ever entered.
|
2018-04-20 00:48:14 +02:00
|
|
|
bool running(const ctx &); // Context is the currently running ctx.
|
|
|
|
bool waiting(const ctx &); // started() && !finished() && !running()
|
2017-09-20 04:01:37 +02:00
|
|
|
|
2018-05-22 09:00:26 +02:00
|
|
|
void interruptible(ctx &, const bool &); // False for interrupt suppression.
|
2018-05-06 10:00:11 +02:00
|
|
|
void interrupt(ctx &); // Interrupt the context.
|
|
|
|
void terminate(ctx &); // Interrupt for termination.
|
2017-09-20 04:01:37 +02:00
|
|
|
void signal(ctx &, std::function<void ()>); // Post function to context strand
|
|
|
|
void notify(ctx &, threadsafe_t); // Notify context with threadsafety.
|
|
|
|
bool notify(ctx &); // Queue a context switch to arg
|
|
|
|
void yield(ctx &); // Direct context switch to arg
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-09-05 17:53:36 +02:00
|
|
|
|
2018-03-25 08:50:39 +02:00
|
|
|
#include "this_ctx.h"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "context.h"
|
|
|
|
#include "prof.h"
|
2018-01-14 08:10:44 +01:00
|
|
|
#include "list.h"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "dock.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "shared_mutex.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"
|
2017-10-19 10:32:53 +02:00
|
|
|
#include "peek.h"
|
|
|
|
#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"
|
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;
|
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;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|