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,40 +249,46 @@ 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());
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
try {
|
||||
eloop = std::make_unique<eventloop>();
|
||||
auto loop = eloop->get();
|
||||
|
||||
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); });
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
close_loop(m_loop.get());
|
||||
uv_loop_close(m_loop.get());
|
||||
// TODO handle return value
|
||||
try {
|
||||
close_loop(m_loop.get());
|
||||
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