eventloop: Throw exception for libuv errors

This commit is contained in:
patrick96 2021-03-11 00:09:06 +01:00 committed by Patrick Ziegler
parent 3cc17a0e57
commit 60ee63c0db
3 changed files with 58 additions and 42 deletions

View File

@ -2,10 +2,22 @@
#include <uv.h> #include <uv.h>
#include <stdexcept>
#include "common.hpp" #include "common.hpp"
POLYBAR_NS POLYBAR_NS
/**
* Runs any libuv function with an integer error code return value and throws an
* exception on error.
*/
#define UV(fun, ...) \
int res = fun(__VA_ARGS__); \
if (res < 0) { \
throw std::runtime_error("libuv error for '" #fun "': "s + uv_strerror(res)); \
}
template <class H, class... Args> template <class H, class... Args>
struct cb_helper { struct cb_helper {
std::function<void(Args...)> func; std::function<void(Args...)> func;
@ -19,8 +31,7 @@ struct cb_helper {
struct SignalHandle { struct SignalHandle {
SignalHandle(uv_loop_t* loop, std::function<void(int)> fun) { SignalHandle(uv_loop_t* loop, std::function<void(int)> fun) {
handle = std::make_unique<uv_signal_t>(); handle = std::make_unique<uv_signal_t>();
// TODO handle return value UV(uv_signal_init, loop, handle.get());
uv_signal_init(loop, handle.get());
cb = cb_helper<uv_signal_t, int>{fun}; cb = cb_helper<uv_signal_t, int>{fun};
handle->data = &cb; handle->data = &cb;
@ -48,7 +59,7 @@ class eventloop {
void signal_handler(int signum, std::function<void(int)> fun) { void signal_handler(int signum, std::function<void(int)> fun) {
auto handle = std::make_unique<SignalHandle>(get(), fun); auto handle = std::make_unique<SignalHandle>(get(), fun);
uv_signal_start(handle->handle.get(), &handle->cb.callback, signum); UV(uv_signal_start, handle->handle.get(), &handle->cb.callback, signum);
m_sig_handles.push_back(std::move(handle)); m_sig_handles.push_back(std::move(handle));
} }

View File

@ -204,6 +204,7 @@ static void conn_cb_wrapper(uv_poll_t* handle, int status, int events) {
} }
void controller::signal_handler(int signum) { void controller::signal_handler(int signum) {
m_log.notice("Received signal SIG%s", sigabbrev_np(signum));
g_terminate = 1; g_terminate = 1;
g_reload = (signum == SIGUSR1); g_reload = (signum == SIGUSR1);
eloop->stop(); eloop->stop();
@ -248,40 +249,46 @@ static void ipc_read_cb_wrapper(uv_stream_t* stream, ssize_t nread, const uv_buf
void controller::read_events() { void controller::read_events() {
m_log.info("Entering event loop (thread-id=%lu)", this_thread::get_id()); m_log.info("Entering event loop (thread-id=%lu)", this_thread::get_id());
eloop = std::make_unique<eventloop>();
auto loop = eloop->get();
auto conn_handle = std::make_unique<uv_poll_t>(); auto conn_handle = std::make_unique<uv_poll_t>();
uv_poll_init(loop, conn_handle.get(), m_connection.get_file_descriptor());
conn_handle->data = this;
uv_poll_start(conn_handle.get(), UV_READABLE, conn_cb_wrapper);
for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) {
eloop->signal_handler(s, [this](int signum) { signal_handler(signum); });
}
auto conf_handle = std::unique_ptr<uv_fs_event_t>(nullptr); auto conf_handle = std::unique_ptr<uv_fs_event_t>(nullptr);
if (m_confwatch) {
conf_handle = std::make_unique<uv_fs_event_t>();
uv_fs_event_init(loop, conf_handle.get());
conf_handle->data = this;
uv_fs_event_start(conf_handle.get(), confwatch_cb_wrapper, m_confwatch->path().c_str(), 0);
}
auto ipc_handle = std::unique_ptr<uv_pipe_t>(nullptr); auto ipc_handle = std::unique_ptr<uv_pipe_t>(nullptr);
if (m_ipc) { try {
ipc_handle = std::make_unique<uv_pipe_t>(); eloop = std::make_unique<eventloop>();
uv_pipe_init(loop, ipc_handle.get(), false); auto loop = eloop->get();
ipc_handle->data = this;
uv_pipe_open(ipc_handle.get(), m_ipc->get_file_descriptor()); uv_poll_init(loop, conn_handle.get(), m_connection.get_file_descriptor());
uv_read_start((uv_stream_t*)ipc_handle.get(), ipc_alloc_cb, ipc_read_cb_wrapper); conn_handle->data = this;
uv_poll_start(conn_handle.get(), UV_READABLE, conn_cb_wrapper);
for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) {
eloop->signal_handler(s, [this](int signum) { signal_handler(signum); });
}
if (m_confwatch) {
conf_handle = std::make_unique<uv_fs_event_t>();
uv_fs_event_init(loop, conf_handle.get());
conf_handle->data = this;
uv_fs_event_start(conf_handle.get(), confwatch_cb_wrapper, m_confwatch->path().c_str(), 0);
}
if (m_ipc) {
ipc_handle = std::make_unique<uv_pipe_t>();
uv_pipe_init(loop, ipc_handle.get(), false);
ipc_handle->data = this;
uv_pipe_open(ipc_handle.get(), m_ipc->get_file_descriptor());
uv_read_start((uv_stream_t*)ipc_handle.get(), ipc_alloc_cb, ipc_read_cb_wrapper);
}
eloop->run();
} catch (const exception& err) {
m_log.err("Fatal Error in eventloop: %s", err.what());
g_terminate = 1;
eloop->stop();
} }
eloop->run();
eloop.reset(); eloop.reset();
} }

View File

@ -4,9 +4,7 @@ POLYBAR_NS
eventloop::eventloop() { eventloop::eventloop() {
m_loop = std::make_unique<uv_loop_t>(); m_loop = std::make_unique<uv_loop_t>();
uv_loop_init(m_loop.get()); UV(uv_loop_init, m_loop.get());
// TODO handle return value
m_loop->data = this; m_loop->data = this;
} }
@ -23,28 +21,28 @@ static void close_walk_cb(uv_handle_t* handle, void*) {
*/ */
static void close_loop(uv_loop_t* loop) { static void close_loop(uv_loop_t* loop) {
uv_walk(loop, close_walk_cb, nullptr); uv_walk(loop, close_walk_cb, nullptr);
uv_run(loop, UV_RUN_DEFAULT); UV(uv_run, loop, UV_RUN_DEFAULT);
// TODO handle return value
} }
eventloop::~eventloop() { eventloop::~eventloop() {
if (m_loop) { if (m_loop) {
close_loop(m_loop.get()); try {
uv_loop_close(m_loop.get()); close_loop(m_loop.get());
// TODO handle return value UV(uv_loop_close, m_loop.get());
} catch (const std::exception& e) {
// TODO log error
}
m_loop.reset(); m_loop.reset();
} }
} }
void eventloop::run() { void eventloop::run() {
uv_run(m_loop.get(), UV_RUN_DEFAULT); UV(uv_run, m_loop.get(), UV_RUN_DEFAULT);
// TODO handle return value
} }
void eventloop::stop() { void eventloop::stop() {
uv_stop(m_loop.get()); uv_stop(m_loop.get());
// TODO handle return value
} }
POLYBAR_NS_END POLYBAR_NS_END