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".
This commit is contained in:
Daniel Eklöf 2018-12-19 19:41:25 +01:00
parent 7f0f096ba4
commit a3eb7ebc08
8 changed files with 35 additions and 1 deletions

17
bar.c
View file

@ -10,6 +10,8 @@
#include <poll.h>
#include <signal.h>
#include <sys/eventfd.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/randr.h>
@ -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);