Skip to content

Commit

Permalink
Use gettimeofday() instead of ftime()
Browse files Browse the repository at this point in the history
ftime() was removed in POSIX.1-2008. gettimeofday() is deprecated but
the recommended function clock_gettime() is often not available in some
systems (e.g., macOS 10.11 or earlier).
  • Loading branch information
tueda committed Jul 13, 2017
1 parent 3767258 commit 858b2c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
16 changes: 14 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,20 @@ AS_IF([test "x$enable_parform" = xyes && test "x$enable_debug" = xyes], [build_p
AM_CONDITIONAL([BUILD_PARFORM], [test "x$build_parform" = xyes])
AM_CONDITIONAL([BUILD_PARVORM], [test "x$build_parvorm" = xyes])

# Check for ftime
AC_SEARCH_LIBS([ftime], [compat], [], [])
# Check for wall-clock time
ok=no
AS_IF([test $ok != yes],
# TODO: gettimeofday is also deprecated.
[AC_SEARCH_LIBS([gettimeofday], [],
[AC_DEFINE([HAVE_GETTIMEOFDAY], [1], [Define to 1 if you have gettimeofday.])
ok=yes])])
AS_IF([test $ok != yes],
# Fallback: ftime. Available also on Windows. Some BSDs require -lcompat.
[AC_SEARCH_LIBS([ftime], [compat],
[AC_DEFINE([HAVE_FTIME], [1], [Define to 1 if you have ftime.])
ok=yes])])
AS_IF([test $ok != yes],
[AC_MSG_FAILURE([Wall-clock time not available])])

# Check for static linking
STATIC_LDFLAGS=
Expand Down
20 changes: 20 additions & 0 deletions sources/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -3366,7 +3366,11 @@ int CompArg(WORD *s1, WORD *s2)
#[ TimeWallClock :
*/

#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#else
#include <sys/timeb.h>
#endif

/**
* Returns the wall-clock time.
Expand All @@ -3379,6 +3383,21 @@ LONG TimeWallClock(WORD par)
/*
* NOTE: this function is not thread-safe. Operations on tp are not atomic.
*/
#ifdef HAVE_GETTIMEOFDAY
struct timeval t;
LONG sec, msec;
gettimeofday(&t, NULL);
sec = (LONG)t.tv_sec;
msec = (LONG)(t.tv_usec/1000);
if ( par ) {
return (sec-AM.OldSecTime)*100 + (msec-AM.OldMilliTime)/10;
}
else {
AM.OldSecTime = sec;
AM.OldMilliTime = msec;
return(0L);
}
#else
struct timeb tp;
ftime(&tp);
if ( par ) {
Expand All @@ -3390,6 +3409,7 @@ LONG TimeWallClock(WORD par)
AM.OldMilliTime = (LONG)(tp.millitm);
return(0L);
}
#endif
}

/*
Expand Down

0 comments on commit 858b2c5

Please sign in to comment.