module/battery: acquire lock when reading/writing state

This commit is contained in:
Daniel Eklöf 2018-12-18 20:24:30 +01:00
parent d279b585db
commit 16553a7216

View file

@ -52,6 +52,9 @@ static struct exposable *
content(const struct module *mod) content(const struct module *mod)
{ {
const struct private *m = mod->private; const struct private *m = mod->private;
mtx_lock(&((struct module *)mod)->lock);
assert(m->state == STATE_FULL || assert(m->state == STATE_FULL ||
m->state == STATE_CHARGING || m->state == STATE_CHARGING ||
m->state == STATE_DISCHARGING); m->state == STATE_DISCHARGING);
@ -89,6 +92,8 @@ content(const struct module *mod)
.count = 6, .count = 6,
}; };
mtx_unlock(&((struct module *)mod)->lock);
struct exposable *exposable = m->label->instantiate(m->label, &tags); struct exposable *exposable = m->label->instantiate(m->label, &tags);
tag_set_destroy(&tags); tag_set_destroy(&tags);
@ -138,6 +143,8 @@ run(struct module_run_context *ctx)
int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY); int base_dir_fd = openat(pw_fd, m->battery, O_RDONLY);
assert(base_dir_fd != -1); assert(base_dir_fd != -1);
mtx_lock(&ctx->module->lock);
{ {
int fd = openat(base_dir_fd, "manufacturer", O_RDONLY); int fd = openat(base_dir_fd, "manufacturer", O_RDONLY);
assert(fd != -1); assert(fd != -1);
@ -175,6 +182,8 @@ run(struct module_run_context *ctx)
m->battery, m->manufacturer, m->model, m->battery, m->manufacturer, m->model,
100.0 * m->energy_full / energy_full_design); 100.0 * m->energy_full / energy_full_design);
mtx_unlock(&ctx->module->lock);
int status_fd = openat(base_dir_fd, "status", O_RDONLY); int status_fd = openat(base_dir_fd, "status", O_RDONLY);
assert(status_fd != -1); assert(status_fd != -1);
@ -242,10 +251,12 @@ run(struct module_run_context *ctx)
LOG_DBG("capacity: %ld, energy: %ld, power: %ld", LOG_DBG("capacity: %ld, energy: %ld, power: %ld",
capacity, energy, power); capacity, energy, power);
mtx_lock(&ctx->module->lock);
m->state = state; m->state = state;
m->capacity = capacity; m->capacity = capacity;
m->energy = energy; m->energy = energy;
m->power = power; m->power = power;
mtx_unlock(&ctx->module->lock);
bar->refresh(bar); bar->refresh(bar);
} }