Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #5803 from ahupowerdns/zero-exception
Fix throwing exceptions from MThreads, plus add unit tests
  • Loading branch information
ahupowerdns committed Oct 10, 2017
2 parents b0694fe + 2eca39a commit 2b55d75
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pdns/mtasker.cc
Expand Up @@ -344,6 +344,8 @@ template<class Key, class Val>bool MTasker<Key,Val>::schedule(struct timeval* n
}
else if(i->ttd.tv_sec)
break;
else
++i;
}
}
return false;
Expand Down
12 changes: 7 additions & 5 deletions pdns/mtasker_fcontext.cc
Expand Up @@ -152,7 +152,7 @@ threadWrapper (transfer_t const t) {
#if BOOST_VERSION < 106100
jump_fcontext (reinterpret_cast<fcontext_t*>(&ctx->uc_mcontext),
static_cast<fcontext_t>(next_ctx),
static_cast<bool>(ctx->exception));
reinterpret_cast<intptr_t>(ctx));
#else
jump_fcontext (static_cast<fcontext_t>(next_ctx), 0);
#endif
Expand Down Expand Up @@ -189,10 +189,12 @@ pdns_swapcontext
or we switch back to pdns_swapcontext(),
in both case we will be returning from a call to jump_fcontext(). */
#if BOOST_VERSION < 106100
if (jump_fcontext (reinterpret_cast<fcontext_t*>(&octx.uc_mcontext),
static_cast<fcontext_t>(ctx.uc_mcontext), 0)) {
std::rethrow_exception (ctx.exception);
}
intptr_t ptr = jump_fcontext(reinterpret_cast<fcontext_t*>(&octx.uc_mcontext),
static_cast<fcontext_t>(ctx.uc_mcontext), 0);

auto origctx = reinterpret_cast<pdns_ucontext_t*>(ptr);
if(origctx && origctx->exception)
std::rethrow_exception (origctx->exception);
#else
transfer_t res = jump_fcontext (static_cast<fcontext_t>(ctx.uc_mcontext), &octx.uc_mcontext);
if (res.data) {
Expand Down
4 changes: 4 additions & 0 deletions pdns/recursordist/Makefile.am
Expand Up @@ -206,6 +206,7 @@ testrunner_SOURCES = \
ixfr.cc ixfr.hh \
logger.cc logger.hh \
misc.cc misc.hh \
mtasker_context.cc \
negcache.hh negcache.cc \
namespaces.hh \
nsecrecords.cc \
Expand Down Expand Up @@ -237,6 +238,7 @@ testrunner_SOURCES = \
test-iputils_hh.cc \
test-ixfr_cc.cc \
test-misc_hh.cc \
test-mtasker.cc \
test-nmtree.cc \
test-negcache_cc.cc \
test-rcpgenerator_cc.cc \
Expand All @@ -254,10 +256,12 @@ testrunner_SOURCES = \

testrunner_LDFLAGS = \
$(AM_LDFLAGS) \
$(BOOST_CONTEXT_LDFLAGS) \
$(BOOST_UNIT_TEST_FRAMEWORK_LDFLAGS) \
$(LIBCRYPTO_LDFLAGS)

testrunner_LDADD = \
$(BOOST_CONTEXT_LIBS) \
$(BOOST_UNIT_TEST_FRAMEWORK_LIBS) \
$(LIBCRYPTO_LIBS) \
$(RT_LIBS)
Expand Down
60 changes: 60 additions & 0 deletions pdns/recursordist/test-mtasker.cc
@@ -0,0 +1,60 @@
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include "mtasker.hh"

BOOST_AUTO_TEST_SUITE(mtasker_cc)

static int g_result;

static void doSomething(void* p)
{
MTasker<>* mt = reinterpret_cast<MTasker<>*>(p);
int i=12, o;
mt->waitEvent(i, &o);
g_result = o;

}

BOOST_AUTO_TEST_CASE(test_Simple) {
MTasker<> mt;
mt.makeThread(doSomething, &mt);
struct timeval now;
gettimeofday(&now, 0);
bool first=true;
int o=24;
for(;;) {
while(mt.schedule(&now));
if(first) {
mt.sendEvent(12, &o);
first=false;
}
if(mt.noProcesses())
break;
}
BOOST_CHECK_EQUAL(g_result, o);
}

static void willThrow(void* p)
{
throw std::runtime_error("Help!");
}


BOOST_AUTO_TEST_CASE(test_MtaskerException) {
BOOST_CHECK_THROW( {
MTasker<> mt;
mt.makeThread(willThrow, 0);
struct timeval now;

for(;;) {
mt.schedule(&now);
}
}, std::exception);
}
BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2b55d75

Please sign in to comment.