eventloop: Throw exception for libuv errors
This commit is contained in:
parent
3cc17a0e57
commit
60ee63c0db
@ -2,10 +2,22 @@
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
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>
|
||||
struct cb_helper {
|
||||
std::function<void(Args...)> func;
|
||||
@ -19,8 +31,7 @@ struct cb_helper {
|
||||
struct SignalHandle {
|
||||
SignalHandle(uv_loop_t* loop, std::function<void(int)> fun) {
|
||||
handle = std::make_unique<uv_signal_t>();
|
||||
// TODO handle return value
|
||||
uv_signal_init(loop, handle.get());
|
||||
UV(uv_signal_init, loop, handle.get());
|
||||
cb = cb_helper<uv_signal_t, int>{fun};
|
||||
|
||||
handle->data = &cb;
|
||||
@ -48,7 +59,7 @@ class eventloop {
|
||||
|
||||
void signal_handler(int signum, std::function<void(int)> 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));
|
||||
}
|
||||
|
||||
|
@ -204,6 +204,7 @@ static void conn_cb_wrapper(uv_poll_t* handle, int status, int events) {
|
||||
}
|
||||
|
||||
void controller::signal_handler(int signum) {
|
||||
m_log.notice("Received signal SIG%s", sigabbrev_np(signum));
|
||||
g_terminate = 1;
|
||||
g_reload = (signum == SIGUSR1);
|
||||
eloop->stop();
|
||||
@ -248,10 +249,14 @@ static void ipc_read_cb_wrapper(uv_stream_t* stream, ssize_t nread, const uv_buf
|
||||
void controller::read_events() {
|
||||
m_log.info("Entering event loop (thread-id=%lu)", this_thread::get_id());
|
||||
|
||||
auto conn_handle = std::make_unique<uv_poll_t>();
|
||||
auto conf_handle = std::unique_ptr<uv_fs_event_t>(nullptr);
|
||||
auto ipc_handle = std::unique_ptr<uv_pipe_t>(nullptr);
|
||||
|
||||
try {
|
||||
eloop = std::make_unique<eventloop>();
|
||||
auto loop = eloop->get();
|
||||
|
||||
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;
|
||||
|
||||
@ -261,8 +266,6 @@ void controller::read_events() {
|
||||
eloop->signal_handler(s, [this](int signum) { signal_handler(signum); });
|
||||
}
|
||||
|
||||
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());
|
||||
@ -271,8 +274,6 @@ void controller::read_events() {
|
||||
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);
|
||||
|
||||
if (m_ipc) {
|
||||
ipc_handle = std::make_unique<uv_pipe_t>();
|
||||
uv_pipe_init(loop, ipc_handle.get(), false);
|
||||
@ -282,6 +283,12 @@ void controller::read_events() {
|
||||
}
|
||||
|
||||
eloop->run();
|
||||
} catch (const exception& err) {
|
||||
m_log.err("Fatal Error in eventloop: %s", err.what());
|
||||
g_terminate = 1;
|
||||
eloop->stop();
|
||||
}
|
||||
|
||||
eloop.reset();
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,7 @@ POLYBAR_NS
|
||||
|
||||
eventloop::eventloop() {
|
||||
m_loop = std::make_unique<uv_loop_t>();
|
||||
uv_loop_init(m_loop.get());
|
||||
// TODO handle return value
|
||||
|
||||
UV(uv_loop_init, m_loop.get());
|
||||
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) {
|
||||
uv_walk(loop, close_walk_cb, nullptr);
|
||||
uv_run(loop, UV_RUN_DEFAULT);
|
||||
// TODO handle return value
|
||||
UV(uv_run, loop, UV_RUN_DEFAULT);
|
||||
}
|
||||
|
||||
eventloop::~eventloop() {
|
||||
if (m_loop) {
|
||||
try {
|
||||
close_loop(m_loop.get());
|
||||
uv_loop_close(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();
|
||||
}
|
||||
}
|
||||
|
||||
void eventloop::run() {
|
||||
uv_run(m_loop.get(), UV_RUN_DEFAULT);
|
||||
// TODO handle return value
|
||||
UV(uv_run, m_loop.get(), UV_RUN_DEFAULT);
|
||||
}
|
||||
|
||||
void eventloop::stop() {
|
||||
uv_stop(m_loop.get());
|
||||
// TODO handle return value
|
||||
}
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
Loading…
Reference in New Issue
Block a user