From d279b585db22c2e6c362c49a4caff9a966b10544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Tue, 18 Dec 2018 20:23:19 +0100 Subject: [PATCH] module/battery: read all data into local variables first This minimizes the time we'll have to hold the lock (once we lock). --- modules/battery/battery.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/battery/battery.c b/modules/battery/battery.c index c374708..66e8439 100644 --- a/modules/battery/battery.c +++ b/modules/battery/battery.c @@ -218,27 +218,34 @@ run(struct module_run_context *ctx) } else first = false; + long capacity = readint_from_fd(capacity_fd); + long energy = readint_from_fd(energy_fd); + long power = readint_from_fd(power_fd); + const char *status = readline_from_fd(status_fd); + enum state state; + if (strcmp(status, "Full") == 0) - m->state = STATE_FULL; + state = STATE_FULL; else if (strcmp(status, "Charging") == 0) - m->state = STATE_CHARGING; + state = STATE_CHARGING; else if (strcmp(status, "Discharging") == 0) - m->state = STATE_DISCHARGING; + state = STATE_DISCHARGING; else if (strcmp(status, "Unknown") == 0) - m->state = STATE_DISCHARGING; + state = STATE_DISCHARGING; else { LOG_ERR("unrecognized battery state: %s", status); - m->state = STATE_DISCHARGING; + state = STATE_DISCHARGING; assert(false && "unrecognized battery state"); } - m->capacity = readint_from_fd(capacity_fd); - m->energy = readint_from_fd(energy_fd); - m->power = readint_from_fd(power_fd); + LOG_DBG("capacity: %ld, energy: %ld, power: %ld", + capacity, energy, power); - LOG_DBG("capacity: %lu, energy: %lu, power: %lu", - m->capacity, m->energy, m->power); + m->state = state; + m->capacity = capacity; + m->energy = energy; + m->power = power; bar->refresh(bar); }