From c2b585ade93eeca742499fab1bdfb29f36e0c59f Mon Sep 17 00:00:00 2001 From: "Eric J. Korpela" Date: Thu, 15 Jun 2006 18:19:06 +0000 Subject: [PATCH] Protected the getrusage call in boinc_calling_thread_cpu_time() with the same mutex used in boinc_api.C svn path=/trunk/boinc/; revision=10358 --- lib/util.C | 11 +++++++++-- lib/util.h | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/util.C b/lib/util.C index e4a24ad2f2..b4a9cd61d4 100755 --- a/lib/util.C +++ b/lib/util.C @@ -813,13 +813,20 @@ int boinc_calling_thread_cpu_time(double& cpu) { #else +pthread_mutex_t getrusage_mutex=PTHREAD_MUTEX_INITIALIZER; + // Unix: pthreads doesn't seem to provide an API for getting // per-thread CPU time. So just get the process's CPU time // int boinc_calling_thread_cpu_time(double &cpu_t) { - int retval; + int retval=1; struct rusage ru; - retval = getrusage(RUSAGE_SELF, &ru); +// getrusage can return an error, so try a few times if it returns an error. + if (!pthread_mutex_trylock(&getrusage_mutex)) { + int i=0; + while (retval=getrusage(RUSAGE_SELF, &ru) && i<10) i++; + pthread_mutex_unlock(&getrusage_mutex); + } if (retval) { return ERR_GETRUSAGE; } diff --git a/lib/util.h b/lib/util.h index ebe8216ae6..ee1aa479ab 100755 --- a/lib/util.h +++ b/lib/util.h @@ -28,6 +28,12 @@ #include #include +#ifdef HAVE_PTHREAD + +#include + +#endif + #if !defined(HAVE_STRLCPY) extern size_t strlcpy(char*, const char*, size_t); #endif @@ -130,6 +136,10 @@ extern void mysql_timestamp(double, char*); // extern const char* boincerror(int which_error); +#ifdef HAVE_PTHREAD +extern pthread_mutex_t getrusage_mutex; +#endif + #ifndef _WIN32 extern int lookup_group(char*, gid_t& gid); extern int check_security(void);