Switched from ctime() to ctime_r(), for thread-safety.

I saw run in which I caught an MPI task hanging in ctime() here.  Swiching
to ctime_r() fixes that.  This function is only called form rank==0, but it
hangs anyway.
master
Jeff Inman 2014-08-28 15:24:58 -06:00
parent 8a33dfb84b
commit 02b6287167
1 changed files with 13 additions and 1 deletions

View File

@ -55,17 +55,29 @@ extern int verbose;
/*
* Returns string containing the current time.
*
* NOTE: On some systems, MPI jobs hang while ctime() waits for a lock.
* This is true even though CurrentTimeString() is only called for rank==0.
* ctime_r() fixes this.
*/
char *CurrentTimeString(void)
{
static time_t currentTime;
char *currentTimePtr;
char* currentTimePtr;
if ((currentTime = time(NULL)) == -1)
ERR("cannot get current time");
#if (_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE)
static char threadSafeBuff[32]; /* "must be at least 26 characters long" */
if ((currentTimePtr = ctime_r(&currentTime, threadSafeBuff)) == NULL) {
ERR("cannot read current time");
}
#else
if ((currentTimePtr = ctime(&currentTime)) == NULL) {
ERR("cannot read current time");
}
#endif
/* ctime string ends in \n */
return (currentTimePtr);
}