From aeeef4f236f838becca52acea6d5797fdcf61e11 Mon Sep 17 00:00:00 2001 From: Delgan Date: Sun, 4 Feb 2024 16:11:35 +0100 Subject: [PATCH] Fix "mem" values updated while it should not Closes #352 --- CHANGELOG.md | 2 ++ modules/mem.c | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c75d270..dd2bfff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,11 +39,13 @@ * Bar not resizing itself when the screen resolution is changed ([#330][330]). * i3/sway: incorrect empty/title state of workspaces ([#343][343]). +* mem: state updated on each bar redraw ([#352][352]). [311]: https://codeberg.org/dnkl/yambar/issues/311 [302]: https://codeberg.org/dnkl/yambar/issues/302 [330]: https://codeberg.org/dnkl/yambar/issues/330 [343]: https://codeberg.org/dnkl/yambar/issues/343 +[352]: https://codeberg.org/dnkl/yambar/issues/352 ### Security diff --git a/modules/mem.c b/modules/mem.c index 3a2eda7..6a196c6 100644 --- a/modules/mem.c +++ b/modules/mem.c @@ -24,6 +24,8 @@ struct private { struct particle *label; uint16_t interval; + uint64_t mem_free; + uint64_t mem_total; }; static void @@ -79,15 +81,12 @@ static struct exposable * content(struct module *mod) { const struct private *p = mod->private; - uint64_t mem_free = 0; - uint64_t mem_used = 0; - uint64_t mem_total = 0; - if (!get_mem_stats(&mem_free, &mem_total)) { - LOG_ERR("unable to retrieve the memory stats"); - } + mtx_lock(&mod->lock); - mem_used = mem_total - mem_free; + const uint64_t mem_free = p->mem_free; + const uint64_t mem_total = p->mem_total; + const uint64_t mem_used = mem_total - mem_free; double percent_used = ((double)mem_used * 100) / (mem_total + 1); double percent_free = ((double)mem_free * 100) / (mem_total + 1); @@ -102,6 +101,7 @@ content(struct module *mod) struct exposable *exposable = p->label->instantiate(p->label, &tags); tag_set_destroy(&tags); + mtx_unlock(&mod->lock); return exposable; } @@ -127,6 +127,13 @@ run(struct module *mod) if (fds[0].revents & POLLIN) break; + mtx_lock(&mod->lock); + p->mem_free = 0; + p->mem_total = 0; + if (!get_mem_stats(&p->mem_free, &p->mem_total)) { + LOG_ERR("unable to retrieve the memory stats"); + } + mtx_unlock(&mod->lock); bar->refresh(bar); }