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
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;

View File

@ -1,14 +1,20 @@
#pragma once
struct LogcatThreadState {
#include <thread>
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);

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!)
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.)