From e0a0365383a3baf1a13b68c476421a85d29a244b Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 20 Oct 2025 22:07:49 +0200 Subject: signals: Replace pipe with eventfd Simplify code. --- src/signals.cc | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src/signals.cc') diff --git a/src/signals.cc b/src/signals.cc index 9f8bff7..5ac1a32 100644 --- a/src/signals.cc +++ b/src/signals.cc @@ -1,13 +1,14 @@ #include "signals.hh" #include "looper.hh" -#include "unique_pipe.hh" +#include "unique_fd.hh" #include #include #include #include #include +#include #include #include #include @@ -31,9 +32,9 @@ int signum(Signal signal) { void signal_handler(int signum) { auto it = g_fds.find(signum); if (it != g_fds.end()) { - char c = 1; + uint64_t c = 1; while (true) { - auto ret = write(it->second, &c, 1); + auto ret = write(it->second, &c, sizeof(c)); if (ret < 0 && errno == EINTR) continue; break; @@ -45,27 +46,28 @@ class HandlerImpl : public Handler { public: HandlerImpl(looper::Looper& looper, Signal signal, std::function callback) - : looper_(looper), + : fd_{eventfd(0, 0)}, + looper_(looper), signal_(signum(signal)), callback_(std::move(callback)) { - looper_.add(pipe_.reader(), looper::EVENT_READ, + looper_.add(fd_.get(), looper::EVENT_READ, [this](auto event) { call(event); }); - g_fds[signal_] = pipe_.writer(); + g_fds[signal_] = fd_.get(); ::signal(signal_, signal_handler); } ~HandlerImpl() override { ::signal(signal_, SIG_DFL); - looper_.remove(pipe_.reader()); + looper_.remove(fd_.get()); g_fds.erase(signal_); } private: void call(uint8_t /* event */) { - char buf[10]; + uint64_t c; while (true) { - auto ret = read(pipe_.reader(), buf, 10); + auto ret = read(fd_.get(), &c, sizeof(c)); if (ret < 0 && errno == EINTR) continue; break; @@ -73,7 +75,7 @@ class HandlerImpl : public Handler { callback_(); } - unique_pipe pipe_; + unique_fd fd_; looper::Looper& looper_; int signal_; std::function callback_; -- cgit v1.2.3-70-g09d2