SignalHandler: add helper async_sigwait_handler
This commit is contained in:
parent
346c835468
commit
7636620625
@ -6,9 +6,11 @@
|
|||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <future>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
namespace sgnl {
|
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:
|
private:
|
||||||
sigset_t set_;
|
sigset_t set_;
|
||||||
};
|
};
|
||||||
|
@ -163,6 +163,37 @@ TEST_CASE("sigwait_handler")
|
|||||||
REQUIRE( signal_handler.sigwait_handler(handler) == SIGUSR2 );
|
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")
|
TEST_CASE("constructor-thread-blocks-signals")
|
||||||
{
|
{
|
||||||
std::atomic last_signal(0);
|
std::atomic last_signal(0);
|
||||||
|
Reference in New Issue
Block a user