diff --git a/logcat_thread.cpp b/logcat_thread.cpp index c6b4b48..bc92c01 100644 --- a/logcat_thread.cpp +++ b/logcat_thread.cpp @@ -7,26 +7,31 @@ #define EPOLL_MAX_EVENTS 10 -LogcatThreadState::LogcatThreadState() { - this->epoll_fd = epoll_create1(EPOLL_CLOEXEC); - if (this->epoll_fd == -1) { +LogcatThread::LogcatThread() { + this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC); + if (this->_epoll_fd == -1) { throw make_system_error("epoll_create1()"); } + this->_thread = std::thread(&LogcatThread::_run, this); } -LogcatThreadState::~LogcatThreadState() { - if (close(this->epoll_fd)) { +LogcatThread::~LogcatThread() { + if (this->_epoll_fd != -1 && close(this->_epoll_fd)) { log(std::string("Failed to close epoll file descriptor: ") + make_system_error("close()").what()); } } -void run_logcat_thread(LogcatThreadState* state) { +void LogcatThread::join() { + this->_thread.join(); +} + +void LogcatThread::_run() { struct epoll_event events[EPOLL_MAX_EVENTS]; // TODO break when run_main_loop is false while (true) { printf("(boop)\n"); - int ready_fds = epoll_wait(state->epoll_fd, events, EPOLL_MAX_EVENTS, 1000); + int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000); if (ready_fds == -1) { printf("%s\n", format_log(make_system_error("epoll_wait()").what()).c_str()); break; diff --git a/logcat_thread.h b/logcat_thread.h index 84098f5..cf8976c 100644 --- a/logcat_thread.h +++ b/logcat_thread.h @@ -1,14 +1,20 @@ #pragma once -struct LogcatThreadState { +#include + +class LogcatThread { +public: // https://stackoverflow.com/a/2173764 - LogcatThreadState(const LogcatThreadState&) = delete; - LogcatThreadState& operator=(const LogcatThreadState&) = delete; + LogcatThread(const LogcatThread&) = delete; + LogcatThread& operator=(const LogcatThread&) = delete; - LogcatThreadState(); - ~LogcatThreadState(); + LogcatThread(); + ~LogcatThread(); + void join(); - int epoll_fd; +private: + void _run(); + + int _epoll_fd = -1; + std::thread _thread; }; - -void run_logcat_thread(LogcatThreadState* state); diff --git a/main.cpp b/main.cpp index 26fb1f7..a8de46f 100644 --- a/main.cpp +++ b/main.cpp @@ -41,8 +41,8 @@ int main(int, char**) { // depending on whether SDL_INIT_GAMECONTROLLER is enabled or disabled.. updating to the latest version of SDL is recommended!) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { - printf("Error: %s\n", SDL_GetError()); - return -1; + fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError()); + return 1; } // Decide GL+GLSL versions @@ -131,8 +131,14 @@ int main(int, char**) { // Main loop bool run_event_loop = true; float config_write_timer = 0.0f; - LogcatThreadState logcat_thread_state; - std::thread logcat_thread(run_logcat_thread, &logcat_thread_state); + LogcatThread logcat_thread = [&]() { + try { + return LogcatThread(); + } catch (const std::exception& e) { + fprintf(stderr, "Failed to spawn logcat thread: %s\n", e.what()); + exit(1); + } + }(); while (run_event_loop) { // Poll and handle events (inputs, window resize, etc.)