From a3eb7ebc0832a140c902478b9eda99afd170b2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 19 Dec 2018 19:41:25 +0100 Subject: [PATCH] bar: wait for all modules to have started up before continuing After starting all the module threads, wait for all modules to have signalled "ready" before continuing. This will allow modules to do initial setup, and knowing that content() will *not* be called until they've signalled "ready". --- bar.c | 17 ++++++++++++++++- module.h | 1 + modules/battery/battery.c | 3 +++ modules/clock/clock.c | 4 ++++ modules/i3/i3.c | 2 ++ modules/label/label.c | 3 +++ modules/xkb/xkb.c | 4 ++++ modules/xwindow/xwindow.c | 2 ++ 8 files changed, 35 insertions(+), 1 deletion(-) diff --git a/bar.c b/bar.c index c839567..2a84a3b 100644 --- a/bar.c +++ b/bar.c @@ -10,6 +10,8 @@ #include #include +#include + #include #include #include @@ -383,10 +385,14 @@ run(struct bar_run_context *run_ctx) struct module_run_context run_ctx_center[bar->center.count]; struct module_run_context run_ctx_right[bar->right.count]; + int ready_fd = eventfd(0, EFD_SEMAPHORE); + assert(ready_fd != -1); + for (size_t i = 0; i < bar->left.count; i++) { struct module_run_context *ctx = &run_ctx_left[i]; ctx->module = bar->left.mods[i]; + ctx->ready_fd = ready_fd; ctx->abort_fd = run_ctx->abort_fd; thrd_create(&thrd_left[i], (int (*)(void *))bar->left.mods[i]->run, ctx); @@ -395,6 +401,7 @@ run(struct bar_run_context *run_ctx) struct module_run_context *ctx = &run_ctx_center[i]; ctx->module = bar->center.mods[i]; + ctx->ready_fd = ready_fd; ctx->abort_fd = run_ctx->abort_fd; thrd_create(&thrd_center[i], (int (*)(void *))bar->center.mods[i]->run, ctx); @@ -403,12 +410,20 @@ run(struct bar_run_context *run_ctx) struct module_run_context *ctx = &run_ctx_right[i]; ctx->module = bar->right.mods[i]; + ctx->ready_fd = ready_fd; ctx->abort_fd = run_ctx->abort_fd; thrd_create(&thrd_right[i], (int (*)(void *))bar->right.mods[i]->run, ctx); } - LOG_DBG("modules started"); + LOG_DBG("waiting for modules to become ready"); + for (size_t i = 0; i < bar->left.count + bar->center.count + bar->right.count; i++) { + uint64_t b; + read(ready_fd, &b, sizeof(b)); + } + + close(ready_fd); + LOG_DBG("all modules started"); int fd = xcb_get_file_descriptor(bar->conn); diff --git a/module.h b/module.h index 18314fc..2bb12bd 100644 --- a/module.h +++ b/module.h @@ -11,6 +11,7 @@ struct module; struct module_run_context { struct module *module; + int ready_fd; int abort_fd; }; diff --git a/modules/battery/battery.c b/modules/battery/battery.c index 3792ea2..61cacee 100644 --- a/modules/battery/battery.c +++ b/modules/battery/battery.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -202,6 +203,8 @@ run(struct module_run_context *ctx) udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL); udev_monitor_enable_receiving(mon); + write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t)); + bool first = true; while (true) { if (!first) { diff --git a/modules/clock/clock.c b/modules/clock/clock.c index 4680a03..5e4ca46 100644 --- a/modules/clock/clock.c +++ b/modules/clock/clock.c @@ -1,6 +1,8 @@ #include "clock.h" #include +#include #include +#include #include #include @@ -50,6 +52,8 @@ run(struct module_run_context *ctx) { const struct bar *bar = ctx->module->bar; + write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t)); + while (true) { time_t now = time(NULL); time_t now_no_secs = now / 60 * 60; diff --git a/modules/i3/i3.c b/modules/i3/i3.c index c9cff0c..11e9caf 100644 --- a/modules/i3/i3.c +++ b/modules/i3/i3.c @@ -287,6 +287,8 @@ run(struct module_run_context *ctx) send_pkg(sock, I3_IPC_MESSAGE_TYPE_GET_WORKSPACES, NULL); send_pkg(sock, I3_IPC_MESSAGE_TYPE_SUBSCRIBE, "[\"workspace\"]"); + write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t)); + char buf[1 * 1024 * 1024]; /* Some replies are *big*. TODO: grow dynamically */ size_t buf_idx = 0; diff --git a/modules/label/label.c b/modules/label/label.c index c215380..4e6c2dc 100644 --- a/modules/label/label.c +++ b/modules/label/label.c @@ -1,6 +1,8 @@ #include "label.h" #include +#include +#include #include #include @@ -28,6 +30,7 @@ content(struct module *mod) static int run(struct module_run_context *ctx) { + write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t)); return 0; } diff --git a/modules/xkb/xkb.c b/modules/xkb/xkb.c index 1087e48..6e70b98 100644 --- a/modules/xkb/xkb.c +++ b/modules/xkb/xkb.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -437,6 +438,9 @@ run(struct module_run_context *ctx) return EXIT_FAILURE; } + /* TODO: move this to later */ + write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t)); + int ret = talk_to_xkb(ctx->abort_fd, ctx->module->bar, ctx->module->private, conn) ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/modules/xwindow/xwindow.c b/modules/xwindow/xwindow.c index 5779e14..7200869 100644 --- a/modules/xwindow/xwindow.c +++ b/modules/xwindow/xwindow.c @@ -207,6 +207,8 @@ run(struct module_run_context *ctx) update_title(m); mod->bar->refresh(mod->bar); + write(ctx->ready_fd, &(uint64_t){1}, sizeof(uint64_t)); + int xcb_fd = xcb_get_file_descriptor(m->conn); while (true) { struct pollfd fds[] = {{.fd = ctx->abort_fd, .events = POLLIN},