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
This commit is contained in:
Eric J. Korpela 2006-06-15 18:19:06 +00:00
parent 0cf32ac71f
commit c2b585ade9
2 changed files with 19 additions and 2 deletions

View File

@ -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;
}

View File

@ -28,6 +28,12 @@
#include <algorithm>
#include <string>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#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);