From d83568e568e73694ff15d94b035631d7846d58d2 Mon Sep 17 00:00:00 2001 From: Andrew Nelless Date: Mon, 22 Feb 2016 16:49:24 +0000 Subject: [PATCH] Initial stab at Boost context backend for MTasker --- pdns/mtasker_fcontext.cc | 71 +++++++++++++++++++++++++++ pdns/recursordist/Makefile.am | 5 +- pdns/recursordist/configure.ac | 1 + pdns/recursordist/mtasker_fcontext.cc | 1 + 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 pdns/mtasker_fcontext.cc create mode 120000 pdns/recursordist/mtasker_fcontext.cc diff --git a/pdns/mtasker_fcontext.cc b/pdns/mtasker_fcontext.cc new file mode 100644 index 000000000..dfe345529 --- /dev/null +++ b/pdns/mtasker_fcontext.cc @@ -0,0 +1,71 @@ +#include "mtasker_context.hh" +#include +#include +#include + +using boost::context::fcontext_t; +using boost::context::jump_fcontext; +using boost::context::make_fcontext; + +struct args_t { + fcontext_t prev_ctx = nullptr; + pdns_ucontext_t* self = nullptr; + std::function* work = nullptr; +}; + +extern "C" { +static +void +threadWrapper (intptr_t const xargs) { + auto args = reinterpret_cast(xargs); + auto ctx = args->self; + auto work = args->work; + jump_fcontext (&ctx->uc_mcontext, args->prev_ctx, 0); + args = nullptr; + + try { + auto start = std::move (*work); + start(); + } catch (...) { + ctx->exception = std::current_exception(); + } + + auto const next_ctx = ctx->uc_link->uc_mcontext; + jump_fcontext (&ctx->uc_mcontext, next_ctx, + static_cast(ctx->exception)); +#ifdef NDEBUG + __builtin_unreachable(); +#endif +} +} + +pdns_ucontext_t::pdns_ucontext_t +(): uc_mcontext(nullptr), uc_link(nullptr) { +} + +pdns_ucontext_t::~pdns_ucontext_t +() { +} + +void +pdns_swapcontext +(pdns_ucontext_t& __restrict octx, pdns_ucontext_t const& __restrict ctx) { + if (jump_fcontext (&octx.uc_mcontext, ctx.uc_mcontext, 0)) { + std::rethrow_exception (ctx.exception); + } +} + +void +pdns_makecontext +(pdns_ucontext_t& ctx, std::function& start) { + assert (ctx.uc_link); + assert (ctx.uc_stack.size() >= 8192); + assert (!ctx.uc_mcontext); + ctx.uc_mcontext = make_fcontext (&ctx.uc_stack[ctx.uc_stack.size()], + ctx.uc_stack.size(), &threadWrapper); + args_t args; + args.self = &ctx; + args.work = &start; + jump_fcontext (&args.prev_ctx, ctx.uc_mcontext, + reinterpret_cast(&args)); +} diff --git a/pdns/recursordist/Makefile.am b/pdns/recursordist/Makefile.am index 48b256030..c73e1d6a6 100644 --- a/pdns/recursordist/Makefile.am +++ b/pdns/recursordist/Makefile.am @@ -85,7 +85,7 @@ pdns_recursor_SOURCES = \ misc.hh misc.cc \ mplexer.hh \ mtasker.hh \ - mtasker_context.hh mtasker_ucontext.cc \ + mtasker_context.hh mtasker_fcontext.cc \ namespaces.hh \ nsecrecords.cc \ opensslsigners.cc opensslsigners.hh \ @@ -127,7 +127,8 @@ pdns_recursor_SOURCES = \ pdns_recursor_LDADD = \ $(YAHTTP_LIBS) \ $(JSON11_LIBS) \ - $(OPENSSL_LIBS) + $(OPENSSL_LIBS) \ + $(BOOST_CONTEXT_LIBS) pdns_recursor_LDFLAGS = $(AM_LDFLAGS) \ $(OPENSSL_LDFLAGS) diff --git a/pdns/recursordist/configure.ac b/pdns/recursordist/configure.ac index 880a99345..72753c92d 100644 --- a/pdns/recursordist/configure.ac +++ b/pdns/recursordist/configure.ac @@ -34,6 +34,7 @@ AC_PROG_LIBTOOL PDNS_CHECK_OS BOOST_REQUIRE([1.35]) +BOOST_CONTEXT() PDNS_ENABLE_REPRODUCIBLE PDNS_WITH_LUAJIT diff --git a/pdns/recursordist/mtasker_fcontext.cc b/pdns/recursordist/mtasker_fcontext.cc new file mode 120000 index 000000000..cac593d9b --- /dev/null +++ b/pdns/recursordist/mtasker_fcontext.cc @@ -0,0 +1 @@ +../mtasker_fcontext.cc \ No newline at end of file -- 2.40.0