From e628f078964561fe84b30ef9a7dfa2d79701494c Mon Sep 17 00:00:00 2001 From: Drath Date: Sat, 13 Sep 2025 23:55:30 +0200 Subject: [PATCH] more c++ like naming, not shared pointed, batch thread spin, assorted fuckery --- main.cpp | 57 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/main.cpp b/main.cpp index 68915bb..4cd6a7c 100644 --- a/main.cpp +++ b/main.cpp @@ -3,14 +3,14 @@ #include #include #include -#include -#include +#include #include #include #include #include #include #include +#include // #include #include @@ -27,7 +27,7 @@ std::binary_semaphore sDisp{0}; // halts display func until we have sensor data std::binary_semaphore sMain{0}; // halts the main thread until SIGINT -struct sensors_t { +struct Sensors { std::array td; std::string loadavg; mem_usage_t ram; @@ -40,12 +40,12 @@ struct sensors_t { void sighandler(int s) { std::println("[Signal] got SIGINT/SIGTERM, exiting..."); - sDisp.release(); sMain.release(); + sDisp.release(); } -void hass_thread(std::stop_token st, std::shared_ptr s) { - std::println("[Thread] Starting hass_thread, pid {}", gettid()); +void hassThread(std::stop_token st, Sensors* s) { + // std::println("[Thread] Starting hass_thread, pid {}", gettid()); HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc"); pc_power.connect(); @@ -56,12 +56,12 @@ void hass_thread(std::stop_token st, std::shared_ptr s) { std::lock_guard lock(s->mtx); s->hass_state = pc_power.state; } - std::this_thread::sleep_for(std::chrono::milliseconds(50)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } -void sensors_thread(std::stop_token st, std::shared_ptr s) { - std::println("[Thread] Starting sensors_thread, pid {}, poll interval {} ms", gettid(), LCD_INTERVAL_MS); +void sensorsThread(std::stop_token st, Sensors* s) { + // std::println("[Thread] Starting sensors_thread, pid {}, poll interval {} ms", gettid(), LCD_INTERVAL_MS); cpu_hwmon_t cpu_hwmon = detectCpuHwmon("0000:00:18.3"); std::string gpu_hwmon = detectGpuHwmon("0000:08:00.0"); @@ -74,7 +74,7 @@ void sensors_thread(std::stop_token st, std::shared_ptr s) { s->ram = getRam(); s->home = getDisk("/home/drath"); s->cpu = getCpu(cpu_hwmon); - s->gpu = getGpu(gpu_hwmon); + s->gpu = getGpu(gpu_hwmon, false); } // std::println("[Sensors] took {} microseconds", std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count()); @@ -83,8 +83,8 @@ void sensors_thread(std::stop_token st, std::shared_ptr s) { } } -void display_thread(std::stop_token st, std::shared_ptr s) { - std::println("[Thread] Starting display_thread, pid {}", gettid()); +void displayThread(std::stop_token st, Sensors* s) { + // std::println("[Thread] Starting display_thread, pid {}", gettid()); LCDDisplay lcd(LCD_PORT); lcd.setContrast(LCD_CONTRAST); @@ -125,26 +125,35 @@ void display_thread(std::stop_token st, std::shared_ptr s) { lcd.clearBuf(); lcd.writeBuf(1, 10, "linuxSmartie closed"); - // lcd.flushBuf(); + lcd.flushBuf(); } int main(int argc, char* argv[]) { std::signal(SIGINT, sighandler); std::signal(SIGTERM, sighandler); - std::println("[Thread] Main thread, pid {}", gettid()); + // std::println("[Thread] Main thread, pid {}", gettid()); - std::shared_ptr pSensors = std::make_shared(); + // not worth using smart pointer, we dont need to fw pointer ownership + Sensors* pSensors = new Sensors; + std::println("sensors: {}", sizeof(Sensors)); - std::jthread disp(display_thread, pSensors); - std::jthread hass(hass_thread, pSensors); - std::jthread sens(sensors_thread, pSensors); + std::vector> funcs = {displayThread, hassThread, sensorsThread}; - // halt main thread until we trigger sighandler - sMain.acquire(); + /* wtf is this? + this spins up funcs as threads and halts until sMain semaphore is released in sighandler + its scoped because when sMain is released, program waits until all the threads join back before leaving the scope + threads object has to be inside the scope too because destruction of std::jthread is what actually makes threads join. + */ - // main thread resumed, tell workers to stop - hass.request_stop(); - sens.request_stop(); - disp.request_stop(); + { + std::vector threads; + for (auto& f : funcs) + threads.emplace_back(f, pSensors); + + sMain.acquire(); + } + + delete pSensors; + std::println("Main thread exiting"); return 0; }