more c++ like naming, not shared pointed, batch thread spin, assorted fuckery

This commit is contained in:
Drath
2025-09-13 23:55:30 +02:00
parent 9bcec42145
commit e628f07896

View File

@@ -3,14 +3,14 @@
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <functional>
#include <mutex>
#include <print>
#include <semaphore>
#include <stop_token>
#include <string>
#include <thread>
#include <vector>
//
#include <LCDDisplay.hpp>
#include <cpu.hpp>
@@ -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<std::string, 2> 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<sensors_t> 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<sensors_t> s) {
std::lock_guard<std::mutex> 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<sensors_t> 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<sensors_t> 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::microseconds>(std::chrono::steady_clock::now() - start).count());
@@ -83,8 +83,8 @@ void sensors_thread(std::stop_token st, std::shared_ptr<sensors_t> s) {
}
}
void display_thread(std::stop_token st, std::shared_ptr<sensors_t> 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<sensors_t> 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<sensors_t> pSensors = std::make_shared<sensors_t>();
// 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<std::function<void(std::stop_token st, Sensors * s)>> 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<std::jthread> threads;
for (auto& f : funcs)
threads.emplace_back(f, pSensors);
sMain.acquire();
}
delete pSensors;
std::println("Main thread exiting");
return 0;
}