SignalHandler: add helper async_sigwait_handler

This commit is contained in:
Tom 2020-07-17 16:51:53 +02:00
parent 346c835468
commit 7636620625
2 changed files with 42 additions and 0 deletions

View File

@ -6,9 +6,11 @@
#include <csignal>
#include <cstring>
#include <functional>
#include <future>
#include <initializer_list>
#include <stdexcept>
#include <string>
#include <utility>
namespace sgnl {
@ -78,6 +80,15 @@ public:
}
}
auto async_sigwait_handler(std::function<bool (int)> handler) const
{
return std::async(
std::launch::async,
&SignalHandler::sigwait_handler,
this,
std::move(handler));
}
private:
sigset_t set_;
};

View File

@ -163,6 +163,37 @@ TEST_CASE("sigwait_handler")
REQUIRE( signal_handler.sigwait_handler(handler) == SIGUSR2 );
}
TEST_CASE("async_sigwait_handler")
{
sgnl::SignalHandler signal_handler({SIGUSR1,SIGUSR2});
auto handler = [](int){
return true;
};
auto future = signal_handler.async_sigwait_handler(handler);
kill(0, SIGUSR1);
REQUIRE( future.get() == SIGUSR1 );
}
TEST_CASE("async_sigwait_handler-condition")
{
sgnl::SignalHandler signal_handler({SIGUSR1});
sgnl::AtomicCondition<int> condition(0);
REQUIRE( condition.get() == 0 );
REQUIRE( condition.get() != SIGUSR1 );
auto handler = [&condition](int signum){
condition.set_and_notify_one(signum);
return true;
};
auto future = signal_handler.async_sigwait_handler(handler);
kill(0, SIGUSR1);
REQUIRE( future.get() == SIGUSR1 );
REQUIRE( condition.get() == SIGUSR1 );
}
TEST_CASE("constructor-thread-blocks-signals")
{
std::atomic last_signal(0);