From b7b9329e3814ae1a3aa8f705c5a7f3279c719db1 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Fri, 11 Sep 2009 01:18:38 +0200 Subject: [PATCH] update machinery: run each update callback in it's own thread --- src/common.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/common.c b/src/common.c index 8069b3c5..538cdfac 100644 --- a/src/common.c +++ b/src/common.c @@ -384,6 +384,7 @@ void format_seconds_short(char *buf, unsigned int n, long seconds) static struct update_cb { struct update_cb *next; void (*func)(void); + pthread_t thread; } update_cb_head = { .next = NULL, }; @@ -423,6 +424,16 @@ void free_update_callbacks(void) update_cb_head.next = NULL; } +static void *run_update_callback(void *) __attribute__((noreturn)); + +static void *run_update_callback(void *data) +{ + struct update_cb *ucb = data; + + (*ucb->func)(); + pthread_exit(NULL); +} + int no_buffers; void update_stuff(void) @@ -446,8 +457,13 @@ void update_stuff(void) prepare_update(); + for (uc = update_cb_head.next; uc; uc = uc->next) { + pthread_create(&uc->thread, NULL, &run_update_callback, uc); + } + /* need to synchronise here, otherwise locking is needed (as data + * would be printed with some update callbacks still running) */ for (uc = update_cb_head.next; uc; uc = uc->next) - (*uc->func)(); + pthread_join(uc->thread, NULL); /* XXX: move the following into the update_meminfo() functions? */ if (no_buffers) {