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);