From 3ed13c3cd123975add4f450d245638bf8a1c2335 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 11 Dec 2009 22:54:11 +0100 Subject: [PATCH] Fix race condition in update_stat() --- src/linux.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/linux.c b/src/linux.c index 12b0104..daaeb73 100644 --- a/src/linux.c +++ b/src/linux.c @@ -63,6 +63,7 @@ #endif #include #include +#include /* The following ifdefs were adapted from gkrellm */ #include @@ -672,14 +673,20 @@ void update_stat(void) const char *stat_template = NULL; unsigned int malloc_cpu_size = 0; extern void* global_cpu; + + static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER; static double last_stat_update = 0.0; /* since we use wrappers for this function, the update machinery * can't eliminate double invocations of this function. Check for * them here, otherwise cpu_usage counters are freaking out. */ - if (last_stat_update == current_update_time) + pthread_mutex_lock(&last_stat_update_mutex); + if (last_stat_update == current_update_time) { + pthread_mutex_unlock(&last_stat_update_mutex); return; + } last_stat_update = current_update_time; + pthread_mutex_unlock(&last_stat_update_mutex); /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */ if (!cpu_setup || !info.cpu_usage) { -- 1.7.9.5