pass by reference instead of passing by pointer, still debating tbh

This commit is contained in:
2025-12-28 22:45:44 +01:00
parent 87580138e0
commit a6654dcff7

View File

@@ -7,7 +7,6 @@
#include <gpu.hpp>
#include <hass.hpp>
#include <log.hpp>
#include <memory>
#include <mutex>
#include <semaphore>
#include <stop_token>
@@ -40,7 +39,7 @@ void signalHandler(int) {
app::semaphores::display.release();
}
void hassThread(std::stop_token st, app::sensors* s) {
void hassThread(std::stop_token st, app::sensors& s) {
applog(LOG::START, "Thread started");
HomeAssistant pc_power("home.drath.win", "sensor.gniazdko_komputer_moc");
@@ -48,35 +47,33 @@ void hassThread(std::stop_token st, app::sensors* s) {
while (!st.stop_requested()) {
pc_power.service();
{
// locks mutex for the lifetime of the scope, unlocks when it goes out of scope
// mutex can only be locked in one place, so when its locked here, other threads need to wait until its unlocked before they can lock it again
std::lock_guard<std::mutex> lock(s->mtx);
s->hass_state = pc_power.state;
std::lock_guard<std::mutex> lock(s.mtx);
s.hass_state = pc_power.state;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void sensorsThread(std::stop_token st, app::sensors* s) {
void sensorsThread(std::stop_token st, app::sensors& s) {
applog(LOG::START, "Thread started, poll interval {} ms", lcd_interval_ms);
cpu::detectHwmon(&s->cpu, cpu_pciaddr);
gpu::detectHwmon(&s->gpu, gpu_pciaddr);
sys::detectHwmon(&s->sys, sys_sensor);
s->home.mountpoint = home_dir;
cpu::detectHwmon(&s.cpu, cpu_pciaddr);
gpu::detectHwmon(&s.gpu, gpu_pciaddr);
sys::detectHwmon(&s.sys, sys_sensor);
s.home.mountpoint = home_dir;
while (!st.stop_requested()) {
auto start = std::chrono::steady_clock::now();
{
std::lock_guard<std::mutex> lock(s->mtx);
std::lock_guard<std::mutex> lock(s.mtx);
sys::getTimeDate(&s->td);
getloadavg(s->avg, 3);
sys::getRam(&s->ram);
sys::getDisk(&s->home);
sys::getNetwork(&s->net, sys_ifname);
sys::updateSensors(&s->sys);
cpu::update(&s->cpu);
gpu::update(&s->gpu);
sys::getTimeDate(&s.td);
getloadavg(s.avg, 3);
sys::getRam(&s.ram);
sys::getDisk(&s.home);
sys::getNetwork(&s.net, sys_ifname);
sys::updateSensors(&s.sys);
cpu::update(&s.cpu);
gpu::update(&s.gpu);
}
// log(LOG::DBG, "took {} microseconds", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count());
@@ -85,7 +82,7 @@ void sensorsThread(std::stop_token st, app::sensors* s) {
}
}
void displayThread(std::stop_token st, app::sensors* s) {
void displayThread(std::stop_token st, app::sensors& s) {
applog(LOG::START, "Thread started");
// init internal variables of LCDDisplay class
LCDDisplay lcd(lcd_port);
@@ -108,43 +105,43 @@ void displayThread(std::stop_token st, app::sensors* s) {
while (!st.stop_requested()) {
app::semaphores::display.acquire(); // holds the loop until sensor data is ready
{
std::lock_guard<std::mutex> lock(s->mtx);
std::lock_guard<std::mutex> lock(s.mtx);
// prepare some tricky sensors
std::string gpu_temps = std::to_string(s->gpu.temp_edge);
if (s->gpu.temp_hotspot)
gpu_temps = utils::pad(lcd.cc(1) + std::to_string(s->gpu.temp_hotspot - s->gpu.temp_edge) + lcd.cc(0), 4) + " " + utils::pad(gpu_temps + "/" + std::to_string(s->gpu.temp_hotspot), 5);
std::string gpu_temps = std::to_string(s.gpu.temp_edge);
if (s.gpu.temp_hotspot)
gpu_temps = utils::pad(lcd.cc(1) + std::to_string(s.gpu.temp_hotspot - s.gpu.temp_edge) + lcd.cc(0), 4) + " " + utils::pad(gpu_temps + "/" + std::to_string(s.gpu.temp_hotspot), 5);
uint16_t cpu_fan = 0;
for (auto& fan : s->sys.fan_speed)
for (auto& fan : s.sys.fan_speed)
cpu_fan += fan;
cpu_fan /= 2;
// write to display buffer
lcd.writeBuf(0, 0, utils::pad(s->td.date, 11, utils::just::left) + " " + s->td.time + lcd.bar(7, s->gpu.usage_core / 100.0));
lcd.writeBuf(0, 26, utils::pNum(s->avg[0], s->avg[0] > 10 ? 1 : 2));
lcd.writeBuf(0, 31, utils::pNum(s->avg[1], s->avg[1] > 10 ? 1 : 2));
lcd.writeBuf(0, 36, utils::pNum(s->avg[2], s->avg[2] > 10 ? 1 : 2));
lcd.writeBuf(0, 0, utils::pad(s.td.date, 11, utils::just::left) + " " + s.td.time + lcd.bar(7, s.gpu.usage_core / 100.0));
lcd.writeBuf(0, 26, utils::pNum(s.avg[0], s.avg[0] > 10 ? 1 : 2));
lcd.writeBuf(0, 31, utils::pNum(s.avg[1], s.avg[1] > 10 ? 1 : 2));
lcd.writeBuf(0, 36, utils::pNum(s.avg[2], s.avg[2] > 10 ? 1 : 2));
lcd.writeBuf(1, 0, utils::pad(std::to_string(s->cpu.tctl) + "/" + std::to_string(s->cpu.tccd1) + lcd.cc(0), 8, utils::just::left));
lcd.writeBuf(1, 7, utils::pad(utils::pBytes(s->ram.used, true), 6, utils::just::left));
lcd.writeBuf(1, 13, utils::pad(utils::pNum(s->hass_state) + "W", 4));
lcd.writeBuf(1, 19, std::to_string(s->sys.t_sensor) + lcd.cc(0)); // t_sensor temp
lcd.writeBuf(1, 32 - gpu_temps.size(), utils::pad(utils::pBytes(s->gpu.vram_used), 6));
lcd.writeBuf(1, 0, utils::pad(std::to_string(s.cpu.tctl) + "/" + std::to_string(s.cpu.tccd1) + lcd.cc(0), 8, utils::just::left));
lcd.writeBuf(1, 7, utils::pad(utils::pBytes(s.ram.used, true), 6, utils::just::left));
lcd.writeBuf(1, 13, utils::pad(utils::pNum(s.hass_state) + "W", 4));
lcd.writeBuf(1, 19, std::to_string(s.sys.t_sensor) + lcd.cc(0)); // t_sensor temp
lcd.writeBuf(1, 32 - gpu_temps.size(), utils::pad(utils::pBytes(s.gpu.vram_used), 6));
lcd.writeBuf(1, 39 - gpu_temps.size(), gpu_temps + lcd.cc(0));
lcd.writeBuf(2, 0, utils::pad(std::to_string((int)s->cpu.ptotal) + "W", 8, utils::just::left));
lcd.writeBuf(2, s->cpu.ptotal < 100 ? 4 : 5, std::to_string(cpu_fan) + "R"); // avg fan speed
lcd.writeBuf(2, 12, lcd.cc(3) + utils::pBytes(s->net.upload));
lcd.writeBuf(2, 19, lcd.cc(4) + utils::pBytes(s->net.download));
lcd.writeBuf(2, 26, utils::pad(std::to_string(s->gpu.fan_speed) + "R", 5));
lcd.writeBuf(2, 31, utils::pad(std::to_string(s->gpu.power_avg) + "/" + std::to_string(s->gpu.power_cap) + "W", 9));
lcd.writeBuf(2, 0, utils::pad(std::to_string((int)s.cpu.ptotal) + "W", 8, utils::just::left));
lcd.writeBuf(2, s.cpu.ptotal < 100 ? 4 : 5, std::to_string(cpu_fan) + "R"); // avg fan speed
lcd.writeBuf(2, 12, lcd.cc(3) + utils::pBytes(s.net.upload));
lcd.writeBuf(2, 19, lcd.cc(4) + utils::pBytes(s.net.download));
lcd.writeBuf(2, 26, utils::pad(std::to_string(s.gpu.fan_speed) + "R", 5));
lcd.writeBuf(2, 31, utils::pad(std::to_string(s.gpu.power_avg) + "/" + std::to_string(s.gpu.power_cap) + "W", 9));
lcd.writeBuf(3, 0, utils::pad(utils::pNum(s->cpu.vcore, 2) + "V", 7, utils::just::left));
lcd.writeBuf(3, 6, std::to_string(s->cpu.avg_freq) + "MHz");
lcd.writeBuf(3, 14, utils::pad(lcd.cc(2) + utils::pBytes(s->home.usage.free), 6));
lcd.writeBuf(3, 21, utils::pad(std::to_string(s->gpu.clock_core) + "/" + std::to_string(s->gpu.clock_mem) + "MHz ", 13));
lcd.writeBuf(3, 34, utils::pad(utils::pNum(s->gpu.vcore, 3) + "V", 5));
lcd.writeBuf(3, 0, utils::pad(utils::pNum(s.cpu.vcore, 2) + "V", 7, utils::just::left));
lcd.writeBuf(3, 6, std::to_string(s.cpu.avg_freq) + "MHz");
lcd.writeBuf(3, 14, utils::pad(lcd.cc(2) + utils::pBytes(s.home.usage.free), 6));
lcd.writeBuf(3, 21, utils::pad(std::to_string(s.gpu.clock_core) + "/" + std::to_string(s.gpu.clock_mem) + "MHz ", 13));
lcd.writeBuf(3, 34, utils::pad(utils::pNum(s.gpu.vcore, 3) + "V", 5));
}
lcd.flushBuf();
@@ -162,12 +159,12 @@ void start(void) {
std::signal(SIGTERM, app::signalHandler);
{
std::unique_ptr<app::sensors> pSensors = std::make_unique<app::sensors>();
app::sensors Sensors;
std::vector<std::jthread> threads;
std::function<void(std::stop_token st, app::sensors * s)> funcs[] = {app::displayThread, app::sensorsThread, app::hassThread};
std::function<void(std::stop_token st, app::sensors & s)> funcs[] = {app::displayThread, app::sensorsThread, app::hassThread};
for (auto& f : funcs) {
threads.emplace_back(f, pSensors.get()); // params: function, stop_token (implied), pointer to sensor struct
threads.emplace_back(f, std::ref(Sensors)); // params: function, stop_token (implied), pointer to sensor struct
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // start threads with delay so displayThread can quit the app if theres no lcd
}