Use a class for the logcat thread

This commit is contained in:
blankie 2023-01-17 20:56:56 +07:00
parent 2778fb039b
commit ace07768ee
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 36 additions and 19 deletions

View File

@ -7,26 +7,31 @@
#define EPOLL_MAX_EVENTS 10 #define EPOLL_MAX_EVENTS 10
LogcatThreadState::LogcatThreadState() { LogcatThread::LogcatThread() {
this->epoll_fd = epoll_create1(EPOLL_CLOEXEC); this->_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (this->epoll_fd == -1) { if (this->_epoll_fd == -1) {
throw make_system_error("epoll_create1()"); throw make_system_error("epoll_create1()");
} }
this->_thread = std::thread(&LogcatThread::_run, this);
} }
LogcatThreadState::~LogcatThreadState() { LogcatThread::~LogcatThread() {
if (close(this->epoll_fd)) { if (this->_epoll_fd != -1 && close(this->_epoll_fd)) {
log(std::string("Failed to close epoll file descriptor: ") + make_system_error("close()").what()); 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]; struct epoll_event events[EPOLL_MAX_EVENTS];
// TODO break when run_main_loop is false // TODO break when run_main_loop is false
while (true) { while (true) {
printf("(boop)\n"); 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) { if (ready_fds == -1) {
printf("%s\n", format_log(make_system_error("epoll_wait()").what()).c_str()); printf("%s\n", format_log(make_system_error("epoll_wait()").what()).c_str());
break; break;

View File

@ -1,14 +1,20 @@
#pragma once #pragma once
struct LogcatThreadState { #include <thread>
class LogcatThread {
public:
// https://stackoverflow.com/a/2173764 // https://stackoverflow.com/a/2173764
LogcatThreadState(const LogcatThreadState&) = delete; LogcatThread(const LogcatThread&) = delete;
LogcatThreadState& operator=(const LogcatThreadState&) = delete; LogcatThread& operator=(const LogcatThread&) = delete;
LogcatThreadState(); LogcatThread();
~LogcatThreadState(); ~LogcatThread();
void join();
int epoll_fd; private:
void _run();
int _epoll_fd = -1;
std::thread _thread;
}; };
void run_logcat_thread(LogcatThreadState* state);

View File

@ -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!) // 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) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{ {
printf("Error: %s\n", SDL_GetError()); fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError());
return -1; return 1;
} }
// Decide GL+GLSL versions // Decide GL+GLSL versions
@ -131,8 +131,14 @@ int main(int, char**) {
// Main loop // Main loop
bool run_event_loop = true; bool run_event_loop = true;
float config_write_timer = 0.0f; float config_write_timer = 0.0f;
LogcatThreadState logcat_thread_state; LogcatThread logcat_thread = [&]() {
std::thread logcat_thread(run_logcat_thread, &logcat_thread_state); 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) { while (run_event_loop) {
// Poll and handle events (inputs, window resize, etc.) // Poll and handle events (inputs, window resize, etc.)