diff options
Diffstat (limited to 'src/signals.cc')
| -rw-r--r-- | src/signals.cc | 22 |
1 files changed, 12 insertions, 10 deletions
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 <cerrno> #include <csignal> #include <cstdint> #include <functional> #include <memory> +#include <sys/eventfd.h> #include <unistd.h> #include <unordered_map> #include <utility> @@ -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<void()> 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<void()> callback_; |
